fix(localize): reset storage correctly
long running imports were polluting newly reset storage
This commit is contained in:
parent
989f24b4ea
commit
5ca3d27501
3 changed files with 37 additions and 2 deletions
5
.changeset/mighty-cars-grow.md
Normal file
5
.changeset/mighty-cars-grow.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@lion/localize': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix localize race condition where data was being added while namespace loader promise was no longer in cache.
|
||||||
|
|
@ -340,8 +340,14 @@ export class LocalizeManager {
|
||||||
* @param {Object} obj.default
|
* @param {Object} obj.default
|
||||||
*/
|
*/
|
||||||
obj => {
|
obj => {
|
||||||
const data = isLocalizeESModule(obj) ? obj.default : obj;
|
// add data only if we have the promise in cache
|
||||||
this.addData(locale, namespace, data);
|
if (
|
||||||
|
this.__namespaceLoaderPromisesCache[locale] &&
|
||||||
|
this.__namespaceLoaderPromisesCache[locale][namespace] === loaderPromise
|
||||||
|
) {
|
||||||
|
const data = isLocalizeESModule(obj) ? obj.default : obj;
|
||||||
|
this.addData(locale, namespace, data);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,30 @@ describe('LocalizeManager', () => {
|
||||||
expect(document.documentElement.lang).to.equal('en-GB');
|
expect(document.documentElement.lang).to.equal('en-GB');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('empties storage after reset() is invoked', async () => {
|
||||||
|
manager = new LocalizeManager();
|
||||||
|
|
||||||
|
let deferredResolve;
|
||||||
|
manager.loadNamespace({
|
||||||
|
generic: () =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
deferredResolve = () => resolve({ greeting: 'Hello!' });
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { loadingComplete } = manager;
|
||||||
|
|
||||||
|
manager.reset();
|
||||||
|
expect(getProtectedMembers(manager).storage).to.be.empty;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
deferredResolve();
|
||||||
|
await loadingComplete;
|
||||||
|
|
||||||
|
// storage still needs to be empty after promise is fulfilled.
|
||||||
|
expect(getProtectedMembers(manager).storage).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
it('has teardown() method removing all side effects', () => {
|
it('has teardown() method removing all side effects', () => {
|
||||||
manager = new LocalizeManager();
|
manager = new LocalizeManager();
|
||||||
const disconnectObserverSpy = sinon.spy(
|
const disconnectObserverSpy = sinon.spy(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue