Skip to content

Commit

Permalink
feat(singleton-manager): add node/ssr support
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse authored Nov 21, 2023
1 parent 64a1b65 commit ef9b1e4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-melons-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'singleton-manager': patch
---

add node/ssr support
1 change: 1 addition & 0 deletions packages/singleton-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"start:singleton": "es-dev-server -c demo/singleton/server.js",
"start:singleton-complex": "es-dev-server -c demo/singleton-complex/server.js",
"test": "cd ../../ && npm run test:browser -- --group singleton-manager",
"test:node": "mocha test-node",
"types": "wireit"
},
"keywords": [
Expand Down
11 changes: 10 additions & 1 deletion packages/singleton-manager/src/SingletonManagerClass.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const sym = Symbol.for('lion::SingletonManagerClassStorage');

/**
* Allow compatibility with node-js (for ssr).
* In the future, we can just use globalThis directly
* (for now, we're backwards compatible with browsers that still only use window, since we don't know all contexts singleton-manager is used in).
*/
// eslint-disable-next-line no-undef
const globalThisOrWindow = globalThis || window;
export class SingletonManagerClass {
constructor() {
/** @protected */
this._map = window[sym] ? window[sym] : (window[sym] = new Map());
this._map = globalThisOrWindow[sym]
? globalThisOrWindow[sym]
: (globalThisOrWindow[sym] = new Map());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { expect } from 'chai';
import { runSingletonManagerClassSuite } from '../test-suites/runSingletonManagerClassSuite.suite.js';

runSingletonManagerClassSuite({ expect });
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SingletonManagerClass } from 'singleton-manager';

/**
* @param {{expect: function}} config
*/
export function runSingletonManagerClassSuite({ expect }) {
describe('SingletonManagerClass', () => {
it('returns undefined and has false if not set', async () => {
const mngr = new SingletonManagerClass();
expect(mngr.get('overlays/overlays.js::0.13.x')).to.be.undefined;
expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.false;
});

it('return value and has true if set', () => {
const mngr = new SingletonManagerClass();
mngr.set('overlays/overlays.js::0.13.x', 'is-set');
expect(mngr.get('overlays/overlays.js::0.13.x')).to.equal('is-set');
expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.true;
// make sure non set values are still correct
expect(mngr.get('overlays/overlays.js::0.14.x')).to.be.undefined;
expect(mngr.has('overlays/overlays.js::0.14.x')).to.be.false;
});

it('does not override existing keys (e.g. subsequential calls for the same keys are ignored)', () => {
const mngr = new SingletonManagerClass();
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');
});
});
}
36 changes: 2 additions & 34 deletions packages/singleton-manager/test/SingletonManagerClass.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
import { expect } from '@open-wc/testing';
import { runSingletonManagerClassSuite } from '../test-suites/runSingletonManagerClassSuite.suite.js';

import { SingletonManagerClass } from 'singleton-manager';

describe('SingletonManagerClass', () => {
it('returns undefined and has false if not set', async () => {
const mngr = new SingletonManagerClass();
expect(mngr.get('overlays/overlays.js::0.13.x')).to.be.undefined;
expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.false;
});

it('return value and has true if set', () => {
const mngr = new SingletonManagerClass();
mngr.set('overlays/overlays.js::0.13.x', 'is-set');
expect(mngr.get('overlays/overlays.js::0.13.x')).to.equal('is-set');
expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.true;
// make sure non set values are still correct
expect(mngr.get('overlays/overlays.js::0.14.x')).to.be.undefined;
expect(mngr.has('overlays/overlays.js::0.14.x')).to.be.false;
});

it('does not override existing keys (e.g. subsequential calls for the same keys are ignored)', () => {
const mngr = new SingletonManagerClass();
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');
});
});
runSingletonManagerClassSuite({ expect });
4 changes: 2 additions & 2 deletions packages/singleton-manager/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist-types",
"rootDir": ".",
"rootDir": "."
},
"include": ["src", "test", "types"],
"include": ["src", "test", "test-node", "test-suites", "types"],
"exclude": ["dist-types"]
}

0 comments on commit ef9b1e4

Please sign in to comment.