@cosystem/solid
v0.2.0
Published
Solid adapter for CoSystem.
Downloads
25
Readme
@cosystem/solid
Solid bindings for CoSystem: a context provider and signals for consuming a CoSystem app (or a worker-hosted app).
Installation
pnpm add @cosystem/solid @cosystem/corePeer dependency: solid-js >=1.9 <2.
Quick start
import { CoSystemProvider, useComputed, useModule } from "@cosystem/solid";
function CounterView() {
const counter = useModule(Counter);
const count = useComputed(Counter, (module) => module.count);
return <button onClick={() => counter.increase()}>{count()}</button>;
}
<CoSystemProvider app={app}>
<CounterView />
</CoSystemProvider>;useComputed returns a Solid Accessor<T> — call it (count()) to read inside
JSX or an effect.
API
| Function | Returns | Description |
| ------------------------------- | ------------------ | -------------------------------- |
| useApp() | App | The app from CoSystemProvider. |
| useModule(token) | T | The bound module facade. |
| useComputed(fn, opts?) | Accessor<T> | Signal for fn(app). |
| useComputed(token, fn, opts?) | Accessor<TValue> | Signal for fn(module, app). |
Both useComputed overloads accept { equals } (defaults to Object.is) and
clean up with onCleanup.
const count = useComputed(Counter, (m) => m.count);
const version = useComputed((app) => app.state.version);Worker-hosted state
import { WorkerClientProvider, useWorkerModule, useWorkerSelector } from "@cosystem/solid";
type CounterState = { readonly counter: { readonly count: number } };
function WorkerCounterView() {
const counter = useWorkerModule<Counter>("counter");
const count = useWorkerSelector((state) => (state as CounterState).counter.count);
return <button onClick={() => counter.increase()}>{count()}</button>;
}
<WorkerClientProvider client={client}>
<WorkerCounterView />
</WorkerClientProvider>;useWorkerClient()→ theWorkerClientfrom context.useWorkerModule<T>(name)→ anAsyncMethodProxy<T>.useWorkerSelector(fn, opts?)/useWorkerComputed(fn, opts?)→Accessor<T>.
Exports
CoSystemProvider, WorkerClientProvider, the CoSystemContext /
WorkerClientContext contexts, useApp, useModule, useComputed,
useWorkerClient, useWorkerModule, useWorkerComputed, useWorkerSelector,
and the CoSystemProviderProps, WorkerClientProviderProps,
UseComputedOptions, AppSelector, ModuleSelector types. Hooks throw a
CosystemError when the matching provider is missing.
License
MIT © Coaction
