@encodable/registry
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.5.0](https://github.com/apache-superset/encodable/compare/@encodable/registry@0.4.0...@encodable/registry@0.5.0) (2020-08-14) | ||
### Features | ||
* **color:** add .register() that automatically use scheme.id as key ([#55](https://github.com/apache-superset/encodable/issues/55)) ([ad7b106](https://github.com/apache-superset/encodable/commit/ad7b106a077310ab295f536046b18e1e5dc811d2)) | ||
# [0.4.0](https://github.com/apache-superset/encodable/compare/@encodable/registry@0.3.0...@encodable/registry@0.4.0) (2020-08-13) | ||
@@ -8,0 +19,0 @@ |
@@ -6,3 +6,3 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import OverwritePolicy from './OverwritePolicy'; | ||
import createRegistryStore from './createRegistryStore'; | ||
import createRegistryState from './createRegistryState'; | ||
/** | ||
@@ -22,8 +22,8 @@ * Registry class | ||
constructor(config = {}) { | ||
_defineProperty(this, "store", void 0); | ||
_defineProperty(this, "state", void 0); | ||
if (typeof config.globalId === 'undefined') { | ||
this.store = createRegistryStore(config); | ||
this.state = createRegistryState(config); | ||
} else { | ||
this.store = globalBox().getOrCreate(config.globalId, () => createRegistryStore(config)); | ||
this.state = globalBox().getOrCreate(config.globalId, () => createRegistryState(config)); | ||
} | ||
@@ -38,5 +38,5 @@ } | ||
clear() { | ||
this.store.items = {}; | ||
this.store.promises = {}; | ||
this.store.defaultKey = this.store.initialDefaultKey; | ||
this.state.items = {}; | ||
this.state.promises = {}; | ||
this.state.defaultKey = this.state.initialDefaultKey; | ||
return this; | ||
@@ -51,3 +51,3 @@ } | ||
has(key) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
return item !== null && item !== undefined; | ||
@@ -63,9 +63,9 @@ } | ||
registerValue(key, value) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
const willOverwrite = this.has(key) && ('value' in item && item.value !== value || 'loader' in item); | ||
if (willOverwrite) { | ||
if (this.store.overwritePolicy === OverwritePolicy.WARN) { | ||
if (this.state.overwritePolicy === OverwritePolicy.WARN) { | ||
console.warn("Item with key \"" + key + "\" already exists. You are assigning a new value."); | ||
} else if (this.store.overwritePolicy === OverwritePolicy.PROHIBIT) { | ||
} else if (this.state.overwritePolicy === OverwritePolicy.PROHIBIT) { | ||
throw new Error("Item with key \"" + key + "\" already exists. Cannot overwrite."); | ||
@@ -76,11 +76,11 @@ } | ||
if (!item || willOverwrite) { | ||
this.store.items[key] = { | ||
this.state.items[key] = { | ||
value | ||
}; | ||
delete this.store.promises[key]; | ||
delete this.state.promises[key]; | ||
} // If there is no default, set as default | ||
if (this.store.setFirstItemAsDefault && !this.store.defaultKey) { | ||
this.store.defaultKey = key; | ||
if (this.state.setFirstItemAsDefault && !this.state.defaultKey) { | ||
this.state.defaultKey = key; | ||
} | ||
@@ -98,9 +98,9 @@ | ||
registerLoader(key, loader) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
const willOverwrite = this.has(key) && ('loader' in item && item.loader !== loader || 'value' in item); | ||
if (willOverwrite) { | ||
if (this.store.overwritePolicy === OverwritePolicy.WARN) { | ||
if (this.state.overwritePolicy === OverwritePolicy.WARN) { | ||
console.warn("Item with key \"" + key + "\" already exists. You are assigning a new value."); | ||
} else if (this.store.overwritePolicy === OverwritePolicy.PROHIBIT) { | ||
} else if (this.state.overwritePolicy === OverwritePolicy.PROHIBIT) { | ||
throw new Error("Item with key \"" + key + "\" already exists. Cannot overwrite."); | ||
@@ -111,11 +111,11 @@ } | ||
if (!item || willOverwrite) { | ||
this.store.items[key] = { | ||
this.state.items[key] = { | ||
loader | ||
}; | ||
delete this.store.promises[key]; | ||
delete this.state.promises[key]; | ||
} // If there is no default, set as default | ||
if (this.store.setFirstItemAsDefault && !this.store.defaultKey) { | ||
this.store.defaultKey = key; | ||
if (this.state.setFirstItemAsDefault && !this.state.defaultKey) { | ||
this.state.defaultKey = key; | ||
} | ||
@@ -133,5 +133,5 @@ | ||
get(key) { | ||
const targetKey = key != null ? key : this.store.defaultKey; | ||
const targetKey = key != null ? key : this.state.defaultKey; | ||
if (typeof targetKey === 'undefined') return undefined; | ||
const item = this.store.items[targetKey]; | ||
const item = this.state.items[targetKey]; | ||
@@ -156,3 +156,3 @@ if (item !== undefined) { | ||
getAsPromise(key) { | ||
const promise = this.store.promises[key]; | ||
const promise = this.state.promises[key]; | ||
@@ -167,3 +167,3 @@ if (typeof promise !== 'undefined') { | ||
const newPromise = Promise.resolve(item); | ||
this.store.promises[key] = newPromise; | ||
this.state.promises[key] = newPromise; | ||
return newPromise; | ||
@@ -181,3 +181,3 @@ } | ||
getDefaultKey() { | ||
return this.store.defaultKey; | ||
return this.state.defaultKey; | ||
} | ||
@@ -192,3 +192,3 @@ /** | ||
setDefaultKey(key) { | ||
this.store.defaultKey = key; | ||
this.state.defaultKey = key; | ||
return this; | ||
@@ -203,3 +203,3 @@ } | ||
clearDefaultKey() { | ||
this.store.defaultKey = undefined; | ||
this.state.defaultKey = undefined; | ||
return this; | ||
@@ -238,3 +238,3 @@ } | ||
keys() { | ||
return Object.keys(this.store.items); | ||
return Object.keys(this.state.items); | ||
} | ||
@@ -289,4 +289,4 @@ /** | ||
remove(key) { | ||
delete this.store.items[key]; | ||
delete this.store.promises[key]; | ||
delete this.state.items[key]; | ||
delete this.state.promises[key]; | ||
return this; | ||
@@ -293,0 +293,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { RegistryStore, RegistryConfig } from '../types'; | ||
import { RegistryState, RegistryConfig } from '../types'; | ||
/** | ||
@@ -14,3 +14,3 @@ * Registry class | ||
export default class Registry<V, L extends V | Promise<V> = V | Promise<V>> { | ||
readonly store: RegistryStore<V, L>; | ||
readonly state: RegistryState<V, L>; | ||
constructor(config?: RegistryConfig); | ||
@@ -17,0 +17,0 @@ /** |
@@ -10,3 +10,3 @@ "use strict"; | ||
var _createRegistryStore = _interopRequireDefault(require("./createRegistryStore")); | ||
var _createRegistryState = _interopRequireDefault(require("./createRegistryState")); | ||
@@ -30,8 +30,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
constructor(config = {}) { | ||
_defineProperty(this, "store", void 0); | ||
_defineProperty(this, "state", void 0); | ||
if (typeof config.globalId === 'undefined') { | ||
this.store = (0, _createRegistryStore.default)(config); | ||
this.state = (0, _createRegistryState.default)(config); | ||
} else { | ||
this.store = (0, _globalBox.globalBox)().getOrCreate(config.globalId, () => (0, _createRegistryStore.default)(config)); | ||
this.state = (0, _globalBox.globalBox)().getOrCreate(config.globalId, () => (0, _createRegistryState.default)(config)); | ||
} | ||
@@ -46,5 +46,5 @@ } | ||
clear() { | ||
this.store.items = {}; | ||
this.store.promises = {}; | ||
this.store.defaultKey = this.store.initialDefaultKey; | ||
this.state.items = {}; | ||
this.state.promises = {}; | ||
this.state.defaultKey = this.state.initialDefaultKey; | ||
return this; | ||
@@ -59,3 +59,3 @@ } | ||
has(key) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
return item !== null && item !== undefined; | ||
@@ -71,9 +71,9 @@ } | ||
registerValue(key, value) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
const willOverwrite = this.has(key) && ('value' in item && item.value !== value || 'loader' in item); | ||
if (willOverwrite) { | ||
if (this.store.overwritePolicy === _OverwritePolicy.default.WARN) { | ||
if (this.state.overwritePolicy === _OverwritePolicy.default.WARN) { | ||
console.warn("Item with key \"" + key + "\" already exists. You are assigning a new value."); | ||
} else if (this.store.overwritePolicy === _OverwritePolicy.default.PROHIBIT) { | ||
} else if (this.state.overwritePolicy === _OverwritePolicy.default.PROHIBIT) { | ||
throw new Error("Item with key \"" + key + "\" already exists. Cannot overwrite."); | ||
@@ -84,11 +84,11 @@ } | ||
if (!item || willOverwrite) { | ||
this.store.items[key] = { | ||
this.state.items[key] = { | ||
value | ||
}; | ||
delete this.store.promises[key]; | ||
delete this.state.promises[key]; | ||
} // If there is no default, set as default | ||
if (this.store.setFirstItemAsDefault && !this.store.defaultKey) { | ||
this.store.defaultKey = key; | ||
if (this.state.setFirstItemAsDefault && !this.state.defaultKey) { | ||
this.state.defaultKey = key; | ||
} | ||
@@ -106,9 +106,9 @@ | ||
registerLoader(key, loader) { | ||
const item = this.store.items[key]; | ||
const item = this.state.items[key]; | ||
const willOverwrite = this.has(key) && ('loader' in item && item.loader !== loader || 'value' in item); | ||
if (willOverwrite) { | ||
if (this.store.overwritePolicy === _OverwritePolicy.default.WARN) { | ||
if (this.state.overwritePolicy === _OverwritePolicy.default.WARN) { | ||
console.warn("Item with key \"" + key + "\" already exists. You are assigning a new value."); | ||
} else if (this.store.overwritePolicy === _OverwritePolicy.default.PROHIBIT) { | ||
} else if (this.state.overwritePolicy === _OverwritePolicy.default.PROHIBIT) { | ||
throw new Error("Item with key \"" + key + "\" already exists. Cannot overwrite."); | ||
@@ -119,11 +119,11 @@ } | ||
if (!item || willOverwrite) { | ||
this.store.items[key] = { | ||
this.state.items[key] = { | ||
loader | ||
}; | ||
delete this.store.promises[key]; | ||
delete this.state.promises[key]; | ||
} // If there is no default, set as default | ||
if (this.store.setFirstItemAsDefault && !this.store.defaultKey) { | ||
this.store.defaultKey = key; | ||
if (this.state.setFirstItemAsDefault && !this.state.defaultKey) { | ||
this.state.defaultKey = key; | ||
} | ||
@@ -141,5 +141,5 @@ | ||
get(key) { | ||
const targetKey = key != null ? key : this.store.defaultKey; | ||
const targetKey = key != null ? key : this.state.defaultKey; | ||
if (typeof targetKey === 'undefined') return undefined; | ||
const item = this.store.items[targetKey]; | ||
const item = this.state.items[targetKey]; | ||
@@ -164,3 +164,3 @@ if (item !== undefined) { | ||
getAsPromise(key) { | ||
const promise = this.store.promises[key]; | ||
const promise = this.state.promises[key]; | ||
@@ -175,3 +175,3 @@ if (typeof promise !== 'undefined') { | ||
const newPromise = Promise.resolve(item); | ||
this.store.promises[key] = newPromise; | ||
this.state.promises[key] = newPromise; | ||
return newPromise; | ||
@@ -189,3 +189,3 @@ } | ||
getDefaultKey() { | ||
return this.store.defaultKey; | ||
return this.state.defaultKey; | ||
} | ||
@@ -200,3 +200,3 @@ /** | ||
setDefaultKey(key) { | ||
this.store.defaultKey = key; | ||
this.state.defaultKey = key; | ||
return this; | ||
@@ -211,3 +211,3 @@ } | ||
clearDefaultKey() { | ||
this.store.defaultKey = undefined; | ||
this.state.defaultKey = undefined; | ||
return this; | ||
@@ -246,3 +246,3 @@ } | ||
keys() { | ||
return Object.keys(this.store.items); | ||
return Object.keys(this.state.items); | ||
} | ||
@@ -297,4 +297,4 @@ /** | ||
remove(key) { | ||
delete this.store.items[key]; | ||
delete this.store.promises[key]; | ||
delete this.state.items[key]; | ||
delete this.state.promises[key]; | ||
return this; | ||
@@ -301,0 +301,0 @@ } |
import OverwritePolicy from '../models/OverwritePolicy'; | ||
interface ItemWithValue<V> { | ||
/** stored value */ | ||
/** item value */ | ||
value: V; | ||
@@ -10,3 +10,3 @@ } | ||
} | ||
export interface RegistryStore<V, L extends V | Promise<V>> { | ||
export interface RegistryState<V, L extends V | Promise<V>> { | ||
/** | ||
@@ -13,0 +13,0 @@ * If this is a global registry, it will be defined. |
{ | ||
"name": "@encodable/registry", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Reusable registry models", | ||
@@ -36,3 +36,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "2123b8cd1b8647d9a0c8a69e068027b65a4a7f2a" | ||
"gitHead": "f63359aa7baa94f2ef371bf1030257ca49769bb5" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44685