diff --git a/djx/src/main/ts/observable.ts b/djx/src/main/ts/observable.ts --- a/djx/src/main/ts/observable.ts +++ b/djx/src/main/ts/observable.ts @@ -1,7 +1,11 @@ +import { id as mid} from "module"; import { Cancellation } from "@implab/core-amd/Cancellation"; import { ICancellation } from "@implab/core-amd/interfaces"; +import { TraceSource } from "@implab/core-amd/log/TraceSource"; import { isPromise } from "@implab/core-amd/safe"; +const trace = TraceSource.get(mid); + /** * The interface for the consumer of an observable sequence */ @@ -148,11 +152,13 @@ export interface Observable extends S const noop = () => { }; +const errorFallback = (e: unknown) => trace.error("Unhandled observable error: {0}", e); + const sink = (consumer: Observer) => { const { next, error, complete } = consumer; return { next: next ? next.bind(consumer) : noop, - error: error ? error.bind(consumer) : noop, + error: error ? error.bind(consumer) : errorFallback, complete: complete ? complete.bind(consumer) : noop, isClosed: () => false }; diff --git a/djx/src/main/ts/store.ts b/djx/src/main/ts/store.ts --- a/djx/src/main/ts/store.ts +++ b/djx/src/main/ts/store.ts @@ -2,7 +2,6 @@ import { PromiseOrValue } from "@implab/ import { isCancellable, isPromise } from "@implab/core-amd/safe"; import { observe, Observable, empty } from "./observable"; import { after } from "dojo/aspect"; -import { subject } from "./operators/subject"; export interface OrderedUpdate { /** The item is being updated */ @@ -98,26 +97,29 @@ export const get = (store: IndexedSto observe>(({ next }) => { const handle = after(store, "notify", (...args: Change) => next(args), true); return () => handle.remove(); - }).pipe(subject) : empty; - + }) : empty; return (id: string | number, opts: GetOpts = {}) => observe(({ next, complete, error }) => { - const result = store.get(id); + try { + const result = store.get(id); - const handle = (x: T | null | undefined) => { - if (x !== null && x !== undefined) - next(x); - complete(); - }; + const handle = (x: T | null | undefined) => { + if (x !== null && x !== undefined) + next(x); + complete(); + }; - if (isPromise(result)) { - result.then(handle, error); + if (isPromise(result)) { + result.then(handle).then(undefined, error); - if (isCancellable(result)) - return () => result.cancel(); - } else { - handle(result); + if (isCancellable(result)) + return () => result.cancel(); + } else { + handle(result); + } + } catch (e) { + error(e); } }).cat(opts.observe !== false ? changes.pipe(filterItem(id)) : empty); }; \ No newline at end of file