From 478164d5243a320219672ff4c54f5aefb633f901 Mon Sep 17 00:00:00 2001 From: Claudia Pellegrino Date: Sun, 5 Feb 2023 20:48:17 +0100 Subject: [PATCH] Make `EventStream` implement `Event` --- extension/src/events.ts | 12 ++++-------- extension/src/events/stream.ts | 24 +++++++++--------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/extension/src/events.ts b/extension/src/events.ts index aa68a3c..fd0c7dc 100644 --- a/extension/src/events.ts +++ b/extension/src/events.ts @@ -30,8 +30,7 @@ const onDidInitiallyFindTextDocument: Event = ( export const onDidInitiallyFindRelevantTextDocument: Event = streamEvents(onDidInitiallyFindTextDocument) .through(excludeIrrelevantTextDocumentsByScheme) - .through(excludeIrrelevantTextDocumentsByLanguage) - .commit(); + .through(excludeIrrelevantTextDocumentsByLanguage); export const onDidChangeRelevantTextDocument: Event = streamEvents(workspace.onDidChangeTextDocument) @@ -42,17 +41,14 @@ export const onDidChangeRelevantTextDocument: Event = CHANGE_EVENT_THROTTLE_MILLIS, (e: TextDocumentChangeEvent) => e.document, ) - .through(ignoreIfAlreadyClosed) - .commit(); + .through(ignoreIfAlreadyClosed); export const onDidOpenRelevantTextDocument: Event = streamEvents(workspace.onDidOpenTextDocument) .through(excludeIrrelevantTextDocumentsByScheme) - .through(excludeIrrelevantTextDocumentsByLanguage) - .commit(); + .through(excludeIrrelevantTextDocumentsByLanguage); export const onDidCloseRelevantTextDocument: Event = streamEvents(workspace.onDidCloseTextDocument) .through(excludeIrrelevantTextDocumentsByScheme) - .through(excludeIrrelevantTextDocumentsByLanguage) - .commit(); + .through(excludeIrrelevantTextDocumentsByLanguage); diff --git a/extension/src/events/stream.ts b/extension/src/events/stream.ts index 8ee383c..ea587ef 100644 --- a/extension/src/events/stream.ts +++ b/extension/src/events/stream.ts @@ -6,25 +6,19 @@ interface EventStreamFunction { (...args: [...A, Event]): Event; } -class EventStream { - #upstreamEvent: Event; - - constructor(event: Event) { - this.#upstreamEvent = event; - } - +interface EventStream extends Event { through( fn: EventStreamFunction, ...args: A - ): EventStream { - return streamEvents(fn(...args, this.#upstreamEvent)); - } - - commit(): Event { - return this.#upstreamEvent; - } + ): EventStream; } export function streamEvents(event: Event): EventStream { - return new EventStream(event); + const stream: EventStream = function (...args) { + return event(...args); + }; + stream.through = (fn, ...args) => { + return streamEvents(fn(...args, event)); + }; + return stream; }