Skip to content

Commit

Permalink
Fixed initial value persistence in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Jun 17, 2024
1 parent 1c6dff6 commit 315c91e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/plugin/synchronizeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export default function synchronizeStorage<Value = any>(
}
};

receiveState(storage.getItem(executorStorageKey));
if (executor.task === null) {
receiveState(storage.getItem(executorStorageKey));
}

executor.subscribe(event => {
switch (event.type) {
Expand All @@ -129,16 +131,19 @@ export default function synchronizeStorage<Value = any>(
case 'fulfilled':
case 'rejected':
case 'invalidated':
if (!executor.isActive) {
break;
}
const stateStr = serializer.stringify(executor.toJSON());

if (latestStateStr !== stateStr) {
storage.setItem(executorStorageKey, stateStr);
}
break;

case 'aborted':
if (!executor.isSettled) {
receiveState(storage.getItem(executorStorageKey));
}
break;

case 'deactivated':
if (typeof window !== 'undefined') {
window.removeEventListener('storage', handleStorage);
Expand Down
35 changes: 35 additions & 0 deletions src/test/plugin/synchronizeStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,41 @@ describe('synchronizeStorage', () => {
);
});

test('sets storage item to the initial value', () => {
const executor = manager.getOrCreate('xxx', 'aaa', [synchronizeStorage(localStorage)]);

expect(executor.value).toBe('aaa');
expect(localStorage.getItem('"xxx"')).toBe(
'{"key":"xxx","isFulfilled":true,"value":"aaa","annotations":{},"settledAt":50,"invalidatedAt":0}'
);
});

test('does not resolve the executor if initial task is provided', async () => {
const executor = manager.getOrCreate('xxx', () => 'aaa', [synchronizeStorage(localStorage)]);

expect(executor.value).toBeUndefined();

await executor.getOrAwait();

expect(executor.value).toBe('aaa');

expect(localStorage.getItem('"xxx"')).toBe(
'{"key":"xxx","isFulfilled":true,"value":"aaa","annotations":{},"settledAt":50,"invalidatedAt":0}'
);
});

test('resolves executor if initial task was aborted', () => {
localStorage.setItem('"xxx"', '{"key":"xxx","isFulfilled":true,"value":"aaa","settledAt":30,"invalidatedAt":0}');

const executor = manager.getOrCreate('xxx', () => 'bbb', [synchronizeStorage(localStorage)]);
executor.abort();

expect(executor.value).toBe('aaa');
expect(localStorage.getItem('"xxx"')).toBe(
'{"key":"xxx","isFulfilled":true,"value":"aaa","annotations":{},"settledAt":30,"invalidatedAt":0}'
);
});

test('does not set storage item or resolve an executor if an executor is pending', async () => {
const executor = manager.getOrCreate('xxx', undefined, [
executor => {
Expand Down

0 comments on commit 315c91e

Please sign in to comment.