bansa 0.0.24 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +224 -193
- package/dist/atom.d.mts +83 -0
- package/dist/atom.d.mts.map +1 -0
- package/dist/atom.mjs +390 -0
- package/dist/atom.mjs.map +1 -0
- package/dist/browser/index.d.ts +96 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/react.d.mts +45 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +59 -0
- package/dist/react.mjs.map +1 -0
- package/dist/utils.d.mts +17 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +70 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +45 -36
- package/tsconfig.json +29 -18
- package/README.ko.md +0 -486
- package/dist/atom.d.ts +0 -80
- package/dist/atom.js +0 -420
- package/dist/index.browser.js +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/react.d.ts +0 -15
- package/dist/react.js +0 -40
- package/dist/utils.d.ts +0 -19
- package/dist/utils.js +0 -80
- package/tests/bansa.test.ts +0 -1340
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/atom.ts","../../src/utils.ts"],"sourcesContent":["export type Atom<Value> = PrimitiveAtom<Value> | DerivedAtom<Value>;\nexport type CommonAtom<Value> = {\n readonly get: () => Value;\n readonly watch: (watcher: AtomWatcher) => () => void;\n readonly subscribe: (subscriber: AtomSubscribe<Value>) => () => void;\n readonly state: AtomState<Value>;\n};\nexport type PrimitiveAtom<Value> = CommonAtom<Value> & {\n readonly set: (value: AtomUpdater<Value>) => void;\n readonly state: AtomSuccessState<Value>;\n};\nexport type DerivedAtom<Value> = CommonAtom<Value>;\n\nexport type AtomWatcher = () => void;\nexport type AtomSubscribe<Value> = (value: Value, options: AtomSubscriberOptions) => void;\nexport type AtomInit<Value> = Value | AtomGetter<Value>;\nexport type AtomUpdater<Value> = Value | AtomReducer<Value>;\n// TODO: readonly\nexport type AtomInactiveState<Value> = {\n active: false;\n error: any;\n promise: undefined;\n value?: Value;\n};\nexport type AtomPromiseState<Value> = {\n active: true;\n error: any;\n promise: PromiseLike<Value>;\n value?: Value;\n};\nexport type AtomSuccessState<Value> = {\n active: true;\n error: undefined;\n promise: undefined;\n value: Value;\n};\nexport type AtomErrorState<Value> = {\n active: true;\n error: any;\n promise: undefined;\n value?: Value;\n};\nexport type AtomState<Value> =\n | AtomInactiveState<Value>\n | AtomPromiseState<Value>\n | AtomSuccessState<Value>\n | AtomErrorState<Value>;\n\nexport type AtomSubscriberOptions = { readonly signal: ThenableSignal };\nexport type AtomGetter<Value> = (\n get: GetAtom,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\nexport type AtomReducer<Value> = (value: Value) => Value;\n\nexport type AtomGetOptions = { readonly signal: ThenableSignal };\nexport type ThenableSignal = AbortSignal & { then: (f: () => void) => void };\ntype ThenableSignalController = {\n abort: () => void;\n signal: ThenableSignal;\n};\n\nexport type GetAtom = <Value>(anotherAtom: Atom<Value>) => Value;\n\ntype CreateAtom = {\n <Value>(init: AtomGetter<Value>, options?: AtomOptions<Value>): DerivedAtom<Value>;\n <Value>(init: Value, options?: AtomOptions<Value>): PrimitiveAtom<Value>;\n <Value>(init: Value | AtomGetter<Value>, options?: AtomOptions<Value>): Atom<Value>;\n};\nexport type AtomOptions<Value> = {\n equals?: AtomEquals<Value>;\n persist?: boolean;\n eager?: boolean;\n};\n\nexport type AtomEquals<Value> = (value: Value, prevValue: Value) => boolean;\nexport type AtomScope = {\n <Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;\n <Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;\n <Value>(baseAtom: Atom<Value>): Atom<Value>;\n <Value>(baseAtom: PrimitiveAtom<Value>, strict: true): PrimitiveAtom<Value> | undefined;\n <Value>(baseAtom: DerivedAtom<Value>, strict: true): DerivedAtom<Value> | undefined;\n <Value>(baseAtom: Atom<Value>, strict: true): Atom<Value> | undefined;\n};\n\nexport type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);\nexport type MapLike<Key, Value> =\n | Map<Key, Value>\n | (Key extends object ? WeakMap<Key, Value> : never)\n | (Key extends string | number | symbol ? Record<Key, Value> : never);\n\ntype GetAtomInternal = <Value>(anotherAtom: AtomInternal<Value>) => Value;\ntype AtomGetterInternal<Value> = (\n get: GetAtomInternal,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\ntype AtomSubscribeInternal<Value> = {\n _subscriber: AtomSubscribe<Value>;\n _options: AtomSubscriberOptions;\n _ctrl?: ThenableSignalController;\n};\n\ntype AtomInternal<Value> = PrimitiveAtomInternal<Value> | DerivedAtomInternal<Value>;\n\nabstract class CommonAtomInternal<Value> {\n _nextValue: Value | undefined;\n _nextError: any | undefined;\n _children: Set<DerivedAtomInternal<any>> | undefined;\n _watchers: Set<AtomWatcher> | undefined;\n _subscribers: Set<AtomSubscribeInternal<Value>> | undefined;\n _valueChanged = true;\n\n abstract readonly _source: boolean;\n abstract _needExecute: boolean;\n abstract _needPropagate: boolean;\n abstract _marked: boolean;\n abstract _resolve: ((value: Value) => void) | undefined;\n abstract _reject: ((reason: any) => void) | undefined;\n\n abstract readonly _init: Value | AtomGetterInternal<Value>;\n abstract readonly _equals: AtomEquals<Value> | undefined;\n\n abstract readonly state: AtomState<Value>;\n\n get(): Value {\n if (!this.state.active) {\n execute(this as unknown as DerivedAtomInternal<Value>);\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n if (this.state.promise) throw this.state.promise;\n if (this.state.error) throw this.state.error;\n return this.state.value!;\n }\n\n watch(watcher: AtomWatcher): () => void {\n if (!this.state.active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n }\n (this._watchers ||= new Set()).add(watcher);\n return () => {\n this._watchers!.delete(watcher);\n if (!this._watchers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n subscribe(subscriber: AtomSubscribe<Value>): () => void {\n const atomSubscriber: AtomSubscribeInternal<Value> = {\n _subscriber: subscriber,\n _options: {\n get signal() {\n return (atomSubscriber._ctrl ||= createThenableSignal()).signal;\n },\n },\n };\n if (!this.state.active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n } else if (!this.state.error && !this.state.promise) {\n try {\n subscriber(this.state.value!, atomSubscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n (this._subscribers ||= new Set()).add(atomSubscriber);\n return () => {\n this._subscribers!.delete(atomSubscriber);\n if (atomSubscriber._ctrl) {\n atomSubscriber._ctrl.abort();\n atomSubscriber._ctrl = undefined;\n }\n if (!this._subscribers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n [Symbol.toPrimitive](): Value | undefined {\n return this.state.value;\n }\n}\n\nclass PrimitiveAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: true;\n declare readonly _needExecute: false;\n _needPropagate: boolean = false;\n _marked: boolean = false;\n\n declare readonly _init: Value;\n declare readonly _equals: AtomEquals<Value> | undefined;\n\n declare state: AtomSuccessState<Value>;\n declare _hasValue: true;\n declare _nextValue: Value;\n declare _nextError: undefined;\n declare _resolve: undefined;\n declare _reject: undefined;\n\n constructor(init: Value, options?: AtomOptions<Value>) {\n super();\n this._nextValue = this._init = init;\n this._equals = options?.equals;\n this.state = {\n active: true,\n promise: undefined,\n error: undefined,\n value: init,\n };\n }\n\n set(this: PrimitiveAtomInternal<Value>, value: AtomUpdater<Value>) {\n const nextValue = value instanceof Function ? value(this._nextValue!) : value;\n if (!Object.is(nextValue, this._nextValue)) {\n this._nextValue = nextValue;\n requestPropagate(this);\n }\n }\n}\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._source = true;\nPrimitiveAtomInternal.prototype._hasValue = true;\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._needExecute = false;\n\nclass DerivedAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: false;\n\n _hasValue = false;\n _needExecute = false;\n _needPropagate = false;\n _marked = false;\n\n _counter = 0;\n _resolve: ((value: Value) => void) | undefined;\n _reject: ((reason: any) => void) | undefined;\n _ctrl: ThenableSignalController | undefined;\n _dependencies: Set<AtomInternal<any>> | undefined;\n _allDependencies: Set<AtomInternal<any>> | undefined;\n\n declare readonly _init: AtomGetterInternal<Value>;\n declare readonly _equals: AtomEquals<Value> | undefined;\n declare readonly _persist: boolean;\n declare readonly _options: AtomGetOptions;\n\n declare state: AtomState<Value>;\n\n constructor(init: AtomGetter<Value>, options?: AtomOptions<Value>) {\n super();\n this._init = init as AtomGetterInternal<Value>;\n this._equals = options?.equals;\n this._persist = !!options?.persist;\n\n const self = this;\n this._options = {\n get signal() {\n return (self._ctrl ||= createThenableSignal()).signal;\n },\n };\n\n this.state = {\n active: false,\n promise: undefined,\n error: undefined,\n value: undefined,\n };\n }\n}\n// @ts-expect-error\nDerivedAtomInternal.prototype._source = false;\n\nexport const $: CreateAtom = <Value>(\n init: Value | AtomGetter<Value>,\n options?: AtomOptions<Value>,\n) => {\n if (init instanceof Function) return new DerivedAtomInternal(init, options);\n return new PrimitiveAtomInternal(init, options) as any;\n};\n\nexport const isAtom = (x: unknown): x is Atom<unknown> => x instanceof CommonAtomInternal;\n\nexport const isPrimitiveAtom = (x: unknown): x is PrimitiveAtom<unknown> =>\n x instanceof PrimitiveAtomInternal;\n\nexport type AtomValuePair<Value> =\n | [Atom<Value>, Value | PrimitiveAtom<Value>]\n | [DerivedAtom<Value>, Value | Atom<Value>];\nexport const createScope = <T extends AtomValuePair<unknown>[]>(\n parentScope?: AtomScope | null,\n atomValuePairs?: T,\n): AtomScope => {\n const scopeMap = new WeakMap<Atom<any>, Atom<any>>();\n const atomMap = parentScope ? new WeakMap<Atom<any>, Atom<any>>() : scopeMap;\n const scope = (<T extends Atom<unknown>>(baseAtom: T, strict = false) => {\n let scopedAtom = scopeMap.get(baseAtom);\n if (!strict) scopedAtom ||= atomMap.get(baseAtom);\n // TODO: 현재 스코프마다 사용되는 모든 아톰을 저장해서 메모리 사용이 비효율적인데 해결할 수 있을까?\n // 의존성이 동적이라 많이 어렵다\n if (!scopedAtom) {\n const parentAtom = parentScope?.(baseAtom, true);\n if (strict) return parentAtom;\n const realBaseAtom = parentAtom || baseAtom;\n atomMap.set(\n baseAtom,\n (scopedAtom = (\n (realBaseAtom as AtomInternal<never>)._init instanceof Function\n ? $(\n (get, options) =>\n (realBaseAtom as AtomInternal<never>)._init((atom) => get(scope(atom)), options),\n {\n equals: (realBaseAtom as AtomInternal<never>)._equals,\n persist: (realBaseAtom as DerivedAtomInternal<never>)._persist,\n },\n )\n : // baseAtom을 전달하지 않고 새로 생성하는 이유는 SSR 등에서 사용자 간 상태 공유를 막기 위함\n parentAtom || $((realBaseAtom as AtomInternal<any>)._init)\n ) as T),\n );\n }\n return scopedAtom;\n }) as AtomScope;\n if (atomValuePairs) {\n for (const [atom, value] of atomValuePairs) {\n scopeMap.set(atom, isAtom(value) ? (parentScope || scope)(value) : $(value));\n }\n }\n return scope;\n};\n\nlet pendingUpdateAtoms = false;\nlet updateQueue: AtomInternal<any>[] = [];\nlet stack: AtomInternal<any>[] = [];\nconst requestActivate = <Value>(atom: DerivedAtomInternal<Value>) => {\n if (!atom._needExecute) {\n atom._needExecute = true;\n requestPropagate(atom);\n }\n};\nconst requestPropagate = <Value>(atom: AtomInternal<Value>) => {\n if (!atom._needPropagate) {\n atom._needPropagate = true;\n updateQueue.push(atom);\n if (!pendingUpdateAtoms) {\n pendingUpdateAtoms = true;\n queueMicrotask(updateAtoms);\n }\n }\n};\nconst updateAtoms = () => {\n pendingUpdateAtoms = false;\n {\n const updatedAtoms = updateQueue;\n updateQueue = [];\n for (const atom of updatedAtoms) {\n if (atom.state.active) {\n const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;\n if ((atom.state.error = atom._nextError)) {\n atom._nextValue = atom.state.value;\n if (atom._reject) {\n atom._reject(atom._nextError);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n } else {\n if (\n !atom._hasValue ||\n (!Object.is(atom._nextValue, atom.state.value) &&\n !atom._equals?.(atom._nextValue, atom.state.value!))\n ) {\n atom.state.value = atom._nextValue;\n atom._valueChanged = atom._hasValue = true;\n } else {\n atom._nextValue = atom.state.value;\n if (prevSuccess) {\n atom._needPropagate = false;\n continue;\n }\n }\n if (atom._resolve) {\n atom._resolve(atom._nextValue!);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n }\n mark(atom);\n }\n }\n const markedAtoms = stack;\n stack = [];\n for (let i = markedAtoms.length; i--; ) {\n const atom = markedAtoms[i]!;\n atom._marked = false;\n if (atom._needExecute) {\n atom._needPropagate = true;\n execute(atom);\n }\n if (atom._needPropagate) {\n propagate(atom);\n }\n }\n};\nconst propagate = <Value>(atom: AtomInternal<Value>) => {\n atom._needPropagate = false;\n if (atom._watchers) {\n for (const watcher of atom._watchers) {\n try {\n watcher();\n } catch (e) {\n logError(e);\n }\n }\n }\n if (atom.state.promise) {\n if (atom._children) {\n for (const child of atom._children) {\n child.state.promise ??= new Promise((resolve, reject) => {\n child._resolve = resolve;\n child._reject = reject;\n });\n child._needPropagate = true;\n }\n }\n } else if (atom.state.error) {\n if (atom._children) {\n for (const child of atom._children) {\n child.state.error = child._nextError = atom.state.error;\n if (child._reject) {\n child._reject(child._nextError);\n child._resolve = child._reject = child.state.promise = undefined;\n }\n child._needPropagate = true;\n }\n }\n } else {\n if (atom._valueChanged && atom._subscribers) {\n for (const subscriber of atom._subscribers) {\n if (subscriber._ctrl) {\n subscriber._ctrl.abort();\n subscriber._ctrl = undefined;\n }\n try {\n subscriber._subscriber(atom.state.value!, subscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n }\n if (atom._children) {\n for (const child of atom._children) {\n child._needExecute = true;\n }\n }\n }\n atom._valueChanged = false;\n};\nconst mark = (atom: AtomInternal<any>) => {\n if (!atom._marked) {\n atom._marked = true;\n if (atom._children) {\n for (const child of atom._children) {\n mark(child);\n }\n }\n stack.push(atom);\n }\n};\n\nclass Wrapped {\n e: unknown;\n constructor(e: unknown) {\n this.e = e;\n }\n}\nconst expired = Symbol();\nconst loading = Symbol();\nconst execute = <Value>(atom: DerivedAtomInternal<Value>) => {\n const counter = ++atom._counter;\n const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;\n\n atom.state.active = true;\n atom._needExecute = false;\n\n if (atom._dependencies) {\n for (const dep of atom._dependencies) {\n dep._children!.delete(atom);\n // TODO?: if (dep.aggressiveGc) disableAtom(dep);\n }\n atom._dependencies.clear();\n }\n if (atom._ctrl) {\n atom._ctrl.abort();\n atom._ctrl = undefined;\n }\n\n try {\n const value = atom._init(<V>(anotherAtom: AtomInternal<V>) => {\n if (counter !== atom._counter) throw expired;\n\n if ((atom as unknown) !== anotherAtom) {\n if (!anotherAtom.state.active) {\n execute(anotherAtom as DerivedAtomInternal<V>);\n }\n if (!atom._allDependencies) {\n atom._allDependencies = new Set();\n atom._dependencies = new Set();\n }\n atom._dependencies!.add(anotherAtom);\n atom._allDependencies.add(anotherAtom);\n (anotherAtom._children ||= new Set()).add(atom);\n }\n\n const { state } = anotherAtom;\n if (state.promise) throw loading;\n if (state.error) throw new Wrapped(state.error);\n return state.value as V;\n }, atom._options);\n\n if (isPromiseLike(value)) {\n atom.state.promise ??= new Promise((resolve, reject) => {\n atom._resolve = resolve;\n atom._reject = reject;\n });\n value.then(\n (value) => {\n if (counter === atom._counter) {\n ++atom._counter;\n if (!atom._hasValue || !Object.is(value, atom._nextValue!)) atom._nextValue = value;\n atom._nextError = undefined;\n requestPropagate(atom);\n }\n },\n (e) => {\n if (counter === atom._counter && e !== expired) {\n ++atom._counter;\n if (e !== loading) {\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom._nextError = e;\n requestPropagate(atom);\n }\n }\n },\n );\n } else {\n ++atom._counter;\n if (\n !atom._hasValue ||\n (!Object.is(value, atom._nextValue) && !atom._equals?.(value, atom._nextValue!))\n ) {\n atom.state.value = atom._nextValue = value;\n atom._valueChanged = atom._hasValue = true;\n } else if (prevSuccess) {\n atom._needPropagate = false;\n }\n atom.state.error = atom._nextError = undefined;\n if (atom._resolve) {\n atom._resolve(atom._nextValue!);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n } catch (e) {\n // assert(e !== expired);\n ++atom._counter;\n if (e === loading) {\n atom.state.promise ??= new Promise((resolve, reject) => {\n atom._resolve = resolve;\n atom._reject = reject;\n });\n } else {\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom.state.error = atom._nextError = e;\n if (atom._reject) {\n atom._reject(e);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n }\n};\n\nlet runningGc = false;\nlet gcCandidates: Set<DerivedAtomInternal<any>> = new Set();\nconst disableAtom = <Value>(atom: AtomInternal<Value>) => {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n gcCandidates.add(atom);\n if (!runningGc) {\n runningGc = true;\n setTimeout(gc, 0);\n }\n }\n};\nconst gc = () => {\n for (const atom of gcCandidates) {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n atom._ctrl?.abort();\n // atom._reject?.(null);\n ++atom._counter;\n atom._nextValue =\n atom._nextError =\n atom.state.error =\n atom.state.value =\n atom.state.promise =\n atom._resolve =\n atom._reject =\n atom._ctrl =\n undefined;\n atom._needPropagate = atom._needExecute = atom._hasValue = atom.state.active = false;\n atom._valueChanged = atom._source;\n if (atom._allDependencies) {\n if (atom._dependencies) {\n for (const dep of atom._dependencies) {\n dep._children!.delete(atom);\n }\n atom._dependencies.clear();\n }\n for (const dep of atom._allDependencies) {\n disableAtom(dep);\n }\n atom._allDependencies.clear();\n }\n }\n }\n gcCandidates.clear();\n runningGc = false;\n};\n\nconst isPromiseLike = (x: unknown): x is PromiseLike<unknown> =>\n typeof (x as PromiseLike<unknown>)?.then === \"function\";\n\nconst createThenableSignal = () => {\n const ctrl = new AbortController();\n const signal = ctrl.signal as ThenableSignal;\n const promise = new Promise((resolve) => {\n signal.then = (f: () => void) => promise.then(f);\n signal.addEventListener(\"abort\", resolve, {\n once: true,\n passive: true,\n });\n });\n return {\n abort: () => ctrl.abort(),\n signal,\n };\n};\n\nconst logError = (e: unknown) => {\n // Chrome's console.error doesn't follow the stack trace of the given Error\n queueMicrotask(() => {\n throw e;\n });\n};\n","import { $, isAtom, isPrimitiveAtom } from \"./atom.ts\";\nimport type { PrimitiveAtom, Atom, AtomGetter, DerivedAtom } from \"./atom.ts\";\n\nexport type Atomized<T> = T extends object ? { [K in keyof T]: Atomized<T[K]> } : PrimitiveAtom<T>;\n\nexport type CollectedAtoms<T> =\n T extends Atom<infer U> ? U : T extends object ? { [K in keyof T]: CollectedAtoms<T[K]> } : T;\n\ntype CollectAtom = {\n <Value>(init: AtomGetter<Value>): DerivedAtom<Value>;\n <Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;\n};\n\nexport type RecursiveOptional<T> =\n | T\n | (T extends object\n ? {\n [P in keyof T]: RecursiveOptional<T[P]>;\n }\n : never);\n\nconst ouroboros: any = () => ouroboros;\nconst toUndefined = () => undefined;\nObject.setPrototypeOf(\n ouroboros,\n new Proxy(ouroboros, {\n get: (_, k) => (k === Symbol.toPrimitive ? toUndefined : ouroboros),\n }),\n);\n\nexport const atomize = <T>(tree: T): Atomized<T> => {\n if (typeof tree !== \"object\" || tree === null) return $(tree) as any;\n if (Array.isArray(tree)) return tree.map(atomize) as any;\n const result = Object.create(null);\n for (const k in tree) result[k] = atomize(tree[k]);\n return result;\n};\n\nconst getAtom = <T>(atom: Atom<T>): T => atom.get();\nexport const collectAtoms = <T>(tree: T, get = getAtom): CollectedAtoms<T> => {\n const recurse = <T>(t: T): CollectedAtoms<T> => {\n if (typeof t !== \"object\" || t === null) return t as any;\n if (isAtom(t)) return get(t) as any;\n if (Array.isArray(t)) return t.map(recurse) as any;\n const result = Object.create(null);\n for (const k in t) result[k] = recurse(t[k] as any);\n return result;\n };\n return recurse(tree);\n};\n\nexport const setAtoms = <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>): void => {\n const recurse = (t: any, v: any) => {\n if (typeof t === \"object\" && t !== null) {\n if (isAtom(t)) {\n if (isPrimitiveAtom(t)) t.set(v);\n } else {\n for (const k in v) recurse(t[k], v[k]);\n }\n }\n };\n recurse(tree, values);\n};\n\nexport const $$: CollectAtom = <Value>(init: Value | AtomGetter<Value>) =>\n init instanceof Function\n ? $(\n (get, options) => {\n let promises: PromiseLike<unknown>[] | undefined;\n let error: unknown;\n const result = init((atom) => {\n try {\n get(atom);\n } catch (e) {}\n const state = atom.state;\n if (state.error) error = state.error;\n else if (state.promise) (promises ||= []).push(state.promise);\n else return state.value;\n return ouroboros;\n }, options);\n if (error) throw error;\n return promises ? Promise.all(promises) : result;\n },\n {\n equals: shallowEquals,\n },\n )\n : $$((get) => collectAtoms(init, get));\n\nconst shallowEquals = (a: any, b: any): boolean => {\n if (typeof a !== \"object\" || typeof b !== \"object\" || !a || !b) return false;\n const c = a.constructor;\n if (c !== b.constructor) return false;\n\n if (c === Array) {\n let i = a.length;\n if (i !== b.length) return false;\n while ((i = (i - 1) | 0) >= 0) if (!Object.is(a[i], b[i])) return false;\n return true;\n }\n\n let n = 0;\n for (const k in a) {\n if (!(k in b && Object.is(a[k], b[k]))) return false;\n n = (n + 1) | 0;\n }\n for (const _ in b) if ((n = (n - 1) | 0) < 0) return false;\n return true;\n};\n"],"mappings":"AAwGA,IAAe,EAAf,KAAyC,CACvC,WACA,WACA,UACA,UACA,aACA,cAAgB,GAchB,KAAa,CAKX,GAJK,KAAK,MAAM,SACd,EAAQ,KAA8C,CACtD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E,GAAI,EAAE,GAAG,EAAG,MAAO,GAC/C,EAAK,EAAI,EAAK,EAEhB,IAAK,IAAM,KAAK,EAAG,IAAK,EAAK,EAAI,EAAK,GAAK,EAAG,MAAO,GACrD,MAAO"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { $, Atom, AtomEquals, AtomErrorState, AtomGetOptions, AtomGetter, AtomInactiveState, AtomInit, AtomOptions, AtomPromiseState, AtomReducer, AtomScope, AtomState, AtomSubscribe, AtomSubscriberOptions, AtomSuccessState, AtomUpdater, AtomValuePair, AtomWatcher, CommonAtom, DerivedAtom, GetAtom, MapLike, PrimitiveAtom, SetLike, ThenableSignal, createScope, isAtom, isPrimitiveAtom } from "./atom.mjs";
|
|
2
|
+
import { $$, Atomized, CollectedAtoms, RecursiveOptional, atomize, collectAtoms, setAtoms } from "./utils.mjs";
|
|
3
|
+
export { $, $$, Atom, AtomEquals, AtomErrorState, AtomGetOptions, AtomGetter, AtomInactiveState, AtomInit, AtomOptions, AtomPromiseState, AtomReducer, AtomScope, AtomState, AtomSubscribe, AtomSubscriberOptions, AtomSuccessState, AtomUpdater, AtomValuePair, AtomWatcher, Atomized, CollectedAtoms, CommonAtom, DerivedAtom, GetAtom, MapLike, PrimitiveAtom, RecursiveOptional, SetLike, ThenableSignal, atomize, collectAtoms, createScope, isAtom, isPrimitiveAtom, setAtoms };
|
package/dist/index.mjs
ADDED
package/dist/react.d.mts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Atom, AtomScope, AtomUpdater, AtomValuePair, DerivedAtom, PrimitiveAtom } from "./atom.mjs";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/react.d.ts
|
|
6
|
+
declare const ScopeContext: React.Context<AtomScope>;
|
|
7
|
+
declare const ScopeProvider: ({
|
|
8
|
+
value,
|
|
9
|
+
children
|
|
10
|
+
}: {
|
|
11
|
+
value?: AtomValuePair<unknown>[];
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
}) => react_jsx_runtime0.JSX.Element;
|
|
14
|
+
declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
|
|
15
|
+
declare const useAtomState: <Value>(atom: DerivedAtom<Value>) => {
|
|
16
|
+
active: false;
|
|
17
|
+
error: any;
|
|
18
|
+
promise: undefined;
|
|
19
|
+
value?: Value | undefined;
|
|
20
|
+
} | {
|
|
21
|
+
active: true;
|
|
22
|
+
error: any;
|
|
23
|
+
promise: PromiseLike<Value>;
|
|
24
|
+
value?: Value | undefined;
|
|
25
|
+
} | {
|
|
26
|
+
active: true;
|
|
27
|
+
error: undefined;
|
|
28
|
+
promise: undefined;
|
|
29
|
+
value: Value;
|
|
30
|
+
} | {
|
|
31
|
+
active: true;
|
|
32
|
+
error: any;
|
|
33
|
+
promise: undefined;
|
|
34
|
+
value?: Value | undefined;
|
|
35
|
+
};
|
|
36
|
+
declare const useScopedAtom: UseScopedAtom;
|
|
37
|
+
declare const useAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: AtomUpdater<Value>) => void];
|
|
38
|
+
type UseScopedAtom = {
|
|
39
|
+
<Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;
|
|
40
|
+
<Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
|
|
41
|
+
<Value>(baseAtom: Atom<Value>): Atom<Value>;
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
export { ScopeContext, ScopeProvider, UseScopedAtom, useAtom, useAtomState, useAtomValue, useScopedAtom };
|
|
45
|
+
//# sourceMappingURL=react.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.mts","names":[],"sources":["../src/react.tsx"],"mappings":";;;;;cAaa,YAAA,EAAY,KAAA,CAAA,OAAA,CAAA,SAAA;AAAA,cAEZ,aAAA;EAAiB,KAAA;EAAA;AAAA;EAI5B,KAAA,GAAQ,aAAA;EACR,QAAA,EAAU,KAAA,CAAM,SAAA;AAAA,MACjB,kBAAA,CAAA,GAAA,CAAA,OAAA;AAAA,cAgBY,YAAA,UAAwB,IAAA,EAAM,IAAA,CAAK,KAAA,MAAM,KAAA;AAAA,cAkBzC,YAAA,UAAwB,IAAA,EAAM,WAAA,CAAY,KAAA;;;;;;;;;;;;;;;;;;;;;cA0B1C,aAAA,EACwB,aAAA;AAAA,cAExB,OAAA,UAAmB,IAAA,EAAM,aAAA,CAAc,KAAA,gBAAM,KAAA,GAAA,QAAA,EAEf,WAAA,CAAY,KAAA;AAAA,KAI3C,aAAA;EAAA,QACF,QAAA,EAAU,aAAA,CAAc,KAAA,IAAS,aAAA,CAAc,KAAA;EAAA,QAC/C,QAAA,EAAU,WAAA,CAAY,KAAA,IAAS,WAAA,CAAY,KAAA;EAAA,QAC3C,QAAA,EAAU,IAAA,CAAK,KAAA,IAAS,IAAA,CAAK,KAAA;AAAA"}
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createScope } from "./atom.mjs";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { createContext, useContext, useMemo, useRef, useSyncExternalStore, version } from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
//#region src/react.tsx
|
|
6
|
+
const ScopeContext = createContext((x) => x);
|
|
7
|
+
const ScopeProvider = ({ value, children }) => {
|
|
8
|
+
const parentScope = useContext(ScopeContext);
|
|
9
|
+
const scope = useMemo(() => createScope(value && parentScope, value), [parentScope, ...(value || []).flat()]);
|
|
10
|
+
return /* @__PURE__ */ jsx(ScopeContext.Provider, {
|
|
11
|
+
value: scope,
|
|
12
|
+
children
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
const sameAtomState = (a, b) => a.promise === b.promise && Object.is(a.error, b.error) && Object.is(a.value, b.value);
|
|
16
|
+
const REACT_USE = (parseInt(version || "19", 10) || 19) >= 19 && "use" in React;
|
|
17
|
+
const useAtomValue = (atom) => {
|
|
18
|
+
atom = useContext(ScopeContext)(atom);
|
|
19
|
+
const getSnapshot = () => {
|
|
20
|
+
try {
|
|
21
|
+
return atom.get();
|
|
22
|
+
} catch (_) {
|
|
23
|
+
if (atom.state.promise) {
|
|
24
|
+
const promise = Promise.resolve(atom.state.promise);
|
|
25
|
+
if (REACT_USE) React.use(promise);
|
|
26
|
+
throw promise;
|
|
27
|
+
}
|
|
28
|
+
throw atom.state.error;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return useSyncExternalStore((watcher) => atom.watch(watcher), getSnapshot, getSnapshot);
|
|
32
|
+
};
|
|
33
|
+
const useAtomState = (atom) => {
|
|
34
|
+
atom = useContext(ScopeContext)(atom);
|
|
35
|
+
const stateSnapshot = useRef({ ...atom.state });
|
|
36
|
+
const getSnapshot = () => {
|
|
37
|
+
try {
|
|
38
|
+
atom.get();
|
|
39
|
+
} catch (_) {}
|
|
40
|
+
if (!sameAtomState(stateSnapshot.current, atom.state)) stateSnapshot.current = { ...atom.state };
|
|
41
|
+
return stateSnapshot.current;
|
|
42
|
+
};
|
|
43
|
+
return useSyncExternalStore((watcher) => atom.watch(() => {
|
|
44
|
+
if (!sameAtomState(stateSnapshot.current, atom.state)) {
|
|
45
|
+
stateSnapshot.current = { ...atom.state };
|
|
46
|
+
watcher();
|
|
47
|
+
}
|
|
48
|
+
}), getSnapshot, getSnapshot);
|
|
49
|
+
};
|
|
50
|
+
const useScopedAtom = ((atom) => useContext(ScopeContext)(atom));
|
|
51
|
+
const useAtom = (atom) => {
|
|
52
|
+
atom = useScopedAtom(atom);
|
|
53
|
+
const setAtom = useMemo(() => (newState) => atom.set(newState), [atom]);
|
|
54
|
+
return [useAtomValue(atom), setAtom];
|
|
55
|
+
};
|
|
56
|
+
//#endregion
|
|
57
|
+
export { ScopeContext, ScopeProvider, useAtom, useAtomState, useAtomValue, useScopedAtom };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=react.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.mjs","names":[],"sources":["../src/react.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { createContext, useContext, useMemo, useRef, useSyncExternalStore, version } from \"react\";\nimport { createScope } from \"./atom.ts\";\nimport type {\n Atom,\n AtomScope,\n AtomState,\n AtomUpdater,\n AtomValuePair,\n DerivedAtom,\n PrimitiveAtom,\n} from \"./atom.ts\";\n\nexport const ScopeContext = createContext<AtomScope>((x) => x as any);\n\nexport const ScopeProvider = ({\n value,\n children,\n}: {\n value?: AtomValuePair<unknown>[];\n children: React.ReactNode;\n}) => {\n const parentScope = useContext(ScopeContext);\n const scope = useMemo(\n () => createScope(value && parentScope, value),\n // oxlint-disable-next-line exhaustive-deps\n [parentScope, ...(value || []).flat()],\n );\n return <ScopeContext.Provider value={scope}>{children}</ScopeContext.Provider>;\n};\n\n// TODO: cleanup\nconst sameAtomState = <Value,>(a: AtomState<Value>, b: AtomState<Value>) =>\n a.promise === b.promise && Object.is(a.error, b.error) && Object.is(a.value, b.value);\n\nconst REACT_MAJOR_VERSION = parseInt(version || \"19\", 10) || 19;\nconst REACT_USE = REACT_MAJOR_VERSION >= 19 && \"use\" in React;\nexport const useAtomValue = <Value,>(atom: Atom<Value>) => {\n atom = useContext(ScopeContext)(atom);\n const getSnapshot = () => {\n // https://github.com/facebook/react/pull/34032\n try {\n return atom.get();\n } catch (_) {\n if (atom.state.promise) {\n const promise = Promise.resolve(atom.state.promise);\n if (REACT_USE) React.use(promise);\n throw promise;\n }\n throw atom.state.error;\n }\n };\n return useSyncExternalStore((watcher) => atom.watch(watcher), getSnapshot, getSnapshot);\n};\n\nexport const useAtomState = <Value,>(atom: DerivedAtom<Value>) => {\n atom = useContext(ScopeContext)(atom);\n const stateSnapshot = useRef({ ...atom.state });\n const getSnapshot = () => {\n // avoid https://github.com/facebook/react/issues/31730\n try {\n atom.get();\n } catch (_) {}\n if (!sameAtomState(stateSnapshot.current, atom.state)) {\n stateSnapshot.current = { ...atom.state };\n }\n return stateSnapshot.current;\n };\n return useSyncExternalStore(\n (watcher) =>\n atom.watch(() => {\n if (!sameAtomState(stateSnapshot.current, atom.state)) {\n stateSnapshot.current = { ...atom.state };\n watcher();\n }\n }),\n getSnapshot,\n getSnapshot,\n );\n};\n\nexport const useScopedAtom = (<Value,>(atom: Atom<Value>) =>\n useContext(ScopeContext)(atom)) as UseScopedAtom;\n\nexport const useAtom = <Value,>(atom: PrimitiveAtom<Value>) => {\n atom = useScopedAtom(atom);\n const setAtom = useMemo(() => (newState: AtomUpdater<Value>) => atom.set(newState), [atom]);\n return [useAtomValue(atom), setAtom] as const;\n};\n\nexport type UseScopedAtom = {\n <Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;\n <Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;\n <Value>(baseAtom: Atom<Value>): Atom<Value>;\n};\n"],"mappings":";;;;;AAaA,MAAa,eAAe,eAA0B,MAAM,EAAS;AAErE,MAAa,iBAAiB,EAC5B,OACA,eAII;CACJ,MAAM,cAAc,WAAW,aAAa;CAC5C,MAAM,QAAQ,cACN,YAAY,SAAS,aAAa,MAAM,EAE9C,CAAC,aAAa,IAAI,SAAS,EAAE,EAAE,MAAM,CAAC,CACvC;AACD,QAAO,oBAAC,aAAa,UAAd;EAAuB,OAAO;EAAQ;EAAiC,CAAA;;AAIhF,MAAM,iBAAyB,GAAqB,MAClD,EAAE,YAAY,EAAE,WAAW,OAAO,GAAG,EAAE,OAAO,EAAE,MAAM,IAAI,OAAO,GAAG,EAAE,OAAO,EAAE,MAAM;AAGvF,MAAM,aADsB,SAAS,WAAW,MAAM,GAAG,IAAI,OACpB,MAAM,SAAS;AACxD,MAAa,gBAAwB,SAAsB;AACzD,QAAO,WAAW,aAAa,CAAC,KAAK;CACrC,MAAM,oBAAoB;AAExB,MAAI;AACF,UAAO,KAAK,KAAK;WACV,GAAG;AACV,OAAI,KAAK,MAAM,SAAS;IACtB,MAAM,UAAU,QAAQ,QAAQ,KAAK,MAAM,QAAQ;AACnD,QAAI,UAAW,OAAM,IAAI,QAAQ;AACjC,UAAM;;AAER,SAAM,KAAK,MAAM;;;AAGrB,QAAO,sBAAsB,YAAY,KAAK,MAAM,QAAQ,EAAE,aAAa,YAAY;;AAGzF,MAAa,gBAAwB,SAA6B;AAChE,QAAO,WAAW,aAAa,CAAC,KAAK;CACrC,MAAM,gBAAgB,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC;CAC/C,MAAM,oBAAoB;AAExB,MAAI;AACF,QAAK,KAAK;WACH,GAAG;AACZ,MAAI,CAAC,cAAc,cAAc,SAAS,KAAK,MAAM,CACnD,eAAc,UAAU,EAAE,GAAG,KAAK,OAAO;AAE3C,SAAO,cAAc;;AAEvB,QAAO,sBACJ,YACC,KAAK,YAAY;AACf,MAAI,CAAC,cAAc,cAAc,SAAS,KAAK,MAAM,EAAE;AACrD,iBAAc,UAAU,EAAE,GAAG,KAAK,OAAO;AACzC,YAAS;;GAEX,EACJ,aACA,YACD;;AAGH,MAAa,kBAA0B,SACrC,WAAW,aAAa,CAAC,KAAK;AAEhC,MAAa,WAAmB,SAA+B;AAC7D,QAAO,cAAc,KAAK;CAC1B,MAAM,UAAU,eAAe,aAAiC,KAAK,IAAI,SAAS,EAAE,CAAC,KAAK,CAAC;AAC3F,QAAO,CAAC,aAAa,KAAK,EAAE,QAAQ"}
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Atom, AtomGetter, DerivedAtom, PrimitiveAtom } from "./atom.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils.d.ts
|
|
4
|
+
type Atomized<T> = T extends object ? { [K in keyof T]: Atomized<T[K]> } : PrimitiveAtom<T>;
|
|
5
|
+
type CollectedAtoms<T> = T extends Atom<infer U> ? U : T extends object ? { [K in keyof T]: CollectedAtoms<T[K]> } : T;
|
|
6
|
+
type CollectAtom = {
|
|
7
|
+
<Value>(init: AtomGetter<Value>): DerivedAtom<Value>;
|
|
8
|
+
<Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;
|
|
9
|
+
};
|
|
10
|
+
type RecursiveOptional<T> = T | (T extends object ? { [P in keyof T]: RecursiveOptional<T[P]> } : never);
|
|
11
|
+
declare const atomize: <T>(tree: T) => Atomized<T>;
|
|
12
|
+
declare const collectAtoms: <T>(tree: T, get?: <T_1>(atom: Atom<T_1>) => T_1) => CollectedAtoms<T>;
|
|
13
|
+
declare const setAtoms: <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>) => void;
|
|
14
|
+
declare const $$: CollectAtom;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { $$, Atomized, CollectedAtoms, RecursiveOptional, atomize, collectAtoms, setAtoms };
|
|
17
|
+
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.mts","names":[],"sources":["../src/utils.ts"],"mappings":";;;KAGY,QAAA,MAAc,CAAA,gCAAiC,CAAA,GAAI,QAAA,CAAS,CAAA,CAAE,CAAA,OAAQ,aAAA,CAAc,CAAA;AAAA,KAEpF,cAAA,MACV,CAAA,SAAU,IAAA,YAAgB,CAAA,GAAI,CAAA,gCAAiC,CAAA,GAAI,cAAA,CAAe,CAAA,CAAE,CAAA,OAAQ,CAAA;AAAA,KAEzF,WAAA;EAAA,QACK,IAAA,EAAM,UAAA,CAAW,KAAA,IAAS,WAAA,CAAY,KAAA;EAAA,QACtC,IAAA,EAAM,KAAA,GAAQ,WAAA,CAAY,cAAA,CAAe,KAAA;AAAA;AAAA,KAGvC,iBAAA,MACR,CAAA,IACC,CAAA,gCAEiB,CAAA,GAAI,iBAAA,CAAkB,CAAA,CAAE,CAAA;AAAA,cAajC,OAAA,MAAc,IAAA,EAAM,CAAA,KAAI,QAAA,CAAS,CAAA;AAAA,cASjC,YAAA,MAAmB,IAAA,EAAM,CAAA,EAAG,GAAA,SAAA,IAAA,EADf,IAAA,CAAK,GAAA,MAAK,GAAA,KACqB,cAAA,CAAe,CAAA;AAAA,cAY3D,QAAA,MAAe,IAAA,EAAM,CAAA,EAAG,MAAA,EAAQ,iBAAA,CAAkB,cAAA,CAAe,CAAA;AAAA,cAajE,EAAA,EAAI,WAAA"}
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { $, isAtom, isPrimitiveAtom } from "./atom.mjs";
|
|
2
|
+
//#region src/utils.ts
|
|
3
|
+
const ouroboros = () => ouroboros;
|
|
4
|
+
const toUndefined = () => void 0;
|
|
5
|
+
Object.setPrototypeOf(ouroboros, new Proxy(ouroboros, { get: (_, k) => k === Symbol.toPrimitive ? toUndefined : ouroboros }));
|
|
6
|
+
const atomize = (tree) => {
|
|
7
|
+
if (typeof tree !== "object" || tree === null) return $(tree);
|
|
8
|
+
if (Array.isArray(tree)) return tree.map(atomize);
|
|
9
|
+
const result = Object.create(null);
|
|
10
|
+
for (const k in tree) result[k] = atomize(tree[k]);
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
const getAtom = (atom) => atom.get();
|
|
14
|
+
const collectAtoms = (tree, get = getAtom) => {
|
|
15
|
+
const recurse = (t) => {
|
|
16
|
+
if (typeof t !== "object" || t === null) return t;
|
|
17
|
+
if (isAtom(t)) return get(t);
|
|
18
|
+
if (Array.isArray(t)) return t.map(recurse);
|
|
19
|
+
const result = Object.create(null);
|
|
20
|
+
for (const k in t) result[k] = recurse(t[k]);
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
return recurse(tree);
|
|
24
|
+
};
|
|
25
|
+
const setAtoms = (tree, values) => {
|
|
26
|
+
const recurse = (t, v) => {
|
|
27
|
+
if (typeof t === "object" && t !== null) if (isAtom(t)) {
|
|
28
|
+
if (isPrimitiveAtom(t)) t.set(v);
|
|
29
|
+
} else for (const k in v) recurse(t[k], v[k]);
|
|
30
|
+
};
|
|
31
|
+
recurse(tree, values);
|
|
32
|
+
};
|
|
33
|
+
const $$ = (init) => init instanceof Function ? $((get, options) => {
|
|
34
|
+
let promises;
|
|
35
|
+
let error;
|
|
36
|
+
const result = init((atom) => {
|
|
37
|
+
try {
|
|
38
|
+
get(atom);
|
|
39
|
+
} catch (e) {}
|
|
40
|
+
const state = atom.state;
|
|
41
|
+
if (state.error) error = state.error;
|
|
42
|
+
else if (state.promise) (promises ||= []).push(state.promise);
|
|
43
|
+
else return state.value;
|
|
44
|
+
return ouroboros;
|
|
45
|
+
}, options);
|
|
46
|
+
if (error) throw error;
|
|
47
|
+
return promises ? Promise.all(promises) : result;
|
|
48
|
+
}, { equals: shallowEquals }) : $$((get) => collectAtoms(init, get));
|
|
49
|
+
const shallowEquals = (a, b) => {
|
|
50
|
+
if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
|
|
51
|
+
const c = a.constructor;
|
|
52
|
+
if (c !== b.constructor) return false;
|
|
53
|
+
if (c === Array) {
|
|
54
|
+
let i = a.length;
|
|
55
|
+
if (i !== b.length) return false;
|
|
56
|
+
while ((i = i - 1 | 0) >= 0) if (!Object.is(a[i], b[i])) return false;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
let n = 0;
|
|
60
|
+
for (const k in a) {
|
|
61
|
+
if (!(k in b && Object.is(a[k], b[k]))) return false;
|
|
62
|
+
n = n + 1 | 0;
|
|
63
|
+
}
|
|
64
|
+
for (const _ in b) if ((n = n - 1 | 0) < 0) return false;
|
|
65
|
+
return true;
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
export { $$, atomize, collectAtoms, setAtoms };
|
|
69
|
+
|
|
70
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import { $, isAtom, isPrimitiveAtom } from \"./atom.ts\";\nimport type { PrimitiveAtom, Atom, AtomGetter, DerivedAtom } from \"./atom.ts\";\n\nexport type Atomized<T> = T extends object ? { [K in keyof T]: Atomized<T[K]> } : PrimitiveAtom<T>;\n\nexport type CollectedAtoms<T> =\n T extends Atom<infer U> ? U : T extends object ? { [K in keyof T]: CollectedAtoms<T[K]> } : T;\n\ntype CollectAtom = {\n <Value>(init: AtomGetter<Value>): DerivedAtom<Value>;\n <Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;\n};\n\nexport type RecursiveOptional<T> =\n | T\n | (T extends object\n ? {\n [P in keyof T]: RecursiveOptional<T[P]>;\n }\n : never);\n\nconst ouroboros: any = () => ouroboros;\nconst toUndefined = () => undefined;\nObject.setPrototypeOf(\n ouroboros,\n new Proxy(ouroboros, {\n get: (_, k) => (k === Symbol.toPrimitive ? toUndefined : ouroboros),\n }),\n);\n\nexport const atomize = <T>(tree: T): Atomized<T> => {\n if (typeof tree !== \"object\" || tree === null) return $(tree) as any;\n if (Array.isArray(tree)) return tree.map(atomize) as any;\n const result = Object.create(null);\n for (const k in tree) result[k] = atomize(tree[k]);\n return result;\n};\n\nconst getAtom = <T>(atom: Atom<T>): T => atom.get();\nexport const collectAtoms = <T>(tree: T, get = getAtom): CollectedAtoms<T> => {\n const recurse = <T>(t: T): CollectedAtoms<T> => {\n if (typeof t !== \"object\" || t === null) return t as any;\n if (isAtom(t)) return get(t) as any;\n if (Array.isArray(t)) return t.map(recurse) as any;\n const result = Object.create(null);\n for (const k in t) result[k] = recurse(t[k] as any);\n return result;\n };\n return recurse(tree);\n};\n\nexport const setAtoms = <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>): void => {\n const recurse = (t: any, v: any) => {\n if (typeof t === \"object\" && t !== null) {\n if (isAtom(t)) {\n if (isPrimitiveAtom(t)) t.set(v);\n } else {\n for (const k in v) recurse(t[k], v[k]);\n }\n }\n };\n recurse(tree, values);\n};\n\nexport const $$: CollectAtom = <Value>(init: Value | AtomGetter<Value>) =>\n init instanceof Function\n ? $(\n (get, options) => {\n let promises: PromiseLike<unknown>[] | undefined;\n let error: unknown;\n const result = init((atom) => {\n try {\n get(atom);\n } catch (e) {}\n const state = atom.state;\n if (state.error) error = state.error;\n else if (state.promise) (promises ||= []).push(state.promise);\n else return state.value;\n return ouroboros;\n }, options);\n if (error) throw error;\n return promises ? Promise.all(promises) : result;\n },\n {\n equals: shallowEquals,\n },\n )\n : $$((get) => collectAtoms(init, get));\n\nconst shallowEquals = (a: any, b: any): boolean => {\n if (typeof a !== \"object\" || typeof b !== \"object\" || !a || !b) return false;\n const c = a.constructor;\n if (c !== b.constructor) return false;\n\n if (c === Array) {\n let i = a.length;\n if (i !== b.length) return false;\n while ((i = (i - 1) | 0) >= 0) if (!Object.is(a[i], b[i])) return false;\n return true;\n }\n\n let n = 0;\n for (const k in a) {\n if (!(k in b && Object.is(a[k], b[k]))) return false;\n n = (n + 1) | 0;\n }\n for (const _ in b) if ((n = (n - 1) | 0) < 0) return false;\n return true;\n};\n"],"mappings":";;AAqBA,MAAM,kBAAuB;AAC7B,MAAM,oBAAoB,KAAA;AAC1B,OAAO,eACL,WACA,IAAI,MAAM,WAAW,EACnB,MAAM,GAAG,MAAO,MAAM,OAAO,cAAc,cAAc,WAC1D,CAAC,CACH;AAED,MAAa,WAAc,SAAyB;AAClD,KAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO,EAAE,KAAK;AAC7D,KAAI,MAAM,QAAQ,KAAK,CAAE,QAAO,KAAK,IAAI,QAAQ;CACjD,MAAM,SAAS,OAAO,OAAO,KAAK;AAClC,MAAK,MAAM,KAAK,KAAM,QAAO,KAAK,QAAQ,KAAK,GAAG;AAClD,QAAO;;AAGT,MAAM,WAAc,SAAqB,KAAK,KAAK;AACnD,MAAa,gBAAmB,MAAS,MAAM,YAA+B;CAC5E,MAAM,WAAc,MAA4B;AAC9C,MAAI,OAAO,MAAM,YAAY,MAAM,KAAM,QAAO;AAChD,MAAI,OAAO,EAAE,CAAE,QAAO,IAAI,EAAE;AAC5B,MAAI,MAAM,QAAQ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EAAE,GAAG,CAAE,QAAO;AAClE,SAAO;;CAGT,IAAI,IAAI;AACR,MAAK,MAAM,KAAK,GAAG;AACjB,MAAI,EAAE,KAAK,KAAK,OAAO,GAAG,EAAE,IAAI,EAAE,GAAG,EAAG,QAAO;AAC/C,MAAK,IAAI,IAAK;;AAEhB,MAAK,MAAM,KAAK,EAAG,MAAK,IAAK,IAAI,IAAK,KAAK,EAAG,QAAO;AACrD,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,32 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bansa",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bansa",
|
|
7
|
+
"react",
|
|
8
|
+
"state"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/cgiosy/bansa",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "cgiosy",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git://github.com/cgiosy/bansa.git"
|
|
16
|
+
},
|
|
5
17
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.
|
|
7
|
-
"types": "./dist/index.d.
|
|
18
|
+
"main": "./dist/index.mjs",
|
|
19
|
+
"types": "./dist/index.d.mts",
|
|
8
20
|
"exports": {
|
|
9
21
|
"./package.json": "./package.json",
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
10
26
|
"./react": {
|
|
11
|
-
"types": "./dist/react.d.
|
|
12
|
-
"default": "./dist/react.
|
|
27
|
+
"types": "./dist/react.d.mts",
|
|
28
|
+
"default": "./dist/react.mjs"
|
|
13
29
|
},
|
|
14
|
-
"
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"default": "./dist/
|
|
30
|
+
"./atom": {
|
|
31
|
+
"types": "./dist/atom.d.mts",
|
|
32
|
+
"default": "./dist/atom.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./utils": {
|
|
35
|
+
"types": "./dist/utils.d.mts",
|
|
36
|
+
"default": "./dist/utils.mjs"
|
|
17
37
|
}
|
|
18
38
|
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
19
42
|
"devDependencies": {
|
|
20
|
-
"@
|
|
21
|
-
"@types/react": "^19.2.7",
|
|
43
|
+
"@types/react": "^19.2.14",
|
|
22
44
|
"@types/react-dom": "^19.2.3",
|
|
23
|
-
"
|
|
45
|
+
"oxfmt": "^0.36.0",
|
|
46
|
+
"oxlint": "^1.51.0",
|
|
47
|
+
"react": "^19.2.4",
|
|
48
|
+
"react-dom": "^19.2.4",
|
|
49
|
+
"tsdown": "^0.21.0",
|
|
24
50
|
"typescript": "^5.9.3",
|
|
25
|
-
"vitest": "^4.0.
|
|
51
|
+
"vitest": "^4.0.18"
|
|
26
52
|
},
|
|
27
53
|
"peerDependencies": {
|
|
28
|
-
"@types/react": ">=
|
|
29
|
-
"react": ">=
|
|
54
|
+
"@types/react": ">=18.0.0",
|
|
55
|
+
"react": ">=18.0.0"
|
|
30
56
|
},
|
|
31
57
|
"peerDependenciesMeta": {
|
|
32
58
|
"@types/react": {
|
|
@@ -36,30 +62,13 @@
|
|
|
36
62
|
"optional": true
|
|
37
63
|
}
|
|
38
64
|
},
|
|
39
|
-
"author": "cgiosy",
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git://github.com/cgiosy/bansa.git"
|
|
44
|
-
},
|
|
45
|
-
"publishConfig": {
|
|
46
|
-
"access": "public"
|
|
47
|
-
},
|
|
48
|
-
"homepage": "https://github.com/cgiosy/bansa",
|
|
49
|
-
"keywords": [
|
|
50
|
-
"bansa",
|
|
51
|
-
"state",
|
|
52
|
-
"react"
|
|
53
|
-
],
|
|
54
65
|
"scripts": {
|
|
55
66
|
"build": "pnpm run \"/^build:.*/\"",
|
|
56
|
-
"build:
|
|
57
|
-
"build:
|
|
58
|
-
"build:index-browser": "esbuild src/index.ts --bundle --mangle-props=^_ --minify --bundle --format=esm --target=es2022 --platform=browser --outfile=dist/index.browser.js",
|
|
59
|
-
"build:atom": "esbuild src/atom.ts --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/atom.js",
|
|
60
|
-
"build:utils": "esbuild src/utils.ts --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/utils.js",
|
|
61
|
-
"build:react": "esbuild src/react.tsx --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/react.js",
|
|
67
|
+
"build:bundler": "tsdown src/index.ts src/react.tsx src/atom.ts src/utils.ts --dts --target=es2022",
|
|
68
|
+
"build:browser": "tsdown src/index.ts --dts --minify --target=es2022 --platform=browser --out-dir=dist/browser",
|
|
62
69
|
"test": "vitest",
|
|
63
|
-
"
|
|
70
|
+
"lint": "pnpm check",
|
|
71
|
+
"check": "tsc -b --noEmit && oxlint && oxfmt --check",
|
|
72
|
+
"format": "oxlint --fix && oxfmt"
|
|
64
73
|
}
|
|
65
74
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,28 +1,39 @@
|
|
|
1
1
|
{
|
|
2
|
+
"include": ["**/*.ts", "**/*.tsx"],
|
|
3
|
+
"exclude": ["node_modules", "dist"],
|
|
2
4
|
"compilerOptions": {
|
|
5
|
+
"paths": {
|
|
6
|
+
"@/*": ["./src/*"]
|
|
7
|
+
},
|
|
8
|
+
"rootDir": ".",
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
|
|
11
|
+
"module": "ESNext",
|
|
12
|
+
"moduleResolution": "Bundler",
|
|
3
13
|
"target": "ES2022",
|
|
4
14
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
5
|
-
"types": ["
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"useDefineForClassFields": true,
|
|
9
|
-
"declaration": true,
|
|
10
|
-
|
|
11
|
-
/* Bundler mode */
|
|
12
|
-
"moduleResolution": "bundler",
|
|
15
|
+
"types": ["react", "react-dom"],
|
|
13
16
|
"moduleDetection": "force",
|
|
14
|
-
"
|
|
17
|
+
"jsx": "react-jsx",
|
|
18
|
+
|
|
19
|
+
"skipLibCheck": true,
|
|
15
20
|
"resolveJsonModule": true,
|
|
16
|
-
"
|
|
21
|
+
"allowImportingTsExtensions": true,
|
|
22
|
+
"allowArbitraryExtensions": true,
|
|
23
|
+
"rewriteRelativeImportExtensions": true,
|
|
24
|
+
|
|
25
|
+
"emitDeclarationOnly": true,
|
|
17
26
|
"noEmit": true,
|
|
18
|
-
"
|
|
27
|
+
"sourceMap": true,
|
|
28
|
+
"declaration": true,
|
|
29
|
+
"declarationMap": true,
|
|
30
|
+
"verbatimModuleSyntax": true, // Enforce keyword `type` for type imports etc. implies "isolatedModules"
|
|
19
31
|
|
|
20
|
-
/* Linting */
|
|
21
|
-
"strict": true,
|
|
22
|
-
"noUnusedLocals": true,
|
|
23
|
-
"noUnusedParameters": true,
|
|
24
32
|
"noFallthroughCasesInSwitch": true,
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
"noImplicitOverride": true,
|
|
34
|
+
"noImplicitReturns": false,
|
|
35
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
36
|
+
"noUncheckedIndexedAccess": true,
|
|
37
|
+
"strict": true
|
|
38
|
+
}
|
|
28
39
|
}
|