diff --git a/src/mutableAtom/index.ts b/src/mutableAtom/index.ts index d3ca809..ac6555f 100644 --- a/src/mutableAtom/index.ts +++ b/src/mutableAtom/index.ts @@ -1,5 +1,4 @@ import { atom } from 'jotai' -import type { PrimitiveAtom } from 'jotai' import { proxy, snapshot, subscribe } from 'valtio' import type { Action, @@ -7,25 +6,18 @@ import type { Options, ProxyState, Store, - Wrapped, } from './types' export function mutableAtom( - value: Value, + initialValue: Value, options: Options = defaultOptions ) { - const valueAtom = atom({ value }) + const valueAtom = atom({ value: initialValue }) if (process.env.NODE_ENV !== 'production') { valueAtom.debugPrivate = true } - return makeMutableAtom(valueAtom, options) -} -export function makeMutableAtom( - valueAtom: PrimitiveAtom>, - options: Options = defaultOptions -) { const { proxyFn } = { ...defaultOptions, ...options } const storeAtom = atom< @@ -34,10 +26,9 @@ export function makeMutableAtom( void | Value >( (_get, { setSelf }) => { - const getValue = () => setSelf({ type: 'getValue' }) as Value const store: Store = { - proxyState: createProxyState(getValue(), () => store), - getValue, + proxyState: createProxyState(() => store), + getValue: () => setSelf({ type: 'getValue' }) as Value, setValue: (value: Value) => setSelf({ type: 'setValue', payload: value }) as void, } @@ -72,7 +63,7 @@ export function makeMutableAtom( /** * create the proxy state and subscribe to it */ - function createProxyState(initialValue: Value, getStore: () => Store) { + function createProxyState(getStore: () => Store) { const proxyState = proxyFn({ value: initialValue }) // We never unsubscribe, but it's garbage collectable. subscribe(proxyState, onChange(getStore), true) diff --git a/src/mutableAtom/types.ts b/src/mutableAtom/types.ts index 4d72403..27db076 100644 --- a/src/mutableAtom/types.ts +++ b/src/mutableAtom/types.ts @@ -1,5 +1,3 @@ -export type Wrapped = { value: T } - type ProxyFn = (obj: Wrapped) => Wrapped export type Store = {