Skip to content

Commit

Permalink
Make EventStream implement Event
Browse files Browse the repository at this point in the history
  • Loading branch information
claui committed Mar 2, 2023
1 parent 6fbf768 commit 478164d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
12 changes: 4 additions & 8 deletions extension/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const onDidInitiallyFindTextDocument: Event<TextDocument> = (
export const onDidInitiallyFindRelevantTextDocument: Event<TextDocument> =
streamEvents(onDidInitiallyFindTextDocument)
.through(excludeIrrelevantTextDocumentsByScheme)
.through(excludeIrrelevantTextDocumentsByLanguage)
.commit();
.through(excludeIrrelevantTextDocumentsByLanguage);

export const onDidChangeRelevantTextDocument: Event<TextDocument> =
streamEvents(workspace.onDidChangeTextDocument)
Expand All @@ -42,17 +41,14 @@ export const onDidChangeRelevantTextDocument: Event<TextDocument> =
CHANGE_EVENT_THROTTLE_MILLIS,
(e: TextDocumentChangeEvent) => e.document,
)
.through(ignoreIfAlreadyClosed)
.commit();
.through(ignoreIfAlreadyClosed);

export const onDidOpenRelevantTextDocument: Event<TextDocument> =
streamEvents(workspace.onDidOpenTextDocument)
.through(excludeIrrelevantTextDocumentsByScheme)
.through(excludeIrrelevantTextDocumentsByLanguage)
.commit();
.through(excludeIrrelevantTextDocumentsByLanguage);

export const onDidCloseRelevantTextDocument: Event<TextDocument> =
streamEvents(workspace.onDidCloseTextDocument)
.through(excludeIrrelevantTextDocumentsByScheme)
.through(excludeIrrelevantTextDocumentsByLanguage)
.commit();
.through(excludeIrrelevantTextDocumentsByLanguage);
24 changes: 9 additions & 15 deletions extension/src/events/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ interface EventStreamFunction<T, U, A extends Arr> {
(...args: [...A, Event<T>]): Event<U>;
}

class EventStream<T> {
#upstreamEvent: Event<T>;

constructor(event: Event<T>) {
this.#upstreamEvent = event;
}

interface EventStream<T> extends Event<T> {
through<U, A extends Arr>(
fn: EventStreamFunction<T, U, A>,
...args: A
): EventStream<U> {
return streamEvents(fn(...args, this.#upstreamEvent));
}

commit(): Event<T> {
return this.#upstreamEvent;
}
): EventStream<U>;
}

export function streamEvents<T>(event: Event<T>): EventStream<T> {
return new EventStream(event);
const stream: EventStream<T> = function (...args) {
return event(...args);
};
stream.through = (fn, ...args) => {
return streamEvents(fn(...args, event));
};
return stream;
}

0 comments on commit 478164d

Please sign in to comment.