1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.createViewGetter = exports.ContextView = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const events_1 = require("events");
|
11 | const util_1 = require("util");
|
12 | const resolution_session_1 = require("./resolution-session");
|
13 | const value_promise_1 = require("./value-promise");
|
14 | const debug = (0, debug_1.default)('loopback:context:view');
|
15 | const nextTick = (0, util_1.promisify)(process.nextTick);
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class ContextView extends events_1.EventEmitter {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | constructor(context, filter, comparator, resolutionOptions) {
|
40 | super();
|
41 | this.context = context;
|
42 | this.filter = filter;
|
43 | this.comparator = comparator;
|
44 | this.resolutionOptions = resolutionOptions;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 |
|
50 | updateCachedValues(values) {
|
51 | var _a;
|
52 | if (this._cachedBindings == null)
|
53 | return undefined;
|
54 | this._cachedValues = new Map();
|
55 | for (let i = 0; i < ((_a = this._cachedBindings) === null || _a === void 0 ? void 0 : _a.length); i++) {
|
56 | this._cachedValues.set(this._cachedBindings[i], values[i]);
|
57 | }
|
58 | return this._cachedValues;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | getCachedValues() {
|
64 | var _a, _b;
|
65 | return Array.from((_b = (_a = this._cachedValues) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : []);
|
66 | }
|
67 | |
68 |
|
69 |
|
70 | open() {
|
71 | debug('Start listening on changes of context %s', this.context.name);
|
72 | if (this.context.isSubscribed(this)) {
|
73 | return this._subscription;
|
74 | }
|
75 | this._subscription = this.context.subscribe(this);
|
76 | return this._subscription;
|
77 | }
|
78 | |
79 |
|
80 |
|
81 | close() {
|
82 | debug('Stop listening on changes of context %s', this.context.name);
|
83 | if (!this._subscription || this._subscription.closed)
|
84 | return;
|
85 | this._subscription.unsubscribe();
|
86 | this._subscription = undefined;
|
87 | this.emit('close');
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 | get bindings() {
|
94 | debug('Reading bindings');
|
95 | if (this._cachedBindings == null) {
|
96 | this._cachedBindings = this.findBindings();
|
97 | }
|
98 | return this._cachedBindings;
|
99 | }
|
100 | |
101 |
|
102 |
|
103 | findBindings() {
|
104 | debug('Finding matching bindings');
|
105 | const found = this.context.find(this.filter);
|
106 | if (typeof this.comparator === 'function') {
|
107 | found.sort(this.comparator);
|
108 | }
|
109 |
|
110 | if (debug.enabled) {
|
111 | debug('Bindings found', found.map(b => b.key));
|
112 | }
|
113 | return found;
|
114 | }
|
115 | |
116 |
|
117 |
|
118 | observe(event, binding, context) {
|
119 | var _a;
|
120 | const ctxEvent = {
|
121 | context,
|
122 | binding,
|
123 | type: event,
|
124 | };
|
125 | debug('Observed event %s %s %s', event, binding.key, context.name);
|
126 | if (event === 'unbind') {
|
127 | const cachedValue = (_a = this._cachedValues) === null || _a === void 0 ? void 0 : _a.get(binding);
|
128 | this.emit(event, { ...ctxEvent, cachedValue });
|
129 | }
|
130 | else {
|
131 | this.emit(event, ctxEvent);
|
132 | }
|
133 | this.refresh();
|
134 | }
|
135 | |
136 |
|
137 |
|
138 | refresh() {
|
139 | debug('Refreshing the view by invalidating cache');
|
140 | this._cachedBindings = undefined;
|
141 | this._cachedValues = undefined;
|
142 | this.emit('refresh');
|
143 | }
|
144 | |
145 |
|
146 |
|
147 |
|
148 | resolve(session) {
|
149 | debug('Resolving values');
|
150 | if (this._cachedValues != null) {
|
151 | return this.getCachedValues();
|
152 | }
|
153 | const bindings = this.bindings;
|
154 | let result = (0, value_promise_1.resolveList)(bindings, b => {
|
155 | const options = {
|
156 | ...this.resolutionOptions,
|
157 | ...(0, resolution_session_1.asResolutionOptions)(session),
|
158 | };
|
159 | options.session = resolution_session_1.ResolutionSession.fork(options.session);
|
160 | return b.getValue(this.context, options);
|
161 | });
|
162 | if ((0, value_promise_1.isPromiseLike)(result)) {
|
163 | result = result.then(values => {
|
164 | const list = values.filter(v => v != null);
|
165 | this.updateCachedValues(list);
|
166 | this.emit('resolve', list);
|
167 | return list;
|
168 | });
|
169 | }
|
170 | else {
|
171 |
|
172 | const list = (result = result.filter(v => v != null));
|
173 | this.updateCachedValues(list);
|
174 | this.emit('resolve', list);
|
175 | }
|
176 | return result;
|
177 | }
|
178 | |
179 |
|
180 |
|
181 |
|
182 | async values(session) {
|
183 | debug('Reading values');
|
184 |
|
185 | await nextTick();
|
186 | if (this._cachedValues == null) {
|
187 | return this.resolve(session);
|
188 | }
|
189 | return this.getCachedValues();
|
190 | }
|
191 | |
192 |
|
193 |
|
194 | asGetter(session) {
|
195 | return () => this.values(session);
|
196 | }
|
197 | |
198 |
|
199 |
|
200 | async singleValue(session) {
|
201 | const values = await this.values(session);
|
202 | if (values.length === 0)
|
203 | return undefined;
|
204 | if (values.length === 1)
|
205 | return values[0];
|
206 | throw new Error('The ContextView has more than one value. Use values() to access them.');
|
207 | }
|
208 |
|
209 | on(event, listener) {
|
210 | return super.on(event, listener);
|
211 | }
|
212 |
|
213 | once(event, listener) {
|
214 | return super.once(event, listener);
|
215 | }
|
216 | }
|
217 | exports.ContextView = ContextView;
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | function createViewGetter(ctx, bindingFilter, bindingComparatorOrSession, session) {
|
227 | let bindingComparator = undefined;
|
228 | if (typeof bindingComparatorOrSession === 'function') {
|
229 | bindingComparator = bindingComparatorOrSession;
|
230 | }
|
231 | else if (bindingComparatorOrSession instanceof resolution_session_1.ResolutionSession) {
|
232 | session = bindingComparatorOrSession;
|
233 | }
|
234 | const options = (0, resolution_session_1.asResolutionOptions)(session);
|
235 | const view = new ContextView(ctx, bindingFilter, bindingComparator, options);
|
236 | view.open();
|
237 | return view.asGetter(options);
|
238 | }
|
239 | exports.createViewGetter = createViewGetter;
|
240 |
|
\ | No newline at end of file |