import * as R from 'ramda'; // import { Observable } from 'rxjs'; // import { BehaviorSubject } from 'rxjs/subject/BehaviorSubject'; import { Observable, BehaviorSubject, Subscription } from 'rxjs'; import 'rxjs/add/operator/combineLatest'; import 'rxjs/add/operator/startWith'; // import { EventEmitter } from '@angular/core'; // // append to array: export function elemToArr(arr: Array, x: T): Array { // concatenates arrays rather than pushing them as a single item return arr.concat(x); // // overwrites the old value // arr.push(x); // return arr; // // slower, but no overriding and arrays become one item // let c = R.cloneDeep(arr); // c.push(x); // return c; } export function arrToArr(a: Array, b: Array): Array { return a.concat(b); } // // append to Set: export function elemToSet(set: Set, x: T): Set { return set.add(x); } export function arrToSet(set: Set, arr: Array): Set { return arr.reduce((set, x) => set.add(x), set); } export function setToSet(a: Set, b: Set): Set { // return new Set(function*() { yield* a; yield* b; }()); return new Set([...Array.from(a), ...Array.from(b)]); } // set of logging functions to use in Observable.subscribe (see `notify`) export function loggers(kw: string = 'obs'): [(v: any) => void, (e: string) => void, () => void] { return [ v => console.log(kw + " next", v), e => console.error(kw + " error", e), () => console.log(kw + " done"), ]; } // log an Observable's values export function notify(kw: string, obs: Observable): Subscription { let [next, complete, error] = loggers(kw); return obs.subscribe(next, complete, error); } // generalizes combineLatest from 2 Observables to an array of n: combLastObs([a$, b$]).map([a, b] => ...) export function combLastObs(arr: Observable[]): Observable { return arr.reduce((obj_obs, o, idx) => { let combiner = (obj: Object, val: any) => Object.assign(obj, {[idx]: val}); return o.subscribe ? obj_obs.combineLatest(o, combiner) : obj_obs.map(obs => combiner(obs, o)); }, new BehaviorSubject({}) ).map(r => R.values(r)) } // maps the latest values of a set of Observables to a lambda export function mapComb(arr: Observable[], fn: Function): Observable { return combLastObs(arr).map(r => fn(...r)); } // use the latest values of a set of Observables to subscribe to a lambda export function subComb(arr: Observable[], fn: Function): Subscription { return combLastObs(arr).subscribe(r => fn(...r)); }