Created
January 4, 2018 07:13
-
-
Save dherges/a69c2ac69dd133357a2eeba7ac9180d6 to your computer and use it in GitHub Desktop.
Event Bus on RxJS
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 type MyStuff = string; | |
export interface Event<T> { | |
type: 'Starts' | 'Finishes', | |
payload: T | |
} | |
/** @experimental */ | |
export class Bus<P> extends Subject<Event<P>> { | |
private handlers: HandlerFn<P>[] = []; | |
public run(): void { | |
this.subscribe( | |
(next) => { debugger; }, | |
(err) => { debugger; }, | |
() => { debugger; } | |
) | |
for (let handler of this.handlers) { | |
this.pipe(handler.attachTo).subscribe(this); | |
} | |
this.next({ type: '/init/', payload: undefined }); | |
} | |
} | |
export interface HandlerFn<T> { | |
(source$: EventBus<T>): Observable<Event<T>> | |
} | |
import { pipe } from 'rxjs/util/pipe'; | |
export class HandlerBuildery<P, A, R> { | |
constructor( | |
private id: string | |
) {} | |
public build(): HandlerFn<any> { | |
return pipe( | |
/* ... */ | |
); | |
} | |
/* | |
return { | |
id: this.identifier, | |
attachTo | |
}; | |
} | |
*/ | |
} | |
export function handler<T>(id: string) { | |
return new HandlerBuilder<T, {}, {}>(id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment