Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "lib/index.js",
"scripts": {
"test": "mocha --reporter spec --require @babel/register test/index.js",
"performance-test": "tsc test/performance/src/index.ts --outDir test/performance/lib && node test/performance/lib/index.js",
"test-watch": "mocha --reporter spec --compilers js:babel-core/register test/index.js --recursive --watch",
"prepublish": "npm run clean && npm run build && npm run test",
"clean": "rm -rf lib/",
Expand Down
27 changes: 11 additions & 16 deletions src/mobx_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface MobxModelSetOptions {
}

export interface MobxModelObservables {
collection: MobxModel[];
collection: Map<string | number, MobxModel>;
}

/*
Expand All @@ -54,7 +54,7 @@ export interface MobxModelObservables {
*/
const initObservables = function(target: any) {
if (!target.observables) {
target.observables = { collection: [] };
target.observables = { collection: new Map() };
}
};

Expand Down Expand Up @@ -119,15 +119,13 @@ class MobxModel {
return null;
}

const items: any[] = result(this, 'observables.collection');
const items: Map<string | number, T> = result(
this,
'observables.collection',
);

if (items) {
let l = items.length;
for (let i = 0; i < l; i++) {
if (items[i].id && items[i].id.toString() === id.toString()) {
return items[i];
}
}
return items.get(id) || null;
}

return null;
Expand All @@ -140,7 +138,7 @@ class MobxModel {
let { topLevelJson, requestId } = options;

/*
requestId is used to allow models to
requestId is used to allow models to
prevent loops when setting same attributes
multiple times, we set one if none is set
*/
Expand All @@ -163,7 +161,7 @@ class MobxModel {
requestId,
});

this.observables.collection.push(model);
this.observables.collection.set(model.id, model);
}

model.set({ modelJson, topLevelJson, requestId });
Expand All @@ -174,16 +172,13 @@ class MobxModel {

static remove(model: MobxModel): void {
if (this.observables && this.observables.collection) {
this.observables.collection.splice(
this.observables.collection.indexOf(model),
1,
);
this.observables.collection.delete(model.id);
}
}

static all(): MobxModel[] {
initObservables(this);
return this.observables.collection.slice();
return Array.from(this.observables.collection.values());
}

static addClassAction(
Expand Down
16 changes: 16 additions & 0 deletions test/performance/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as mobx from 'mobx';
import * as models from './models';
import { RootModel } from './models';
import MobxModel from '../../../lib';

MobxModel.config({
mobx,
models,
});

const rootModel = new RootModel();
const modelJson = rootModel.generateJson();

console.time();
rootModel.set({ modelJson, topLevelJson: modelJson });
console.timeEnd();
33 changes: 33 additions & 0 deletions test/performance/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Model12 from './model12';
import Model14 from './model14';
import Model3 from './model3';
import Model2 from './model2';
import Model6 from './model6';
import Model8 from './model8';
import Model4 from './model4';
import Model5 from './model5';
import Model7 from './model7';
import Model11 from './model11';
import Model1 from './model1';
import Model13 from './model13';
import Model9 from './model9';
import Model10 from './model10';
import RootModel from './root-model';

export {
Model12,
Model14,
Model3,
Model2,
Model6,
Model8,
Model4,
Model7,
Model11,
Model1,
Model13,
Model5,
Model9,
Model10,
RootModel,
};
26 changes: 26 additions & 0 deletions test/performance/src/models/model1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RelationType } from '../../../../lib/mobx_model';
import ProtoTestModel from './proto-test-model';

class Model1 extends ProtoTestModel {
static modelName = 'Model1';
static numberOfItemsInJson = 7165;

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
attribute4: '',
attribute5: '',
attribute6: '',
};

static relations = [
{
type: RelationType.hasOne,
relatedModel: 'Model11',
reverseRelation: true,
},
];
}

export default Model1;
87 changes: 87 additions & 0 deletions test/performance/src/models/model10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const times = require('lodash/times');
const random = require('lodash/random');

import { RelationType } from '../../../../lib/mobx_model';
import ProtoTestModel, { TestModelAttributeType } from './proto-test-model';

interface IItem1 {
item1Attribute1: string;
item1Attribute2: string;
item1Attribute3: string;
}

interface IItem2 {
item2Attribute1: string;
item2Attribute2: string;
item2Attribute3: string;
item2Attribute4: string;
}

