SharedSource
Shared sources, for the Streams an Environment hands out: each hot and counted, so however many
subscribe, one underlying source runs, started by the first subscriber and stopped when the last one
leaves. byTarget keeps one per target, one observer on each Subject, and make one for the whole
Environment. An Environment builds its sources with these, as @wardic/dom does, and counted shares
anything else it opens per target.
import * as SharedSource from "@wardic/core/SharedSource"
const resize = yield* SharedSource.byTarget((target) => Effect.succeed(resizesOf(target)))
// Both share one observer on `card`:
Stream.runForEach(resize(card), onLayout)
Stream.runForEach(resize(card), onScroll)Variables
byTarget
const byTarget: <A>(lookup) => Effect.Effect<(target) => Stream.Stream<A>, never, Scope.Scope>;A shared source per target: a function from a target to a Stream that's hot and counted for that
target. The first subscriber for a target starts lookup(target)'s source, later ones share it, and it
stops when the last one for that target leaves.
Type Parameters
A
A
Parameters
lookup
(target) => Effect.Effect<Stream.Stream<A>, never, Scope.Scope>
Returns
Effect.Effect<(target) => Stream.Stream<A>, never, Scope.Scope>
Example
// `resizesOf(target)`: an Environment's own Stream of a target's sizes
const resize = yield* SharedSource.byTarget((target) => Effect.succeed(resizesOf(target)))make
const make: <A>(acquire) => Effect.Effect<Stream.Stream<A>, never, Scope.Scope>;One shared source for the whole Environment: a Stream that's hot and counted. The first subscriber
starts acquire's source, later ones share it, and it stops when the last one leaves. byTarget's
form without a target.
Type Parameters
A
A
Parameters
acquire
Effect.Effect<Stream.Stream<A>, never, Scope.Scope>
Returns
Effect.Effect<Stream.Stream<A>, never, Scope.Scope>
Example
// `viewportChanges`: an Environment's own Stream of its viewport's size
const viewport = yield* SharedSource.make(Effect.succeed(viewportChanges))
Stream.runForEach(viewport, onResize) // one listener, however many subscribeFunctions
counted()
function counted<K, V>(open, close): (key) => Effect<V, never, Scope>;Values shared by key and counted: the first hold of a key opens its value with open, later holds
share it, and the last one's release closes it with close. A key is held by identity and never
hashed, so a Subject is its own key. open runs inline, so it mustn't wait.
Type Parameters
K
K
V
V
Parameters
open
(key) => V
close
(value) => Effect<void>
Returns
(key) => Effect<V, never, Scope>
Example
// One listener set per container, however many readers hold it
const containers = SharedSource.counted(
(container: object) => ({ remove: listenTo(container) }),
(listening) => Effect.sync(listening.remove),
)
const listening = yield* containers(list) // held until the current Scope closes