Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
76d734d
docs: document Signal class for v2
bowheart Jul 23, 2025
3b226d1
docs: document `injectSignal` injector for v2
bowheart Jul 23, 2025
25f5442
docs: document `ZeduxNode` class for v2
bowheart Jul 23, 2025
b85ec47
docs: document `AtomInstance` class for v2
bowheart Jul 23, 2025
dda386e
docs: document `Ecosystem` class for v2
bowheart Jul 24, 2025
7399aa8
docs: document `AtomTemplate` class for v2
bowheart Jul 24, 2025
8feebc5
docs: document glossary for v2
bowheart Jul 24, 2025
08ab6e6
docs: document `atom` factory for v2
bowheart Jul 24, 2025
15ab275
docs: document `AtomApi` class for v2
bowheart Jul 24, 2025
cd3e5c1
docs: document `api` factory for v2
bowheart Jul 24, 2025
98223ed
docs: document `SelectorInstance` class for v2
bowheart Jul 24, 2025
142c2f6
docs: document `AtomProvider` component for v2
bowheart Jul 24, 2025
66a18e6
docs: document `EcosystemProvider` component for v2
bowheart Jul 24, 2025
a653a5e
docs: document `ion` factory for v2
bowheart Jul 24, 2025
a4bc835
docs: document `MappedSignal` class for v2
bowheart Jul 24, 2025
040c9b2
docs: document `useEcosystem` hook for v2
bowheart Jul 24, 2025
81d257f
docs: document `createEcosystem` factory for v2
bowheart Jul 24, 2025
ecf9bd9
docs: document `injectEcosystem` injector for v2
bowheart Jul 24, 2025
2a4365c
docs: document `useAtomContext` hook for v2
bowheart Jul 24, 2025
4f03145
docs: document `useAtomInstance` hook for v2
bowheart Jul 24, 2025
09f5137
docs: document `useAtomSelector` hook for v2
bowheart Jul 25, 2025
f4797fc
docs: document `SelectorTemplate` type for v2
bowheart Jul 25, 2025
6a96082
docs: document `useAtomState` hook for v2
bowheart Jul 25, 2025
12d4322
docs: document `useAtomValue` hook for v2
bowheart Jul 25, 2025
f4b616f
docs: document `injectAtomInstance` for v2
bowheart Jul 25, 2025
df53ac7
docs: document `injectAtomState` injector for v2
bowheart Jul 25, 2025
95aaf09
docs: document `injectAtomValue` injector for v2
bowheart Jul 25, 2025
97b1ea9
docs: document `injectCallback` injector for v2
bowheart Jul 25, 2025
94d8f41
docs: document `injectEffect` injector for v2
bowheart Jul 31, 2025
e9e1309
docs: document `injectHydration` injector for v2
bowheart Aug 1, 2025
9b64c91
docs: document `injectMappedSignal` for v2
bowheart Aug 4, 2025
e261b14
docs: document `injectMemo` for v2
bowheart Aug 5, 2025
71368db
docs: document `injectPromise` for v2
bowheart Nov 7, 2025
6dbc2af
update /not-done injectPromise links
bowheart Nov 7, 2025
5b5d326
fix pnpm-lock zedux v1 peer deps
bowheart Nov 15, 2025
4ad17cc
docs: document `injectRef` for v2
bowheart Nov 15, 2025
0dd2766
update /not-done paths for `injectRef`
bowheart Nov 15, 2025
9bdb2c3
docs: document `injectSelf` for v2
bowheart Nov 15, 2025
efbe060
some tweaks
bowheart Nov 15, 2025
ec91a3a
docs: document `injectWhy` for v2
bowheart Nov 15, 2025
767885f
docs: document the `AtomConfig` type for v2
bowheart Nov 15, 2025
6f7583b
docs: document the new `inject` util
bowheart Nov 15, 2025
eca6cc0
docs: document the new `As` util
bowheart Nov 15, 2025
50a0921
docs: document the new `getDefaultEcosystem` util for v2
bowheart Nov 15, 2025
89cd2b2
docs: document `getInternals` for v2
bowheart Nov 15, 2025
3413f6a
docs: document `is` util for v2
bowheart Nov 16, 2025
2f720fc
docs: document `setInternals` for v2
bowheart Nov 16, 2025
6d61266
docs: document new `untrack` util
bowheart Nov 16, 2025
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
Prev Previous commit
Next Next commit
docs: document atom factory for v2
  • Loading branch information
