Skip to content

Commit

Permalink
feat(scope): Add property scopedOriginal for jotai-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
yf-yang committed Jan 22, 2024
1 parent 83731ed commit a58f246
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/vanilla/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export interface Atom<Value> {
* @private
*/
debugPrivate?: boolean
/**
* @internal
* Original atom of a scoped atom. For package `jotai-scope` ONLY.
*/
scopedOriginal?: Atom<Value>
}

export interface WritableAtom<Value, Args extends unknown[], Result>
Expand Down
10 changes: 9 additions & 1 deletion src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ const returnAtomValue = <Value>(atomState: AtomState<Value>): Value => {
return atomState.v
}

/**
* @returns Original atom if anAtom is a scoped atom in `jotai-scope`. Otherwise it
* returns anAtom itself.
*/
const getScopedOriginal = (anAtom: AnyAtom): AnyAtom => {
return anAtom.scopedOriginal ?? anAtom;
}

type Listeners = Set<() => void>
type Dependents = Set<AnyAtom>

Expand Down Expand Up @@ -524,7 +532,7 @@ export const createStore = () => {
...args: As
) => {
let r: R | undefined
if ((a as AnyWritableAtom) === atom) {
if (getScopedOriginal(a) === getScopedOriginal(atom)) {
if (!hasInitialValue(a)) {
// NOTE technically possible but restricted as it may cause bugs
throw new Error('atom not writable')
Expand Down

0 comments on commit a58f246

Please sign in to comment.