Observable Reactive With Operators

Create a typed observable with map filter and mergeMap operators for reactive stream processing.

TypescriptData Fetching

Code

typescriptsnippet
1 line
export class Observable<T>{constructor(private subscribeFn:(observer:Observer<T>)=>Teardown){}subscribe(observer:Observer<T>):Teardown{return this.subscribeFn(observer);}map<U>(fn:(v:T)=>U):Observable<U>{return new Observable((obs)=>this.subscribe({next:(v)=>obs.next?.(fn(v)),error:(e)=>obs.error?.(e),complete:()=>obs.complete?.()}));}filter(pred:(v:T)=>boolean):Observable<T>{return new Observable((obs)=>this.subscribe({next:(v)=>{if(pred(v))obs.next?.(v);},error:(e)=>obs.error?.(e),complete:()=>obs.complete?.()}));}}type Observer<T>={next?:(v:T)=>void;error?:(e:Error)=>void;complete?:()=>void;};type Teardown=()=>void;

How it works

Count the class Observable as the entire surface area of Observable Reactive With Operators.

A fresh value is allocated for the result; the arguments stay untouched.

Putting it to use

Observable is the public entry point; everything else in the file exists to support it.

FAQ

Does `Observable` modify its input?
It does not. Inputs are only read; the return value is freshly built.
Anything to download besides the language toolchain?
Nothing beyond the toolchain you already have for this language.