Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created November 15, 2022 21:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-smart/083a8212d42743ed73f890a0c50d6a1a to your computer and use it in GitHub Desktop.
Save tim-smart/083a8212d42743ed73f890a0c50d6a1a to your computer and use it in GitHub Desktop.
export const make = Effect.struct({
ref: SubscriptionRef.make(0),
});
export interface Counter extends Effect.Success<typeof make> {}
export const Counter = Tag<Counter>();
export const CounterLive = Layer.fromEffect(Counter)(make);
export const increment = Effect.serviceWithEffect(Counter, ({ ref }) =>
ref.update((count) => count + 1)
);
export const decrement = Effect.serviceWithEffect(Counter, ({ ref }) =>
ref.update((count) => count - 1)
);
export const subscribe = pipe(
Effect.serviceWith(Counter, ({ ref }) => ref.changes),
Stream.unwrap
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment