Skip to content

Commit

Permalink
style(thread): improve delegation typing
Browse files Browse the repository at this point in the history
  • Loading branch information
vladjerca committed Aug 15, 2023
1 parent e95e650 commit bab593a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions source/platform/browser/worker/imgstry.thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ export class ImgstryThread implements IImgstryThread, IDisposable {
this._logger = new Logger(!!_options.isDebugEnabled);
this._worker = new ImgstryWorker();

const delegate = (event: string, delegate: Function) => {
fromEvent<MessageEvent | ErrorEvent>(this._worker, event)
type EventMap = {
message: MessageEvent
error: ErrorEvent
};

const delegate = <
TEvent extends keyof EventMap,
TMessage = EventMap[TEvent]
>(event: TEvent, cb: (message: TMessage) => void) => {
fromEvent<TMessage>(this._worker, event)
.pipe(
takeUntil(this._disposed$),
)
.subscribe(delegate);
.subscribe(cb);
};

delegate('message', this._handleMessage);
Expand Down

0 comments on commit bab593a

Please sign in to comment.