ObservableImpl.ts
263 lines
| 8.8 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r153 | import { id as mid } from "module"; | |
cin
|
r157 | import { AccumulatorFn, Observable, Observer, OperatorFn, Producer, Sink, Subscribable, Unsubscribable } from "./interfaces"; | |
cin
|
r153 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |
cin
|
r155 | import { Predicate } from "@implab/core-amd/interfaces"; | |
import { Cancellation } from "@implab/core-amd/Cancellation"; | |||
cin
|
r153 | ||
const trace = TraceSource.get(mid); | |||
const noop = () => { }; | |||
const errorFallback = (e: unknown) => trace.error("Unhandled observable error: {0}", e); | |||
cin
|
r157 | const subscription = (reject: (err: unknown) => void, ct = Cancellation.none) => { | |
cin
|
r155 | let done = false; | |
let cleanup = noop; | |||
const finalize = () => done ? false : (cleanup(), done = true); | |||
const isClosed = () => done; | |||
const teardown = (cb: void | (() => void)) => { | |||
if (cb) { | |||
if (done) { | |||
cb(); | |||
} else { | |||
const _prev = cleanup; | |||
cin
|
r157 | cleanup = _prev === noop ? cb : () => (_prev(), cb()); | |
cin
|
r155 | } | |
} | |||
cin
|
r153 | }; | |
cin
|
r155 | ||
cin
|
r157 | const error = (err: unknown) => finalize() && reject(err); | |
if (ct.isSupported()) { | |||
const h = ct.register(error); | |||
teardown(() => h.destroy()); | |||
} | |||
return { finalize, isClosed, teardown, error }; | |||
cin
|
r153 | }; | |
cin
|
r157 | type FusedSink<T> = Omit<Sink<T>, "isClosed">; | |
export const fuse = <T>(producer: Producer<T>) => (consumer: FusedSink<T>) => { | |||
const reject = (err: unknown) => consumer.error ? consumer.error(err) : errorFallback(err); | |||
const { teardown, isClosed, finalize, error } = subscription(reject); | |||
const next = consumer.next ? consumer.next.bind(consumer) : noop; | |||
teardown(producer({ | |||
next: next !== noop ? (v => isClosed() || next(v)) : noop, | |||
error, | |||
complete: () => finalize() && consumer.complete && consumer.complete(), | |||
isClosed | |||
})); | |||
return { unsubscribe: finalize }; | |||
}; | |||
type FusedProducer<T> = (consumer: FusedSink<T>) => void | (() => void); | |||
cin
|
r153 | export class ObservableImpl<T> implements Observable<T> { | |
cin
|
r157 | private readonly _producer: FusedProducer<T>; | |
cin
|
r153 | ||
cin
|
r157 | constructor(producer: FusedProducer<T>) { | |
cin
|
r153 | this._producer = producer; | |
} | |||
subscribe(consumer: Observer<T> = {}) { | |||
cin
|
r157 | const reject = (err: unknown) => consumer.error ? consumer.error(err) : errorFallback(err); | |
const next = consumer.next ? consumer.next.bind(consumer) : noop; | |||
const { teardown, isClosed, finalize, error } = subscription(reject); | |||
cin
|
r155 | ||
cin
|
r157 | teardown((0, this._producer)({ | |
next: next !== noop ? (v => isClosed() || next(v)) : noop, | |||
error, | |||
complete: () => finalize() && consumer.complete && consumer.complete() | |||
cin
|
r155 | })); | |
return { unsubscribe: finalize }; | |||
cin
|
r153 | } | |
map<T2>(mapper: (value: T) => T2) { | |||
return new ObservableImpl<T2>(({ next, ...rest }) => | |||
this._producer({ | |||
next: next !== noop ? (v: T) => next(mapper(v)) : noop, | |||
...rest | |||
})); | |||
} | |||
tap({ next: tapNext, complete: tapComplete, error: tapError }: Observer<T>) { | |||
cin
|
r157 | return new ObservableImpl<T>(({ next, complete, error }) => | |
cin
|
r153 | this._producer({ | |
next: tapNext ? (v => (tapNext(v), next(v))) : next, | |||
complete: tapComplete ? (() => (tapComplete(), complete())) : complete, | |||
cin
|
r157 | error: tapError ? (e => (tapError(e), error(e))) : error | |
cin
|
r153 | })); | |
} | |||
cin
|
r155 | filter(predicate: Predicate<T>) { | |
return new ObservableImpl<T>(({ next, ...rest }) => | |||
this._producer({ | |||
next: next !== noop ? (v: T) => predicate(v) ? next(v) : void (0) : noop, | |||
...rest | |||
}) | |||
); | |||
} | |||
cin
|
r153 | ||
cin
|
r155 | until(predicate: Predicate<T>) { | |
return new ObservableImpl<T>(({ next, complete, ...rest }) => | |||
this._producer({ | |||
next: v => predicate(v) ? complete() : next(v), | |||
complete, | |||
...rest | |||
}) | |||
); | |||
} | |||
cin
|
r153 | ||
cin
|
r155 | while(predicate: Predicate<T>) { | |
return new ObservableImpl<T>(({ next, complete, ...rest }) => | |||
this._producer({ | |||
next: v => predicate(v) ? next(v) : complete(), | |||
complete, | |||
...rest | |||
}) | |||
); | |||
} | |||
cin
|
r157 | scan<A>(accumulator: AccumulatorFn<T, A>, initial: A): Observable<A>; | |
scan(accumulator: AccumulatorFn<T, T>): Observable<T>; | |||
cin
|
r155 | scan<A>(...args: [AccumulatorFn<T, A>, A] | [AccumulatorFn<T, T>]) { | |
cin
|
r153 | if (args.length === 1) { | |
cin
|
r155 | return new ObservableImpl<T | A>(({ next, ...rest }) => { | |
cin
|
r153 | const [accumulator] = args; | |
let _acc: T; | |||
let index = 0; | |||
cin
|
r155 | return this._producer({ | |
next: next !== noop ? (v: T) => next(index++ === 0 ? _acc = v : _acc = accumulator(_acc, v)) : noop, | |||
...rest | |||
}); | |||
}); | |||
} else { | |||
return new ObservableImpl<T | A>(({ next, ...rest }) => { | |||
const [accumulator, initial] = args; | |||
let _acc = initial; | |||
return this._producer({ | |||
next: next !== noop ? (v: T) => next(_acc = accumulator(_acc, v)) : noop, | |||
...rest | |||
}); | |||
}); | |||
} | |||
} | |||
cin
|
r157 | reduce<A>(accumulator: AccumulatorFn<T, A>, initial: A): Observable<A>; | |
reduce(accumulator: AccumulatorFn<T, T>): Observable<T>; | |||
cin
|
r155 | reduce<A>(...args: [AccumulatorFn<T, A>, A] | [AccumulatorFn<T, T>]) { | |
if (args.length === 1) { | |||
cin
|
r157 | return new ObservableImpl<T | A>(({ next, complete, error }) => { | |
cin
|
r155 | const [accumulator] = args; | |
let _acc: T; | |||
let index = 0; | |||
return this._producer({ | |||
cin
|
r153 | next: next !== noop ? (v: T) => { | |
_acc = index++ === 0 ? v : accumulator(_acc, v); | |||
} : noop, | |||
complete: () => { | |||
if (index === 0) { | |||
error(new Error("The sequence can't be empty")); | |||
} else { | |||
next(_acc); | |||
complete(); | |||
} | |||
}, | |||
cin
|
r157 | error | |
cin
|
r153 | }); | |
cin
|
r155 | }); | |
} else { | |||
cin
|
r157 | return new ObservableImpl<T | A>(({ next, complete, error }) => { | |
cin
|
r153 | const [accumulator, initial] = args; | |
let _acc = initial; | |||
cin
|
r155 | return this._producer({ | |
cin
|
r153 | next: next !== noop ? (v: T) => { | |
_acc = accumulator(_acc, v); | |||
} : noop, | |||
complete: () => { | |||
next(_acc); | |||
complete(); | |||
}, | |||
cin
|
r157 | error | |
cin
|
r153 | }); | |
cin
|
r155 | }); | |
} | |||
} | |||
cin
|
r153 | ||
cin
|
r155 | cat(...seq: Subscribable<T>[]) { | |
return new ObservableImpl<T>(({ next, complete: final, ...rest }) => { | |||
let cleanup: () => void; | |||
const len = seq.length; | |||
const complete = (i: number) => i < len ? | |||
() => { | |||
const subscription = seq[i].subscribe({ next, complete: complete(i + 1), ...rest }); | |||
cleanup = subscription.unsubscribe.bind(subscription); | |||
} : final; | |||
cleanup = this._producer({ next, complete: complete(0), ...rest }) ?? noop; | |||
cin
|
r153 | ||
cin
|
r155 | return () => cleanup(); | |
}); | |||
} | |||
cin
|
r153 | ||
cin
|
r155 | pipe<U>(op: OperatorFn<T, U>): Observable<U> { | |
return op(this); | |||
} | |||
cin
|
r153 | ||
cin
|
r155 | next(ct = Cancellation.none) { | |
return new Promise<T>((resolve, reject) => { | |||
ct.throwIfRequested(); | |||
cin
|
r157 | const { teardown, finalize, isClosed, error } = subscription(reject, ct); | |
cin
|
r155 | teardown((0, this._producer)({ | |
next: (item: T) => finalize() && resolve(item), | |||
complete: () => finalize() && reject(new Error("The sequence is empty")), | |||
error, | |||
isClosed | |||
})); | |||
}); | |||
} | |||
collect(ct = Cancellation.none) { | |||
return new Promise<T[]>((resolve, reject) => { | |||
ct.throwIfRequested(); | |||
cin
|
r153 | ||
cin
|
r155 | const acc: T[] = []; | |
cin
|
r157 | const { teardown, finalize, isClosed, error } = subscription(reject, ct); | |
cin
|
r155 | teardown((0, this._producer)({ | |
next: (item: T) => isClosed() ? void (0) : acc.push(item), | |||
complete: () => finalize() && resolve(acc), | |||
error, | |||
isClosed | |||
})); | |||
}); | |||
} | |||
cin
|
r157 | forEach(next: (item: T) => void, ct = Cancellation.none): Promise<void> { | |
return new Promise<void>((resolve, reject) => { | |||
ct.throwIfRequested(); | |||
const { teardown, finalize, isClosed, error } = subscription(reject, ct); | |||
teardown((0, this._producer)({ | |||
next: item => !isClosed() && next(item), | |||
complete: () => finalize() && resolve(), | |||
error, | |||
isClosed | |||
})); | |||
}); | |||
} | |||
cin
|
r153 | } |