Skip to content

Runtime without upfront layer #405

@lucas-barake

Description

@lucas-barake

I'd like to define atoms with service dependencies in a shared package and provide different layer implementations per platform (e.g. web vs native).

Currently Atom.runtime(layer) requires the layer at definition time, which couples the atom definitions to a specific implementation.

Would something like this make sense?

// core/runtime.ts - create runtime without layer
export const runtime = Atom.lazyRuntime<Api | Storage>()

// core/atoms.ts - real atoms with real dependencies, defined once
export const users = runtime.atom(Effect.flatMap(Api, a => a.getUsers()))
export const posts = runtime.atom(Effect.flatMap(Api, a => a.getPosts()))
export const userCount = Atom.make((get) => Result.map(get(users), u => u.length))

// web/main.tsx - provide layer
<RegistryProvider initialValues={[Atom.initialValue(runtime.layer, WebApiLayer)]}>

// native/main.tsx - different layer, same atoms
<RegistryProvider initialValues={[Atom.initialValue(runtime.layer, NativeApiLayer)]}>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions