-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Stream.fromEventListener (#2513)
Co-authored-by: Tim <hello@timsmart.co>
- Loading branch information
1 parent
2b30749
commit 66575e3
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"effect": minor | ||
"@effect/platform-browser": patch | ||
--- | ||
|
||
added Stream.fromEventListener, and BrowserStream.{fromEventListenerWindow, fromEventListenerDocument} for constructing a stream from addEventListener |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Effect, Stream } from "effect" | ||
import * as it from "effect-test/utils/extend" | ||
import { describe } from "vitest" | ||
|
||
class TestTarget extends EventTarget { | ||
emit() { | ||
this.dispatchEvent(new Event("test-event")) | ||
} | ||
} | ||
|
||
describe("Stream.fromEventListener", () => { | ||
it.effect("emitted count", (ctx) => | ||
Effect.gen(function*(_) { | ||
const target = new TestTarget() | ||
|
||
const count = yield* _( | ||
Stream.fromEventListener(target, "test-event"), | ||
Stream.interruptWhen(Effect.sync(() => target.emit()).pipe(Effect.repeatN(2))), | ||
Stream.runCount | ||
) | ||
ctx.expect(count).toEqual(3) | ||
})) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
|
||
import type * as Stream from "effect/Stream" | ||
import * as internal from "./internal/stream.js" | ||
|
||
/** | ||
* Creates a `Stream` from window.addEventListener. | ||
* @since 1.0.0 | ||
*/ | ||
export const fromEventListenerWindow: <K extends keyof WindowEventMap>( | ||
type: K, | ||
options?: boolean | Omit<AddEventListenerOptions, "signal"> | ||
) => Stream.Stream<WindowEventMap[K]> = internal.fromEventListenerWindow | ||
|
||
/** | ||
* Creates a `Stream` from document.addEventListener. | ||
* @since 1.0.0 | ||
*/ | ||
export const fromEventListenerDocument: <K extends keyof DocumentEventMap>( | ||
type: K, | ||
options?: boolean | Omit<AddEventListenerOptions, "signal"> | ||
) => Stream.Stream<DocumentEventMap[K]> = internal.fromEventListenerDocument |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
|
||
import * as Stream from "effect/Stream" | ||
|
||
/** @internal */ | ||
export const fromEventListenerWindow = <K extends keyof WindowEventMap>( | ||
type: K, | ||
options?: boolean | Omit<AddEventListenerOptions, "signal"> | ||
) => Stream.fromEventListener<WindowEventMap[K]>(window, type, options) | ||
|
||
/** @internal */ | ||
export const fromEventListenerDocument = <K extends keyof DocumentEventMap>( | ||
type: K, | ||
options?: boolean | Omit<AddEventListenerOptions, "signal"> | ||
) => Stream.fromEventListener<DocumentEventMap[K]>(document, type, options) |