From 3260dd808a6532d03d55a905636286cf48bd904b Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 8 Mar 2024 12:40:23 +0900 Subject: [PATCH 1/3] wip: try jsr --- jsr.json | 13 +++++++++++++ src/react/Provider.ts | 5 ++++- src/react/utils/useResetAtom.ts | 6 +++--- src/vanilla/store.ts | 22 ++++++++++++++++------ src/vanilla/utils/atomWithObservable.ts | 6 ------ src/vanilla/utils/atomWithReset.ts | 5 ++++- src/vanilla/utils/constants.ts | 2 +- src/vanilla/utils/freezeAtom.ts | 2 +- 8 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 jsr.json diff --git a/jsr.json b/jsr.json new file mode 100644 index 0000000000..d089179493 --- /dev/null +++ b/jsr.json @@ -0,0 +1,13 @@ +{ + "name": "@jotai/jotai", + "version": "2.7.0-jsr.0", + "exports": { + ".": "./src/index.ts", + "./vanilla": "./src/vanilla.ts", + "./vanilla/utils": "./src/vanilla/utils.ts", + "./react": "./src/react.ts", + "./react/utils": "./src/react/utils.ts" + }, + "include": ["src/**"], + "exclude": ["**"] +} diff --git a/src/react/Provider.ts b/src/react/Provider.ts index 5819561dea..3c57cee40c 100644 --- a/src/react/Provider.ts +++ b/src/react/Provider.ts @@ -4,7 +4,10 @@ import { createStore, getDefaultStore } from '../vanilla.ts' type Store = ReturnType -const StoreContext = createContext(undefined) +type StoreContextType = ReturnType> +const StoreContext: StoreContextType = createContext( + undefined, +) type Options = { store?: Store diff --git a/src/react/utils/useResetAtom.ts b/src/react/utils/useResetAtom.ts index baa7a77639..1d13125350 100644 --- a/src/react/utils/useResetAtom.ts +++ b/src/react/utils/useResetAtom.ts @@ -5,10 +5,10 @@ import type { WritableAtom } from '../../vanilla.ts' type Options = Parameters[1] -export function useResetAtom( - anAtom: WritableAtom, +export function useResetAtom( + anAtom: WritableAtom, options?: Options, -) { +): () => T { const setAtom = useSetAtom(anAtom, options) const resetAtom = useCallback(() => setAtom(RESET), [setAtom]) return resetAtom diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index b20722e92d..f68f13483d 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -8,7 +8,7 @@ type OnUnmount = () => void type Getter = Parameters[0] type Setter = Parameters[1] -const isSelfAtom = (atom: AnyAtom, a: AnyAtom) => +const isSelfAtom = (atom: AnyAtom, a: AnyAtom): boolean => atom.unstable_is ? atom.unstable_is(a) : a === atom const hasInitialValue = >( @@ -20,7 +20,10 @@ const isActuallyWritableAtom = (atom: AnyAtom): atom is AnyWritableAtom => !!(atom as AnyWritableAtom).write type CancelPromise = (next?: Promise) => void -const cancelPromiseMap = new WeakMap, CancelPromise>() +const cancelPromiseMap: WeakMap, CancelPromise> = new WeakMap< + Promise, + CancelPromise +>() const registerCancelPromise = ( promise: Promise, @@ -136,6 +139,15 @@ type StoreListenerRev2 = ( type MountedAtoms = Set +type Store = { + get: (atom: Atom) => Value + set: ( + atom: WritableAtom, + ...args: Args + ) => Result + sub: (atom: AnyAtom, listener: () => void) => () => void +} + /** * Create a new store. Each store is an independent, isolated universe of atom * states. @@ -152,7 +164,7 @@ type MountedAtoms = Set * * @returns A store. */ -export const createStore = () => { +export const createStore = (): Store => { const atomStateMap = new WeakMap() const mountedMap = new WeakMap() const pendingStack: Set[] = [] @@ -841,8 +853,6 @@ export const createStore = () => { } } -type Store = ReturnType - let defaultStore: Store | undefined if (import.meta.env?.MODE !== 'production') { @@ -853,7 +863,7 @@ if (import.meta.env?.MODE !== 'production') { } } -export const getDefaultStore = () => { +export const getDefaultStore = (): Store => { if (!defaultStore) { if ( import.meta.env?.MODE !== 'production' && diff --git a/src/vanilla/utils/atomWithObservable.ts b/src/vanilla/utils/atomWithObservable.ts index fa24605456..6c2c8f2293 100644 --- a/src/vanilla/utils/atomWithObservable.ts +++ b/src/vanilla/utils/atomWithObservable.ts @@ -4,12 +4,6 @@ import type { Atom, Getter, WritableAtom } from '../../vanilla.ts' type Timeout = ReturnType type AnyError = unknown -declare global { - interface SymbolConstructor { - readonly observable: symbol - } -} - type Subscription = { unsubscribe: () => void } diff --git a/src/vanilla/utils/atomWithReset.ts b/src/vanilla/utils/atomWithReset.ts index 8dff76b7e5..2d9ae5929c 100644 --- a/src/vanilla/utils/atomWithReset.ts +++ b/src/vanilla/utils/atomWithReset.ts @@ -13,7 +13,10 @@ type WithInitialValue = { init: Value } -export function atomWithReset(initialValue: Value) { +export function atomWithReset( + initialValue: Value, +): WritableAtom], void> & + WithInitialValue { type Update = SetStateActionWithReset const anAtom = atom( initialValue, diff --git a/src/vanilla/utils/constants.ts b/src/vanilla/utils/constants.ts index 0b35c80e60..3ccf5091a8 100644 --- a/src/vanilla/utils/constants.ts +++ b/src/vanilla/utils/constants.ts @@ -1,3 +1,3 @@ -export const RESET = Symbol( +export const RESET: unique symbol = Symbol( import.meta.env?.MODE !== 'production' ? 'RESET' : '', ) diff --git a/src/vanilla/utils/freezeAtom.ts b/src/vanilla/utils/freezeAtom.ts index b5c0d05001..d4276c83c9 100644 --- a/src/vanilla/utils/freezeAtom.ts +++ b/src/vanilla/utils/freezeAtom.ts @@ -30,7 +30,7 @@ export function freezeAtom>( export function freezeAtomCreator< CreateAtom extends (...params: any[]) => Atom, ->(createAtom: CreateAtom) { +>(createAtom: CreateAtom): CreateAtom { return ((...params: any[]) => { const anAtom = createAtom(...params) const origRead = anAtom.read From 934b2789fd51a6e15fb22aa62cd70816d904ab3e Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 8 Mar 2024 12:42:32 +0900 Subject: [PATCH 2/3] fix include --- jsr.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsr.json b/jsr.json index d089179493..c374b74694 100644 --- a/jsr.json +++ b/jsr.json @@ -8,6 +8,6 @@ "./react": "./src/react.ts", "./react/utils": "./src/react/utils.ts" }, - "include": ["src/**"], + "include": ["jsr.json", "src/**"], "exclude": ["**"] } From 5db726ae680390f9316e2488a6b7eae25def2561 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 8 Mar 2024 12:52:50 +0900 Subject: [PATCH 3/3] fix exclude? --- jsr.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jsr.json b/jsr.json index c374b74694..82413e4c66 100644 --- a/jsr.json +++ b/jsr.json @@ -8,6 +8,6 @@ "./react": "./src/react.ts", "./react/utils": "./src/react/utils.ts" }, - "include": ["jsr.json", "src/**"], - "exclude": ["**"] + "include": ["src/**"], + "exclude": ["website/**"] }