diff --git a/store.js b/store.js index 91603bb15b..e285439ddf 100644 --- a/store.js +++ b/store.js @@ -78,6 +78,7 @@ assign(Store.prototype, { } Object.defineProperty(this._proto, key, { + enumerable: true, get: function() { if (store._dirty[key]) { var values = deps.map(function(dep) { @@ -162,4 +163,24 @@ assign(Store.prototype, { } }); -export default Store; \ No newline at end of file +function combineStores(store, children) { + var updates = {}; + + for (const key in children) { + const child = children[key]; + updates[key] = child.get(); + + child.onchange(state => { + var update = {}; + update[key] = state; + store.set(update); + }); + } + + console.log('updates', updates); + + store.set(updates); + return store; +} + +export { Store, combineStores }; \ No newline at end of file diff --git a/test/runtime/samples/store-binding/_config.js b/test/runtime/samples/store-binding/_config.js index c9ae020777..aefc4ec652 100644 --- a/test/runtime/samples/store-binding/_config.js +++ b/test/runtime/samples/store-binding/_config.js @@ -1,4 +1,4 @@ -import Store from '../../../../store.js'; +import { Store } from '../../../../store.js'; const store = new Store({ name: 'world' diff --git a/test/runtime/samples/store-computed/_config.js b/test/runtime/samples/store-computed/_config.js index 13fd745f53..f4e6f49e54 100644 --- a/test/runtime/samples/store-computed/_config.js +++ b/test/runtime/samples/store-computed/_config.js @@ -1,4 +1,4 @@ -import Store from '../../../../store.js'; +import { Store } from '../../../../store.js'; class MyStore extends Store { setFilter(filter) { diff --git a/test/runtime/samples/store-event/_config.js b/test/runtime/samples/store-event/_config.js index 6f192ff76f..2779db5fc2 100644 --- a/test/runtime/samples/store-event/_config.js +++ b/test/runtime/samples/store-event/_config.js @@ -1,4 +1,4 @@ -import Store from '../../../../store.js'; +import { Store } from '../../../../store.js'; class MyStore extends Store { setName(name) { diff --git a/test/runtime/samples/store/_config.js b/test/runtime/samples/store/_config.js index 0e1663315b..4e266ff095 100644 --- a/test/runtime/samples/store/_config.js +++ b/test/runtime/samples/store/_config.js @@ -1,4 +1,4 @@ -import Store from '../../../../store.js'; +import { Store } from '../../../../store.js'; const store = new Store({ name: 'world' diff --git a/test/store/index.js b/test/store/index.js index 50b6572774..258bc01c7c 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -1,5 +1,5 @@ import assert from 'assert'; -import Store from '../../store.js'; +import { Store, combineStores } from '../../store.js'; describe('store', () => { describe('get', () => { @@ -143,4 +143,40 @@ describe('store', () => { }, /'bar' is a read-only property/); }); }); + + describe('combineStores', () => { + it('merges stores', () => { + const a = new Store({ + x: 1, + y: 2 + }); + + a.compute('z', ['x', 'y'], (x, y) => x + y); + + const b = new Store({ + x: 3, + y: 4 + }); + + b.compute('z', ['x', 'y'], (x, y) => x + y); + + const c = combineStores(new Store(), { a, b }); + + c.compute('total', ['a', 'b'], (a, b) => a.z + b.z); + + assert.deepEqual(c.get(), { + a: { + x: 1, + y: 2, + z: 3 + }, + b: { + x: 3, + y: 4, + z: 7 + }, + total: 10 + }); + }); + }); });