singleton-manager
Advanced tools
Comparing version 1.1.2 to 1.2.0
# Change Log | ||
## 1.2.0 | ||
### Minor Changes | ||
- 39d5e767: Changed on how to handle multiple instances of the singleton manager | ||
- save the map instance on the window object so multiple singleton manager versions can share the data. | ||
- ignore subsequential set calls if the key is already set (did throw before) | ||
## 1.1.2 | ||
@@ -4,0 +13,0 @@ |
{ | ||
"name": "singleton-manager", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Manage singletons across multiple major versions so they converge to a single instance", | ||
@@ -26,2 +26,5 @@ "license": "MIT", | ||
"scripts": { | ||
"debug": "cd ../../ && npm run debug -- --group singleton-manager", | ||
"debug:firefox": "cd ../../ && npm run debug:firefox -- --group singleton-manager", | ||
"debug:webkit": "cd ../../ && npm run debug:webkit -- --group singleton-manager", | ||
"prepublishOnly": "../../scripts/npm-prepublish.js", | ||
@@ -31,4 +34,3 @@ "start:fail": "es-dev-server -c demo/fail/server.js", | ||
"start:singleton-complex": "es-dev-server -c demo/singleton-complex/server.js", | ||
"test": "cd ../../ && yarn test:browser --grep \"packages/singleton-manager/test/**/*.test.js\"", | ||
"test:watch": "cd ../../ && yarn test:browser:watch --grep \"packages/singleton-manager/test/**/*.test.js\"" | ||
"test": "cd ../../ && npm run test:browser -- --group singleton-manager" | ||
}, | ||
@@ -35,0 +37,0 @@ "sideEffects": false, |
@@ -56,3 +56,3 @@ # Singleton Manager | ||
Overriding version is an App level concern hence components or "features" are not allowed to use it. | ||
If you try to call multiple times for the same key then it will throw an error. | ||
If you try to call it multiple times for the same key then it will be ignored. | ||
@@ -64,5 +64,6 @@ ```js | ||
// somewhere in a dependency | ||
singletonManager.set('my-singleton/index.js::1.x', compatibleSingleton); | ||
singletonManager.set('my-singleton/index.js::1.x', otherSingleton); | ||
// will throw an error that it is already defined | ||
// .get('my-singleton/index.js::1.x') will always return the first set value | ||
// e.g. the app can set it and no one can later override it | ||
``` | ||
@@ -69,0 +70,0 @@ |
export class SingletonManagerClass { | ||
_map: Map<any, any>; | ||
_map: any; | ||
/** | ||
* Ignores already existing keys (e.g. it will not override) | ||
* | ||
* @param {string} key | ||
* @param {any} value | ||
* @throws {Error} Will throw if the key is already defined | ||
*/ | ||
@@ -17,3 +18,3 @@ set(key: string, value: any): void; | ||
*/ | ||
has(key: string): boolean; | ||
has(key: string): any; | ||
} |
@@ -0,16 +1,18 @@ | ||
const sym = Symbol.for('lion::SingletonManagerClassStorage'); | ||
export class SingletonManagerClass { | ||
constructor() { | ||
this._map = new Map(); | ||
this._map = window[sym] ? window[sym] : (window[sym] = new Map()); | ||
} | ||
/** | ||
* Ignores already existing keys (e.g. it will not override) | ||
* | ||
* @param {string} key | ||
* @param {any} value | ||
* @throws {Error} Will throw if the key is already defined | ||
*/ | ||
set(key, value) { | ||
if (this.has(key)) { | ||
throw new Error(`The key "${key}" is already defined and can not be overridden.`); | ||
if (!this.has(key)) { | ||
this._map.set(key, value); | ||
} | ||
this._map.set(key, value); | ||
} | ||
@@ -17,0 +19,0 @@ |
@@ -22,11 +22,16 @@ import { expect } from '@open-wc/testing'; | ||
it('throws if an app tries to set an existing key again', () => { | ||
it('does not override existing keys (e.g. subsequentual calls for the same keys are ignored)', () => { | ||
const mngr = new SingletonManagerClass(); | ||
mngr.set('overlays/overlays.js::0.13.x', 'is-set'); | ||
expect(() => { | ||
mngr.set('overlays/overlays.js::0.13.x', 'new-set'); | ||
}).to.throw( | ||
'The key "overlays/overlays.js::0.13.x" is already defined and can not be overridden.', | ||
); | ||
mngr.set('overlays/overlays.js::0.14.x', 'is-set'); | ||
mngr.set('overlays/overlays.js::0.14.x', 'new-set'); | ||
expect(mngr.get('overlays/overlays.js::0.14.x')).to.equal('is-set'); | ||
}); | ||
it('should return the same value with two SingletonManager instances', () => { | ||
const mngr1 = new SingletonManagerClass(); | ||
const mngr2 = new SingletonManagerClass(); | ||
mngr1.set('overlays/overlays.js::0.15.x', 'is-set'); | ||
expect(mngr2.get('overlays/overlays.js::0.15.x')).to.equal('is-set'); | ||
}); | ||
}); |
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
15136
97
310