solid2-effector
v0.0.5
Published
Async effect, memo, and experimental writable signal primitives for Solid 2
Readme
solid2-effector
Async-aware effects, memos, and writable signals for Solid 2. Each asynchronous source tracks its reactive dependencies independently. Once all current sources have resolved, a synchronous derivation runs; unrelated derivation dependencies can update without refetching.
createAsyncSignal is experimental.
Installation
npm install [email protected]Requires solid-js 2 (^2.0.0-rc.17). Create these primitives inside a Solid reactive owner (for example, a component or createRoot).
Usage
import { createSignal } from "solid-js";
import { createAsyncEffect, createAsyncMemo, createAsyncSignal } from "solid2-effector";
const [userId, setUserId] = createSignal(1);
const [teamId, setTeamId] = createSignal(7);
const [uppercase, setUppercase] = createSignal(false);
// These functions are application-provided; each returns a Promise.
const sources = [
() => fetchUser(userId()),
() => fetchTeam(teamId())
] as const;
const derive = ([user, team]: [User, Team]) => {
const name = `${user.name} — ${team.name}`;
return uppercase() ? name.toUpperCase() : name;
};
// Effect: apply a result, optionally returning a cleanup function.
createAsyncEffect(sources, derive, value => {
console.log(value);
});
// Memo: read the derived result reactively.
const label = createAsyncMemo(sources, derive);
// Signal: read and manually update the derived result.
const [draft, setDraft] = createAsyncSignal(sources, derive);
setDraft("Custom label");
setDraft(previous => (previous ?? "") + "!");These are three independent instances: they do not share requests. Within each instance, changing userId() reruns only its user source; changing uppercase() reruns only the synchronous derivation.
Initial loading and errors
createAsyncMemo and createAsyncSignal throw Solid 2's NotReadyError when read before their first value is ready. In a Solid component, <Loading> can show a fallback until the initial dependencies settle:
import { Loading, Errored } from "solid-js";
<Errored fallback={error => <p>{String(error())}</p>}>
<Loading fallback={<p>Loading…</p>}>
{label()}
</Loading>
</Errored>After the first successful result, an async memo retains its last value during revalidation. The async signal also supports direct writes while pending. Initial failures can be handled with <Errored>; use onError for diagnostic reporting.
Writable signal precedence
By default, createAsyncSignal uses native-style setter precedence: a pending derivation may overwrite a manual write when it resolves. Choose preserve to prevent an already in-flight request from replacing the manual value:
const [value, setValue] = createAsyncSignal(sources, derive, {
writePolicy: "preserve"
});A later dependency change may still derive a new value. Native-style precedence is not identical to Solid's entire scheduling behavior: a manual write during initial loading may reveal the value inside <Loading> earlier than a native writable derived signal.
API and TypeScript
Each source is () => T | PromiseLike<T>. Sources form a tuple, so TypeScript infers the corresponding resolved values in derive.
type AsyncSource<T> = () => T | PromiseLike<T>;
type Resolved<S extends readonly AsyncSource<any>[]> = {
-readonly [K in keyof S]: Awaited<ReturnType<S[K]>>;
};
type Equality<T> = false | ((previous: T, next: T) => boolean);
createAsyncEffect(sources, derive, apply, options?): void;
createAsyncMemo(sources, derive, options?): () => Result;
createAsyncSignal(sources, derive, options?): [get, set];derive(values) must be synchronous. apply(result) may return a cleanup function; other return values are ignored. The async signal setter accepts either a direct result or an updater (previous: Result | undefined) => Result and returns the assigned value.
| Option | Supported by | Purpose |
| --- | --- | --- |
| sourceEquals | All three | false (default), a comparator, or a per-source comparator tuple. Skip recomputation if newly resolved inputs are equivalent. |
| resultEquals | All three | false (default) or comparator. Suppress equivalent derived results. |
| onError(error, info) | All three | Report source, derivation, or application errors. info.stage and optional info.sourceIndex identify the failure. |
| equals | Signal | Equality of stored signal values, including manual writes. Default is strict equality (===); false disables suppression. |
| writePolicy | Signal | "native" (default) or "preserve". Controls manual writes versus in-flight derivations. |
For precise generic types, import AsyncEffectOptions, AsyncMemoOptions, AsyncSignalOptions, AsyncSignalSetter, AsyncSource, Resolved, and Equality from solid2-effector. Subpath imports (solid2-effector/effect, /memo, /signal) are also available.
Behavior and limitations
- Each async source is tracked separately. The derivation waits until all current sources are resolved; stale Promise completions are ignored.
- Equality compares resolved values, not requests. New source dependencies can still trigger network calls even when the result compares equal.
- Source and result equality do not affect unrelated synchronous dependencies read by
derive. - The primitives do not provide built-in request caching, cancellation, deduplication, or retries.
createAsyncSignalis experimental and aims for native-style setter precedence, not complete equivalence to Solid 2's scheduling and loading semantics.
