@angular/core 16.0.0-next.7 → 16.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm2022/rxjs-interop/src/index.mjs +3 -3
- package/esm2022/rxjs-interop/src/to_observable.mjs +39 -0
- package/esm2022/rxjs-interop/src/to_signal.mjs +52 -0
- package/esm2022/src/application_ref.mjs +3 -3
- package/esm2022/src/application_tokens.mjs +11 -1
- package/esm2022/src/core_private_export.mjs +2 -1
- package/esm2022/src/errors.mjs +1 -1
- package/esm2022/src/hydration/annotate.mjs +42 -33
- package/esm2022/src/hydration/api.mjs +47 -19
- package/esm2022/src/hydration/error_handling.mjs +1 -17
- package/esm2022/src/initial_render_pending_tasks.mjs +10 -10
- package/esm2022/src/render3/component_ref.mjs +3 -3
- package/esm2022/src/render3/di.mjs +3 -3
- package/esm2022/src/render3/hooks.mjs +3 -3
- package/esm2022/src/render3/instructions/i18n.mjs +14 -2
- package/esm2022/src/render3/instructions/mark_view_dirty.mjs +1 -1
- package/esm2022/src/render3/instructions/shared.mjs +14 -14
- package/esm2022/src/render3/interfaces/type_checks.mjs +1 -1
- package/esm2022/src/render3/interfaces/view.mjs +1 -1
- package/esm2022/src/render3/node_manipulation.mjs +9 -9
- package/esm2022/src/render3/reactivity/effect.mjs +1 -1
- package/esm2022/src/render3/util/discovery_utils.mjs +1 -1
- package/esm2022/src/render3/util/view_traversal_utils.mjs +1 -1
- package/esm2022/src/render3/util/view_utils.mjs +2 -2
- package/esm2022/src/render3/view_ref.mjs +4 -4
- package/esm2022/src/signals/src/watch.mjs +6 -2
- package/esm2022/src/version.mjs +1 -1
- package/esm2022/testing/src/logger.mjs +3 -3
- package/esm2022/testing/src/ng_zone_mock.mjs +3 -3
- package/fesm2022/core.mjs +156 -113
- package/fesm2022/core.mjs.map +1 -1
- package/fesm2022/rxjs-interop.mjs +704 -56
- package/fesm2022/rxjs-interop.mjs.map +1 -1
- package/fesm2022/testing.mjs +67 -57
- package/fesm2022/testing.mjs.map +1 -1
- package/index.d.ts +32 -15
- package/package.json +2 -2
- package/rxjs-interop/index.d.ts +53 -46
- package/schematics/migrations/guard-and-resolve-interfaces/bundle.js +13 -13
- package/schematics/migrations/remove-module-id/bundle.js +14 -14
- package/schematics/ng-generate/standalone-migration/bundle.js +2938 -1229
- package/schematics/ng-generate/standalone-migration/bundle.js.map +4 -4
- package/testing/index.d.ts +1 -1
- package/esm2022/rxjs-interop/src/from_observable.mjs +0 -46
- package/esm2022/rxjs-interop/src/from_signal.mjs +0 -36
|
@@ -1,49 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Angular v16.0.0-
|
|
2
|
+
* @license Angular v16.0.0-rc.0
|
|
3
3
|
* (c) 2010-2022 Google LLC. https://angular.io/
|
|
4
4
|
* License: MIT
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { assertInInjectionContext,
|
|
7
|
+
import { assertInInjectionContext, inject, DestroyRef, Injector, effect, signal as signal$1, computed as computed$1 } from '@angular/core';
|
|
8
8
|
import { Observable } from 'rxjs';
|
|
9
9
|
import { takeUntil } from 'rxjs/operators';
|
|
10
|
+
import { RuntimeError } from '@angular/core/src/errors';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Operator which completes the Observable when the calling context (component, directive, service,
|
|
14
|
+
* etc) is destroyed.
|
|
15
|
+
*
|
|
16
|
+
* @param destroyRef optionally, the `DestroyRef` representing the current context. This can be
|
|
17
|
+
* passed explicitly to use `takeUntilDestroyed` outside of an injection context. Otherwise, the
|
|
18
|
+
* current `DestroyRef` is injected.
|
|
19
|
+
*
|
|
20
|
+
* @developerPreview
|
|
21
|
+
*/
|
|
22
|
+
function takeUntilDestroyed(destroyRef) {
|
|
23
|
+
if (!destroyRef) {
|
|
24
|
+
assertInInjectionContext(takeUntilDestroyed);
|
|
25
|
+
destroyRef = inject(DestroyRef);
|
|
24
26
|
}
|
|
25
|
-
const
|
|
26
|
-
next
|
|
27
|
-
error: error => state.set({ kind: 2 /* StateKind.Error */, error }),
|
|
28
|
-
// Completion of the Observable is meaningless to the signal. Signals don't have a concept of
|
|
29
|
-
// "complete".
|
|
30
|
-
});
|
|
31
|
-
// Unsubscribe when the current context is destroyed.
|
|
32
|
-
inject(DestroyRef).onDestroy(sub.unsubscribe.bind(sub));
|
|
33
|
-
// The actual returned signal is a `computed` of the `State` signal, which maps the various states
|
|
34
|
-
// to either values or errors.
|
|
35
|
-
return computed(() => {
|
|
36
|
-
const current = state();
|
|
37
|
-
switch (current.kind) {
|
|
38
|
-
case 1 /* StateKind.Value */:
|
|
39
|
-
return current.value;
|
|
40
|
-
case 2 /* StateKind.Error */:
|
|
41
|
-
throw current.error;
|
|
42
|
-
case 0 /* StateKind.NoValue */:
|
|
43
|
-
// TODO(alxhub): use a RuntimeError when we finalize the error semantics
|
|
44
|
-
throw new Error(`fromObservable() signal read before the Observable emitted`);
|
|
45
|
-
}
|
|
27
|
+
const destroyed$ = new Observable(observer => {
|
|
28
|
+
destroyRef.onDestroy(observer.next.bind(observer));
|
|
46
29
|
});
|
|
30
|
+
return (source) => {
|
|
31
|
+
return source.pipe(takeUntil(destroyed$));
|
|
32
|
+
};
|
|
47
33
|
}
|
|
48
34
|
|
|
49
35
|
/**
|
|
@@ -51,54 +37,716 @@ function fromObservable(source, initialValue) {
|
|
|
51
37
|
*
|
|
52
38
|
* The signal's value will be propagated into the `Observable`'s subscribers using an `effect`.
|
|
53
39
|
*
|
|
54
|
-
* `
|
|
40
|
+
* `toObservable` must be called in an injection context.
|
|
55
41
|
*
|
|
56
42
|
* @developerPreview
|
|
57
43
|
*/
|
|
58
|
-
function
|
|
59
|
-
!options?.injector && assertInInjectionContext(
|
|
44
|
+
function toObservable(source, options) {
|
|
45
|
+
!options?.injector && assertInInjectionContext(toObservable);
|
|
60
46
|
const injector = options?.injector ?? inject(Injector);
|
|
61
47
|
// Creating a new `Observable` allows the creation of the effect to be lazy. This allows for all
|
|
62
48
|
// references to `source` to be dropped if the `Observable` is fully unsubscribed and thrown away.
|
|
63
49
|
return new Observable(observer => {
|
|
64
50
|
const watcher = effect(() => {
|
|
51
|
+
let value;
|
|
65
52
|
try {
|
|
66
|
-
|
|
53
|
+
value = source();
|
|
67
54
|
}
|
|
68
55
|
catch (err) {
|
|
69
56
|
observer.error(err);
|
|
57
|
+
return;
|
|
70
58
|
}
|
|
71
|
-
|
|
59
|
+
observer.next(value);
|
|
60
|
+
}, { injector, manualCleanup: true, allowSignalWrites: true });
|
|
72
61
|
return () => watcher.destroy();
|
|
73
62
|
});
|
|
74
63
|
}
|
|
75
64
|
|
|
76
65
|
/**
|
|
77
|
-
*
|
|
78
|
-
* etc) is destroyed.
|
|
66
|
+
* Symbol used to tell `Signal`s apart from other functions.
|
|
79
67
|
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
68
|
+
* This can be used to auto-unwrap signals in various cases, or to auto-wrap non-signal values.
|
|
69
|
+
*/
|
|
70
|
+
const SIGNAL = Symbol('SIGNAL');
|
|
71
|
+
/**
|
|
72
|
+
* Checks if the given `value` function is a reactive `Signal`.
|
|
73
|
+
*/
|
|
74
|
+
function isSignal(value) {
|
|
75
|
+
return value[SIGNAL] !== undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Converts `fn` into a marked signal function (where `isSignal(fn)` will be `true`), and
|
|
79
|
+
* potentially add some set of extra properties (passed as an object record `extraApi`).
|
|
80
|
+
*/
|
|
81
|
+
function createSignalFromFunction(node, fn, extraApi = {}) {
|
|
82
|
+
fn[SIGNAL] = node;
|
|
83
|
+
// Copy properties from `extraApi` to `fn` to complete the desired API of the `Signal`.
|
|
84
|
+
return Object.assign(fn, extraApi);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The default equality function used for `signal` and `computed`, which treats objects and arrays
|
|
88
|
+
* as never equal, and all other primitive values using identity semantics.
|
|
89
|
+
*
|
|
90
|
+
* This allows signals to hold non-primitive values (arrays, objects, other collections) and still
|
|
91
|
+
* propagate change notification upon explicit mutation without identity change.
|
|
83
92
|
*
|
|
84
93
|
* @developerPreview
|
|
85
94
|
*/
|
|
86
|
-
function
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
95
|
+
function defaultEquals(a, b) {
|
|
96
|
+
// `Object.is` compares two values using identity semantics which is desired behavior for
|
|
97
|
+
// primitive values. If `Object.is` determines two values to be equal we need to make sure that
|
|
98
|
+
// those don't represent objects (we want to make sure that 2 objects are always considered
|
|
99
|
+
// "unequal"). The null check is needed for the special case of JavaScript reporting null values
|
|
100
|
+
// as objects (`typeof null === 'object'`).
|
|
101
|
+
return (a === null || typeof a !== 'object') && Object.is(a, b);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Always use __globalThis if available, which is the spec-defined global variable across all
|
|
105
|
+
// environments, then fallback to __global first, because in Node tests both __global and
|
|
106
|
+
// __window may be defined and _global should be __global in that case. Note: Typeof/Instanceof
|
|
107
|
+
// checks are considered side-effects in Terser. We explicitly mark this as side-effect free:
|
|
108
|
+
// https://github.com/terser/terser/issues/250.
|
|
109
|
+
const _global = ( /* @__PURE__ */(() => (typeof globalThis !== 'undefined' && globalThis) ||
|
|
110
|
+
(typeof global !== 'undefined' && global) || (typeof window !== 'undefined' && window) ||
|
|
111
|
+
(typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
|
|
112
|
+
self instanceof WorkerGlobalScope && self))());
|
|
113
|
+
|
|
114
|
+
function ngDevModeResetPerfCounters() {
|
|
115
|
+
const locationString = typeof location !== 'undefined' ? location.toString() : '';
|
|
116
|
+
const newCounters = {
|
|
117
|
+
namedConstructors: locationString.indexOf('ngDevMode=namedConstructors') != -1,
|
|
118
|
+
firstCreatePass: 0,
|
|
119
|
+
tNode: 0,
|
|
120
|
+
tView: 0,
|
|
121
|
+
rendererCreateTextNode: 0,
|
|
122
|
+
rendererSetText: 0,
|
|
123
|
+
rendererCreateElement: 0,
|
|
124
|
+
rendererAddEventListener: 0,
|
|
125
|
+
rendererSetAttribute: 0,
|
|
126
|
+
rendererRemoveAttribute: 0,
|
|
127
|
+
rendererSetProperty: 0,
|
|
128
|
+
rendererSetClassName: 0,
|
|
129
|
+
rendererAddClass: 0,
|
|
130
|
+
rendererRemoveClass: 0,
|
|
131
|
+
rendererSetStyle: 0,
|
|
132
|
+
rendererRemoveStyle: 0,
|
|
133
|
+
rendererDestroy: 0,
|
|
134
|
+
rendererDestroyNode: 0,
|
|
135
|
+
rendererMoveNode: 0,
|
|
136
|
+
rendererRemoveNode: 0,
|
|
137
|
+
rendererAppendChild: 0,
|
|
138
|
+
rendererInsertBefore: 0,
|
|
139
|
+
rendererCreateComment: 0,
|
|
140
|
+
hydratedNodes: 0,
|
|
141
|
+
hydratedComponents: 0,
|
|
142
|
+
dehydratedViewsRemoved: 0,
|
|
143
|
+
dehydratedViewsCleanupRuns: 0,
|
|
144
|
+
componentsSkippedHydration: 0,
|
|
145
|
+
};
|
|
146
|
+
// Make sure to refer to ngDevMode as ['ngDevMode'] for closure.
|
|
147
|
+
const allowNgDevModeTrue = locationString.indexOf('ngDevMode=false') === -1;
|
|
148
|
+
_global['ngDevMode'] = allowNgDevModeTrue && newCounters;
|
|
149
|
+
return newCounters;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* This function checks to see if the `ngDevMode` has been set. If yes,
|
|
153
|
+
* then we honor it, otherwise we default to dev mode with additional checks.
|
|
154
|
+
*
|
|
155
|
+
* The idea is that unless we are doing production build where we explicitly
|
|
156
|
+
* set `ngDevMode == false` we should be helping the developer by providing
|
|
157
|
+
* as much early warning and errors as possible.
|
|
158
|
+
*
|
|
159
|
+
* `ɵɵdefineComponent` is guaranteed to have been called before any component template functions
|
|
160
|
+
* (and thus Ivy instructions), so a single initialization there is sufficient to ensure ngDevMode
|
|
161
|
+
* is defined for the entire instruction set.
|
|
162
|
+
*
|
|
163
|
+
* When checking `ngDevMode` on toplevel, always init it before referencing it
|
|
164
|
+
* (e.g. `((typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode())`), otherwise you can
|
|
165
|
+
* get a `ReferenceError` like in https://github.com/angular/angular/issues/31595.
|
|
166
|
+
*
|
|
167
|
+
* Details on possible values for `ngDevMode` can be found on its docstring.
|
|
168
|
+
*
|
|
169
|
+
* NOTE:
|
|
170
|
+
* - changes to the `ngDevMode` name must be synced with `compiler-cli/src/tooling.ts`.
|
|
171
|
+
*/
|
|
172
|
+
function initNgDevMode() {
|
|
173
|
+
// The below checks are to ensure that calling `initNgDevMode` multiple times does not
|
|
174
|
+
// reset the counters.
|
|
175
|
+
// If the `ngDevMode` is not an object, then it means we have not created the perf counters
|
|
176
|
+
// yet.
|
|
177
|
+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
178
|
+
if (typeof ngDevMode !== 'object') {
|
|
179
|
+
ngDevModeResetPerfCounters();
|
|
180
|
+
}
|
|
181
|
+
return typeof ngDevMode !== 'undefined' && !!ngDevMode;
|
|
90
182
|
}
|
|
91
|
-
|
|
92
|
-
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Required as the signals library is in a separate package, so we need to explicitly ensure the
|
|
187
|
+
/**
|
|
188
|
+
* A `WeakRef`-compatible reference that fakes the API with a strong reference
|
|
189
|
+
* internally.
|
|
190
|
+
*/
|
|
191
|
+
class LeakyRef {
|
|
192
|
+
constructor(ref) {
|
|
193
|
+
this.ref = ref;
|
|
194
|
+
}
|
|
195
|
+
deref() {
|
|
196
|
+
return this.ref;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// `WeakRef` is not always defined in every TS environment where Angular is compiled. Instead,
|
|
200
|
+
// read it off of the global context if available.
|
|
201
|
+
// tslint:disable-next-line: no-toplevel-property-access
|
|
202
|
+
let WeakRefImpl = _global['WeakRef'] ?? LeakyRef;
|
|
203
|
+
function newWeakRef(value) {
|
|
204
|
+
if (typeof ngDevMode !== 'undefined' && ngDevMode && WeakRefImpl === undefined) {
|
|
205
|
+
throw new Error(`Angular requires a browser which supports the 'WeakRef' API`);
|
|
206
|
+
}
|
|
207
|
+
return new WeakRefImpl(value);
|
|
208
|
+
}
|
|
209
|
+
function setAlternateWeakRefImpl(impl) {
|
|
210
|
+
// no-op since the alternate impl is included by default by the framework. Remove once internal
|
|
211
|
+
// migration is complete.
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Required as the signals library is in a separate package, so we need to explicitly ensure the
|
|
215
|
+
/**
|
|
216
|
+
* Counter tracking the next `ProducerId` or `ConsumerId`.
|
|
217
|
+
*/
|
|
218
|
+
let _nextReactiveId = 0;
|
|
219
|
+
/**
|
|
220
|
+
* Tracks the currently active reactive consumer (or `null` if there is no active
|
|
221
|
+
* consumer).
|
|
222
|
+
*/
|
|
223
|
+
let activeConsumer = null;
|
|
224
|
+
/**
|
|
225
|
+
* Whether the graph is currently propagating change notifications.
|
|
226
|
+
*/
|
|
227
|
+
let inNotificationPhase = false;
|
|
228
|
+
function setActiveConsumer(consumer) {
|
|
229
|
+
const prev = activeConsumer;
|
|
230
|
+
activeConsumer = consumer;
|
|
231
|
+
return prev;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* A node in the reactive graph.
|
|
235
|
+
*
|
|
236
|
+
* Nodes can be producers of reactive values, consumers of other reactive values, or both.
|
|
237
|
+
*
|
|
238
|
+
* Producers are nodes that produce values, and can be depended upon by consumer nodes.
|
|
239
|
+
*
|
|
240
|
+
* Producers expose a monotonic `valueVersion` counter, and are responsible for incrementing this
|
|
241
|
+
* version when their value semantically changes. Some producers may produce their values lazily and
|
|
242
|
+
* thus at times need to be polled for potential updates to their value (and by extension their
|
|
243
|
+
* `valueVersion`). This is accomplished via the `onProducerUpdateValueVersion` method for
|
|
244
|
+
* implemented by producers, which should perform whatever calculations are necessary to ensure
|
|
245
|
+
* `valueVersion` is up to date.
|
|
246
|
+
*
|
|
247
|
+
* Consumers are nodes that depend on the values of producers and are notified when those values
|
|
248
|
+
* might have changed.
|
|
249
|
+
*
|
|
250
|
+
* Consumers do not wrap the reads they consume themselves, but rather can be set as the active
|
|
251
|
+
* reader via `setActiveConsumer`. Reads of producers that happen while a consumer is active will
|
|
252
|
+
* result in those producers being added as dependencies of that consumer node.
|
|
253
|
+
*
|
|
254
|
+
* The set of dependencies of a consumer is dynamic. Implementers expose a monotonically increasing
|
|
255
|
+
* `trackingVersion` counter, which increments whenever the consumer is about to re-run any reactive
|
|
256
|
+
* reads it needs and establish a new set of dependencies as a result.
|
|
257
|
+
*
|
|
258
|
+
* Producers store the last `trackingVersion` they've seen from `Consumer`s which have read them.
|
|
259
|
+
* This allows a producer to identify whether its record of the dependency is current or stale, by
|
|
260
|
+
* comparing the consumer's `trackingVersion` to the version at which the dependency was
|
|
261
|
+
* last observed.
|
|
262
|
+
*/
|
|
263
|
+
class ReactiveNode {
|
|
264
|
+
constructor() {
|
|
265
|
+
this.id = _nextReactiveId++;
|
|
266
|
+
/**
|
|
267
|
+
* A cached weak reference to this node, which will be used in `ReactiveEdge`s.
|
|
268
|
+
*/
|
|
269
|
+
this.ref = newWeakRef(this);
|
|
270
|
+
/**
|
|
271
|
+
* Edges to producers on which this node depends (in its consumer capacity).
|
|
272
|
+
*/
|
|
273
|
+
this.producers = new Map();
|
|
274
|
+
/**
|
|
275
|
+
* Edges to consumers on which this node depends (in its producer capacity).
|
|
276
|
+
*/
|
|
277
|
+
this.consumers = new Map();
|
|
278
|
+
/**
|
|
279
|
+
* Monotonically increasing counter representing a version of this `Consumer`'s
|
|
280
|
+
* dependencies.
|
|
281
|
+
*/
|
|
282
|
+
this.trackingVersion = 0;
|
|
283
|
+
/**
|
|
284
|
+
* Monotonically increasing counter which increases when the value of this `Producer`
|
|
285
|
+
* semantically changes.
|
|
286
|
+
*/
|
|
287
|
+
this.valueVersion = 0;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Polls dependencies of a consumer to determine if they have actually changed.
|
|
291
|
+
*
|
|
292
|
+
* If this returns `false`, then even though the consumer may have previously been notified of a
|
|
293
|
+
* change, the values of its dependencies have not actually changed and the consumer should not
|
|
294
|
+
* rerun any reactions.
|
|
295
|
+
*/
|
|
296
|
+
consumerPollProducersForChange() {
|
|
297
|
+
for (const [producerId, edge] of this.producers) {
|
|
298
|
+
const producer = edge.producerNode.deref();
|
|
299
|
+
if (producer === undefined || edge.atTrackingVersion !== this.trackingVersion) {
|
|
300
|
+
// This dependency edge is stale, so remove it.
|
|
301
|
+
this.producers.delete(producerId);
|
|
302
|
+
producer?.consumers.delete(this.id);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (producer.producerPollStatus(edge.seenValueVersion)) {
|
|
306
|
+
// One of the dependencies reports a real value change.
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// No dependency reported a real value change, so the `Consumer` has also not been
|
|
311
|
+
// impacted.
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Notify all consumers of this producer that its value may have changed.
|
|
316
|
+
*/
|
|
317
|
+
producerMayHaveChanged() {
|
|
318
|
+
// Prevent signal reads when we're updating the graph
|
|
319
|
+
const prev = inNotificationPhase;
|
|
320
|
+
inNotificationPhase = true;
|
|
321
|
+
try {
|
|
322
|
+
for (const [consumerId, edge] of this.consumers) {
|
|
323
|
+
const consumer = edge.consumerNode.deref();
|
|
324
|
+
if (consumer === undefined || consumer.trackingVersion !== edge.atTrackingVersion) {
|
|
325
|
+
this.consumers.delete(consumerId);
|
|
326
|
+
consumer?.producers.delete(this.id);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
consumer.onConsumerDependencyMayHaveChanged();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
inNotificationPhase = prev;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Mark that this producer node has been accessed in the current reactive context.
|
|
338
|
+
*/
|
|
339
|
+
producerAccessed() {
|
|
340
|
+
if (inNotificationPhase) {
|
|
341
|
+
throw new Error(typeof ngDevMode !== 'undefined' && ngDevMode ?
|
|
342
|
+
`Assertion error: signal read during notification phase` :
|
|
343
|
+
'');
|
|
344
|
+
}
|
|
345
|
+
if (activeConsumer === null) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
// Either create or update the dependency `Edge` in both directions.
|
|
349
|
+
let edge = activeConsumer.producers.get(this.id);
|
|
350
|
+
if (edge === undefined) {
|
|
351
|
+
edge = {
|
|
352
|
+
consumerNode: activeConsumer.ref,
|
|
353
|
+
producerNode: this.ref,
|
|
354
|
+
seenValueVersion: this.valueVersion,
|
|
355
|
+
atTrackingVersion: activeConsumer.trackingVersion,
|
|
356
|
+
};
|
|
357
|
+
activeConsumer.producers.set(this.id, edge);
|
|
358
|
+
this.consumers.set(activeConsumer.id, edge);
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
edge.seenValueVersion = this.valueVersion;
|
|
362
|
+
edge.atTrackingVersion = activeConsumer.trackingVersion;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Whether this consumer currently has any producers registered.
|
|
367
|
+
*/
|
|
368
|
+
get hasProducers() {
|
|
369
|
+
return this.producers.size > 0;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Whether this `ReactiveNode` in its producer capacity is currently allowed to initiate updates,
|
|
373
|
+
* based on the current consumer context.
|
|
374
|
+
*/
|
|
375
|
+
get producerUpdatesAllowed() {
|
|
376
|
+
return activeConsumer?.consumerAllowSignalWrites !== false;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Checks if a `Producer` has a current value which is different than the value
|
|
380
|
+
* last seen at a specific version by a `Consumer` which recorded a dependency on
|
|
381
|
+
* this `Producer`.
|
|
382
|
+
*/
|
|
383
|
+
producerPollStatus(lastSeenValueVersion) {
|
|
384
|
+
// `producer.valueVersion` may be stale, but a mismatch still means that the value
|
|
385
|
+
// last seen by the `Consumer` is also stale.
|
|
386
|
+
if (this.valueVersion !== lastSeenValueVersion) {
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
// Trigger the `Producer` to update its `valueVersion` if necessary.
|
|
390
|
+
this.onProducerUpdateValueVersion();
|
|
391
|
+
// At this point, we can trust `producer.valueVersion`.
|
|
392
|
+
return this.valueVersion !== lastSeenValueVersion;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Create a computed `Signal` which derives a reactive value from an expression.
|
|
398
|
+
*
|
|
399
|
+
* @developerPreview
|
|
400
|
+
*/
|
|
401
|
+
function computed(computation, options) {
|
|
402
|
+
const node = new ComputedImpl(computation, options?.equal ?? defaultEquals);
|
|
403
|
+
// Casting here is required for g3, as TS inference behavior is slightly different between our
|
|
404
|
+
// version/options and g3's.
|
|
405
|
+
return createSignalFromFunction(node, node.signal.bind(node));
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* A dedicated symbol used before a computed value has been calculated for the first time.
|
|
409
|
+
* Explicitly typed as `any` so we can use it as signal's value.
|
|
410
|
+
*/
|
|
411
|
+
const UNSET = Symbol('UNSET');
|
|
412
|
+
/**
|
|
413
|
+
* A dedicated symbol used in place of a computed signal value to indicate that a given computation
|
|
414
|
+
* is in progress. Used to detect cycles in computation chains.
|
|
415
|
+
* Explicitly typed as `any` so we can use it as signal's value.
|
|
416
|
+
*/
|
|
417
|
+
const COMPUTING = Symbol('COMPUTING');
|
|
418
|
+
/**
|
|
419
|
+
* A dedicated symbol used in place of a computed signal value to indicate that a given computation
|
|
420
|
+
* failed. The thrown error is cached until the computation gets dirty again.
|
|
421
|
+
* Explicitly typed as `any` so we can use it as signal's value.
|
|
422
|
+
*/
|
|
423
|
+
const ERRORED = Symbol('ERRORED');
|
|
424
|
+
/**
|
|
425
|
+
* A computation, which derives a value from a declarative reactive expression.
|
|
426
|
+
*
|
|
427
|
+
* `Computed`s are both producers and consumers of reactivity.
|
|
428
|
+
*/
|
|
429
|
+
class ComputedImpl extends ReactiveNode {
|
|
430
|
+
constructor(computation, equal) {
|
|
431
|
+
super();
|
|
432
|
+
this.computation = computation;
|
|
433
|
+
this.equal = equal;
|
|
434
|
+
/**
|
|
435
|
+
* Current value of the computation.
|
|
436
|
+
*
|
|
437
|
+
* This can also be one of the special values `UNSET`, `COMPUTING`, or `ERRORED`.
|
|
438
|
+
*/
|
|
439
|
+
this.value = UNSET;
|
|
440
|
+
/**
|
|
441
|
+
* If `value` is `ERRORED`, the error caught from the last computation attempt which will
|
|
442
|
+
* be re-thrown.
|
|
443
|
+
*/
|
|
444
|
+
this.error = null;
|
|
445
|
+
/**
|
|
446
|
+
* Flag indicating that the computation is currently stale, meaning that one of the
|
|
447
|
+
* dependencies has notified of a potential change.
|
|
448
|
+
*
|
|
449
|
+
* It's possible that no dependency has _actually_ changed, in which case the `stale`
|
|
450
|
+
* state can be resolved without recomputing the value.
|
|
451
|
+
*/
|
|
452
|
+
this.stale = true;
|
|
453
|
+
this.consumerAllowSignalWrites = false;
|
|
454
|
+
}
|
|
455
|
+
onConsumerDependencyMayHaveChanged() {
|
|
456
|
+
if (this.stale) {
|
|
457
|
+
// We've already notified consumers that this value has potentially changed.
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// Record that the currently cached value may be stale.
|
|
461
|
+
this.stale = true;
|
|
462
|
+
// Notify any consumers about the potential change.
|
|
463
|
+
this.producerMayHaveChanged();
|
|
464
|
+
}
|
|
465
|
+
onProducerUpdateValueVersion() {
|
|
466
|
+
if (!this.stale) {
|
|
467
|
+
// The current value and its version are already up to date.
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
// The current value is stale. Check whether we need to produce a new one.
|
|
471
|
+
if (this.value !== UNSET && this.value !== COMPUTING &&
|
|
472
|
+
!this.consumerPollProducersForChange()) {
|
|
473
|
+
// Even though we were previously notified of a potential dependency update, all of
|
|
474
|
+
// our dependencies report that they have not actually changed in value, so we can
|
|
475
|
+
// resolve the stale state without needing to recompute the current value.
|
|
476
|
+
this.stale = false;
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
// The current value is stale, and needs to be recomputed. It still may not change -
|
|
480
|
+
// that depends on whether the newly computed value is equal to the old.
|
|
481
|
+
this.recomputeValue();
|
|
482
|
+
}
|
|
483
|
+
recomputeValue() {
|
|
484
|
+
if (this.value === COMPUTING) {
|
|
485
|
+
// Our computation somehow led to a cyclic read of itself.
|
|
486
|
+
throw new Error('Detected cycle in computations.');
|
|
487
|
+
}
|
|
488
|
+
const oldValue = this.value;
|
|
489
|
+
this.value = COMPUTING;
|
|
490
|
+
// As we're re-running the computation, update our dependent tracking version number.
|
|
491
|
+
this.trackingVersion++;
|
|
492
|
+
const prevConsumer = setActiveConsumer(this);
|
|
493
|
+
let newValue;
|
|
494
|
+
try {
|
|
495
|
+
newValue = this.computation();
|
|
496
|
+
}
|
|
497
|
+
catch (err) {
|
|
498
|
+
newValue = ERRORED;
|
|
499
|
+
this.error = err;
|
|
500
|
+
}
|
|
501
|
+
finally {
|
|
502
|
+
setActiveConsumer(prevConsumer);
|
|
503
|
+
}
|
|
504
|
+
this.stale = false;
|
|
505
|
+
if (oldValue !== UNSET && oldValue !== ERRORED && newValue !== ERRORED &&
|
|
506
|
+
this.equal(oldValue, newValue)) {
|
|
507
|
+
// No change to `valueVersion` - old and new values are
|
|
508
|
+
// semantically equivalent.
|
|
509
|
+
this.value = oldValue;
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
this.value = newValue;
|
|
513
|
+
this.valueVersion++;
|
|
514
|
+
}
|
|
515
|
+
signal() {
|
|
516
|
+
// Check if the value needs updating before returning it.
|
|
517
|
+
this.onProducerUpdateValueVersion();
|
|
518
|
+
// Record that someone looked at this signal.
|
|
519
|
+
this.producerAccessed();
|
|
520
|
+
if (this.value === ERRORED) {
|
|
521
|
+
throw this.error;
|
|
522
|
+
}
|
|
523
|
+
return this.value;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function defaultThrowError() {
|
|
528
|
+
throw new Error();
|
|
529
|
+
}
|
|
530
|
+
let throwInvalidWriteToSignalErrorFn = defaultThrowError;
|
|
531
|
+
function throwInvalidWriteToSignalError() {
|
|
532
|
+
throwInvalidWriteToSignalErrorFn();
|
|
533
|
+
}
|
|
534
|
+
function setThrowInvalidWriteToSignalError(fn) {
|
|
535
|
+
throwInvalidWriteToSignalErrorFn = fn;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* If set, called after `WritableSignal`s are updated.
|
|
540
|
+
*
|
|
541
|
+
* This hook can be used to achieve various effects, such as running effects synchronously as part
|
|
542
|
+
* of setting a signal.
|
|
543
|
+
*/
|
|
544
|
+
let postSignalSetFn = null;
|
|
545
|
+
class WritableSignalImpl extends ReactiveNode {
|
|
546
|
+
constructor(value, equal) {
|
|
547
|
+
super();
|
|
548
|
+
this.value = value;
|
|
549
|
+
this.equal = equal;
|
|
550
|
+
this.consumerAllowSignalWrites = false;
|
|
551
|
+
}
|
|
552
|
+
onConsumerDependencyMayHaveChanged() {
|
|
553
|
+
// This never happens for writable signals as they're not consumers.
|
|
554
|
+
}
|
|
555
|
+
onProducerUpdateValueVersion() {
|
|
556
|
+
// Writable signal value versions are always up to date.
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Directly update the value of the signal to a new value, which may or may not be
|
|
560
|
+
* equal to the previous.
|
|
561
|
+
*
|
|
562
|
+
* In the event that `newValue` is semantically equal to the current value, `set` is
|
|
563
|
+
* a no-op.
|
|
564
|
+
*/
|
|
565
|
+
set(newValue) {
|
|
566
|
+
if (!this.producerUpdatesAllowed) {
|
|
567
|
+
throwInvalidWriteToSignalError();
|
|
568
|
+
}
|
|
569
|
+
if (!this.equal(this.value, newValue)) {
|
|
570
|
+
this.value = newValue;
|
|
571
|
+
this.valueVersion++;
|
|
572
|
+
this.producerMayHaveChanged();
|
|
573
|
+
postSignalSetFn?.();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Derive a new value for the signal from its current value using the `updater` function.
|
|
578
|
+
*
|
|
579
|
+
* This is equivalent to calling `set` on the result of running `updater` on the current
|
|
580
|
+
* value.
|
|
581
|
+
*/
|
|
582
|
+
update(updater) {
|
|
583
|
+
if (!this.producerUpdatesAllowed) {
|
|
584
|
+
throwInvalidWriteToSignalError();
|
|
585
|
+
}
|
|
586
|
+
this.set(updater(this.value));
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Calls `mutator` on the current value and assumes that it has been mutated.
|
|
590
|
+
*/
|
|
591
|
+
mutate(mutator) {
|
|
592
|
+
if (!this.producerUpdatesAllowed) {
|
|
593
|
+
throwInvalidWriteToSignalError();
|
|
594
|
+
}
|
|
595
|
+
// Mutate bypasses equality checks as it's by definition changing the value.
|
|
596
|
+
mutator(this.value);
|
|
597
|
+
this.valueVersion++;
|
|
598
|
+
this.producerMayHaveChanged();
|
|
599
|
+
postSignalSetFn?.();
|
|
600
|
+
}
|
|
601
|
+
signal() {
|
|
602
|
+
this.producerAccessed();
|
|
603
|
+
return this.value;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Create a `Signal` that can be set or updated directly.
|
|
608
|
+
*
|
|
609
|
+
* @developerPreview
|
|
610
|
+
*/
|
|
611
|
+
function signal(initialValue, options) {
|
|
612
|
+
const signalNode = new WritableSignalImpl(initialValue, options?.equal ?? defaultEquals);
|
|
613
|
+
// Casting here is required for g3, as TS inference behavior is slightly different between our
|
|
614
|
+
// version/options and g3's.
|
|
615
|
+
const signalFn = createSignalFromFunction(signalNode, signalNode.signal.bind(signalNode), {
|
|
616
|
+
set: signalNode.set.bind(signalNode),
|
|
617
|
+
update: signalNode.update.bind(signalNode),
|
|
618
|
+
mutate: signalNode.mutate.bind(signalNode),
|
|
619
|
+
});
|
|
620
|
+
return signalFn;
|
|
621
|
+
}
|
|
622
|
+
function setPostSignalSetFn(fn) {
|
|
623
|
+
const prev = postSignalSetFn;
|
|
624
|
+
postSignalSetFn = fn;
|
|
625
|
+
return prev;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Execute an arbitrary function in a non-reactive (non-tracking) context. The executed function
|
|
630
|
+
* can, optionally, return a value.
|
|
631
|
+
*
|
|
632
|
+
* @developerPreview
|
|
633
|
+
*/
|
|
634
|
+
function untracked(nonReactiveReadsFn) {
|
|
635
|
+
const prevConsumer = setActiveConsumer(null);
|
|
636
|
+
// We are not trying to catch any particular errors here, just making sure that the consumers
|
|
637
|
+
// stack is restored in case of errors.
|
|
638
|
+
try {
|
|
639
|
+
return nonReactiveReadsFn();
|
|
640
|
+
}
|
|
641
|
+
finally {
|
|
642
|
+
setActiveConsumer(prevConsumer);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const NOOP_CLEANUP_FN = () => { };
|
|
647
|
+
/**
|
|
648
|
+
* Watches a reactive expression and allows it to be scheduled to re-run
|
|
649
|
+
* when any dependencies notify of a change.
|
|
650
|
+
*
|
|
651
|
+
* `Watch` doesn't run reactive expressions itself, but relies on a consumer-
|
|
652
|
+
* provided scheduling operation to coordinate calling `Watch.run()`.
|
|
653
|
+
*/
|
|
654
|
+
class Watch extends ReactiveNode {
|
|
655
|
+
constructor(watch, schedule, allowSignalWrites) {
|
|
656
|
+
super();
|
|
657
|
+
this.watch = watch;
|
|
658
|
+
this.schedule = schedule;
|
|
659
|
+
this.dirty = false;
|
|
660
|
+
this.cleanupFn = NOOP_CLEANUP_FN;
|
|
661
|
+
this.registerOnCleanup = (cleanupFn) => {
|
|
662
|
+
this.cleanupFn = cleanupFn;
|
|
663
|
+
};
|
|
664
|
+
this.consumerAllowSignalWrites = allowSignalWrites;
|
|
665
|
+
}
|
|
666
|
+
notify() {
|
|
667
|
+
if (!this.dirty) {
|
|
668
|
+
this.schedule(this);
|
|
669
|
+
}
|
|
670
|
+
this.dirty = true;
|
|
671
|
+
}
|
|
672
|
+
onConsumerDependencyMayHaveChanged() {
|
|
673
|
+
this.notify();
|
|
674
|
+
}
|
|
675
|
+
onProducerUpdateValueVersion() {
|
|
676
|
+
// Watches are not producers.
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Execute the reactive expression in the context of this `Watch` consumer.
|
|
680
|
+
*
|
|
681
|
+
* Should be called by the user scheduling algorithm when the provided
|
|
682
|
+
* `schedule` hook is called by `Watch`.
|
|
683
|
+
*/
|
|
684
|
+
run() {
|
|
685
|
+
this.dirty = false;
|
|
686
|
+
if (this.trackingVersion !== 0 && !this.consumerPollProducersForChange()) {
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
const prevConsumer = setActiveConsumer(this);
|
|
690
|
+
this.trackingVersion++;
|
|
691
|
+
try {
|
|
692
|
+
this.cleanupFn();
|
|
693
|
+
this.cleanupFn = NOOP_CLEANUP_FN;
|
|
694
|
+
this.watch(this.registerOnCleanup);
|
|
695
|
+
}
|
|
696
|
+
finally {
|
|
697
|
+
setActiveConsumer(prevConsumer);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
cleanup() {
|
|
701
|
+
this.cleanupFn();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// toSignal(Observable<Animal>) -> Signal<Animal|undefined>
|
|
706
|
+
function toSignal(source, options) {
|
|
707
|
+
assertInInjectionContext(toSignal);
|
|
708
|
+
// Note: T is the Observable value type, and U is the initial value type. They don't have to be
|
|
709
|
+
// the same - the returned signal gives values of type `T`.
|
|
710
|
+
let state;
|
|
711
|
+
if (options?.requireSync) {
|
|
712
|
+
// Initially the signal is in a `NoValue` state.
|
|
713
|
+
state = signal$1({ kind: 0 /* StateKind.NoValue */ });
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
// If an initial value was passed, use it. Otherwise, use `undefined` as the initial value.
|
|
717
|
+
state = signal$1({ kind: 1 /* StateKind.Value */, value: options?.initialValue });
|
|
718
|
+
}
|
|
719
|
+
const sub = source.subscribe({
|
|
720
|
+
next: value => state.set({ kind: 1 /* StateKind.Value */, value }),
|
|
721
|
+
error: error => state.set({ kind: 2 /* StateKind.Error */, error }),
|
|
722
|
+
// Completion of the Observable is meaningless to the signal. Signals don't have a concept of
|
|
723
|
+
// "complete".
|
|
724
|
+
});
|
|
725
|
+
if (ngDevMode && options?.requireSync && untracked(state).kind === 0 /* StateKind.NoValue */) {
|
|
726
|
+
throw new RuntimeError(601 /* RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT */, '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.');
|
|
727
|
+
}
|
|
728
|
+
// Unsubscribe when the current context is destroyed.
|
|
729
|
+
inject(DestroyRef).onDestroy(sub.unsubscribe.bind(sub));
|
|
730
|
+
// The actual returned signal is a `computed` of the `State` signal, which maps the various states
|
|
731
|
+
// to either values or errors.
|
|
732
|
+
return computed$1(() => {
|
|
733
|
+
const current = state();
|
|
734
|
+
switch (current.kind) {
|
|
735
|
+
case 1 /* StateKind.Value */:
|
|
736
|
+
return current.value;
|
|
737
|
+
case 2 /* StateKind.Error */:
|
|
738
|
+
throw current.error;
|
|
739
|
+
case 0 /* StateKind.NoValue */:
|
|
740
|
+
// This shouldn't really happen because the error is thrown on creation.
|
|
741
|
+
// TODO(alxhub): use a RuntimeError when we finalize the error semantics
|
|
742
|
+
throw new RuntimeError(601 /* RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT */, '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.');
|
|
743
|
+
}
|
|
93
744
|
});
|
|
94
|
-
return (source) => {
|
|
95
|
-
return source.pipe(takeUntil(destroyed$));
|
|
96
|
-
};
|
|
97
745
|
}
|
|
98
746
|
|
|
99
747
|
/**
|
|
100
748
|
* Generated bundle index. Do not edit.
|
|
101
749
|
*/
|
|
102
750
|
|
|
103
|
-
export {
|
|
751
|
+
export { takeUntilDestroyed, toObservable, toSignal };
|
|
104
752
|
//# sourceMappingURL=rxjs-interop.mjs.map
|