@nativescript/compiler-angular 0.0.0-stage → 9.2.0-alpha.9

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.
@@ -0,0 +1,248 @@
1
+ import Foundation
2
+
3
+ /// An Angular component view: a listener marks it and the views above it dirty (`markViewDirty`), and refreshing a
4
+ /// view re-checks every binding (`Check`) of its template and of its embedded views, applying what is no longer
5
+ /// `Object.is` the value last applied. Zoneless, a view marked dirty is refreshed in the update that follows, and child
6
+ /// components only through their own inputs, which are signals here; with zone.js, `Zone.tick` decides which views refresh.
7
+ /// It is the context (`EffectOrder.Scope.context`) of the scopes of its template.
8
+ public final class AngularView {
9
+ nonisolated deinit {}
10
+ weak var parent: AngularView?
11
+ private var checks: [Weak] = []
12
+ /// Checked OnPush: with zone.js a tick refreshes it only while it is `dirty`, or marked to `refresh`.
13
+ let onPush: Bool
14
+ var dirty = false
15
+ /// Angular's `RefreshView`: a signal one of its bindings read changed, or `detectChanges()`.
16
+ var refresh = false
17
+ /// Being refreshed by a tick: a binding re-read now is not a signal's.
18
+ var refreshing = false
19
+
20
+ private struct Weak { weak var effect: Effect? }
21
+
22
+ init(parent: AngularView?, onPush: Bool = false) {
23
+ self.parent = parent
24
+ self.onPush = onPush
25
+ }
26
+
27
+ func add(_ effect: Effect) { checks.append(Weak(effect: effect)) }
28
+
29
+ func markDirty() {
30
+ var view: AngularView? = self
31
+ while let current = view {
32
+ if Zone.enabled {
33
+ current.dirty = true
34
+ } else {
35
+ current.checks.removeAll { $0.effect?.disposed ?? true }
36
+ for check in current.checks { check.effect?.invalidate() }
37
+ }
38
+ view = current.parent
39
+ }
40
+ }
41
+ }
42
+
43
+ extension EffectOrder.Scope {
44
+ /// The component view whose bindings this scope's checks are.
45
+ public var view: AngularView? { context as? AngularView }
46
+ }
47
+
48
+ extension EffectOrder {
49
+ /// Angular's component view: after its parent view's bindings and embedded views. `injector` is the component's
50
+ /// own (`providers`); without one, the view's children inject from the injector the component was made in.
51
+ /// `onPush`: the component, checked OnPush under zone.js, whose inputs mark this view dirty (`Zone.inputChanged`).
52
+ public static func view<T>(onPush component: AnyObject? = nil, injector: Injector? = nil, _ body: () throws -> T) rethrows -> T {
53
+ Zone.install()
54
+ let view = AngularView(parent: current.view, onPush: component != nil)
55
+ if let component { Zone.views.setObject(view, forKey: component) }
56
+ return try run(Scope(prefix: current.next(2), context: view, ambient: injector ?? Injector.current), body)
57
+ }
58
+
59
+ /// Angular's embedded view (`@if`, `@for` content): after its declaring view's own bindings.
60
+ public static func embedded<T>(in view: Scope, _ body: () throws -> T) rethrows -> T {
61
+ try run(Scope(prefix: view.next(1), context: view.context, ambient: view.ambient), body)
62
+ }
63
+ }
64
+
65
+ extension Effect {
66
+ /// The component view it is a binding of.
67
+ var view: AngularView? { context as? AngularView }
68
+
69
+ /// Angular's `effect()`: its first run is in the next update, as change detection runs it, not at creation.
70
+ @discardableResult
71
+ public static func deferred(_ body: @escaping () -> Void) -> Effect {
72
+ Zone.install()
73
+ return Effect(key: EffectOrder.key(), deferred: true, body)
74
+ }
75
+ }
76
+
77
+ extension Reactivity {
78
+ /// A listener of a zoneless Angular template: the update it schedules re-checks the bindings of
79
+ /// `view` and of every view above it.
80
+ public static func event(dirty view: AngularView?, _ handler: () -> Void) {
81
+ Zone.install()
82
+ view?.markDirty()
83
+ event { site(handler) }
84
+ }
85
+
86
+ /// `ChangeDetectorRef.markForCheck()` on a zoneless view: an update re-checks it and the views above it, scheduled
87
+ /// as a write schedules one, or run at once.
88
+ static func markForCheck(_ view: AngularView?, now: Bool = false) {
89
+ view?.markDirty()
90
+ requestUpdate(now: now)
91
+ }
92
+ }
93
+
94
+ /// Angular's zone-based change detection: after every JavaScript task (an
95
+ /// event handler, a timer callback, a promise job) and the jobs it queued,
96
+ /// `ApplicationRef.tick()` re-reads every binding in view order and applies
97
+ /// the ones whose value changed. In a zone app every effect is a binding
98
+ /// re-run on each tick, in its `EffectOrder` key order, which is that order.
99
+ public enum Zone {
100
+ public static var enabled = false {
101
+ didSet {
102
+ install()
103
+ Microtasks.onStable = enabled ? { tick() } : nil
104
+ Effect.created = enabled ? { effects.append($0) } : nil
105
+ Reactivity.rerunsUnchanged = enabled
106
+ }
107
+ }
108
+
109
+ /// Held until disposed: a binding reads no signal that would keep it alive.
110
+ private static var effects: [Effect] = []
111
+
112
+ private static var handing = false
113
+
114
+ /// Hands uncaught errors to Angular (`handOff`). Called where Angular's catch sites (`Reactivity.site`: a template
115
+ /// listener, a check of a view, an `effect()`) and zone.js are first set up, so it is in place before either can
116
+ /// catch anything.
117
+ static func install() {
118
+ if jsUncaughtHandOff == nil { jsUncaughtHandOff = handOff }
119
+ }
120
+
121
+ /// An uncaught error as Angular takes it: with zone.js every task's error goes to the ErrorHandler (the app's,
122
+ /// else Angular's) and never reaches the runtime; without it only an Angular catch site's does. False when it is
123
+ /// the runtime's, as is what the handler itself throws.
124
+ public static func handOff(_ error: Any?) -> Bool {
125
+ guard !handing, enabled || Reactivity.sites > 0 else { return false }
126
+ handing = true
127
+ defer { handing = false }
128
+ (Injector.providedErrorHandler() ?? ErrorHandler.standard).handleError(error)
129
+ return true
130
+ }
131
+
132
+ /// The views of OnPush components, by component.
133
+ static let views = NSMapTable<AnyObject, AngularView>.weakToWeakObjects()
134
+
135
+ /// An input of an OnPush component set to a value not `Object.is` the last (`markDirtyIfOnPush`).
136
+ public static func inputChanged(_ component: AnyObject) {
137
+ views.object(forKey: component)?.dirty = true
138
+ }
139
+
140
+ /// `ApplicationRef.tick()` (Angular 22 `detectChangesInViewWhileDirty`/`detectChangesInView`): from the root, a view
141
+ /// is refreshed when it is checked always (Eager) or dirty and the view above it is refreshed, or on its own when
142
+ /// marked to refresh; a view not refreshed leaves its dirty mark and skips the views below it. Views marked while
143
+ /// the pass ran (an OnPush child's input) are refreshed in a further pass, as Angular re-runs while views need it.
144
+ public static func tick() {
145
+ effects.removeAll { $0.disposed }
146
+ var done = Set<ObjectIdentifier>()
147
+ for pass in 0..<100 {
148
+ var known: [ObjectIdentifier: Bool] = [:]
149
+ func refreshes(_ view: AngularView) -> Bool {
150
+ if let r = known[ObjectIdentifier(view)] { return r }
151
+ let above = view.parent.map { refreshes($0) || done.contains(ObjectIdentifier($0)) } ?? true
152
+ let r = view.refresh || above && (view.dirty || pass == 0 && !view.onPush)
153
+ known[ObjectIdentifier(view)] = r
154
+ return r
155
+ }
156
+ let live = effects.filter { $0.view.map(refreshes) ?? (pass == 0) }
157
+ if live.isEmpty { return }
158
+ var views: [AngularView] = []
159
+ for effect in live {
160
+ guard let view = effect.view, !view.refreshing else { continue }
161
+ view.refreshing = true
162
+ view.dirty = false
163
+ view.refresh = false
164
+ done.insert(ObjectIdentifier(view))
165
+ views.append(view)
166
+ }
167
+ batch { for effect in live { effect.invalidate() } }
168
+ for view in views { view.refreshing = false }
169
+ }
170
+ }
171
+ }
172
+
173
+ /// A binding as Angular checks it (`bindingUpdated`): re-read on every check of its view,
174
+ /// applied only when the value is not `Object.is` the last one applied; what reading it
175
+ /// throws is reported, and nothing is applied.
176
+ public func Check<T>(_ read: @escaping () throws -> T, _ apply: @escaping (T) -> Void) {
177
+ var last: T?
178
+ var started = false
179
+ let effect = Effect {
180
+ // A signal it read changed: the tick refreshes its whole view, in template order.
181
+ if started, Zone.enabled, let view = EffectOrder.current.view, !view.refreshing { view.refresh = true; return }
182
+ started = true
183
+ guard let value = jsReported(read) else { return }
184
+ if let previous = last, jsSameValue(previous, value) { return }
185
+ last = value
186
+ untrack { apply(value) }
187
+ }
188
+ EffectOrder.current.view?.add(effect)
189
+ }
190
+
191
+
192
+ /// A pure Angular pipe at one binding site (`ɵɵpipeBind1`…): `transform` runs when an argument is no longer
193
+ /// `Object.is` the one it last ran with, and otherwise the result it gave is returned. What it throws is not kept.
194
+ public final class PurePipeCache {
195
+ nonisolated deinit {}
196
+ private var arguments: [Any?]?
197
+ private var result: AnyObject?
198
+
199
+ private final class Kept<R> {
200
+ let value: R
201
+ init(_ value: R) { self.value = value }
202
+ }
203
+
204
+ public init() {}
205
+
206
+ private func run<R>(_ given: [Any?], _ transform: () throws -> R) rethrows -> R {
207
+ if let arguments, arguments.count == given.count, zip(arguments, given).allSatisfy({ jsSameValue($0, $1) }), let kept = result as? Kept<R> {
208
+ return kept.value
209
+ }
210
+ let value = try transform()
211
+ arguments = given
212
+ result = Kept(value)
213
+ return value
214
+ }
215
+
216
+ public func pure1<A, R>(_ a: A, _ transform: (A) throws -> R) rethrows -> R { try run([a]) { try transform(a) } }
217
+ public func pure2<A, B, R>(_ a: A, _ b: B, _ transform: (A, B) throws -> R) rethrows -> R { try run([a, b]) { try transform(a, b) } }
218
+ public func pure3<A, B, C, R>(_ a: A, _ b: B, _ c: C, _ transform: (A, B, C) throws -> R) rethrows -> R { try run([a, b, c]) { try transform(a, b, c) } }
219
+ public func pure4<A, B, C, D, R>(_ a: A, _ b: B, _ c: C, _ d: D, _ transform: (A, B, C, D) throws -> R) rethrows -> R { try run([a, b, c, d]) { try transform(a, b, c, d) } }
220
+ public func pure5<A, B, C, D, E, R>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ transform: (A, B, C, D, E) throws -> R) rethrows -> R { try run([a, b, c, d, e]) { try transform(a, b, c, d, e) } }
221
+ }
222
+
223
+ /// `inject(ChangeDetectorRef)`: the view being created when it is injected (a component's, or the embedded view a
224
+ /// pipe is in). `markForCheck` marks it and the views above it dirty; zoneless, it schedules the update that re-checks
225
+ /// them, and with zone.js the tick after the task refreshes them.
226
+ public final class ChangeDetectorRef {
227
+ nonisolated deinit {}
228
+ private weak var view: AngularView?
229
+
230
+ init(_ view: AngularView?) { self.view = view }
231
+
232
+ public static func current() -> ChangeDetectorRef { ChangeDetectorRef(EffectOrder.current.view) }
233
+
234
+ public func markForCheck() {
235
+ if Zone.enabled { view?.markDirty(); return }
236
+ Reactivity.markForCheck(view)
237
+ }
238
+
239
+ /// Checks the view now: its bindings and the views above it, which is more than Angular checks and gives the same values.
240
+ public func detectChanges() {
241
+ if Zone.enabled { view?.refresh = true; Zone.tick(); return }
242
+ Reactivity.markForCheck(view, now: true)
243
+ }
244
+
245
+ public func detach() {}
246
+ public func reattach() {}
247
+ public func checkNoChanges() {}
248
+ }