class Model10 extends ProtoTestModel {
static modelName = 'Model10';
static numberOfItemsInJson = 133;

static attributeTypes = {
attribute4: TestModelAttributeType.boolean,
};

static generateAttributeValue(attributeName: string): any {
if (attributeName === 'attribute7') {
return this.generateItem1();
}

if (attributeName === 'attribute8') {
return times(random(5), this.generateItem1);
}

if (attributeName === 'attribute9') {
return times(random(5), this.generateItem2);
}

return super.generateAttributeValue(attributeName);
}

static generateItem1(): IItem1 {
return {
item1Attribute1: ProtoTestModel.generateRandomString(),
item1Attribute2: ProtoTestModel.generateRandomString(),
item1Attribute3: ProtoTestModel.generateRandomString(),
};
}

static generateItem2(): IItem2 {
return {
item2Attribute1: ProtoTestModel.generateRandomString(),
item2Attribute2: ProtoTestModel.generateRandomString(),
item2Attribute3: ProtoTestModel.generateRandomString(),
item2Attribute4: ProtoTestModel.generateRandomString(),
};
}

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
attribute4: false,
attribute5: '',
attribute6: '',
attribute7: void 0,
attribute8: [],
attribute9: [],
};

static relations = [
{
type: RelationType.hasOne,
relatedModel: 'Model2',
reverseRelation: true,
},
{
type: RelationType.hasMany,
relatedModel: 'Model12',
reverseRelation: true,
},
];
}

export default Model10;
37 changes: 37 additions & 0 deletions test/performance/src/models/model11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { RelationType } from '../../../../lib/mobx_model';
import ProtoTestModel from './proto-test-model';

class Model11 extends ProtoTestModel {
static modelName = 'Model11';
static numberOfItemsInJson = 3326;

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
attribute4: '',
attribute5: '',
attribute6: '',
attribute7: '',
attribute8: '',
};

static relations = [
{
type: RelationType.hasOne,
relatedModel: 'Model2',
reverseRelation: true,
},
{
type: RelationType.hasOne,
relatedModel: 'Model14',
reverseRelation: true,
},
{
type: RelationType.hasMany,
relatedModel: 'Model1',
},
];
}

export default Model11;
57 changes: 57 additions & 0 deletions test/performance/src/models/model12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { RelationType } from '../../../../lib/mobx_model';
import ProtoTestModel, { TestModelAttributeType } from './proto-test-model';

class Model12 extends ProtoTestModel {
static modelName = 'Model12';
static numberOfItemsInJson = 2605;

static attributeTypes = {
attribute5: TestModelAttributeType.boolean,
attribute6: TestModelAttributeType.boolean,
attribute7: TestModelAttributeType.number,
attribute18: TestModelAttributeType.number,
attribute19: TestModelAttributeType.boolean,
};

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
attribute4: '',
attribute5: false,
attribute6: false,
attribute7: 0,
attribute8: '',
attribute9: '',
attribute10: '',
attribute11: '',
attribute12: '',
attribute13: '',
attribute14: '',
attribute15: '',
attribute16: '',
attribute17: '',
attribute18: 0,
attribute19: true,
};

static relations = [
{
type: RelationType.hasOne,
relatedModel: 'Model14',
reverseRelation: true,
},
{
type: RelationType.hasOne,
relatedModel: 'Model2',
reverseRelation: true,
},
{
type: RelationType.hasOne,
relatedModel: 'Model10',
reverseRelation: true,
},
];
}

export default Model12;
14 changes: 14 additions & 0 deletions test/performance/src/models/model13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ProtoTestModel from './proto-test-model';

class Model13 extends ProtoTestModel {
static modelName = 'Model13';
static numberOfItemsInJson = 175;

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
};
}

export default Model13;
33 changes: 33 additions & 0 deletions test/performance/src/models/model14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RelationType } from '../../../../lib/mobx_model';
import ProtoTestModel, { TestModelAttributeType } from './proto-test-model';

class Model14 extends ProtoTestModel {
static modelName = 'Model14';
static numberOfItemsInJson = 40;

static attributeTypes = {
attribute4: TestModelAttributeType.boolean,
};

static attributes = {
attribute1: '',
attribute2: '',
attribute3: '',
attribute4: false,
};

static relations = [
{
type: RelationType.hasMany,
relatedModel: 'Model12',
reverseRelation: true,
},
{
type: RelationType.hasMany,
relatedModel: 'Model11',
reverseRelation: true,
},
];
}

export default Model14;
Loading