Last active
July 13, 2023 11:34
-
-
Save cowboyd/1ed648240a44a5bbfc4ce60c590b098f to your computer and use it in GitHub Desktop.
Create a context-free function that dispatches values to a subscriber context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createChannel, run, type Stream } from "./mod.ts"; | |
export function createSignal<T>(): [(event: T) => void, Stream<T, never>] { | |
let { input, output } = createChannel<T, never>(); | |
let pulse = (event: T) => run(() => input.send(event)); | |
return [pulse, output]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function onclick(element: HTMLElement): Stream<MouseEvent, never> { | |
let [onClick, receiver] = createSignal<MouseEvent>(); | |
return resource(function* (provide) { | |
try { | |
element.addEventlistener("click", onClick); | |
let subscription = yield* receiver; | |
yield* provide(subscription); | |
} finally { | |
element.removeEventListener("click", onClick); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment