diff --git a/dataStore.js b/dataStore.js deleted file mode 100644 index 4caf0ad..0000000 --- a/dataStore.js +++ /dev/null @@ -1,37 +0,0 @@ -/** -@license -Copyright (c) 2022 trading_peter -This program is available under Apache License Version 2.0 -*/ - -const data = new Map(); -const instancesPerKey = new Map(); - -export const DataStore = new class { - addInstance(instance, key, targetProperty) { - if (instancesPerKey.has(key) === false) { - instancesPerKey.set(key, [ { instance, targetProperty } ]); - } else { - instancesPerKey.get(key).push({ instance, targetProperty }); - } - - if (data.has(key)) { - this._notifyInstance(instance, key, data.get(key), targetProperty); - } - } - - writeKey(key, value) { - data.set(key, value); - - const instances = instancesPerKey.get(key); - if (Array.isArray(instances)) { - instances.forEach(entry => { - this._notifyInstance(entry.instance, key, value, entry.targetProperty); - }); - } - } - - _notifyInstance(instance, key, value, targetProperty) { - instance._storeUpdated(key, value, targetProperty); - } -}; diff --git a/package.json b/package.json index 8a07eef..3ed8a2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tp/tp-store", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "tp-store.js", "scripts": { diff --git a/store.js b/store.js index 356b32d..74e5f52 100644 --- a/store.js +++ b/store.js @@ -4,7 +4,8 @@ Copyright (c) 2022 trading_peter This program is available under Apache License Version 2.0 */ -import { DataStore } from './dataStore'; +const data = new Map(); +const instancesPerKey = new Map(); /** * # Store @@ -13,7 +14,7 @@ import { DataStore } from './dataStore'; */ export const Store = function(superClass) { return class extends superClass { - _storeSubscribe(keys) { + storeSubscribe(keys) { if (Array.isArray(keys) === false) { keys = [ keys ]; } @@ -24,16 +25,43 @@ export const Store = function(superClass) { targetProperty = key.targetProperty; key = key.key; } - DataStore.addInstance(this, key, targetProperty); + this._addInstance(this, key, targetProperty); }); } - _storeUpdated(key, newValue, targetProperty) { + storeUpdated(key, newValue, targetProperty) { this[targetProperty || key] = newValue; } - _storeWrite(key, value) { - DataStore.writeKey(key, value); + storeWrite(key, value) { + this._writeKey(key, value); + } + + _addInstance(instance, key, targetProperty) { + if (instancesPerKey.has(key) === false) { + instancesPerKey.set(key, [ { instance, targetProperty } ]); + } else { + instancesPerKey.get(key).push({ instance, targetProperty }); + } + + if (data.has(key)) { + this._notifyInstance(instance, key, data.get(key), targetProperty); + } + } + + _writeKey(key, value) { + data.set(key, value); + + const instances = instancesPerKey.get(key); + if (Array.isArray(instances)) { + instances.forEach(entry => { + this._notifyInstance(entry.instance, key, value, entry.targetProperty); + }); + } + } + + _notifyInstance(instance, key, value, targetProperty) { + instance._storeUpdated(key, value, targetProperty); } }; }