@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,285 @@
1
+ import Foundation
2
+
3
+ // MARK: @angular/router value types
4
+
5
+ public final class ParamMap {
6
+ nonisolated deinit {}
7
+ private var storage: [String: [String]] = [:]
8
+ private var keyOrder: [String] = []
9
+
10
+ public init(_ params: Any? = nil) {
11
+ guard let flat = jsFlat(params) else { return }
12
+ if let dict = flat as? [String: String] {
13
+ for (k, v) in dict {
14
+ keyOrder.append(k)
15
+ storage[k] = [v]
16
+ }
17
+ return
18
+ }
19
+ for k in jsKeysOf(flat) {
20
+ let v = (try? jsGet(flat, k)) ?? nil
21
+ guard !jsIsNullish(v) else { continue }
22
+ keyOrder.append(k)
23
+ if let arr = jsFlat(v) as? JSArrayProtocol {
24
+ storage[k] = arr.jsAnyElements.map { jsToString($0) }
25
+ } else {
26
+ storage[k] = [jsToString(v)]
27
+ }
28
+ }
29
+ }
30
+
31
+ public func has(_ name: String) -> Bool {
32
+ storage[name] != nil
33
+ }
34
+
35
+ public func get(_ name: String) -> String? {
36
+ storage[name]?.first
37
+ }
38
+
39
+ public func getAll(_ name: String) -> JSArray<String> {
40
+ JSArray(storage[name] ?? [])
41
+ }
42
+
43
+ public var keys: JSArray<String> {
44
+ JSArray(keyOrder)
45
+ }
46
+ }
47
+
48
+ public func convertToParamMap(_ params: Any?) -> ParamMap {
49
+ ParamMap(params)
50
+ }
51
+
52
+ public final class UrlSegment {
53
+ nonisolated deinit {}
54
+ public let path: String
55
+ public let parameters: JSRecord<String>
56
+ public let parameterMap: ParamMap
57
+
58
+ public init(_ path: String, _ parameters: Any? = nil) {
59
+ self.path = path
60
+ var dict: [String: String] = [:]
61
+ if let flat = jsFlat(parameters) {
62
+ if let d = flat as? [String: String] {
63
+ dict = d
64
+ } else {
65
+ for k in jsKeysOf(flat) {
66
+ if let v = try? jsGet(flat, k), !jsIsNullish(v) {
67
+ dict[k] = jsToString(v)
68
+ }
69
+ }
70
+ }
71
+ }
72
+ self.parameters = JSRecord(dict)
73
+ self.parameterMap = ParamMap(dict)
74
+ }
75
+
76
+ public func toString() -> String { path }
77
+ }
78
+
79
+ public final class UrlTree {
80
+ nonisolated deinit {}
81
+ private let path: String
82
+ public var queryParams: JSObject
83
+ public var fragment: String?
84
+
85
+ public init(_ path: String = "/", _ queryParams: Any? = nil, _ fragment: String? = nil) {
86
+ self.path = path.isEmpty ? "/" : path
87
+ if let obj = jsFlat(queryParams) as? JSObject {
88
+ self.queryParams = obj
89
+ } else if let flat = jsFlat(queryParams) {
90
+ var pairs: [(String, Any?)] = []
91
+ for k in jsKeysOf(flat) {
92
+ pairs.append((k, (try? jsGet(flat, k)) ?? nil))
93
+ }
94
+ self.queryParams = JSObject(pairs)
95
+ } else {
96
+ self.queryParams = JSObject()
97
+ }
98
+ self.fragment = fragment
99
+ }
100
+
101
+ public var queryParamMap: ParamMap {
102
+ ParamMap(queryParams)
103
+ }
104
+
105
+ public func toString() -> String {
106
+ var qParts: [String] = []
107
+ for k in jsKeysOf(queryParams) {
108
+ if let v = try? jsGet(queryParams, k), !jsIsNullish(v) {
109
+ qParts.append("\(jsEncodeURIComponent(k))=\(jsEncodeURIComponent(jsToString(v)))")
110
+ }
111
+ }
112
+ let q = qParts.isEmpty ? "" : "?\(qParts.joined(separator: "&"))"
113
+ let f = fragment.map { "#\($0)" } ?? ""
114
+ return "\(path)\(q)\(f)"
115
+ }
116
+ }
117
+
118
+ public final class DefaultUrlSerializer {
119
+ nonisolated deinit {}
120
+ public init() {}
121
+ public func parse(_ url: String) -> UrlTree {
122
+ var raw = url.isEmpty ? "/" : url
123
+ var fragment: String? = nil
124
+ if let hashIdx = raw.firstIndex(of: "#") {
125
+ fragment = String(raw[raw.index(after: hashIdx)...])
126
+ raw = String(raw[..<hashIdx])
127
+ }
128
+ var qPairs: [(String, Any?)] = []
129
+ if let qIdx = raw.firstIndex(of: "?") {
130
+ let qStr = String(raw[raw.index(after: qIdx)...])
131
+ raw = String(raw[..<qIdx])
132
+ for part in qStr.split(separator: "&") {
133
+ let s = String(part)
134
+ if s.isEmpty { continue }
135
+ if let eq = s.firstIndex(of: "=") {
136
+ let k = String(s[..<eq]).removingPercentEncoding ?? String(s[..<eq])
137
+ let v = String(s[s.index(after: eq)...]).removingPercentEncoding ?? String(s[s.index(after: eq)...])
138
+ qPairs.append((k, v))
139
+ } else {
140
+ qPairs.append((s.removingPercentEncoding ?? s, ""))
141
+ }
142
+ }
143
+ }
144
+ return UrlTree(raw.isEmpty ? "/" : raw, JSObject(qPairs), fragment)
145
+ }
146
+ public func serialize(_ tree: UrlTree) -> String {
147
+ tree.toString()
148
+ }
149
+ }
150
+
151
+ public final class ActivatedRouteSnapshot {
152
+ nonisolated deinit {}
153
+ public let params: JSRecord<String>
154
+ public let queryParams: JSRecord<String>
155
+ public let data: JSRecord<Any?>
156
+ public let url: JSArray<UrlSegment>
157
+ public let fragment: String?
158
+ public let outlet: String
159
+ public let routeConfig: JSObject?
160
+ public var title: String?
161
+
162
+ public init(
163
+ params: JSRecord<String> = JSRecord([:]),
164
+ queryParams: JSRecord<String> = JSRecord([:]),
165
+ data: JSRecord<Any?> = JSRecord([:]),
166
+ url: JSArray<UrlSegment> = JSArray(),
167
+ fragment: String? = nil,
168
+ outlet: String = "primary",
169
+ routeConfig: JSObject? = nil
170
+ ) {
171
+ self.params = params
172
+ self.queryParams = queryParams
173
+ self.data = data
174
+ self.url = url
175
+ self.fragment = fragment
176
+ self.outlet = outlet
177
+ self.routeConfig = routeConfig
178
+ }
179
+
180
+ public var paramMap: ParamMap { ParamMap(params) }
181
+ public var queryParamMap: ParamMap { ParamMap(queryParams) }
182
+ public var root: ActivatedRouteSnapshot { self }
183
+ public var parent: ActivatedRouteSnapshot? { nil }
184
+ public var firstChild: ActivatedRouteSnapshot? { nil }
185
+ public var children: JSArray<ActivatedRouteSnapshot> { JSArray() }
186
+ public var pathFromRoot: JSArray<ActivatedRouteSnapshot> { JSArray([self]) }
187
+ }
188
+
189
+ public final class NavigationStart {
190
+ nonisolated deinit {}
191
+ public let id: Double
192
+ public let url: String
193
+ public let navigationTrigger: String = "imperative"
194
+ public let restoredState: Any? = nil
195
+ public init(_ id: Double, _ url: String) {
196
+ self.id = id
197
+ self.url = url
198
+ }
199
+ }
200
+
201
+ /// `NavigationEnd`, the router event each completed navigation emits.
202
+ public final class NavigationEnd {
203
+ nonisolated deinit {}
204
+ public let id: Double
205
+ public let url: String
206
+ public let urlAfterRedirects: String
207
+ public init(id: Double, url: String, urlAfterRedirects: String) {
208
+ self.id = id
209
+ self.url = url
210
+ self.urlAfterRedirects = urlAfterRedirects
211
+ }
212
+ public convenience init(_ id: Double, _ url: String, _ urlAfterRedirects: String? = nil) {
213
+ self.init(id: id, url: url, urlAfterRedirects: urlAfterRedirects ?? url)
214
+ }
215
+ }
216
+
217
+ public final class NavigationCancel {
218
+ nonisolated deinit {}
219
+ public let id: Double
220
+ public let url: String
221
+ public let reason: String
222
+ public init(_ id: Double, _ url: String, _ reason: String = "") {
223
+ self.id = id
224
+ self.url = url
225
+ self.reason = reason
226
+ }
227
+ }
228
+
229
+ public final class NavigationError {
230
+ nonisolated deinit {}
231
+ public let id: Double
232
+ public let url: String
233
+ public let error: Any?
234
+ public init(_ id: Double, _ url: String, _ error: Any? = nil) {
235
+ self.id = id
236
+ self.url = url
237
+ self.error = error
238
+ }
239
+ }
240
+
241
+ public final class RoutesRecognized {
242
+ nonisolated deinit {}
243
+ public let id: Double
244
+ public let url: String
245
+ public let urlAfterRedirects: String
246
+ public let state: Any?
247
+ public init(_ id: Double, _ url: String, _ urlAfterRedirects: String? = nil, _ state: Any? = nil) {
248
+ self.id = id
249
+ self.url = url
250
+ self.urlAfterRedirects = urlAfterRedirects ?? url
251
+ self.state = state
252
+ }
253
+ }
254
+
255
+ public final class ResolveStart {
256
+ nonisolated deinit {}
257
+ public let id: Double
258
+ public let url: String
259
+ public let urlAfterRedirects: String
260
+ public let state: Any?
261
+ public init(_ id: Double, _ url: String, _ urlAfterRedirects: String? = nil, _ state: Any? = nil) {
262
+ self.id = id
263
+ self.url = url
264
+ self.urlAfterRedirects = urlAfterRedirects ?? url
265
+ self.state = state
266
+ }
267
+ }
268
+
269
+ public final class ResolveEnd {
270
+ nonisolated deinit {}
271
+ public let id: Double
272
+ public let url: String
273
+ public let urlAfterRedirects: String
274
+ public let state: Any?
275
+ public init(_ id: Double, _ url: String, _ urlAfterRedirects: String? = nil, _ state: Any? = nil) {
276
+ self.id = id
277
+ self.url = url
278
+ self.urlAfterRedirects = urlAfterRedirects ?? url
279
+ self.state = state
280
+ }
281
+ }
282
+
283
+ public func createRouterStateSnapshot(_ url: String = "/") -> JSObject {
284
+ JSObject([("url", url), ("root", ActivatedRouteSnapshot())])
285
+ }
@@ -0,0 +1,246 @@
1
+ import Foundation
2
+
3
+ // The RxJS subset Angular services use, with RxJS 7's semantics: subscribers
4
+ // are called synchronously in subscription order, a BehaviorSubject replays
5
+ // its value to each new subscriber, and an operator subscribes to its source
6
+ // once per subscriber.
7
+
8
+ public class RxObservable<T> {
9
+ nonisolated deinit {}
10
+ private let producer: (@escaping (T) -> Void) -> TeardownHandle
11
+ private var fullProducer: ((@escaping (T) -> Void, @escaping (Any?) -> Void, @escaping () -> Void) -> TeardownHandle)?
12
+ /// A source that settles once (an HTTP request): its value or its error, for `firstValueFrom`.
13
+ var first: (() -> JSPromise<T>)?
14
+
15
+ public init(_ producer: @escaping (@escaping (T) -> Void) -> TeardownHandle) { self.producer = producer }
16
+
17
+ public init(full producer: @escaping (@escaping (T) -> Void, @escaping (Any?) -> Void, @escaping () -> Void) -> TeardownHandle) {
18
+ self.producer = { next in producer(next, { _ in }, {}) }
19
+ self.fullProducer = producer
20
+ }
21
+
22
+ /// Each value converted: how a typed `get<T>()` reads the parsed body.
23
+ public func mapValues<R>(_ transform: @escaping (T) -> R) -> RxObservable<R> {
24
+ let mapped = RxObservable<R> { next in self.subscribe { next(transform($0)) } }
25
+ if let first { mapped.first = { first().then { transform($0) } } }
26
+ return mapped
27
+ }
28
+
29
+ @discardableResult
30
+ public func subscribe(_ next: @escaping (T) -> Void) -> TeardownHandle { producer(next) }
31
+
32
+ @discardableResult
33
+ public func subscribe(_ next: @escaping (T) -> Void, _ error: @escaping (Any?) -> Void, _ complete: @escaping () -> Void) -> TeardownHandle {
34
+ if let fullProducer { return fullProducer(next, error, complete) }
35
+ return subscribe(next)
36
+ }
37
+
38
+ @discardableResult
39
+ public func subscribe(_ next: @escaping (T) throws -> Void, _ error: @escaping (Any?) throws -> Void, _ complete: @escaping () throws -> Void) -> TeardownHandle {
40
+ subscribe({ value in jsReport { try next(value) } }, { err in jsReport { try error(err) } }, { jsReport { try complete() } })
41
+ }
42
+
43
+ /// `subscribe(() => …)`: a handler that ignores the value.
44
+ @discardableResult
45
+ public func subscribe(_ next: @escaping () -> Void) -> TeardownHandle { subscribe { (_: T) in next() } }
46
+
47
+ /// A handler that throws: RxJS reports what it throws as unhandled, and the subscription goes on.
48
+ @discardableResult
49
+ public func subscribe(_ next: @escaping (T) throws -> Void) -> TeardownHandle { subscribe { (value: T) in jsReport { try next(value) } } }
50
+
51
+ @discardableResult
52
+ public func subscribe(_ next: @escaping () throws -> Void) -> TeardownHandle { subscribe { (_: T) in jsReport { try next() } } }
53
+
54
+ public func pipe<A>(_ a: RxOperatorFunction<T, A>) -> RxObservable<A> { a.apply(self) }
55
+
56
+ public func pipe<A, B>(_ a: RxOperatorFunction<T, A>, _ b: RxOperatorFunction<A, B>) -> RxObservable<B> { b.apply(a.apply(self)) }
57
+
58
+ public func pipe<A, B, C>(_ a: RxOperatorFunction<T, A>, _ b: RxOperatorFunction<A, B>, _ c: RxOperatorFunction<B, C>) -> RxObservable<C> { c.apply(b.apply(a.apply(self))) }
59
+ }
60
+
61
+ public struct RxOperatorFunction<T, R> {
62
+ let apply: (RxObservable<T>) -> RxObservable<R>
63
+ }
64
+
65
+ /// `map(project)`: each value projected, with its index among the values this subscription saw.
66
+ /// A projection that throws reports the error, and its value is not passed on.
67
+ public func map<T, R>(_ project: @escaping (T, Double) throws -> R) -> RxOperatorFunction<T, R> {
68
+ RxOperatorFunction { source in
69
+ RxObservable { next in
70
+ var index = 0.0
71
+ return source.subscribe { (value: T) in
72
+ let i = index
73
+ index += 1
74
+ do { next(try project(value, i)) } catch { jsReportUncaught(jsCaught(error)) }
75
+ }
76
+ }
77
+ }
78
+ }
79
+
80
+ public func map<T, R>(_ project: @escaping (T) throws -> R) -> RxOperatorFunction<T, R> {
81
+ map { value, _ in try project(value) }
82
+ }
83
+
84
+ /// `filter(predicate)`: the values the predicate holds for, with each value's index among those this subscription saw.
85
+ public func filter<T>(_ predicate: @escaping (T, Double) throws -> Bool) -> RxOperatorFunction<T, T> {
86
+ RxOperatorFunction { source in
87
+ RxObservable { next in
88
+ var index = 0.0
89
+ return source.subscribe { (value: T) in
90
+ let i = index
91
+ index += 1
92
+ do { if try predicate(value, i) { next(value) } } catch { jsReportUncaught(jsCaught(error)) }
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ public func filter<T>(_ predicate: @escaping (T) throws -> Bool) -> RxOperatorFunction<T, T> {
99
+ filter { value, _ in try predicate(value) }
100
+ }
101
+
102
+ /// `take(count)`: the first `count` values of each subscription, then none.
103
+ public func take<T>(_ count: Double) -> RxOperatorFunction<T, T> {
104
+ RxOperatorFunction { source in
105
+ RxObservable { next in
106
+ var taken = 0.0
107
+ var subscription: TeardownHandle?
108
+ var done = false
109
+ subscription = source.subscribe { value in
110
+ guard taken < count else { return }
111
+ taken += 1
112
+ next(value)
113
+ if taken >= count { done = true; subscription?.unsubscribe() }
114
+ }
115
+ if done { subscription?.unsubscribe() }
116
+ return TeardownHandle { subscription?.unsubscribe() }
117
+ }
118
+ }
119
+ }
120
+
121
+ /// `firstValueFrom(source)`: settles with the source's first value.
122
+ public func rxFirstValueFrom<T>(_ source: RxObservable<T>) -> JSPromise<T> {
123
+ if let first = source.first { return first() }
124
+ let (promise, resolvers) = JSPromise<T>.pending()
125
+ var subscription: TeardownHandle?
126
+ var settled = false
127
+ subscription = source.subscribe { value in
128
+ guard !settled else { return }
129
+ settled = true
130
+ resolvers.resolve(value)
131
+ subscription?.unsubscribe()
132
+ }
133
+ if settled { subscription?.unsubscribe() }
134
+ return promise
135
+ }
136
+
137
+ public class RxSubject<T>: RxObservable<T> {
138
+ nonisolated deinit {}
139
+ private final class Observer {
140
+ nonisolated deinit {}
141
+ let next: (T) -> Void
142
+ init(_ next: @escaping (T) -> Void) { self.next = next }
143
+ }
144
+
145
+ private var observers: [Observer] = []
146
+
147
+ public init() { super.init { _ in TeardownHandle() } }
148
+
149
+ @discardableResult
150
+ public override func subscribe(_ next: @escaping (T) -> Void) -> TeardownHandle { add(next) }
151
+
152
+ func add(_ next: @escaping (T) -> Void) -> TeardownHandle {
153
+ let observer = Observer(next)
154
+ observers.append(observer)
155
+ return TeardownHandle { [weak self] in self?.observers.removeAll { $0 === observer } }
156
+ }
157
+
158
+ /// Calls the observers subscribed when `next` is called, as RxJS copies its observer list.
159
+ public func next(_ value: T) {
160
+ for observer in observers { observer.next(value) }
161
+ }
162
+
163
+ public func asObservable() -> RxObservable<T> { RxObservable { next in self.subscribe(next) } }
164
+ }
165
+
166
+ extension RxSubject where T == Void {
167
+ public func next() { next(()) }
168
+ }
169
+
170
+ public final class RxBehaviorSubject<T>: RxSubject<T> {
171
+ nonisolated deinit {}
172
+ private var current: T
173
+
174
+ public init(_ value: T) {
175
+ current = value
176
+ super.init()
177
+ }
178
+
179
+ public var value: T { current }
180
+
181
+ public func getValue() -> T { current }
182
+
183
+ override func add(_ next: @escaping (T) -> Void) -> TeardownHandle {
184
+ let subscription = super.add(next)
185
+ if !subscription.closed { next(current) }
186
+ return subscription
187
+ }
188
+
189
+ public override func next(_ value: T) {
190
+ current = value
191
+ super.next(value)
192
+ }
193
+ }
194
+
195
+ /// Angular's `async` pipe at one binding site: subscribes to the observable
196
+ /// it is given (again when given another), and returns its latest value.
197
+ public final class AsyncPipe {
198
+ nonisolated deinit {}
199
+ private var source: AnyObject?
200
+ private var subscription: TeardownHandle?
201
+ private var latest: Any?
202
+
203
+ public init() {
204
+ Owner.current?.onCleanup { [weak self] in self?.dispose() }
205
+ }
206
+
207
+ public func transform<T>(_ observable: RxObservable<T>?) -> T? {
208
+ if observable !== source {
209
+ dispose()
210
+ source = observable
211
+ subscription = observable?.subscribe { [weak self] value in self?.latest = value }
212
+ }
213
+ return latest as? T
214
+ }
215
+
216
+ private func dispose() {
217
+ subscription?.unsubscribe()
218
+ subscription = nil
219
+ source = nil
220
+ latest = nil
221
+ }
222
+ }
223
+
224
+ /// `new ReplaySubject(bufferSize)`: replays the last `bufferSize` values to each new subscriber.
225
+ public final class RxReplaySubject<T>: RxSubject<T> {
226
+ nonisolated deinit {}
227
+ private var buffer: [T] = []
228
+ private let size: Int
229
+
230
+ public init(_ bufferSize: Double = .infinity) {
231
+ size = bufferSize.isFinite ? max(1, Int(bufferSize)) : Int.max
232
+ super.init()
233
+ }
234
+
235
+ override func add(_ next: @escaping (T) -> Void) -> TeardownHandle {
236
+ let subscription = super.add(next)
237
+ for value in buffer where !subscription.closed { next(value) }
238
+ return subscription
239
+ }
240
+
241
+ public override func next(_ value: T) {
242
+ buffer.append(value)
243
+ if buffer.count > size { buffer.removeFirst(buffer.count - size) }
244
+ super.next(value)
245
+ }
246
+ }