bowheart committed Jul 24, 2025
commit 08ab6e6f5ef2a477119499232507112ce05bd9f0
18 changes: 9 additions & 9 deletions docs/docs/v2/api/classes/AtomTemplate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export const AtomKeyDesc = () => (
</>
)

The object returned from [the `atom()` factory](/not-done?path=../factories/atom). Instances of this class are passed to most of Zedux's hooks and injectors.
The object returned from [the `atom()` factory](../factories/atom). Instances of this class are passed to most of Zedux's hooks and injectors.

An atom template defines a skeleton that Zedux will use to create [atom instances](./AtomInstance) on demand.

## Creation

Use [the `atom()` factory](/not-done?path=../factories/atom) to create atom templates:
Use [the `atom()` factory](../factories/atom) to create atom templates:

```ts
import { AtomTemplate, atom } from '@zedux/react'
Expand Down Expand Up @@ -60,35 +60,35 @@ When creating your own, custom atom types, you'll usually want to extend this cl
<Item name="dehydrate">
A function. Can be undefined.

A reference to the [`dehydrate` atom config option](/not-done?path=../types/AtomConfig#dehydrate) passed to [the `atom()` factory](/not-done?path=../factories/atom), if any.
A reference to the [`dehydrate` atom config option](/not-done?path=../types/AtomConfig#dehydrate) passed to [the `atom()` factory](../factories/atom), if any.

</Item>
<Item name="tags">
An array of strings. Can be undefined.

A reference to the [`tags` atom config option](/not-done?path=../types/AtomConfig#tags) passed to [the `atom()` factory](/not-done?path=../factories/atom), if any.
A reference to the [`tags` atom config option](/not-done?path=../types/AtomConfig#tags) passed to [the `atom()` factory](../factories/atom), if any.

If the ecosystem has [tags configured](./Ecosystem#tags), these tags will be checked against the ecosystem's to warn about unsafe atom templates being used in certain environments.

</Item>
<Item name="hydrate">
A function. Can be undefined.

A reference to the [`hydrate` atom config option](/not-done?path=../types/AtomConfig#hydrate) passed to [`atom()`](/not-done?path=../factories/atom), if any.
A reference to the [`hydrate` atom config option](/not-done?path=../types/AtomConfig#hydrate) passed to [`atom()`](../factories/atom), if any.

</Item>
<Item name="key">
A string.

This is the key string passed as the first argument to [the `atom()` factory](/not-done?path=../factories/atom).
This is the key string passed as the first argument to [the `atom()` factory](../factories/atom).

<AtomKeyDesc />

</Item>
<Item name="ttl">
A number. Can be undefined.

This is the [`ttl` atom config option](/not-done?path=../types/AtomConfig#ttl) passed to [the `atom()` factory](/not-done?path=../factories/atom), if any.
This is the [`ttl` atom config option](/not-done?path=../types/AtomConfig#ttl) passed to [the `atom()` factory](../factories/atom), if any.

If not set, instances of this atom will live forever unless configured with [`.setTtl()`](/not-done?path=./AtomApi#setttl) on an AtomApi returned by the state factory.

Expand Down Expand Up @@ -140,7 +140,7 @@ When creating your own, custom atom types, you'll usually want to extend this cl
override = (newValue) => newAtom
```

