From 7bd810fcfaa8a2a51879241f13ce62f1179f9016 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 08:43:03 +0900 Subject: [PATCH 01/10] fix(store): combine dev3 methods --- src/vanilla/store.ts | 32 ++++++++++++++++---------------- src/vanilla/store2.ts | 22 ++++++++++++++-------- tests/vanilla/storedev.test.tsx | 12 ++++++------ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 6d500efe9b..0d0d7a8f86 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -148,11 +148,16 @@ type DevListenerRev3 = ( type DevStoreRev3 = { dev3_subscribe_store: (l: DevListenerRev3) => () => void dev3_get_mounted_atoms: () => Iterable - dev3_get_atom_state: ( - a: AnyAtom, - ) => { readonly v: AnyValue } | { readonly e: AnyError } | undefined - // deps are atoms that specified atom depends on (not including self) - dev3_get_atom_deps: (a: AnyAtom) => Iterable | undefined + dev3_get_atom_state: (a: AnyAtom) => + | { + readonly v: AnyValue + readonly d: Iterable // deps excluding self + } + | { + readonly e: AnyError + readonly d: Iterable // deps excluding self + } + | undefined dev3_restore_atoms: (values: Iterable) => void } @@ -867,22 +872,17 @@ export const createStore = (): Store => { dev3_get_atom_state: (a) => { const aState = atomStateMap.get(a) if (aState && 'v' in aState) { - return { v: aState.v } + const d = new Set(aState.d.keys()) + d.delete(a) + return { v: aState.v, d } } if (aState && 'e' in aState) { - return { e: aState.e } + const d = new Set(aState.d.keys()) + d.delete(a) + return { e: aState.e, d } } return undefined }, - dev3_get_atom_deps: (a) => { - const aState = atomStateMap.get(a) - if (!aState) { - return undefined - } - const deps = new Set(aState.d.keys()) - deps.delete(a) - return deps - }, dev3_restore_atoms: (values) => { pendingStack.push(new Set()) for (const [atom, valueOrPromise] of values) { diff --git a/src/vanilla/store2.ts b/src/vanilla/store2.ts index 3d3cf67081..ca253cdb00 100644 --- a/src/vanilla/store2.ts +++ b/src/vanilla/store2.ts @@ -266,11 +266,16 @@ type DevListenerRev3 = ( type DevStoreRev3 = { dev3_subscribe_store: (l: DevListenerRev3) => () => void dev3_get_mounted_atoms: () => Iterable - dev3_get_atom_state: ( - a: AnyAtom, - ) => { readonly v: AnyValue } | { readonly e: AnyError } | undefined - // deps are atoms that specified atom depends on (not including self) - dev3_get_atom_deps: (a: AnyAtom) => Iterable | undefined + dev3_get_atom_state: (a: AnyAtom) => + | { + readonly v: AnyValue + readonly d: Iterable // deps excluding self + } + | { + readonly e: AnyError + readonly d: Iterable // deps excluding self + } + | undefined dev3_restore_atoms: (values: Iterable) => void } @@ -740,10 +745,11 @@ export const createStore = (): Store => { } }, dev3_get_mounted_atoms: () => mountedAtoms.values(), - dev3_get_atom_state: (a) => atomStateMap.get(a)?.s, - dev3_get_atom_deps: (a) => { + dev3_get_atom_state: (a) => { const aState = atomStateMap.get(a) - return aState ? new Set(aState.d.keys()) : undefined + return aState?.s + ? { ...aState.s, d: new Set(aState.d.keys()) } + : undefined }, dev3_restore_atoms: (values) => { const pendingPair = createPendingPair() diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index a950ea417a..5d450b2a4e 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -165,21 +165,21 @@ describe('[DEV-ONLY] dev-only methods rev3', () => { it('should get atom deps', () => { const store = createStore() - if (!('dev3_get_atom_deps' in store)) { + if (!('dev3_get_atom_state' in store)) { throw new Error('dev methods are not available') } const countAtom = atom(0) const cb = vi.fn() const unsub = store.sub(countAtom, cb) store.set(countAtom, 1) - const result = store.dev3_get_atom_deps(countAtom) - expect(result && Array.from(result)).toStrictEqual([]) + const result = store.dev3_get_atom_state(countAtom) + expect(result?.d && Array.from(result.d)).toStrictEqual([]) unsub() }) it('should get atom deps 2', () => { const store = createStore() - if (!('dev3_get_atom_deps' in store)) { + if (!('dev3_get_atom_state' in store)) { throw new Error('dev methods are not available') } const countAtom = atom(0) @@ -187,8 +187,8 @@ describe('[DEV-ONLY] dev-only methods rev3', () => { const cb = vi.fn() const unsub = store.sub(doubleAtom, cb) store.set(countAtom, 1) - const result = store.dev3_get_atom_deps(doubleAtom) - expect(result && Array.from(result)).toStrictEqual([countAtom]) + const result = store.dev3_get_atom_state(doubleAtom) + expect(result?.d && Array.from(result.d)).toStrictEqual([countAtom]) unsub() }) From 85f0c94c357164c60898efbc003d03e72cca797a Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 09:53:31 +0900 Subject: [PATCH 02/10] dev methods rev4 --- src/vanilla/store2.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/vanilla/store2.ts b/src/vanilla/store2.ts index ca253cdb00..a733be636e 100644 --- a/src/vanilla/store2.ts +++ b/src/vanilla/store2.ts @@ -278,6 +278,13 @@ type DevStoreRev3 = { | undefined dev3_restore_atoms: (values: Iterable) => void } +type DevStoreRev4 = { + dev4_get_internal_weak_map: () => WeakMap + dev4_override_method: ( + key: K, + fn: PrdStore[K], + ) => void +} type PrdStore = { get: (atom: Atom) => Value @@ -289,7 +296,7 @@ type PrdStore = { } type Store = | (PrdStore & Partial) - | (PrdStore & DevStoreRev2 & DevStoreRev3) + | (PrdStore & DevStoreRev2 & DevStoreRev3 & DevStoreRev4) export const createStore = (): Store => { const atomStateMap = new WeakMap() @@ -686,7 +693,7 @@ export const createStore = (): Store => { } if (import.meta.env?.MODE !== 'production') { - return { + const store: Store = { get: readAtom, set: writeAtom, sub: subscribeAtom, @@ -762,7 +769,12 @@ export const createStore = (): Store => { devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) }) }, + dev4_get_internal_weak_map: () => atomStateMap, + dev4_override_method: (key, fn) => { + ;(store as any)[key] = fn + }, } + return store } return { get: readAtom, From 0d80aa303848e164062d4744c4785e596d103226 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 22:51:36 +0900 Subject: [PATCH 03/10] remove rev3 and add rev4 --- src/vanilla/store.ts | 84 +------- src/vanilla/store2.ts | 181 +--------------- tests/vanilla/storedev.test.tsx | 364 +++++++++++--------------------- 3 files changed, 143 insertions(+), 486 deletions(-) diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 0d0d7a8f86..b382bbbb13 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -142,24 +142,6 @@ type DevStoreRev2 = { dev_get_mounted: (a: AnyAtom) => Mounted | undefined dev_restore_atoms: (values: Iterable) => void } -type DevListenerRev3 = ( - action: { type: 'set'; atom: AnyAtom } | { type: 'unsub' }, -) => void -type DevStoreRev3 = { - dev3_subscribe_store: (l: DevListenerRev3) => () => void - dev3_get_mounted_atoms: () => Iterable - dev3_get_atom_state: (a: AnyAtom) => - | { - readonly v: AnyValue - readonly d: Iterable // deps excluding self - } - | { - readonly e: AnyError - readonly d: Iterable // deps excluding self - } - | undefined - dev3_restore_atoms: (values: Iterable) => void -} type PrdStore = { get: (atom: Atom) => Value @@ -169,12 +151,9 @@ type PrdStore = { ) => Result sub: (atom: AnyAtom, listener: () => void) => () => void } -type Store = - | (PrdStore & Partial) - | (PrdStore & DevStoreRev2 & DevStoreRev3) +type Store = PrdStore & Partial export type INTERNAL_DevStoreRev2 = DevStoreRev2 -export type INTERNAL_DevStoreRev3 = DevStoreRev3 export type INTERNAL_PrdStore = PrdStore /** @@ -201,10 +180,10 @@ export const createStore = (): Store => { AnyAtom, [prevAtomState: AtomState | undefined, dependents: Dependents] >() - let devListenersRev3: Set + let devListenersRev2: Set let mountedAtoms: MountedAtoms if (import.meta.env?.MODE !== 'production') { - devListenersRev3 = new Set() + devListenersRev2 = new Set() mountedAtoms = new Set() } @@ -607,7 +586,7 @@ export const createStore = (): Store => { if (!isSync) { const flushed = flushPending(pendingStack.pop()!) if (import.meta.env?.MODE !== 'production') { - devListenersRev3.forEach((l) => + devListenersRev2.forEach((l) => l({ type: 'async-write', flushed: flushed! }), ) } @@ -626,7 +605,7 @@ export const createStore = (): Store => { const result = writeAtomState(atom, ...args) const flushed = flushPending(pendingStack.pop()!) if (import.meta.env?.MODE !== 'production') { - devListenersRev3.forEach((l) => l({ type: 'write', flushed: flushed! })) + devListenersRev2.forEach((l) => l({ type: 'write', flushed: flushed! })) } return result } @@ -811,7 +790,7 @@ export const createStore = (): Store => { const listeners = mounted.l listeners.add(listener) if (import.meta.env?.MODE !== 'production') { - devListenersRev3.forEach((l) => + devListenersRev2.forEach((l) => l({ type: 'sub', flushed: flushed as Set }), ) } @@ -820,7 +799,7 @@ export const createStore = (): Store => { tryUnmountAtom(atom, mounted) if (import.meta.env?.MODE !== 'production') { // devtools uses this to detect if it _can_ unmount or not - devListenersRev3.forEach((l) => l({ type: 'unsub' })) + devListenersRev2.forEach((l) => l({ type: 'unsub' })) } } } @@ -832,9 +811,9 @@ export const createStore = (): Store => { sub: subscribeAtom, // store dev methods (these are tentative and subject to change without notice) dev_subscribe_store: (l) => { - devListenersRev3.add(l) + devListenersRev2.add(l) return () => { - devListenersRev3.delete(l) + devListenersRev2.delete(l) } }, dev_get_mounted_atoms: () => mountedAtoms.values(), @@ -849,50 +828,7 @@ export const createStore = (): Store => { } } const flushed = flushPending(pendingStack.pop()!) - devListenersRev3.forEach((l) => - l({ type: 'restore', flushed: flushed! }), - ) - }, - dev3_subscribe_store: (l) => { - const l2: DevListenerRev2 = (action) => { - if (action.type === 'unsub') { - l(action) - } else if (action.type !== 'sub' && 'flushed' in action) { - for (const a of action.flushed) { - l({ type: 'set', atom: a }) - } - } - } - devListenersRev3.add(l2) - return () => { - devListenersRev3.delete(l2) - } - }, - dev3_get_mounted_atoms: () => mountedAtoms.values(), - dev3_get_atom_state: (a) => { - const aState = atomStateMap.get(a) - if (aState && 'v' in aState) { - const d = new Set(aState.d.keys()) - d.delete(a) - return { v: aState.v, d } - } - if (aState && 'e' in aState) { - const d = new Set(aState.d.keys()) - d.delete(a) - return { e: aState.e, d } - } - return undefined - }, - dev3_restore_atoms: (values) => { - pendingStack.push(new Set()) - for (const [atom, valueOrPromise] of values) { - if (hasInitialValue(atom)) { - setAtomValueOrPromise(atom, valueOrPromise) - recomputeDependents(atom) - } - } - const flushed = flushPending(pendingStack.pop()!) - devListenersRev3.forEach((l) => + devListenersRev2.forEach((l) => l({ type: 'restore', flushed: flushed! }), ) }, diff --git a/src/vanilla/store2.ts b/src/vanilla/store2.ts index a733be636e..2f3c12e5cd 100644 --- a/src/vanilla/store2.ts +++ b/src/vanilla/store2.ts @@ -240,44 +240,6 @@ const setAtomStateValueOrPromise = ( } // for debugging purpose only -type OldAtomState = { d: Map } & ( - | { e: AnyError } - | { v: AnyValue } -) -type OldMounted = { l: Set<() => void>; t: Set; u?: OnUnmount } -type DevListenerRev2 = ( - action: - | { type: 'write'; flushed: Set } - | { type: 'async-write'; flushed: Set } - | { type: 'sub'; flushed: Set } - | { type: 'unsub' } - | { type: 'restore'; flushed: Set }, -) => void -type DevStoreRev2 = { - dev_subscribe_store: (l: DevListenerRev2, rev: 2) => () => void - dev_get_mounted_atoms: () => IterableIterator - dev_get_atom_state: (a: AnyAtom) => OldAtomState | undefined - dev_get_mounted: (a: AnyAtom) => OldMounted | undefined - dev_restore_atoms: (values: Iterable) => void -} -type DevListenerRev3 = ( - action: { type: 'set'; atom: AnyAtom } | { type: 'unsub' }, -) => void -type DevStoreRev3 = { - dev3_subscribe_store: (l: DevListenerRev3) => () => void - dev3_get_mounted_atoms: () => Iterable - dev3_get_atom_state: (a: AnyAtom) => - | { - readonly v: AnyValue - readonly d: Iterable // deps excluding self - } - | { - readonly e: AnyError - readonly d: Iterable // deps excluding self - } - | undefined - dev3_restore_atoms: (values: Iterable) => void -} type DevStoreRev4 = { dev4_get_internal_weak_map: () => WeakMap dev4_override_method: ( @@ -294,9 +256,10 @@ type PrdStore = { ) => Result sub: (atom: AnyAtom, listener: () => void) => () => void } -type Store = - | (PrdStore & Partial) - | (PrdStore & DevStoreRev2 & DevStoreRev3 & DevStoreRev4) +type Store = PrdStore | (PrdStore & DevStoreRev4) + +export type INTERNAL_DevStoreRev4 = DevStoreRev4 +export type INTERNAL_PrdStore = PrdStore export const createStore = (): Store => { const atomStateMap = new WeakMap() @@ -310,15 +273,6 @@ export const createStore = (): Store => { return atomState } - let devListenersRev2: Set - let devListenersRev3: Set - let mountedAtoms: Set - if (import.meta.env?.MODE !== 'production') { - devListenersRev2 = new Set() - devListenersRev3 = new Set() - mountedAtoms = new Set() - } - const clearDependencies = (atom: Atom) => { const atomState = getAtomState(atom) for (const a of atomState.d.keys()) { @@ -342,12 +296,7 @@ export const createStore = (): Store => { if (!isSync && atomState.m) { const pendingPair = createPendingPair() mountDependencies(pendingPair, atomState) - const flushed = flushPending(pendingPair) - if (import.meta.env?.MODE !== 'production' && flushed) { - flushed.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - } + flushPending(pendingPair) } } @@ -436,12 +385,7 @@ export const createStore = (): Store => { if (atomState.m) { const pendingPair = createPendingPair() mountDependencies(pendingPair, atomState) - const flushed = flushPending(pendingPair) - if (import.meta.env?.MODE !== 'production' && flushed) { - flushed.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - } + flushPending(pendingPair) } }, ) @@ -552,13 +496,7 @@ export const createStore = (): Store => { } else { r = writeAtomState(pendingPair, a as AnyWritableAtom, ...args) as R } - const flushed = flushPending(pendingPair, true) - if (import.meta.env?.MODE !== 'production' && flushed) { - flushed.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - devListenersRev2.forEach((l) => l({ type: 'async-write', flushed })) - } + flushPending(pendingPair, true) return r as R } const result = atom.write(getter, setter, ...args) @@ -571,13 +509,7 @@ export const createStore = (): Store => { ): Result => { const pendingPair = createPendingPair() const result = writeAtomState(pendingPair, atom, ...args) - const flushed = flushPending(pendingPair) - if (import.meta.env?.MODE !== 'production' && flushed) { - flushed.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - devListenersRev2.forEach((l) => l({ type: 'write', flushed })) - } + flushPending(pendingPair) return result } @@ -612,9 +544,6 @@ export const createStore = (): Store => { } // mount self atomState.m = { l: new Set(), d: new Set(atomState.d.keys()) } - if (import.meta.env?.MODE !== 'production') { - mountedAtoms.add(atom) - } if (isActuallyWritableAtom(atom) && atom.onMount) { const mounted = atomState.m const { onMount } = atom @@ -644,9 +573,6 @@ export const createStore = (): Store => { addPending(pendingPair, onUnmount) } delete atomState.m - if (import.meta.env?.MODE !== 'production') { - mountedAtoms.delete(atom) - } // unmount dependencies for (const a of atomState.d.keys()) { unmountAtom(pendingPair, a) @@ -661,34 +587,16 @@ export const createStore = (): Store => { } const subscribeAtom = (atom: AnyAtom, listener: () => void) => { - let prevMounted: Mounted | undefined - if (import.meta.env?.MODE !== 'production') { - prevMounted = atomStateMap.get(atom)?.m - } const pendingPair = createPendingPair() const mounted = mountAtom(pendingPair, atom) - const flushed = flushPending(pendingPair) + flushPending(pendingPair) const listeners = mounted.l listeners.add(listener) - if (import.meta.env?.MODE !== 'production' && flushed) { - flushed.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - if (!prevMounted) { - flushed.add(atom) // HACK to include self - } - devListenersRev2.forEach((l) => l({ type: 'sub', flushed })) - } return () => { listeners.delete(listener) const pendingPair = createPendingPair() unmountAtom(pendingPair, atom) flushPending(pendingPair) - if (import.meta.env?.MODE !== 'production') { - // devtools uses this to detect if it _can_ unmount or not - devListenersRev2.forEach((l) => l({ type: 'unsub' })) - devListenersRev3.forEach((l) => l({ type: 'unsub' })) - } } } @@ -698,77 +606,6 @@ export const createStore = (): Store => { set: writeAtom, sub: subscribeAtom, // store dev methods (these are tentative and subject to change without notice) - dev_subscribe_store: (l) => { - devListenersRev2.add(l) - return () => { - devListenersRev2.delete(l) - } - }, - dev_get_mounted_atoms: () => mountedAtoms.values(), - dev_get_atom_state: (a: AnyAtom) => { - const getOldAtomState = (a: AnyAtom): OldAtomState | undefined => { - const aState = atomStateMap.get(a) - return ( - aState && - aState.s && { - d: new Map( - Array.from(aState.d.keys()).flatMap((a) => { - const s = getOldAtomState(a) - return s ? [[a, s]] : [] - }), - ), - ...aState.s, - } - ) - } - return getOldAtomState(a) - }, - dev_get_mounted: (a: AnyAtom) => { - const aState = atomStateMap.get(a) - return ( - aState && - aState.m && { - l: aState.m.l, - t: new Set([...aState.t, a]), // HACK to include self - ...(aState.m.u ? { u: aState.m.u } : {}), - } - ) - }, - dev_restore_atoms: (values: Iterable) => { - const pendingPair = createPendingPair() - for (const [atom, value] of values) { - setAtomStateValueOrPromise(getAtomState(atom), value) - recomputeDependents(pendingPair, atom) - } - const flushed = flushPending(pendingPair) - devListenersRev2.forEach((l) => - l({ type: 'restore', flushed: flushed! }), - ) - }, - dev3_subscribe_store: (l) => { - devListenersRev3.add(l) - return () => { - devListenersRev3.delete(l) - } - }, - dev3_get_mounted_atoms: () => mountedAtoms.values(), - dev3_get_atom_state: (a) => { - const aState = atomStateMap.get(a) - return aState?.s - ? { ...aState.s, d: new Set(aState.d.keys()) } - : undefined - }, - dev3_restore_atoms: (values) => { - const pendingPair = createPendingPair() - for (const [atom, value] of values) { - setAtomStateValueOrPromise(getAtomState(atom), value) - recomputeDependents(pendingPair, atom) - } - const flushed = flushPending(pendingPair) - flushed?.forEach((a) => { - devListenersRev3.forEach((l) => l({ type: 'set', atom: a })) - }) - }, dev4_get_internal_weak_map: () => atomStateMap, dev4_override_method: (key, fn) => { ;(store as any)[key] = fn diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index 5d450b2a4e..eadf9f9ca5 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -1,265 +1,149 @@ import { describe, expect, it, vi } from 'vitest' import { atom, createStore } from 'jotai/vanilla' +import type { + INTERNAL_DevStoreRev4, + INTERNAL_PrdStore, +} from '../../src/vanilla/store2.js' + +describe.skipIf(import.meta.env?.USE_STORE2)( + '[DEV-ONLY] dev-only methods rev2', + () => { + it('should return the values of all mounted atoms', () => { + const store = createStore() + const countAtom = atom(0) + countAtom.debugLabel = 'countAtom' + const derivedAtom = atom((get) => get(countAtom) * 0) + const unsub = store.sub(derivedAtom, vi.fn()) + store.set(countAtom, 1) -describe('[DEV-ONLY] dev-only methods rev2', () => { - it('should return the values of all mounted atoms', () => { - const store = createStore() - const countAtom = atom(0) - countAtom.debugLabel = 'countAtom' - const derivedAtom = atom((get) => get(countAtom) * 0) - const unsub = store.sub(derivedAtom, vi.fn()) - store.set(countAtom, 1) - - const result = store.dev_get_mounted_atoms?.() || [] - expect( - Array.from(result).sort( - (a, b) => Object.keys(a).length - Object.keys(b).length, - ), - ).toStrictEqual([ - { toString: expect.any(Function), read: expect.any(Function) }, - { - toString: expect.any(Function), - init: 0, - read: expect.any(Function), - write: expect.any(Function), - debugLabel: 'countAtom', - }, - ]) - unsub() - }) - - it('should get atom state of a given atom', () => { - const store = createStore() - const countAtom = atom(0) - const unsub = store.sub(countAtom, vi.fn()) - store.set(countAtom, 1) - const result = store.dev_get_atom_state?.(countAtom) - expect(result).toHaveProperty('v', 1) - unsub() - }) - - it('should get mounted atom from mounted map', () => { - const store = createStore() - const countAtom = atom(0) - const cb = vi.fn() - const unsub = store.sub(countAtom, cb) - store.set(countAtom, 1) - const result = store.dev_get_mounted?.(countAtom) - expect(result).toStrictEqual({ l: new Set([cb]), t: new Set([countAtom]) }) - unsub() - }) - - it('should restore atoms and its dependencies correctly', () => { - const store = createStore() - const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - store.set(countAtom, 1) - store.dev_restore_atoms?.([[countAtom, 2]]) - expect(store.get(countAtom)).toBe(2) - expect(store.get?.(derivedAtom)).toBe(4) - }) + const result = store.dev_get_mounted_atoms?.() || [] + expect( + Array.from(result).sort( + (a, b) => Object.keys(a).length - Object.keys(b).length, + ), + ).toStrictEqual([ + { toString: expect.any(Function), read: expect.any(Function) }, + { + toString: expect.any(Function), + init: 0, + read: expect.any(Function), + write: expect.any(Function), + debugLabel: 'countAtom', + }, + ]) + unsub() + }) - describe('dev_subscribe_store rev2', () => { - it('should call the callback when state changes', () => { + it('should get atom state of a given atom', () => { const store = createStore() - const callback = vi.fn() - const unsub = store.dev_subscribe_store?.(callback, 2) const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) + const unsub = store.sub(countAtom, vi.fn()) store.set(countAtom, 1) - expect(callback).toHaveBeenNthCalledWith(1, { - type: 'sub', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenNthCalledWith(2, { - type: 'write', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenCalledTimes(2) - unsub?.() - unsubAtom?.() + const result = store.dev_get_atom_state?.(countAtom) + expect(result).toHaveProperty('v', 1) + unsub() }) - it('should call unsub only when atom is unsubscribed', () => { + it('should get mounted atom from mounted map', () => { const store = createStore() - const callback = vi.fn() - const unsub = store.dev_subscribe_store?.(callback, 2) const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) - const unsubAtomSecond = store.sub(countAtom, vi.fn()) - unsubAtom?.() - expect(callback).toHaveBeenNthCalledWith(1, { - type: 'sub', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenNthCalledWith(2, { - type: 'sub', - flushed: new Set(), + const cb = vi.fn() + const unsub = store.sub(countAtom, cb) + store.set(countAtom, 1) + const result = store.dev_get_mounted?.(countAtom) + expect(result).toStrictEqual({ + l: new Set([cb]), + t: new Set([countAtom]), }) - expect(callback).toHaveBeenNthCalledWith(3, { type: 'unsub' }) - expect(callback).toHaveBeenCalledTimes(3) - unsub?.() - unsubAtomSecond?.() + unsub() }) - }) - - it('should unmount tree dependencies with store.get', async () => { - const store = createStore() - const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - const anotherDerivedAtom = atom((get) => get(countAtom) * 3) - const callback = vi.fn() - const unsubStore = store.dev_subscribe_store?.(() => { - // Comment this line to make the test pass - store.get(derivedAtom) - }, 2) - const unsub = store.sub(anotherDerivedAtom, callback) - unsub() - unsubStore?.() - const result = Array.from(store.dev_get_mounted_atoms?.() ?? []) - expect(result).toEqual([]) - }) -}) - -describe('[DEV-ONLY] dev-only methods rev3', () => { - it('should return the values of all mounted atoms', () => { - const store = createStore() - if (!('dev3_get_mounted_atoms' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - countAtom.debugLabel = 'countAtom' - const derivedAtom = atom((get) => get(countAtom) * 0) - const unsub = store.sub(derivedAtom, vi.fn()) - store.set(countAtom, 1) - const result = store.dev3_get_mounted_atoms() || [] - expect( - Array.from(result).sort( - (a, b) => Object.keys(a).length - Object.keys(b).length, - ), - ).toStrictEqual([ - { toString: expect.any(Function), read: expect.any(Function) }, - { - toString: expect.any(Function), - init: 0, - read: expect.any(Function), - write: expect.any(Function), - debugLabel: 'countAtom', - }, - ]) - unsub() - }) - - it('should get atom state of a given atom', () => { - const store = createStore() - if (!('dev3_get_atom_state' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - const unsub = store.sub(countAtom, vi.fn()) - store.set(countAtom, 1) - const result = store.dev3_get_atom_state(countAtom) - expect(result).toHaveProperty('v', 1) - unsub() - }) - it('should get atom deps', () => { - const store = createStore() - if (!('dev3_get_atom_state' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - const cb = vi.fn() - const unsub = store.sub(countAtom, cb) - store.set(countAtom, 1) - const result = store.dev3_get_atom_state(countAtom) - expect(result?.d && Array.from(result.d)).toStrictEqual([]) - unsub() - }) - - it('should get atom deps 2', () => { - const store = createStore() - if (!('dev3_get_atom_state' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - const doubleAtom = atom((get) => get(countAtom) * 2) - const cb = vi.fn() - const unsub = store.sub(doubleAtom, cb) - store.set(countAtom, 1) - const result = store.dev3_get_atom_state(doubleAtom) - expect(result?.d && Array.from(result.d)).toStrictEqual([countAtom]) - unsub() - }) - - it('should restore atoms and its dependencies correctly', () => { - const store = createStore() - if (!('dev3_restore_atoms' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - store.set(countAtom, 1) - store.dev3_restore_atoms([[countAtom, 2]]) - expect(store.get(countAtom)).toBe(2) - expect(store.get(derivedAtom)).toBe(4) - }) - - describe('dev3_subscribe_store', () => { - it('should call the callback when state changes', () => { + it('should restore atoms and its dependencies correctly', () => { const store = createStore() - if (!('dev3_subscribe_store' in store)) { - throw new Error('dev methods are not available') - } - const callback = vi.fn() - const unsub = store.dev3_subscribe_store(callback) const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) + const derivedAtom = atom((get) => get(countAtom) * 2) store.set(countAtom, 1) - expect(callback).toHaveBeenNthCalledWith(1, { - type: 'set', - atom: countAtom, + store.dev_restore_atoms?.([[countAtom, 2]]) + expect(store.get(countAtom)).toBe(2) + expect(store.get?.(derivedAtom)).toBe(4) + }) + + describe('dev_subscribe_store rev2', () => { + it('should call the callback when state changes', () => { + const store = createStore() + const callback = vi.fn() + const unsub = store.dev_subscribe_store?.(callback, 2) + const countAtom = atom(0) + const unsubAtom = store.sub(countAtom, vi.fn()) + store.set(countAtom, 1) + expect(callback).toHaveBeenNthCalledWith(1, { + type: 'sub', + flushed: new Set([countAtom]), + }) + expect(callback).toHaveBeenNthCalledWith(2, { + type: 'write', + flushed: new Set([countAtom]), + }) + expect(callback).toHaveBeenCalledTimes(2) + unsub?.() + unsubAtom?.() + }) + + it('should call unsub only when atom is unsubscribed', () => { + const store = createStore() + const callback = vi.fn() + const unsub = store.dev_subscribe_store?.(callback, 2) + const countAtom = atom(0) + const unsubAtom = store.sub(countAtom, vi.fn()) + const unsubAtomSecond = store.sub(countAtom, vi.fn()) + unsubAtom?.() + expect(callback).toHaveBeenNthCalledWith(1, { + type: 'sub', + flushed: new Set([countAtom]), + }) + expect(callback).toHaveBeenNthCalledWith(2, { + type: 'sub', + flushed: new Set(), + }) + expect(callback).toHaveBeenNthCalledWith(3, { type: 'unsub' }) + expect(callback).toHaveBeenCalledTimes(3) + unsub?.() + unsubAtomSecond?.() }) - expect(callback).toHaveBeenCalledTimes(1) - unsub() - unsubAtom() }) - it('should call unsub only when atom is unsubscribed', () => { + it('should unmount tree dependencies with store.get', async () => { const store = createStore() - if (!('dev3_subscribe_store' in store)) { - throw new Error('dev methods are not available') - } - const callback = vi.fn() - const unsub = store.dev3_subscribe_store(callback) const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) - const unsubAtomSecond = store.sub(countAtom, vi.fn()) - unsubAtom() - expect(callback).toHaveBeenNthCalledWith(1, { type: 'unsub' }) - expect(callback).toHaveBeenCalledTimes(1) + const derivedAtom = atom((get) => get(countAtom) * 2) + const anotherDerivedAtom = atom((get) => get(countAtom) * 3) + const callback = vi.fn() + const unsubStore = store.dev_subscribe_store?.(() => { + // Comment this line to make the test pass + store.get(derivedAtom) + }, 2) + const unsub = store.sub(anotherDerivedAtom, callback) unsub() - unsubAtomSecond() + unsubStore?.() + const result = Array.from(store.dev_get_mounted_atoms?.() ?? []) + expect(result).toEqual([]) }) - }) - - it('should unmount tree dependencies with store.get', async () => { - const store = createStore() - if (!('dev3_subscribe_store' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - const anotherDerivedAtom = atom((get) => get(countAtom) * 3) - const callback = vi.fn() - const unsubStore = store.dev3_subscribe_store(() => { - // Comment this line to make the test pass - store.get(derivedAtom) + }, +) + +describe.skipIf(!import.meta.env?.USE_STORE2)( + '[DEV-ONLY] dev-only methods rev4', + () => { + it('should get atom value', () => { + const store = createStore() as INTERNAL_PrdStore & INTERNAL_DevStoreRev4 + if (!('dev4_get_internal_weak_map' in store)) { + throw new Error('dev methods are not available') + } + const countAtom = atom(0) + countAtom.debugLabel = 'countAtom' + store.set(countAtom, 1) + const weakMap = store.dev4_get_internal_weak_map() + expect(weakMap.get(countAtom)?.s).toEqual({ v: 1 }) }) - const unsub = store.sub(anotherDerivedAtom, callback) - unsub() - unsubStore() - const result = Array.from(store.dev3_get_mounted_atoms() ?? []) - expect(result).toEqual([]) - }) -}) + }, +) From f2d27f341d95da6c4031262d17f99d8efe0d9875 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 22:58:09 +0900 Subject: [PATCH 04/10] hack type --- tests/vanilla/storedev.test.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index eadf9f9ca5..97e12ed543 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -1,9 +1,5 @@ import { describe, expect, it, vi } from 'vitest' import { atom, createStore } from 'jotai/vanilla' -import type { - INTERNAL_DevStoreRev4, - INTERNAL_PrdStore, -} from '../../src/vanilla/store2.js' describe.skipIf(import.meta.env?.USE_STORE2)( '[DEV-ONLY] dev-only methods rev2', @@ -135,14 +131,14 @@ describe.skipIf(!import.meta.env?.USE_STORE2)( '[DEV-ONLY] dev-only methods rev4', () => { it('should get atom value', () => { - const store = createStore() as INTERNAL_PrdStore & INTERNAL_DevStoreRev4 + const store = createStore() if (!('dev4_get_internal_weak_map' in store)) { throw new Error('dev methods are not available') } const countAtom = atom(0) countAtom.debugLabel = 'countAtom' store.set(countAtom, 1) - const weakMap = store.dev4_get_internal_weak_map() + const weakMap = (store as any).dev4_get_internal_weak_map() expect(weakMap.get(countAtom)?.s).toEqual({ v: 1 }) }) }, From f52417396af5613ff70df91ea31e8165906fb389 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 23:04:59 +0900 Subject: [PATCH 05/10] hack type 2 --- tests/vanilla/storedev.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index 97e12ed543..d44ba893ae 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -131,14 +131,14 @@ describe.skipIf(!import.meta.env?.USE_STORE2)( '[DEV-ONLY] dev-only methods rev4', () => { it('should get atom value', () => { - const store = createStore() + const store = createStore() as any if (!('dev4_get_internal_weak_map' in store)) { throw new Error('dev methods are not available') } const countAtom = atom(0) countAtom.debugLabel = 'countAtom' store.set(countAtom, 1) - const weakMap = (store as any).dev4_get_internal_weak_map() + const weakMap = store.dev4_get_internal_weak_map() expect(weakMap.get(countAtom)?.s).toEqual({ v: 1 }) }) }, From bc2040ccee4f14c18a00aad572a39a0ca7b5125b Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 23:06:36 +0900 Subject: [PATCH 06/10] remove unusable types --- src/vanilla/store2.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/vanilla/store2.ts b/src/vanilla/store2.ts index 2f3c12e5cd..4115aca4a9 100644 --- a/src/vanilla/store2.ts +++ b/src/vanilla/store2.ts @@ -258,9 +258,6 @@ type PrdStore = { } type Store = PrdStore | (PrdStore & DevStoreRev4) -export type INTERNAL_DevStoreRev4 = DevStoreRev4 -export type INTERNAL_PrdStore = PrdStore - export const createStore = (): Store => { const atomStateMap = new WeakMap() From 14d73be85151f746608e2b467cf426b29ca5a978 Mon Sep 17 00:00:00 2001 From: daishi Date: Sun, 7 Apr 2024 13:46:57 +0900 Subject: [PATCH 07/10] fix test condition --- tests/vanilla/storedev.test.tsx | 246 ++++++++++++++++---------------- 1 file changed, 121 insertions(+), 125 deletions(-) diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index d44ba893ae..7aaac1d360 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -1,145 +1,141 @@ import { describe, expect, it, vi } from 'vitest' import { atom, createStore } from 'jotai/vanilla' -describe.skipIf(import.meta.env?.USE_STORE2)( - '[DEV-ONLY] dev-only methods rev2', - () => { - it('should return the values of all mounted atoms', () => { - const store = createStore() - const countAtom = atom(0) - countAtom.debugLabel = 'countAtom' - const derivedAtom = atom((get) => get(countAtom) * 0) - const unsub = store.sub(derivedAtom, vi.fn()) - store.set(countAtom, 1) +const IS_STORE2 = 'dev4_get_internal_weak_map' in createStore() - const result = store.dev_get_mounted_atoms?.() || [] - expect( - Array.from(result).sort( - (a, b) => Object.keys(a).length - Object.keys(b).length, - ), - ).toStrictEqual([ - { toString: expect.any(Function), read: expect.any(Function) }, - { - toString: expect.any(Function), - init: 0, - read: expect.any(Function), - write: expect.any(Function), - debugLabel: 'countAtom', - }, - ]) - unsub() - }) +describe.skipIf(IS_STORE2)('[DEV-ONLY] dev-only methods rev2', () => { + it('should return the values of all mounted atoms', () => { + const store = createStore() + const countAtom = atom(0) + countAtom.debugLabel = 'countAtom' + const derivedAtom = atom((get) => get(countAtom) * 0) + const unsub = store.sub(derivedAtom, vi.fn()) + store.set(countAtom, 1) - it('should get atom state of a given atom', () => { - const store = createStore() - const countAtom = atom(0) - const unsub = store.sub(countAtom, vi.fn()) - store.set(countAtom, 1) - const result = store.dev_get_atom_state?.(countAtom) - expect(result).toHaveProperty('v', 1) - unsub() + const result = store.dev_get_mounted_atoms?.() || [] + expect( + Array.from(result).sort( + (a, b) => Object.keys(a).length - Object.keys(b).length, + ), + ).toStrictEqual([ + { toString: expect.any(Function), read: expect.any(Function) }, + { + toString: expect.any(Function), + init: 0, + read: expect.any(Function), + write: expect.any(Function), + debugLabel: 'countAtom', + }, + ]) + unsub() + }) + + it('should get atom state of a given atom', () => { + const store = createStore() + const countAtom = atom(0) + const unsub = store.sub(countAtom, vi.fn()) + store.set(countAtom, 1) + const result = store.dev_get_atom_state?.(countAtom) + expect(result).toHaveProperty('v', 1) + unsub() + }) + + it('should get mounted atom from mounted map', () => { + const store = createStore() + const countAtom = atom(0) + const cb = vi.fn() + const unsub = store.sub(countAtom, cb) + store.set(countAtom, 1) + const result = store.dev_get_mounted?.(countAtom) + expect(result).toStrictEqual({ + l: new Set([cb]), + t: new Set([countAtom]), }) + unsub() + }) - it('should get mounted atom from mounted map', () => { + it('should restore atoms and its dependencies correctly', () => { + const store = createStore() + const countAtom = atom(0) + const derivedAtom = atom((get) => get(countAtom) * 2) + store.set(countAtom, 1) + store.dev_restore_atoms?.([[countAtom, 2]]) + expect(store.get(countAtom)).toBe(2) + expect(store.get?.(derivedAtom)).toBe(4) + }) + + describe('dev_subscribe_store rev2', () => { + it('should call the callback when state changes', () => { const store = createStore() + const callback = vi.fn() + const unsub = store.dev_subscribe_store?.(callback, 2) const countAtom = atom(0) - const cb = vi.fn() - const unsub = store.sub(countAtom, cb) + const unsubAtom = store.sub(countAtom, vi.fn()) store.set(countAtom, 1) - const result = store.dev_get_mounted?.(countAtom) - expect(result).toStrictEqual({ - l: new Set([cb]), - t: new Set([countAtom]), + expect(callback).toHaveBeenNthCalledWith(1, { + type: 'sub', + flushed: new Set([countAtom]), + }) + expect(callback).toHaveBeenNthCalledWith(2, { + type: 'write', + flushed: new Set([countAtom]), }) - unsub() + expect(callback).toHaveBeenCalledTimes(2) + unsub?.() + unsubAtom?.() }) - it('should restore atoms and its dependencies correctly', () => { + it('should call unsub only when atom is unsubscribed', () => { const store = createStore() + const callback = vi.fn() + const unsub = store.dev_subscribe_store?.(callback, 2) const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - store.set(countAtom, 1) - store.dev_restore_atoms?.([[countAtom, 2]]) - expect(store.get(countAtom)).toBe(2) - expect(store.get?.(derivedAtom)).toBe(4) - }) - - describe('dev_subscribe_store rev2', () => { - it('should call the callback when state changes', () => { - const store = createStore() - const callback = vi.fn() - const unsub = store.dev_subscribe_store?.(callback, 2) - const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) - store.set(countAtom, 1) - expect(callback).toHaveBeenNthCalledWith(1, { - type: 'sub', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenNthCalledWith(2, { - type: 'write', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenCalledTimes(2) - unsub?.() - unsubAtom?.() + const unsubAtom = store.sub(countAtom, vi.fn()) + const unsubAtomSecond = store.sub(countAtom, vi.fn()) + unsubAtom?.() + expect(callback).toHaveBeenNthCalledWith(1, { + type: 'sub', + flushed: new Set([countAtom]), }) - - it('should call unsub only when atom is unsubscribed', () => { - const store = createStore() - const callback = vi.fn() - const unsub = store.dev_subscribe_store?.(callback, 2) - const countAtom = atom(0) - const unsubAtom = store.sub(countAtom, vi.fn()) - const unsubAtomSecond = store.sub(countAtom, vi.fn()) - unsubAtom?.() - expect(callback).toHaveBeenNthCalledWith(1, { - type: 'sub', - flushed: new Set([countAtom]), - }) - expect(callback).toHaveBeenNthCalledWith(2, { - type: 'sub', - flushed: new Set(), - }) - expect(callback).toHaveBeenNthCalledWith(3, { type: 'unsub' }) - expect(callback).toHaveBeenCalledTimes(3) - unsub?.() - unsubAtomSecond?.() + expect(callback).toHaveBeenNthCalledWith(2, { + type: 'sub', + flushed: new Set(), }) + expect(callback).toHaveBeenNthCalledWith(3, { type: 'unsub' }) + expect(callback).toHaveBeenCalledTimes(3) + unsub?.() + unsubAtomSecond?.() }) + }) - it('should unmount tree dependencies with store.get', async () => { - const store = createStore() - const countAtom = atom(0) - const derivedAtom = atom((get) => get(countAtom) * 2) - const anotherDerivedAtom = atom((get) => get(countAtom) * 3) - const callback = vi.fn() - const unsubStore = store.dev_subscribe_store?.(() => { - // Comment this line to make the test pass - store.get(derivedAtom) - }, 2) - const unsub = store.sub(anotherDerivedAtom, callback) - unsub() - unsubStore?.() - const result = Array.from(store.dev_get_mounted_atoms?.() ?? []) - expect(result).toEqual([]) - }) - }, -) + it('should unmount tree dependencies with store.get', async () => { + const store = createStore() + const countAtom = atom(0) + const derivedAtom = atom((get) => get(countAtom) * 2) + const anotherDerivedAtom = atom((get) => get(countAtom) * 3) + const callback = vi.fn() + const unsubStore = store.dev_subscribe_store?.(() => { + // Comment this line to make the test pass + store.get(derivedAtom) + }, 2) + const unsub = store.sub(anotherDerivedAtom, callback) + unsub() + unsubStore?.() + const result = Array.from(store.dev_get_mounted_atoms?.() ?? []) + expect(result).toEqual([]) + }) +}) -describe.skipIf(!import.meta.env?.USE_STORE2)( - '[DEV-ONLY] dev-only methods rev4', - () => { - it('should get atom value', () => { - const store = createStore() as any - if (!('dev4_get_internal_weak_map' in store)) { - throw new Error('dev methods are not available') - } - const countAtom = atom(0) - countAtom.debugLabel = 'countAtom' - store.set(countAtom, 1) - const weakMap = store.dev4_get_internal_weak_map() - expect(weakMap.get(countAtom)?.s).toEqual({ v: 1 }) - }) - }, -) +describe.skipIf(!IS_STORE2)('[DEV-ONLY] dev-only methods rev4', () => { + it('should get atom value', () => { + const store = createStore() as any + if (!('dev4_get_internal_weak_map' in store)) { + throw new Error('dev methods are not available') + } + const countAtom = atom(0) + countAtom.debugLabel = 'countAtom' + store.set(countAtom, 1) + const weakMap = store.dev4_get_internal_weak_map() + expect(weakMap.get(countAtom)?.s).toEqual({ v: 1 }) + }) +}) From 9b5d920b88c91dcf80ebf54efca0494bab8c8375 Mon Sep 17 00:00:00 2001 From: daishi Date: Sun, 7 Apr 2024 13:47:58 +0900 Subject: [PATCH 08/10] format --- tests/vanilla/storedev.test.tsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index 7aaac1d360..3ca43cf2bb 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -47,10 +47,7 @@ describe.skipIf(IS_STORE2)('[DEV-ONLY] dev-only methods rev2', () => { const unsub = store.sub(countAtom, cb) store.set(countAtom, 1) const result = store.dev_get_mounted?.(countAtom) - expect(result).toStrictEqual({ - l: new Set([cb]), - t: new Set([countAtom]), - }) + expect(result).toStrictEqual({ l: new Set([cb]), t: new Set([countAtom]) }) unsub() }) From 80b8633cd91e40e9ef154a1727c94a0a2d0dfd70 Mon Sep 17 00:00:00 2001 From: daishi Date: Sun, 7 Apr 2024 14:06:53 +0900 Subject: [PATCH 09/10] fix dev store --- tests/vanilla/storedev.test.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/vanilla/storedev.test.tsx b/tests/vanilla/storedev.test.tsx index 3ca43cf2bb..6a0d6ee0ca 100644 --- a/tests/vanilla/storedev.test.tsx +++ b/tests/vanilla/storedev.test.tsx @@ -1,9 +1,10 @@ import { describe, expect, it, vi } from 'vitest' import { atom, createStore } from 'jotai/vanilla' -const IS_STORE2 = 'dev4_get_internal_weak_map' in createStore() +const IS_DEV_STORE = 'dev_subscribe_store' in createStore() +const IS_DEV_STORE2 = 'dev4_get_internal_weak_map' in createStore() -describe.skipIf(IS_STORE2)('[DEV-ONLY] dev-only methods rev2', () => { +describe.skipIf(!IS_DEV_STORE)('[DEV-ONLY] dev-only methods rev2', () => { it('should return the values of all mounted atoms', () => { const store = createStore() const countAtom = atom(0) @@ -123,7 +124,7 @@ describe.skipIf(IS_STORE2)('[DEV-ONLY] dev-only methods rev2', () => { }) }) -describe.skipIf(!IS_STORE2)('[DEV-ONLY] dev-only methods rev4', () => { +describe.skipIf(!IS_DEV_STORE2)('[DEV-ONLY] dev-only methods rev4', () => { it('should get atom value', () => { const store = createStore() as any if (!('dev4_get_internal_weak_map' in store)) { From d7426602f8b30580b80cdedfa7a10e0ea5799c45 Mon Sep 17 00:00:00 2001 From: daishi Date: Mon, 8 Apr 2024 10:16:46 +0900 Subject: [PATCH 10/10] export internal types --- src/vanilla/store2.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/vanilla/store2.ts b/src/vanilla/store2.ts index 4115aca4a9..2f3c12e5cd 100644 --- a/src/vanilla/store2.ts +++ b/src/vanilla/store2.ts @@ -258,6 +258,9 @@ type PrdStore = { } type Store = PrdStore | (PrdStore & DevStoreRev4) +export type INTERNAL_DevStoreRev4 = DevStoreRev4 +export type INTERNAL_PrdStore = PrdStore + export const createStore = (): Store => { const atomStateMap = new WeakMap()