Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'debugPrivate' flag to atom #1779

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/vanilla/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ type OnMount<Args extends unknown[], Result> = <

export interface Atom<Value> {
toString: () => string
debugLabel?: string
read: Read<Value>
debugLabel?: string
/**
* To ONLY be used by Jotai libraries to mark atoms as private. Subject to change.
* @private
*/
debugPrivate?: boolean
}

export interface WritableAtom<Value, Args extends unknown[], Result>
Expand Down
5 changes: 5 additions & 0 deletions src/vanilla/utils/atomWithDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export function atomWithDefault<Value>(
) {
const EMPTY = Symbol()
const overwrittenAtom = atom<Value | typeof EMPTY>(EMPTY)

if (import.meta.env?.MODE !== 'production') {
overwrittenAtom.debugPrivate = true
}

const anAtom: WritableAtom<
Value,
[SetStateAction<Awaited<Value>> | typeof RESET],
Expand Down
9 changes: 9 additions & 0 deletions src/vanilla/utils/atomWithObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export function atomWithObservable<Data>(
start()

const resultAtom = atom(lastResult || initialResult)

if (import.meta.env?.MODE !== 'production') {
resultAtom.debugPrivate = true
}

resultAtom.onMount = (update) => {
setResult = update
if (lastResult) {
Expand All @@ -156,6 +161,10 @@ export function atomWithObservable<Data>(
return [resultAtom, observable, makePending, start, isNotMounted] as const
})

if (import.meta.env?.MODE !== 'production') {
observableResultAtom.debugPrivate = true
}

const observableAtom = atom(
(get) => {
const [resultAtom] = get(observableResultAtom)
Expand Down
4 changes: 4 additions & 0 deletions src/vanilla/utils/atomWithStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export function atomWithStorage<Value>(
): WritableAtom<Value, [SetStateActionWithReset<Value>], void> {
const baseAtom = atom(initialValue)

if (import.meta.env?.MODE !== 'production') {
baseAtom.debugPrivate = true
}

baseAtom.onMount = (setAtom) => {
const value = storage.getItem(key)
if (value instanceof Promise) {
Expand Down
10 changes: 10 additions & 0 deletions src/vanilla/utils/loadable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function loadable<Value>(anAtom: Atom<Value>): Atom<Loadable<Value>> {
return memo1(() => {
const loadableCache = new WeakMap<Promise<void>, Loadable<Value>>()
const refreshAtom = atom(0)

if (import.meta.env?.MODE !== 'production') {
refreshAtom.debugPrivate = true
}

const derivedAtom = atom(
(get, { setSelf }) => {
get(refreshAtom)
Expand Down Expand Up @@ -44,6 +49,11 @@ export function loadable<Value>(anAtom: Atom<Value>): Atom<Loadable<Value>> {
set(refreshAtom, (c) => c + 1)
}
)

if (import.meta.env?.MODE !== 'production') {
derivedAtom.debugPrivate = true
}

return atom((get) => get(derivedAtom))
}, anAtom)
}
5 changes: 5 additions & 0 deletions src/vanilla/utils/splitAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ export function splitAtom<Item, Key>(
const mapping = getMapping(arr, prev?.arr)
return mapping
})

if (import.meta.env?.MODE !== 'production') {
mappingAtom.debugPrivate = true
}

// HACK to read mapping atom before initialization
mappingAtom.init = undefined
const splittedAtom = isWritable(arrAtom)
Expand Down
10 changes: 10 additions & 0 deletions src/vanilla/utils/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export function unwrap<Value, PendingValue>(
const promiseErrorCache = new WeakMap<Promise<Value>, unknown>()
const promiseResultCache = new WeakMap<Promise<Value>, Awaited<Value>>()
const refreshAtom = atom(0)

if (import.meta.env?.MODE !== 'production') {
refreshAtom.debugPrivate = true
}

const promiseAndValueAtom: WritableAtom<PromiseAndValue, [], void> & {
init?: undefined
} = atom(
Expand Down Expand Up @@ -70,6 +75,11 @@ export function unwrap<Value, PendingValue>(
)
// HACK to read PromiseAndValue atom before initialization
promiseAndValueAtom.init = undefined

if (import.meta.env?.MODE !== 'production') {
promiseAndValueAtom.debugPrivate = true
}

return atom((get) => {
const state = get(promiseAndValueAtom)
if ('v' in state) {
Expand Down
15 changes: 15 additions & 0 deletions tests/vanilla/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ it('creates atoms', () => {
}
`)
})

it('should let users mark atoms as private', () => {
const internalAtom = atom(0)
internalAtom.debugPrivate = true

expect(internalAtom).toMatchInlineSnapshot(`
{
"debugPrivate": true,
"init": 0,
"read": [Function],
"toString": [Function],
"write": [Function],
}
`)
})