Accepts any of the same [value types](/not-done?path=../factories/atom#value) that [the `atom()` factory](/not-done?path=../factories/atom) accepts. The state, promise, and exports type of the new value should match the corresponding types in the overridden atom. TypeScript will enforce this.
Accepts any of the same [value types](../factories/atom#value) that [the `atom()` factory](../factories/atom) accepts. The state, promise, and exports type of the new value should match the corresponding types in the overridden atom. TypeScript will enforce this.

Returns the new atom template. See the [overrides walkthrough](../../../walkthrough/overrides) for more details.

Expand All @@ -149,5 +149,5 @@ When creating your own, custom atom types, you'll usually want to extend this cl

## See Also

- [`atom()`](/not-done?path=../factories/atom)
- [`atom()`](../factories/atom)
- [The Configuring Atoms walkthrough](../../../walkthrough/configuring-atoms)
2 changes: 1 addition & 1 deletion docs/docs/v2/api/classes/Signal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Legend, Item, Link, Tabs, Ts, tab1, tab2 } from '@site/src/all'

The object returned by [`injectSignal`](../injectors/injectSignal). Signals are reactive state containers. Each signal holds a value and provides methods for accessing and updating that value. In Zedux, the term "signal" refers to an instance of this class.

Atoms themselves are signals. That simply means the [AtomInstance](./AtomInstance) class extend this class. An atom can also contain many inner signals. When a signal is returned from an [atom state factory](/not-done?path=../factories/atom#valueorfactory), the atom becomes a thin wrapper around the returned signal.
Atoms themselves are signals. That simply means the [AtomInstance](./AtomInstance) class extend this class. An atom can also contain many inner signals. When a signal is returned from an [atom state factory](../factories/atom#valueorfactory), the atom becomes a thin wrapper around the returned signal.

[Mapped signals](/not-done?path=./MappedSignal) are also signals themselves.

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/v2/api/classes/ZeduxNode.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Every node has the following **readonly** properties:
<Item name="id">
A string. The unique id of this node. Zedux always tries to make this somewhat human-readable for easier debugging.

For nodes that take params, the id is the combination of the node template's key and a deterministic stringification of the params. A node template's "key" can the string passed to the [`atom()`](/not-done?path=../factories/atom) factory for atoms or the selector function's name for selectors See [`ecosystem.hash()`](./Ecosystem#hash) or [`node.params`](#params) for more details on how params are stringified.
For nodes that take params, the id is the combination of the node template's key and a deterministic stringification of the params. A node template's "key" can the string passed to the [`atom()`](../factories/atom) factory for atoms or the selector function's name for selectors See [`ecosystem.hash()`](./Ecosystem#hash) or [`node.params`](#params) for more details on how params are stringified.

```ts
ecosystem.getNode(atom('a', null)).id // 'a'
Expand Down
235 changes: 235 additions & 0 deletions docs/docs/v2/api/factories/atom.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
---
id: atom
title: atom
---

import { Legend, Item, Link, Tabs, Ts, tab1, tab2 } from '@site/src/all'
import { AtomKeyDesc } from '../classes/AtomTemplate.mdx'

```ts
import { atom } from '@zedux/react'
```

Where it all starts. `atom()` is a factory for creating atom templates. Zedux creates atoms from these templates as you use them in various hooks and injectors.

An atom template is actually an instance of [the AtomTemplate class](../classes/AtomTemplate).

## Example

```tsx live ecosystemId=atom/example resultVar=App version=2
const simpleAtom = atom('simple', 'Hello, world!')

const complexAtom = atom(
'complex',
() => {
const signal = injectSignal({ date: new Date() })

injectEffect(() => {
const intervalId = setInterval(
() => signal.set({ date: new Date() }),
1000
)

return () => clearInterval(intervalId)
}, [])

return signal
},
{
flags: ['side-effect'],
}
)

function App() {
const simple = useAtomValue(simpleAtom)
const { date } = useAtomValue(complexAtom)

return (
<>
<div>simple state: {simple}</div>
<div>complex state: {date.toLocaleTimeString()}</div>
</>
)
}
```

## Signature

<Tabs>
{tab1(`atom = (key, valueOrFactory, config?) => newAtom`)}
{tab2(`declare const atom: {
// Query Atoms
<
State = any,
Params extends any[] = [],
Exports extends Record<string, any> = None
>(
key: string,
value: (...params: Params) => AtomApi<{
Exports: Exports
Promise: any
Signal: undefined
State: Promise<State>
}>,
config?: AtomConfig<State>
): AtomTemplateRecursive<{
State: PromiseState<State>
Params: Params
Events: None
Exports: Exports
Promise: Promise<State>
}>\n
// Signals
<
StateType,
EventsType extends Record<string, any> = None,
Params extends any[] = [],
Exports extends Record<string, any> = None,
PromiseType extends AtomApiPromise = undefined,
ResolvedState = StateType
>(
key: string,
value: (...params: Params) =>
| Signal<{
Events: EventsType
Params: any
State: StateType
Template: any
}>
| AtomApi<{
Exports: Exports
Promise: PromiseType
Signal: Signal<{
Events: EventsType
Params: any
State: StateType
Template: any
}>
State: StateType
}>
| Signal<{
Events: EventsType
Params: any
ResolvedState: ResolvedState
State: StateType
Template: any
}>
| AtomApi<{
Exports: Exports
Promise: PromiseType
Signal: Signal<{
Events: EventsType
Params: any
ResolvedState: ResolvedState
State: StateType
Template: any
}>
State: StateType
}>,
config?: AtomConfig<StateType>
): AtomTemplateRecursive<{
State: StateType
Params: Params
Events: EventsType
Exports: Exports
Promise: PromiseType
ResolvedState: ResolvedState
}>\n
// Catch-all
<
State = any,
Params extends any[] = [],
Exports extends Record<string, any> = None,
Events extends Record<string, any> = None,
SignalType extends
| Signal<{
Events: Events
Params: any
State: State
Template: any
}>
| undefined = undefined,
PromiseType extends AtomApiPromise = undefined
>(
key: string,
value: AtomValueOrFactory<{
Exports: Exports
Params: Params
Promise: PromiseType
Signal: SignalType
State: State
}>,
config?: AtomConfig<State>
): AtomTemplateRecursive<{
Events: Events
Exports: Exports
Params: Params
Promise: PromiseType
State: State
}>
}
`)}

</Tabs>

<Legend>
<Item name="key">
Required. A string.

This key must be unique **except** when creating [atom overrides](../../../walkthrough/overrides).

<AtomKeyDesc />

:::tip
Currently, Zedux leaves it up to you to ensure keys are unique across your codebase. As such, it's recommended to use a namespace-based naming convention - e.g. based on your project's file structure - to minimize the chance of conflicts.

```ts
const accountDetailsAtom = atom('dashboard/account/details', ...)
const registrationFormAtom = atom('signup/registrationForm', ...)
```
:::

</Item>
<Item name="valueOrFactory">
Required. Can be any of the following:

- A raw value. Can be anything except a function. When the atom is instantiated, this value as-is will be its initial state.

- A state factory function that returns a raw value. That raw value can be anything (including a function). The returned value will be the atom instance's initial state.

- A state factory function that returns a [signal](../classes/Signal). When the atom is instantiated, the new atom instance will become a thin wrapper around the returned signal, forwarding events and state changes between it and the atom's own observers.

- A state factory function that returns an [AtomApi](/not-done?path=../classes/AtomApi) instance.

The Atom API's value can be any of the following:

- A raw value. Can be anything. This value will be the atom instance's initial state.

- A signal. The atom instance will become a thin wrapper around the returned signal, forwarding events and state changes between it and the atom's own observers.

- A promise. This will turn the atom into a [query atom](../../../walkthrough/query-atoms).

The Atom API's exports will be set as the atom instance's `.exports`.

The Atom API's promise will be set as the atom instance's `.promise`.

Any [`ttl`](/not-done?path=../classes/AtomApi#ttl) configured in the returned Atom API will control the atom instance's destruction timing.

</Item>
<Item name="config">
Optional. An [AtomConfig](/not-done?path=../types/AtomConfig) object.
</Item>
<Item name="Returns">
An [atom template](../classes/AtomTemplate).

Zedux will manage creating and maintaining instances of the atom template as you use it in various hooks, injectors, and ecosystem methods.

</Item>
</Legend>

## See Also

- [The `AtomTemplate` class](../classes/AtomTemplate)
- [The `AtomApi` class](/not-done?path=../classes/AtomApi)
- [The Quick Start](../../../walkthrough/quick-start)
- [The Configuring Atoms walkthrough](../../../walkthrough/configuring-atoms)
2 changes: 1 addition & 1 deletion docs/docs/v2/api/glossary.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ An atom that contains one or more [`inject`](/not-done?path=./injectors/inject)

### State Factory

A function passed to [`atom()`](/not-done?path=./factories/atom) (or other atom factory functions like [`ion()`](/not-done?path=./factories/ion)). This function is called to produce the initial value of the atom instance. It also runs every time an atom instance reevaluates.
A function passed to [`atom()`](./factories/atom) (or other atom factory functions like [`ion()`](/not-done?path=./factories/ion)). This function is called to produce the initial value of the atom instance. It also runs every time an atom instance reevaluates.

These are similar to render functions in React. Except of course they return state instead of UI.

Expand Down
5 changes: 5 additions & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ module.exports = {
label: 'Injectors',
items: ['v2/api/injectors/injectSignal'],
},
{
type: 'category',
label: 'Factories',
items: ['v2/api/factories/atom'],
},
'v2/api/glossary',
],
},
Expand Down