@@ -1,7 +1,11 | |||||
|
1 | import { id as mid} from "module"; | |||
1 | import { Cancellation } from "@implab/core-amd/Cancellation"; |
|
2 | import { Cancellation } from "@implab/core-amd/Cancellation"; | |
2 | import { ICancellation } from "@implab/core-amd/interfaces"; |
|
3 | import { ICancellation } from "@implab/core-amd/interfaces"; | |
|
4 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |||
3 | import { isPromise } from "@implab/core-amd/safe"; |
|
5 | import { isPromise } from "@implab/core-amd/safe"; | |
4 |
|
6 | |||
|
7 | const trace = TraceSource.get(mid); | |||
|
8 | ||||
5 | /** |
|
9 | /** | |
6 | * The interface for the consumer of an observable sequence |
|
10 | * The interface for the consumer of an observable sequence | |
7 | */ |
|
11 | */ | |
@@ -148,11 +152,13 export interface Observable<T> extends S | |||||
148 |
|
152 | |||
149 | const noop = () => { }; |
|
153 | const noop = () => { }; | |
150 |
|
154 | |||
|
155 | const errorFallback = (e: unknown) => trace.error("Unhandled observable error: {0}", e); | |||
|
156 | ||||
151 | const sink = <T>(consumer: Observer<T>) => { |
|
157 | const sink = <T>(consumer: Observer<T>) => { | |
152 | const { next, error, complete } = consumer; |
|
158 | const { next, error, complete } = consumer; | |
153 | return { |
|
159 | return { | |
154 | next: next ? next.bind(consumer) : noop, |
|
160 | next: next ? next.bind(consumer) : noop, | |
155 |
error: error ? error.bind(consumer) : |
|
161 | error: error ? error.bind(consumer) : errorFallback, | |
156 | complete: complete ? complete.bind(consumer) : noop, |
|
162 | complete: complete ? complete.bind(consumer) : noop, | |
157 | isClosed: () => false |
|
163 | isClosed: () => false | |
158 | }; |
|
164 | }; |
@@ -2,7 +2,6 import { PromiseOrValue } from "@implab/ | |||||
2 | import { isCancellable, isPromise } from "@implab/core-amd/safe"; |
|
2 | import { isCancellable, isPromise } from "@implab/core-amd/safe"; | |
3 | import { observe, Observable, empty } from "./observable"; |
|
3 | import { observe, Observable, empty } from "./observable"; | |
4 | import { after } from "dojo/aspect"; |
|
4 | import { after } from "dojo/aspect"; | |
5 | import { subject } from "./operators/subject"; |
|
|||
6 |
|
5 | |||
7 | export interface OrderedUpdate<T> { |
|
6 | export interface OrderedUpdate<T> { | |
8 | /** The item is being updated */ |
|
7 | /** The item is being updated */ | |
@@ -98,26 +97,29 export const get = <T>(store: IndexedSto | |||||
98 | observe<Change<T>>(({ next }) => { |
|
97 | observe<Change<T>>(({ next }) => { | |
99 | const handle = after(store, "notify", (...args: Change<T>) => next(args), true); |
|
98 | const handle = after(store, "notify", (...args: Change<T>) => next(args), true); | |
100 | return () => handle.remove(); |
|
99 | return () => handle.remove(); | |
101 |
} |
|
100 | }) : empty; | |
102 |
|
||||
103 |
|
101 | |||
104 | return (id: string | number, opts: GetOpts = {}) => |
|
102 | return (id: string | number, opts: GetOpts = {}) => | |
105 | observe<T>(({ next, complete, error }) => { |
|
103 | observe<T>(({ next, complete, error }) => { | |
106 | const result = store.get(id); |
|
104 | try { | |
|
105 | const result = store.get(id); | |||
107 |
|
106 | |||
108 | const handle = (x: T | null | undefined) => { |
|
107 | const handle = (x: T | null | undefined) => { | |
109 | if (x !== null && x !== undefined) |
|
108 | if (x !== null && x !== undefined) | |
110 | next(x); |
|
109 | next(x); | |
111 | complete(); |
|
110 | complete(); | |
112 | }; |
|
111 | }; | |
113 |
|
112 | |||
114 | if (isPromise(result)) { |
|
113 | if (isPromise(result)) { | |
115 | result.then(handle, error); |
|
114 | result.then(handle).then(undefined, error); | |
116 |
|
115 | |||
117 | if (isCancellable(result)) |
|
116 | if (isCancellable(result)) | |
118 | return () => result.cancel(); |
|
117 | return () => result.cancel(); | |
119 | } else { |
|
118 | } else { | |
120 | handle(result); |
|
119 | handle(result); | |
|
120 | } | |||
|
121 | } catch (e) { | |||
|
122 | error(e); | |||
121 | } |
|
123 | } | |
122 | }).cat(opts.observe !== false ? changes.pipe(filterItem(id)) : empty); |
|
124 | }).cat(opts.observe !== false ? changes.pipe(filterItem(id)) : empty); | |
123 | }; No newline at end of file |
|
125 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now