Skip to content

Commit

Permalink
Do not flood keys with same value in meta in sessionStorageCache an…
Browse files Browse the repository at this point in the history
…d `localStorageCache`
  • Loading branch information
igorkamyshev committed Mar 15, 2023
1 parent 0822244 commit 4c7f25f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-eels-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Do not flood keys with same value in meta in `sessionStorageCache` and `localStorageCache`
19 changes: 19 additions & 0 deletions packages/core/src/cache/adapters/__test__/local_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { createEvent, fork, scopeBind } from 'effector';
import { describe, beforeEach, test, expect, vi } from 'vitest';

import { META_KEY } from '../browser_storage';
import { localStorageCache } from '../local_storage';

describe('localStorageCache', () => {
Expand Down Expand Up @@ -53,4 +54,22 @@ describe('localStorageCache', () => {
expect(listener).toBeCalledTimes(1);
expect(listener).toBeCalledWith({ key: 'key' });
});

test('do not flood keys with same value in meta', async () => {
const cache = localStorageCache();

const scope = fork();

await scopeBind(cache.set, {
scope,
})({ key: 'key', value: 'myValue' });

await scopeBind(cache.set, {
scope,
})({ key: 'key', value: 'myValue' });

expect(JSON.parse(localStorage.getItem(META_KEY)!)).toEqual({
keys: ['key'],
});
});
});
19 changes: 19 additions & 0 deletions packages/core/src/cache/adapters/__test__/session_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { createEvent, fork, scopeBind } from 'effector';
import { describe, beforeEach, test, expect, vi } from 'vitest';

import { META_KEY } from '../browser_storage';
import { sessionStorageCache } from '../session_storage';

describe('sessionStorageCache', () => {
Expand Down Expand Up @@ -53,4 +54,22 @@ describe('sessionStorageCache', () => {
expect(listener).toBeCalledTimes(1);
expect(listener).toBeCalledWith({ key: 'key' });
});

test('do not flood keys with same value in meta', async () => {
const cache = sessionStorageCache();

const scope = fork();

await scopeBind(cache.set, {
scope,
})({ key: 'key', value: 'myValue' });

await scopeBind(cache.set, {
scope,
})({ key: 'key', value: 'myValue' });

expect(JSON.parse(sessionStorage.getItem(META_KEY)!)).toEqual({
keys: ['key'],
});
});
});
14 changes: 11 additions & 3 deletions packages/core/src/cache/adapters/browser_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { attachObservability } from './observability';
import { CacheAdapter, CacheAdapterOptions } from './type';
import { get } from '../../libs/lohyphen';

export const META_KEY = '__farfetched_meta__';

export function browserStorageCache(
config: {
storage: () => Storage;
Expand Down Expand Up @@ -132,8 +134,6 @@ export function browserStorageCache(
}

// -- meta storage
const META_KEY = '__farfetched_meta__';

const $meta = createStore<Meta | null>(null, { serialize: 'ignore' });

const getMetaFx = createEffect(async () => {
Expand Down Expand Up @@ -168,7 +168,15 @@ export function browserStorageCache(
sample({
clock: addKey,
source: $meta,
fn: (meta, { key }) => ({ ...meta, keys: [...(meta?.keys ?? []), key] }),
fn: (meta, { key }) => {
const knownKeys = meta?.keys ?? [];

if (knownKeys.includes(key)) {
return meta;
}

return { ...meta, keys: [...knownKeys, key] };
},
target: $meta,
});
sample({
Expand Down

0 comments on commit 4c7f25f

Please sign in to comment.