Feature unstable
Features are components a parent refers to by service instead of by value, so the implementation can be swapped with a Layer.
A feature service holds the contract: the Props and Events Schemas. Model
and Message belong to the implementation. The app provides an implementation
with Todos.toLayer(TodoList), and a test or storybook provides a fake with
Todos.toLayer(FakeTodos). A parent renders it with Todos.render(h, ...) and
requires Todos only: the implementation's requirements are closed where
the feature's Layer is built.
Example Declaring a feature and providing an implementation
import { Component, Feature, UiSchema } from "effect/ui"import { Schema } from "effect"
const TodosProps = Schema.Struct({ filter: Schema.Literals(["all", "open"]) })const TodosEvents = UiSchema.TaggedEnum({ Counted: { open: Schema.Number } })
class Todos extends Feature.Service<Todos>()("Todos", { props: TodosProps, events: TodosEvents }) {}
const TodoList = Component.define({ name: "TodoList", props: TodosProps, model: Schema.Struct({}), message: Schema.Never, events: TodosEvents}).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.el("ul")})
// The app picks the implementation. A test provides a fake the same way.const TodosLive = Todos.toLayer(TodoList)Constructors
Declares a feature service from its Props and Events Schemas.
When to use
Use when a piece of UI should be swappable: the app provides the live
implementation and a test provides a fake. To load a component from a
chunk of its own, use Component.lazy.
Details
The class is a Context.Service with props, events, render and
toLayer. name is also the path segment of every instance of the
feature, so paths don't change when the implementation does. toLayer(C)
doesn't compile if C's props or events don't fit the contract.
Signature
declare const Service: <Self>() => <Name extends string, Props extends Codec<any, any, never, never>, Events extends Codec<any, any, never, never> = Never>(name: Name, schemas: { readonly events?: Events; readonly props: Props;}) => ServiceClass<Self, `feature/${Name}`, Implementation<Props["Type"], Events["Type"]>> & Service<Self, Props["Type"], Events["Type"]> & { readonly events: Events; readonly props: Props;}Example A feature with a fake for tests
import { Component, Feature } from "effect/ui"import { Schema } from "effect"
class Todos extends Feature.Service<Todos>()("Todos", { props: Schema.Struct({ filter: Schema.Literals(["all", "open"]) })}) {}
const FakeTodos = Component.define({ name: "FakeTodos", props: Todos.props, model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.el("p", "no todos")})
const TodosFake = Todos.toLayer(FakeTodos)Guards
Models
Any feature service.
Signature
type Any = Service<any, any, any>The value behind a feature service: a closed component.
Details
context is the Context the feature's Layer was built with, minus Scope,
which closes the implementation's requirements. It isn't an instance, so
Layer memoisation is harmless and every child still gets its own instance,
Model and Scope.
Signature
interface Implementation<in out Props, out Events> { readonly component: Mountable<Props, Events>; readonly context: Context<never>;}A feature service class, as returned by Service.
Signature
interface Service<in out Self, in out Props, in out Events> extends Key<Self, Implementation<Props, Events>> { readonly "~effect/ui/Feature": { readonly name: string; }; readonly events: Codec<Events, any>; readonly props: Codec<Props, any>; render<Msg, RF = never>(h: HelperBase<Msg, unknown>, options: { readonly key?: string; readonly onEmit?: (events: Events) => NoInfer<Msg> | undefined | EmitHandlers<Events, NoInfer<Msg>>; readonly onFailure?: (failure: Failure<never, unknown>) => NoInfer<Msg> | undefined; } & {} extends Props ? { readonly props?: Props; } : { readonly props: Props; } & { readonly fallback: (result: Failure<never, unknown> | Initial<never, unknown>) => Content<NoInfer<Msg>, RF>; readonly slots?: undefined; }): Child<Msg, Self | NoInfer<RF>>; toLayer<R>(component: Mountable<Props, Events, R>): Layer<Self, never, R>;}