From 97df9c330678bb509c4e331da8046e9db0ecbd7d Mon Sep 17 00:00:00 2001 From: Daniel Nakov Date: Fri, 31 Mar 2017 12:36:06 -0400 Subject: [PATCH 1/7] Support primitives as memoization key for `createTransformer` --- src/api/createtransformer.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/api/createtransformer.ts b/src/api/createtransformer.ts index 92424df12..4cf4b3b64 100644 --- a/src/api/createtransformer.ts +++ b/src/api/createtransformer.ts @@ -45,6 +45,8 @@ export function createTransformer(transformer: ITransformer, onClean } function getMemoizationId(object) { + if(typeof object === 'string' || typeof object === 'number') + return object if (object === null || typeof object !== "object") throw new Error("[mobx] transform expected some kind of object, got: " + object); let tid = object.$transformId; @@ -53,4 +55,4 @@ function getMemoizationId(object) { addHiddenProp(object, "$transformId", tid); } return tid; -} \ No newline at end of file +} From cc9fc15550c9d20807c12c26d523fdbda8dfafa1 Mon Sep 17 00:00:00 2001 From: daniel nakov Date: Sun, 2 Apr 2017 08:56:58 -0400 Subject: [PATCH 2/7] add tests for createTransform with primitive keys --- test/transform.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/transform.js b/test/transform.js index 115127ed4..5a0f4cb68 100644 --- a/test/transform.js +++ b/test/transform.js @@ -1003,4 +1003,48 @@ test('transform tree (dynamic tags - peek / rebuild)', function(t) { t.deepEqual(state.renderedNodes.length, 0); t.end(); -}); \ No newline at end of file +}); + +// https://github.com/mobxjs/mobx/issues/886 +test('transform with primitive key', function(t) { + m.extras.resetGlobalState(); + + function Bob() { + this.num = Math.floor(Math.random() * 1000); + m.extendObservable(this, { + get name() { + return 'Bob' + this.num; + } + }); + } + + var _state = m.createTransformer( function(key) { + return m.observable([]); + }); + + var observableBobs = m.observable([]); + var bobs = []; + + var bobFactory = m.createTransformer(function(key) { + return new Bob(); + }); + + m.autorun(function() { + bobs = observableBobs.map(function(bob) { + return bobFactory(bob); + }); + }); + + var b = m.observable({name: 'Bob1'}); + observableBobs.push('Bob1'); + observableBobs.push('Bob1'); + t.equal(bobs[0].name, bobs[1].name); + + observableBobs.clear(); + observableBobs.push('Bob1'); + observableBobs.push('Bob2'); + t.notEqual(bobs[0].name, bobs[1].name); + + t.end(); +}); + From f6c9415802daafd90b9dd951fd1cc41c97c7f76b Mon Sep 17 00:00:00 2001 From: daniel nakov Date: Sun, 2 Apr 2017 09:01:48 -0400 Subject: [PATCH 3/7] add tests for createTransform with primitive integer keys --- test/transform.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/transform.js b/test/transform.js index 5a0f4cb68..9292c8946 100644 --- a/test/transform.js +++ b/test/transform.js @@ -1018,10 +1018,6 @@ test('transform with primitive key', function(t) { }); } - var _state = m.createTransformer( function(key) { - return m.observable([]); - }); - var observableBobs = m.observable([]); var bobs = []; @@ -1035,7 +1031,6 @@ test('transform with primitive key', function(t) { }); }); - var b = m.observable({name: 'Bob1'}); observableBobs.push('Bob1'); observableBobs.push('Bob1'); t.equal(bobs[0].name, bobs[1].name); @@ -1045,6 +1040,16 @@ test('transform with primitive key', function(t) { observableBobs.push('Bob2'); t.notEqual(bobs[0].name, bobs[1].name); + observableBobs.clear(); + observableBobs.push(1); + observableBobs.push(1); + t.equal(bobs[0].name, bobs[1].name); + + observableBobs.clear(); + observableBobs.push(1); + observableBobs.push(2); + t.notEqual(bobs[0].name, bobs[1].name); + t.end(); }); From bbbe5ed2c6ec1d43266aa4c670b9eb675ae1a841 Mon Sep 17 00:00:00 2001 From: Andrey Kogut Date: Fri, 17 Mar 2017 15:15:38 +0200 Subject: [PATCH 4/7] Fixed typescript error ERROR in ./src/types/intercept-utils.ts (30,29): error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'T'. Type 'null' is not assignable to type 'T'. webpack: Failed to compile. --- src/types/intercept-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/intercept-utils.ts b/src/types/intercept-utils.ts index 015134e44..1d620dcdf 100644 --- a/src/types/intercept-utils.ts +++ b/src/types/intercept-utils.ts @@ -22,7 +22,7 @@ export function registerInterceptor(interceptable: IInterceptable, handler }); } -export function interceptChange(interceptable: IInterceptable, change: T): T { +export function interceptChange(interceptable: IInterceptable, change: T | null): T | null { const prevU = untrackedStart(); try { const interceptors = interceptable.interceptors; From c14361fd6881145b3771c0eaba861781d88fa5c2 Mon Sep 17 00:00:00 2001 From: codefalling Date: Fri, 24 Mar 2017 19:10:10 +0800 Subject: [PATCH 5/7] fix #893 --- src/types/observablemap.ts | 4 +- test/map.js | 17 ++++++++ test/perf/perf.txt | 84 +++++++++++++++++++------------------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/types/observablemap.ts b/src/types/observablemap.ts index 24f1e261a..12408040b 100644 --- a/src/types/observablemap.ts +++ b/src/types/observablemap.ts @@ -73,8 +73,8 @@ export type IObservableMapInitialValues = IMapEntries | IKeyValueMap | export class ObservableMap implements IInterceptable>, IListenable, IMap { $mobx = ObservableMapMarker; - private _data: { [key: string]: ObservableValue } = {}; - private _hasMap: { [key: string]: ObservableValue } = {}; // hasMap, not hashMap >-). + private _data: { [key: string]: ObservableValue } = Object.create(null); + private _hasMap: { [key: string]: ObservableValue } = Object.create(null); // hasMap, not hashMap >-). private _keys: IObservableArray = new ObservableArray(undefined, referenceEnhancer, `${this.name}.keys()`, true); interceptors = null; changeListeners = null; diff --git a/test/map.js b/test/map.js index aa86c1a41..205bd8abf 100644 --- a/test/map.js +++ b/test/map.js @@ -603,3 +603,20 @@ test('using deep map', t => { t.end(); }); + +test("issue 893", t => { + const m = mobx.observable.map(); + const keys = ['constructor', 'toString', 'assertValidKey', 'isValidKey', 'toJSON', 'toJS'] + for (let key of keys) { + t.equal(m.get(key), undefined); + } + t.end(); +}); + +test("work with 'toString' key", t => { + const m = mobx.observable.map(); + t.equal(m.get('toString'), undefined); + m.set('toString', 'test'); + t.equal(m.get('toString'), 'test'); + t.end(); +}); \ No newline at end of file diff --git a/test/perf/perf.txt b/test/perf/perf.txt index 130cec343..c534b58ff 100644 --- a/test/perf/perf.txt +++ b/test/perf/perf.txt @@ -1,51 +1,51 @@ -One observers many observes one - Started/Updated in 60/34 ms. -500 props observing sibling - Started/Updated in 3/11 ms. -Late dependency change - Updated in 135ms. +One observers many observes one - Started/Updated in 67/43 ms. +500 props observing sibling - Started/Updated in 6/7 ms. +Late dependency change - Updated in 150ms. Unused computables - Updated in 0 ms. -Unused observables - Updated in 25 ms. -Array reduce - Started/Updated in 57/44 ms. -Array loop - Started/Updated in 127/231 ms. -Order system batched: false tracked: true Started/Updated in 473/37 ms. -Order system batched: true tracked: true Started/Updated in 102/28 ms. -Order system batched: false tracked: false Started/Updated in 117/37 ms. -Order system batched: true tracked: false Started/Updated in 118/35 ms. +Unused observables - Updated in 15 ms. +Array reduce - Started/Updated in 96/49 ms. +Array loop - Started/Updated in 163/225 ms. +Order system batched: false tracked: true Started/Updated in 923/52 ms. +Order system batched: true tracked: true Started/Updated in 1210/38 ms. +Order system batched: false tracked: false Started/Updated in 224/64 ms. +Order system batched: true tracked: false Started/Updated in 201/46 ms. -Create array - Created in 552ms. +Create array - Created in 1434ms. -Create array (non-recursive) Created in 297ms. -Observable with many observers + dispose: 768ms -expensive sort: created 2787 -expensive sort: updated 12518 -expensive sort: disposed561 -native plain sort: updated 1087 +Create array (non-recursive) Created in 1198ms. +Observable with many observers + dispose: 1395ms +expensive sort: created 5179 +expensive sort: updated 16789 +expensive sort: disposed746 +native plain sort: updated 2074 computed memoization 1ms -create folders 0ms. -create displayfolders 1ms. -create text 471ms. -collapse folder 1ms. -uncollapse folder 1ms. -change name of folder 498ms. -search 103ms. -unsearch 438ms. -reactive folder tree [total] - 1517ms. -create folders 21ms. -create displayfolders 1ms. -create text 51ms. +create folders 1ms. +create displayfolders 3ms. +create text 1183ms. collapse folder 2ms. -uncollapse folder 5ms. -change name of folder 10ms. -search 15ms. -unsearch 24ms. +uncollapse folder 2ms. +change name of folder 1249ms. +search 225ms. +unsearch 1167ms. +reactive folder tree [total] + 3834ms. +create folders 64ms. +create displayfolders 2ms. +create text 116ms. +collapse folder 6ms. +uncollapse folder 12ms. +change name of folder 26ms. +search 54ms. +unsearch 26ms. reactive folder tree [total] - 131ms. -create boxes 89ms. -mutations 523ms. -total 625ms. -create boxes 90ms. -mutations 928ms. -total 1084ms. + 307ms. +create boxes 177ms. +mutations 1176ms. +total 1376ms. +create boxes 206ms. +mutations 1865ms. +total 2182ms. -Completed performance suite in 25.475 sec. \ No newline at end of file +Completed performance suite in 42.369 sec. \ No newline at end of file From 85510554026f0ae21f634aca055aef83d1e1a7d1 Mon Sep 17 00:00:00 2001 From: Andrey Kogut Date: Sat, 1 Apr 2017 21:43:12 +0300 Subject: [PATCH 6/7] Added test/perf/perf.txt to .gitignore --- .gitignore | 1 + test/perf/perf.txt | 51 ---------------------------------------------- 2 files changed, 1 insertion(+), 51 deletions(-) delete mode 100644 test/perf/perf.txt diff --git a/.gitignore b/.gitignore index a95f94be7..f9d22d2b4 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ notes.md lib test/babel-tests.js test/typescript/typescript-tests.js +test/perf/perf.txt .vscode dist/ .build/ diff --git a/test/perf/perf.txt b/test/perf/perf.txt deleted file mode 100644 index c534b58ff..000000000 --- a/test/perf/perf.txt +++ /dev/null @@ -1,51 +0,0 @@ - -One observers many observes one - Started/Updated in 67/43 ms. -500 props observing sibling - Started/Updated in 6/7 ms. -Late dependency change - Updated in 150ms. -Unused computables - Updated in 0 ms. -Unused observables - Updated in 15 ms. -Array reduce - Started/Updated in 96/49 ms. -Array loop - Started/Updated in 163/225 ms. -Order system batched: false tracked: true Started/Updated in 923/52 ms. -Order system batched: true tracked: true Started/Updated in 1210/38 ms. -Order system batched: false tracked: false Started/Updated in 224/64 ms. -Order system batched: true tracked: false Started/Updated in 201/46 ms. - -Create array - Created in 1434ms. - -Create array (non-recursive) Created in 1198ms. -Observable with many observers + dispose: 1395ms -expensive sort: created 5179 -expensive sort: updated 16789 -expensive sort: disposed746 -native plain sort: updated 2074 -computed memoization 1ms -create folders 1ms. -create displayfolders 3ms. -create text 1183ms. -collapse folder 2ms. -uncollapse folder 2ms. -change name of folder 1249ms. -search 225ms. -unsearch 1167ms. -reactive folder tree [total] - 3834ms. -create folders 64ms. -create displayfolders 2ms. -create text 116ms. -collapse folder 6ms. -uncollapse folder 12ms. -change name of folder 26ms. -search 54ms. -unsearch 26ms. -reactive folder tree [total] - 307ms. -create boxes 177ms. -mutations 1176ms. -total 1376ms. -create boxes 206ms. -mutations 1865ms. -total 2182ms. - - -Completed performance suite in 42.369 sec. \ No newline at end of file From 5edbcbcaa8362eba23c7423ff1be7413b88a8882 Mon Sep 17 00:00:00 2001 From: Andrey Kogut Date: Sun, 2 Apr 2017 17:34:41 +0300 Subject: [PATCH 7/7] Updated error message --- src/api/createtransformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/createtransformer.ts b/src/api/createtransformer.ts index 4cf4b3b64..c3485ed81 100644 --- a/src/api/createtransformer.ts +++ b/src/api/createtransformer.ts @@ -48,7 +48,7 @@ function getMemoizationId(object) { if(typeof object === 'string' || typeof object === 'number') return object if (object === null || typeof object !== "object") - throw new Error("[mobx] transform expected some kind of object, got: " + object); + throw new Error("[mobx] transform expected some kind of object or primitive value, got: " + object); let tid = object.$transformId; if (tid === undefined) { tid = getNextId();