Simplify
This commit is contained in:
parent
bb907d9880
commit
6c0abed855
37
dataStore.js
37
dataStore.js
@ -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);
|
||||
}
|
||||
};
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tp/tp-store",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "",
|
||||
"main": "tp-store.js",
|
||||
"scripts": {
|
||||
|
40
store.js
40
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user