refactor: remove sync storage (#2269)

This commit is contained in:
Himself65
2023-05-09 06:21:42 +08:00
committed by GitHub
parent 3a6be4510b
commit 95bc5cac49
9 changed files with 22 additions and 102 deletions
+1 -1
View File
@@ -23,7 +23,7 @@
"@affine/debug": "workspace:*",
"@affine/i18n": "workspace:*",
"@affine/jotai": "workspace:*",
"@affine/workspace": "workspace:^",
"@affine/workspace": "workspace:*",
"@dnd-kit/core": "^6.0.8",
"@dnd-kit/sortable": "^7.0.2",
"@emotion/cache": "^11.11.0",
@@ -1,36 +0,0 @@
/**
* @vitest-environment happy-dom
*/
import { atomWithSyncStorage } from '@affine/jotai';
import { createStore } from 'jotai';
import { afterEach, describe, expect, test } from 'vitest';
afterEach(() => {
localStorage.clear();
});
describe('atomWithSyncStorage', () => {
test('basic', () => {
const store = createStore();
{
[0, '0', false, null, undefined, NaN].forEach(value => {
const atom = atomWithSyncStorage('test', value);
expect(store.get(atom)).toBe(value);
});
}
});
test('mutate', () => {
{
const store = createStore();
const atom = atomWithSyncStorage('test', 0);
expect(store.get(atom)).toBe(0);
store.set(atom, 1);
expect(store.get(atom)).toBe(1);
}
{
const store = createStore();
const atom = atomWithSyncStorage('test', 0);
expect(store.get(atom)).toBe(1);
}
});
});
-49
View File
@@ -1,50 +1 @@
import { createJSONStorage, RESET } from 'jotai/utils';
import { atom } from 'jotai/vanilla';
const storage = createJSONStorage<any>(() =>
typeof window !== 'undefined'
? window.localStorage
: (undefined as unknown as Storage)
);
type SetStateActionWithReset<Value> =
| Value
| typeof RESET
| ((prev: Value) => Value | typeof RESET);
// similar to atomWithStorage, but will not trigger twice on init
// https://github.com/pmndrs/jotai/discussions/1737
export function atomWithSyncStorage<Value>(key: string, initialValue: Value) {
const storedValue = storage.getItem(key, initialValue) as Value;
const _value =
typeof storedValue === 'symbol'
? initialValue
: (storage.getItem(key, initialValue) as Value);
const baseAtom = atom(_value);
baseAtom.onMount = setAtom => {
if (storage.subscribe) {
return storage.subscribe(key, setAtom, initialValue);
}
};
const anAtom = atom(
get => get(baseAtom),
(get, set, update: SetStateActionWithReset<Value>) => {
const nextValue =
typeof update === 'function'
? (update as (prev: Value) => Value | typeof RESET)(get(baseAtom))
: update;
if (nextValue === RESET) {
set(baseAtom, initialValue);
return storage.removeItem(key);
}
set(baseAtom, nextValue);
return storage.setItem(key, nextValue);
}
);
return anAtom;
}
export * from './resource';
+5 -3
View File
@@ -1,5 +1,7 @@
import { atomWithSyncStorage } from '@affine/jotai';
import type { AccessTokenMessage } from '@affine/workspace/affine/login';
import { atomWithStorage } from 'jotai/utils';
export const currentAffineUserAtom =
atomWithSyncStorage<AccessTokenMessage | null>('affine-user-atom', null);
export const currentAffineUserAtom = atomWithStorage<AccessTokenMessage | null>(
'affine-user-atom',
null
);
+1 -2
View File
@@ -1,4 +1,3 @@
import { atomWithSyncStorage } from '@affine/jotai';
import type { EditorContainer } from '@blocksuite/editor';
import { atom, createStore } from 'jotai';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
@@ -19,7 +18,7 @@ export type RootWorkspaceMetadata = {
* this atom stores the metadata of all workspaces,
* which is `id` and `flavor`, that is enough to load the real workspace data
*/
export const rootWorkspacesMetadataAtom = atomWithSyncStorage<
export const rootWorkspacesMetadataAtom = atomWithStorage<
RootWorkspaceMetadata[]
>(
// don't change this key,