From bb907d98801b279db270b7820cec04bd7ac80df0 Mon Sep 17 00:00:00 2001 From: trading_peter Date: Sat, 12 Mar 2022 22:45:49 +0100 Subject: [PATCH] Initial version --- README.md | 2 +- dataStore.js | 37 +++++++++++++++++++++++++++++++++++++ package.json | 13 +++++-------- store.js | 39 +++++++++++++++++++++++++++++++++++++++ tp-element.js | 35 ----------------------------------- 5 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 dataStore.js create mode 100644 store.js delete mode 100644 tp-element.js diff --git a/README.md b/README.md index 1ab27b7..0e6b018 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# tp-element +# tp-store diff --git a/dataStore.js b/dataStore.js new file mode 100644 index 0000000..4caf0ad --- /dev/null +++ b/dataStore.js @@ -0,0 +1,37 @@ +/** +@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 c39fdff..8a07eef 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,15 @@ { - "name": "@tp/tp-element", - "version": "0.0.1", + "name": "@tp/tp-store", + "version": "1.0.0", "description": "", - "main": "tp-element.js", + "main": "tp-store.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "https://gitea.codeblob.work/tp-elements/tp-element.git" + "url": "https://gitea.codeblob.work/tp-stores/tp-store.git" }, "author": "trading_peter", - "license": "Apache-2.0", - "dependencies": { - "lit": "^2.2.0" - } + "license": "Apache-2.0" } diff --git a/store.js b/store.js new file mode 100644 index 0000000..356b32d --- /dev/null +++ b/store.js @@ -0,0 +1,39 @@ +/** +@license +Copyright (c) 2022 trading_peter +This program is available under Apache License Version 2.0 +*/ + +import { DataStore } from './dataStore'; + +/** + * # Store + * + * A simple key value store. + */ +export const Store = function(superClass) { + return class extends superClass { + _storeSubscribe(keys) { + if (Array.isArray(keys) === false) { + keys = [ keys ]; + } + + keys.forEach(key => { + let targetProperty; + if (typeof key === 'object') { + targetProperty = key.targetProperty; + key = key.key; + } + DataStore.addInstance(this, key, targetProperty); + }); + } + + _storeUpdated(key, newValue, targetProperty) { + this[targetProperty || key] = newValue; + } + + _storeWrite(key, value) { + DataStore.writeKey(key, value); + } + }; +} diff --git a/tp-element.js b/tp-element.js deleted file mode 100644 index 6a92a2f..0000000 --- a/tp-element.js +++ /dev/null @@ -1,35 +0,0 @@ -/** -@license -Copyright (c) 2022 trading_peter -This program is available under Apache License Version 2.0 -*/ - -import { LitElement, html, css } from 'lit'; - -class TpElement extends LitElement { - static get styles() { - return [ - css` - :host { - display: block; - } - ` - ]; - } - - render() { - const { } = this; - - return html` - - `; - } - - static get properties() { - return { }; - } - - -} - -window.customElements.define('tp-element', TpElement);