@nstudio/nativescript-compiler-angular 9.2.0-alpha.10 → 9.2.0-alpha.11

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.
@@ -6,6 +6,16 @@ import java.util.IdentityHashMap
6
6
 
7
7
  typealias EffectRef = Effect
8
8
 
9
+ /** `inject(PendingTasks)`: tracks pending async tasks (no-op in compiled native apps). */
10
+ class PendingTasks {
11
+ fun add(): () -> Unit = {}
12
+ fun <T> run(task: () -> T): T = task()
13
+
14
+ companion object {
15
+ val shared = PendingTasks()
16
+ }
17
+ }
18
+
9
19
  /** `inject(DestroyRef)`: callbacks run when the injecting component's scope ends. */
10
20
  class DestroyRef internal constructor(owner: Owner?) {
11
21
  private val owner = java.lang.ref.WeakReference(owner)
@@ -51,6 +61,23 @@ class SimpleChange(var previousValue: Any?, var currentValue: Any?, var firstCha
51
61
 
52
62
  fun forwardRef(fn: () -> Any?): Any? = fn
53
63
 
64
+ /** Angular's `booleanAttribute` input transform: `false` / `"false"` / `null` / `undefined` -> `false`, else `true`. */
65
+ fun booleanAttribute(value: Any?): Boolean {
66
+ val v = jsBox(value)
67
+ if (jsIsNullish(v)) return false
68
+ if (v is Boolean) return v
69
+ return jsToString(v) != "false"
70
+ }
71
+
72
+ /** Angular's `numberAttribute` input transform: parses a number or returns `fallbackValue` (`NaN`). */
73
+ fun numberAttribute(value: Any?, fallbackValue: Double = Double.NaN): Double {
74
+ val v = jsBox(value)
75
+ if (jsIsNullish(v)) return fallbackValue
76
+ if (v is String && v.trim().isEmpty()) return fallbackValue
77
+ val n = jsToNumber(v)
78
+ return if (n.isNaN()) fallbackValue else n
79
+ }
80
+
54
81
  fun resolveForwardRef(value: Any?): Any? {
55
82
  val flat = jsBox(value) ?: return null
56
83
  if (flat is Function0<*>) return try { flat.invoke() } catch (t: Throwable) { jsReportUncaught(jsCaught(t)); null }
@@ -104,6 +131,10 @@ object AppInjector {
104
131
  }
105
132
  }
106
133
 
134
+ interface JSTokenKeyed {
135
+ val jsTokenKey: Class<*>?
136
+ }
137
+
107
138
  typealias NodeInjector = Injector
108
139
 
109
140
  class Injector(providers: Any? = null, private val parent: Injector? = null) {
@@ -200,6 +231,11 @@ class Injector(providers: Any? = null, private val parent: Injector? = null) {
200
231
  walk(providers)
201
232
  }
202
233
 
234
+ fun provideSelf(instance: Any?) {
235
+ val obj = jsBox(instance) ?: return
236
+ instanceValues[obj::class.java] = obj
237
+ }
238
+
203
239
  fun leave() {
204
240
  current = entered
205
241
  }
@@ -222,7 +258,10 @@ class Injector(providers: Any? = null, private val parent: Injector? = null) {
222
258
  private val tokenFactories = IdentityHashMap<Any, () -> Any?>()
223
259
  private val multiFactories = IdentityHashMap<Any, MutableList<() -> Any?>>()
224
260
 
225
- private fun tokenKey(token: Any?): Any? = jsBox(resolveForwardRef(token))
261
+ private fun tokenKey(token: Any?): Any? {
262
+ val unwrapped = jsBox(resolveForwardRef(token))
263
+ return (unwrapped as? JSTokenKeyed)?.jsTokenKey ?: unwrapped
264
+ }
226
265
 
227
266
  /** The `ErrorHandler` the app provides, which hears what no script code caught, as Angular's zone hands it over. */
228
267
  /** `inject(ErrorHandler)`: the one provided, else Angular's own. */
@@ -464,6 +503,7 @@ class Injector(providers: Any? = null, private val parent: Injector? = null) {
464
503
  DestroyRef::class.java -> return DestroyRef.current()
465
504
  ElementRef::class.java -> return ElementRef.current()
466
505
  ApplicationRef::class.java -> return ApplicationRef.shared
506
+ PendingTasks::class.java -> return PendingTasks.shared
467
507
  Location::class.java -> return Location.shared
468
508
  ViewContainerRef::class.java -> return ViewContainerRef()
469
509
  }
@@ -577,13 +617,33 @@ class NgZone {
577
617
  }
578
618
  }
579
619
 
580
- class TemplateRef(val elementRef: ElementRef = ElementRef())
620
+ class TemplateRef(val elementRef: ElementRef = ElementRef()) {
621
+ var _render: ((Any, Any?) -> Unit)? = null
622
+ var _detached: ((Any?, Any?) -> EmbeddedViewRef)? = null
581
623
 
582
- class EmbeddedViewRef(var context: Any? = null) {
624
+ fun createEmbeddedView(context: Any? = null, injector: Any? = null): EmbeddedViewRef =
625
+ _detached?.invoke(context, injector) ?: EmbeddedViewRef(context)
626
+
627
+ companion object {
628
+ private val stack = mutableListOf<TemplateRef>()
629
+ fun current(): TemplateRef = stack.lastOrNull() ?: TemplateRef()
630
+ fun <T> with(ref: TemplateRef, body: () -> T): T {
631
+ stack.add(ref)
632
+ try { return body() } finally { stack.removeAt(stack.lastIndex) }
633
+ }
634
+ }
635
+ }
636
+
637
+ class EmbeddedViewRef(var context: Any? = null, private var onDestroyFn: (() -> Unit)? = null) {
583
638
  var rootNodes: JSArray<Any?> = JSArray()
584
639
  var destroyed: Boolean = false
585
640
  private set
586
- fun destroy() { destroyed = true }
641
+ fun destroy() {
642
+ if (destroyed) return
643
+ destroyed = true
644
+ onDestroyFn?.invoke()
645
+ onDestroyFn = null
646
+ }
587
647
  fun detectChanges() {}
588
648
  fun markForCheck() {}
589
649
  }
@@ -601,17 +661,24 @@ class ComponentRef(
601
661
  fun destroy() { hostView.destroy() }
602
662
  }
603
663
 
604
- class ViewContainerRef {
664
+ class ViewContainerRef(val injector: Injector = Injector.current ?: Injector.shared) {
605
665
  val element = ElementRef()
606
- val injector = Injector.shared
607
666
  private val views = mutableListOf<EmbeddedViewRef>()
667
+ var _mount: ((TemplateRef, Any?) -> EmbeddedViewRef)? = null
668
+ var _clear: (() -> Unit)? = null
608
669
  val length: Double get() = views.size.toDouble()
609
670
  fun clear() {
610
671
  for (v in views) v.destroy()
611
672
  views.clear()
673
+ _clear?.invoke()
612
674
  }
613
- fun createEmbeddedView(@Suppress("UNUSED_PARAMETER") templateRef: Any?, context: Any? = null, @Suppress("UNUSED_PARAMETER") options: Any? = null): EmbeddedViewRef {
614
- val view = EmbeddedViewRef(context)
675
+ fun createEmbeddedView(templateRef: Any?, context: Any? = null, @Suppress("UNUSED_PARAMETER") options: Any? = null): EmbeddedViewRef {
676
+ val tpl = jsBox(templateRef) as? TemplateRef
677
+ val view = when {
678
+ tpl != null && _mount != null -> _mount!!.invoke(tpl, context)
679
+ tpl != null -> tpl.createEmbeddedView(context)
680
+ else -> EmbeddedViewRef(context)
681
+ }
615
682
  views.add(view)
616
683
  return view
617
684
  }
@@ -638,17 +705,63 @@ class ViewContainerRef {
638
705
  views.removeAt(idx)
639
706
  }
640
707
  }
708
+
709
+ companion object {
710
+ private val stack = mutableListOf<ViewContainerRef>()
711
+ fun current(): ViewContainerRef = stack.lastOrNull() ?: ViewContainerRef()
712
+ fun <T> with(ref: ViewContainerRef, body: () -> T): T {
713
+ stack.add(ref)
714
+ try { return body() } finally { stack.removeAt(stack.lastIndex) }
715
+ }
716
+ }
641
717
  }
642
718
 
643
719
  class ApplicationRef {
644
720
  val isStable = Emitter<Boolean>()
645
721
  fun tick() {}
722
+ fun attachView(@Suppress("UNUSED_PARAMETER") view: Any?) {}
723
+ fun detachView(@Suppress("UNUSED_PARAMETER") view: Any?) {}
646
724
  fun destroy() {}
647
725
  companion object {
648
726
  val shared = ApplicationRef()
649
727
  }
650
728
  }
651
729
 
730
+ open class Renderer2 {
731
+ val data: JSObject = JSObject()
732
+ open fun destroy() {}
733
+ open fun createElement(name: String, @Suppress("UNUSED_PARAMETER") namespace: String? = null): Any? = JSObject("tagName" to name)
734
+ open fun createComment(value: String): Any? = JSObject("comment" to value)
735
+ open fun createText(value: String): Any? = JSObject("text" to value)
736
+ open fun appendChild(@Suppress("UNUSED_PARAMETER") parent: Any?, @Suppress("UNUSED_PARAMETER") newChild: Any?) {}
737
+ open fun insertBefore(@Suppress("UNUSED_PARAMETER") parent: Any?, @Suppress("UNUSED_PARAMETER") newChild: Any?, @Suppress("UNUSED_PARAMETER") refChild: Any?, @Suppress("UNUSED_PARAMETER") isMove: Boolean? = null) {}
738
+ open fun removeChild(@Suppress("UNUSED_PARAMETER") parent: Any?, @Suppress("UNUSED_PARAMETER") oldChild: Any?, @Suppress("UNUSED_PARAMETER") isHostElement: Boolean? = null) {}
739
+ open fun selectRootElement(selectorOrNode: Any?, @Suppress("UNUSED_PARAMETER") preserveContent: Boolean? = null): Any? = selectorOrNode
740
+ open fun parentNode(node: Any?): Any? = jsGetOptional(node, "parent")
741
+ open fun nextSibling(@Suppress("UNUSED_PARAMETER") node: Any?): Any? = null
742
+ open fun setAttribute(el: Any?, name: String, value: String, @Suppress("UNUSED_PARAMETER") namespace: String? = null) { jsSet(el, name, value) }
743
+ open fun removeAttribute(el: Any?, name: String, @Suppress("UNUSED_PARAMETER") namespace: String? = null) { jsSet(el, name, null) }
744
+ open fun addClass(@Suppress("UNUSED_PARAMETER") el: Any?, @Suppress("UNUSED_PARAMETER") name: String) {}
745
+ open fun removeClass(@Suppress("UNUSED_PARAMETER") el: Any?, @Suppress("UNUSED_PARAMETER") name: String) {}
746
+ open fun setStyle(@Suppress("UNUSED_PARAMETER") el: Any?, @Suppress("UNUSED_PARAMETER") style: String, @Suppress("UNUSED_PARAMETER") value: Any?, @Suppress("UNUSED_PARAMETER") flags: Any? = null) {}
747
+ open fun removeStyle(@Suppress("UNUSED_PARAMETER") el: Any?, @Suppress("UNUSED_PARAMETER") style: String, @Suppress("UNUSED_PARAMETER") flags: Any? = null) {}
748
+ open fun setProperty(el: Any?, name: String, value: Any?) { jsSet(el, name, value) }
749
+ open fun setValue(node: Any?, value: String) { jsSet(node, "text", value) }
750
+ open fun listen(@Suppress("UNUSED_PARAMETER") target: Any?, @Suppress("UNUSED_PARAMETER") eventName: String, @Suppress("UNUSED_PARAMETER") callback: (Any?) -> Any?): () -> Unit = {}
751
+ companion object {
752
+ val shared = Renderer2()
753
+ val current: Renderer2 get() = Injector.inject(Renderer2::class.java, JSObject("optional" to true)) as? Renderer2 ?: shared
754
+ }
755
+ }
756
+
757
+ open class RendererFactory2 {
758
+ open fun createRenderer(@Suppress("UNUSED_PARAMETER") hostElement: Any?, @Suppress("UNUSED_PARAMETER") type: Any? = null): Renderer2 = Renderer2.current
759
+ companion object {
760
+ val shared = RendererFactory2()
761
+ val current: RendererFactory2 get() = Injector.inject(RendererFactory2::class.java, JSObject("optional" to true)) as? RendererFactory2 ?: shared
762
+ }
763
+ }
764
+
652
765
  class Location {
653
766
  private var currentPath = "/"
654
767
  fun path(@Suppress("UNUSED_PARAMETER") includeHash: Boolean = false): String = currentPath
@@ -138,6 +138,10 @@ open class RxSubject<T> : RxObservable<T>({ TeardownHandle() }) {
138
138
  for (observer in observers.toList()) observer.next(value)
139
139
  }
140
140
 
141
+ fun complete() {
142
+ observers.clear()
143
+ }
144
+
141
145
  fun asObservable(): RxObservable<T> = RxObservable { next -> subscribe(next) }
142
146
  }
143
147
 
@@ -5,10 +5,14 @@ import Foundation
5
5
  // instance (`providedIn: 'root'`).
6
6
 
7
7
  /// A component class used as a value (`dialog.open(SheetComponent)`): what creating and rendering it gives.
8
- public final class ComponentFactory {
8
+ public final class ComponentFactory: JSTokenKeyed {
9
9
  nonisolated deinit {}
10
10
  let make: () throws -> View
11
- public init(_ make: @escaping () throws -> View) { self.make = make }
11
+ public let jsTokenKey: Any.Type?
12
+ public init(_ componentType: Any.Type? = nil, _ make: @escaping () throws -> View) {
13
+ self.jsTokenKey = componentType
14
+ self.make = make
15
+ }
12
16
  }
13
17
 
14
18
  /// `NativeDialogRef`: the open dialog a component was created in.
@@ -4,6 +4,15 @@ import Foundation
4
4
 
5
5
  public typealias EffectRef = Effect
6
6
 
7
+ /// `inject(PendingTasks)`: tracks pending async tasks (no-op in compiled native apps).
8
+ public final class PendingTasks {
9
+ nonisolated deinit {}
10
+ public static let shared = PendingTasks()
11
+ public init() {}
12
+ public func add() -> () -> Void { {} }
13
+ public func run<T>(_ task: () throws -> T) rethrows -> T { try task() }
14
+ }
15
+
7
16
  /// `inject(DestroyRef)`: callbacks run when the injecting component's scope ends.
8
17
  public final class DestroyRef {
9
18
  nonisolated deinit {}
@@ -65,8 +74,27 @@ public protocol JSInjectableType: AnyObject {
65
74
  static func jsConstruct() throws -> Any?
66
75
  }
67
76
 
77
+ public protocol JSTokenKeyed: AnyObject {
78
+ var jsTokenKey: Any.Type? { get }
79
+ }
80
+
68
81
  public func forwardRef(_ fn: @escaping () throws -> Any?) -> Any? { fn }
69
82
 
83
+ /// Angular's `booleanAttribute` input transform: `false` / `"false"` / `null` / `undefined` → `false`, else `true`.
84
+ public func booleanAttribute(_ value: Any?) -> Bool {
85
+ guard let v = jsFlat(value), !jsIsNullish(v) else { return false }
86
+ if let b = v as? Bool { return b }
87
+ return jsToString(v) != "false"
88
+ }
89
+
90
+ /// Angular's `numberAttribute` input transform: parses a finite number or returns `fallbackValue` (`NaN`).
91
+ public func numberAttribute(_ value: Any?, _ fallbackValue: Double = .nan) -> Double {
92
+ guard let v = jsFlat(value), !jsIsNullish(v) else { return fallbackValue }
93
+ if let s = v as? String, s.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return fallbackValue }
94
+ let n = jsToNumber(v)
95
+ return n.isNaN ? fallbackValue : n
96
+ }
97
+
70
98
  public func resolveForwardRef(_ value: Any?) -> Any? {
71
99
  guard let flat = jsFlat(value) else { return nil }
72
100
  if let fn = flat as? () throws -> Any? { return jsReported { try fn() } ?? nil }
@@ -146,6 +174,12 @@ public final class Injector {
146
174
  return node
147
175
  }
148
176
 
177
+ public func provideSelf(_ instance: Any?) {
178
+ guard let flat = jsFlat(instance) else { return }
179
+ let obj = flat as AnyObject
180
+ instanceValues[ObjectIdentifier(type(of: obj))] = obj
181
+ }
182
+
149
183
  public func leave() { Injector.current = entered }
150
184
 
151
185
  /// `createRootViewInjector`: a standalone component created as a root view (bootstrap, route, dialog) is made and
@@ -269,6 +303,7 @@ public final class Injector {
269
303
 
270
304
  private static func tokenKey(_ token: Any?) -> ObjectIdentifier? {
271
305
  guard let token = jsFlat(resolveForwardRef(token)) else { return nil }
306
+ if let keyed = token as? JSTokenKeyed, let t = keyed.jsTokenKey { return ObjectIdentifier(t) }
272
307
  if let type = token as? Any.Type { return ObjectIdentifier(type) }
273
308
  return ObjectIdentifier(token as AnyObject)
274
309
  }
@@ -489,6 +524,7 @@ public final class Injector {
489
524
  if flat is DestroyRef.Type { return DestroyRef.current() }
490
525
  if flat is ElementRef.Type { return ElementRef.current() }
491
526
  if flat is ApplicationRef.Type { return ApplicationRef.shared }
527
+ if flat is PendingTasks.Type { return PendingTasks.shared }
492
528
  if flat is Location.Type { return Location.shared }
493
529
  if flat is ViewContainerRef.Type { return ViewContainerRef() }
494
530
  return notFoundValue
@@ -628,9 +664,22 @@ public final class NgZone {
628
664
  public final class TemplateRef {
629
665
  nonisolated deinit {}
630
666
  public let elementRef: ElementRef
667
+ public var _render: ((Any, Any?) throws -> Void)?
668
+ public var _detached: ((Any?, Any?) -> EmbeddedViewRef)?
669
+ private static var stack: [TemplateRef] = []
631
670
  public init(_ elementRef: ElementRef = ElementRef()) {
632
671
  self.elementRef = elementRef
633
672
  }
673
+ public func createEmbeddedView(_ context: Any? = nil, _ injector: Any? = nil) -> EmbeddedViewRef {
674
+ if let detached = _detached { return detached(context, injector) }
675
+ return EmbeddedViewRef(context)
676
+ }
677
+ public static func current() -> TemplateRef { stack.last ?? TemplateRef() }
678
+ public static func with<T>(_ ref: TemplateRef, _ body: () throws -> T) rethrows -> T {
679
+ stack.append(ref)
680
+ defer { stack.removeLast() }
681
+ return try body()
682
+ }
634
683
  }
635
684
 
636
685
  public final class EmbeddedViewRef {
@@ -638,8 +687,17 @@ public final class EmbeddedViewRef {
638
687
  public var context: Any?
639
688
  public var rootNodes: JSArray<Any?> = JSArray()
640
689
  public private(set) var destroyed = false
641
- public init(_ context: Any? = nil) { self.context = context }
642
- public func destroy() { destroyed = true }
690
+ private var onDestroyFn: (() -> Void)?
691
+ public init(_ context: Any? = nil, onDestroy: (() -> Void)? = nil) {
692
+ self.context = context
693
+ self.onDestroyFn = onDestroy
694
+ }
695
+ public func destroy() {
696
+ if destroyed { return }
697
+ destroyed = true
698
+ onDestroyFn?()
699
+ onDestroyFn = nil
700
+ }
643
701
  public func detectChanges() {}
644
702
  public func markForCheck() {}
645
703
  }
@@ -669,17 +727,30 @@ public final class ComponentRef {
669
727
  public final class ViewContainerRef {
670
728
  nonisolated deinit {}
671
729
  public let element = ElementRef()
672
- public let injector = Injector.shared
730
+ public let injector: Injector
673
731
  private var views: [EmbeddedViewRef] = []
674
- public init() {}
732
+ public var _mount: ((TemplateRef, Any?) -> EmbeddedViewRef)?
733
+ public var _clear: (() -> Void)?
734
+ private static var stack: [ViewContainerRef] = []
735
+ public init(injector: Injector? = nil) {
736
+ self.injector = injector ?? Injector.current ?? Injector.shared
737
+ }
675
738
  public var length: Double { Double(views.count) }
676
739
  public func clear() {
677
740
  for v in views { v.destroy() }
678
741
  views.removeAll()
742
+ _clear?()
679
743
  }
680
744
  @discardableResult
681
745
  public func createEmbeddedView(_ templateRef: Any?, _ context: Any? = nil, _ options: Any? = nil) -> EmbeddedViewRef {
682
- let view = EmbeddedViewRef(context)
746
+ let view: EmbeddedViewRef
747
+ if let tpl = jsFlat(templateRef) as? TemplateRef, let mount = _mount {
748
+ view = mount(tpl, context)
749
+ } else if let tpl = jsFlat(templateRef) as? TemplateRef {
750
+ view = tpl.createEmbeddedView(context)
751
+ } else {
752
+ view = EmbeddedViewRef(context)
753
+ }
683
754
  views.append(view)
684
755
  return view
685
756
  }
@@ -706,6 +777,12 @@ public final class ViewContainerRef {
706
777
  views.remove(at: idx)
707
778
  }
708
779
  }
780
+ public static func current() -> ViewContainerRef { stack.last ?? ViewContainerRef() }
781
+ public static func with<T>(_ ref: ViewContainerRef, _ body: () throws -> T) rethrows -> T {
782
+ stack.append(ref)
783
+ defer { stack.removeLast() }
784
+ return try body()
785
+ }
709
786
  }
710
787
 
711
788
  public final class ApplicationRef {
@@ -714,9 +791,46 @@ public final class ApplicationRef {
714
791
  public let isStable = Emitter<Bool>()
715
792
  public init() {}
716
793
  public func tick() {}
794
+ public func attachView(_ view: Any?) {}
795
+ public func detachView(_ view: Any?) {}
717
796
  public func destroy() {}
718
797
  }
719
798
 
799
+ open class Renderer2 {
800
+ nonisolated deinit {}
801
+ public static let shared = Renderer2()
802
+ public static var current: Renderer2 { Injector.inject(Renderer2.self, JSObject([("optional", true)])) as? Renderer2 ?? shared }
803
+ public var data: JSObject = JSObject()
804
+ public init() {}
805
+ open func destroy() {}
806
+ open func createElement(_ name: String, _ namespace: String? = nil) -> Any? { JSObject([("tagName", name)]) }
807
+ open func createComment(_ value: String) -> Any? { JSObject([("comment", value)]) }
808
+ open func createText(_ value: String) -> Any? { JSObject([("text", value)]) }
809
+ open func appendChild(_ parent: Any?, _ newChild: Any?) {}
810
+ open func insertBefore(_ parent: Any?, _ newChild: Any?, _ refChild: Any?, _ isMove: Bool? = nil) {}
811
+ open func removeChild(_ parent: Any?, _ oldChild: Any?, _ isHostElement: Bool? = nil) {}
812
+ open func selectRootElement(_ selectorOrNode: Any?, _ preserveContent: Bool? = nil) -> Any? { selectorOrNode }
813
+ open func parentNode(_ node: Any?) -> Any? { (try? jsGet(node, "parent")) ?? nil }
814
+ open func nextSibling(_ node: Any?) -> Any? { nil }
815
+ open func setAttribute(_ el: Any?, _ name: String, _ value: String, _ namespace: String? = nil) { _ = try? jsSet(el, name, value) }
816
+ open func removeAttribute(_ el: Any?, _ name: String, _ namespace: String? = nil) { _ = try? jsSet(el, name, nil) }
817
+ open func addClass(_ el: Any?, _ name: String) {}
818
+ open func removeClass(_ el: Any?, _ name: String) {}
819
+ open func setStyle(_ el: Any?, _ style: String, _ value: Any?, _ flags: Any? = nil) {}
820
+ open func removeStyle(_ el: Any?, _ style: String, _ flags: Any? = nil) {}
821
+ open func setProperty(_ el: Any?, _ name: String, _ value: Any?) { _ = try? jsSet(el, name, value) }
822
+ open func setValue(_ node: Any?, _ value: String) { _ = try? jsSet(node, "text", value) }
823
+ open func listen(_ target: Any?, _ eventName: String, _ callback: @escaping (Any?) throws -> Any?) -> () -> Void { {} }
824
+ }
825
+
826
+ open class RendererFactory2 {
827
+ nonisolated deinit {}
828
+ public static let shared = RendererFactory2()
829
+ public static var current: RendererFactory2 { Injector.inject(RendererFactory2.self, JSObject([("optional", true)])) as? RendererFactory2 ?? shared }
830
+ public init() {}
831
+ open func createRenderer(_ hostElement: Any?, _ type: Any? = nil) -> Renderer2 { Renderer2.current }
832
+ }
833
+
720
834
  public final class Location {
721
835
  nonisolated deinit {}
722
836
  public static let shared = Location()
@@ -653,3 +653,108 @@ private func field(_ object: Any?, _ key: String) -> Any? { object == nil ? nil
653
653
 
654
654
  public typealias KitRouter = Router
655
655
  public typealias KitActivatedRoute = ActivatedRoute
656
+
657
+ /// Runtime registry and mount helper for Angular's `[ngComponentOutlet]` / `*ngComponentOutlet`.
658
+ public enum ComponentOutlet {
659
+ private static var factories: [ObjectIdentifier: (Region, Any?) throws -> Void] = [:]
660
+
661
+ public static func register(_ type: Any.Type, _ render: @escaping (Region, Any?) throws -> Void) {
662
+ factories[ObjectIdentifier(type)] = render
663
+ }
664
+
665
+ public static func mount(_ region: Region, component: @escaping () throws -> Any?, injector: (() throws -> Any?)? = nil, inputs: (() throws -> Any?)? = nil) {
666
+ var currentType: ObjectIdentifier? = nil
667
+ var branchOwner: Owner? = nil
668
+ Owner.current?.onCleanup { branchOwner?.dispose() }
669
+ Effect {
670
+ let raw = (try? component()) ?? nil
671
+ let flat = jsFlat(raw)
672
+ let metatype = flat as? Any.Type
673
+ let id = metatype.map { ObjectIdentifier($0) }
674
+ if id == currentType { return }
675
+ currentType = id
676
+ let inj = ((try? injector?()) ?? nil).flatMap { jsFlat($0) as? Injector }
677
+ let inp = (try? inputs?()) ?? nil
678
+ untrack {
679
+ branchOwner?.dispose()
680
+ branchOwner = nil
681
+ guard let id, let factory = factories[id] else {
682
+ region.set([])
683
+ return
684
+ }
685
+ let owner = Owner(parent: nil)
686
+ branchOwner = owner
687
+ let run: () throws -> Void = { try owner.run { try factory(region, inp) } }
688
+ jsReport {
689
+ if let inj { try Injector.runInContext(inj, run) }
690
+ else { try run() }
691
+ }
692
+ }
693
+ }
694
+ }
695
+ }
696
+
697
+ /// Runtime helper for dynamic/recursive `[ngTemplateOutlet]` / `*ngTemplateOutlet` and `TemplateRef` closures.
698
+ public enum TemplateOutlet {
699
+ public static func bind(_ ref: TemplateRef, _ render: @escaping (Region, Any?) throws -> Void) {
700
+ ref._render = { anyRegion, ctx in
701
+ if let region = anyRegion as? Region { try render(region, ctx) }
702
+ }
703
+ ref._detached = { ctx, injector in
704
+ let owner = Owner(parent: nil)
705
+ let region = Region(host: nil)
706
+ let run: () throws -> Void = { try owner.run { try render(region, ctx) } }
707
+ jsReport {
708
+ if let inj = jsFlat(injector) as? Injector { try Injector.runInContext(inj, run) }
709
+ else { try run() }
710
+ }
711
+ let view = EmbeddedViewRef(ctx) { owner.dispose() }
712
+ return view
713
+ }
714
+ }
715
+
716
+ public static func make(_ render: @escaping (Region, Any?) throws -> Void) -> TemplateRef {
717
+ let ref = TemplateRef()
718
+ bind(ref, render)
719
+ return ref
720
+ }
721
+
722
+ public static func mount(_ region: Region, template: @escaping () throws -> Any?, context: (() throws -> Any?)? = nil) {
723
+ var branchOwner: Owner? = nil
724
+ Owner.current?.onCleanup { branchOwner?.dispose() }
725
+ Effect {
726
+ let tpl = jsFlat((try? template()) ?? nil) as? TemplateRef
727
+ let ctx = (try? context?()) ?? nil
728
+ untrack {
729
+ branchOwner?.dispose()
730
+ branchOwner = nil
731
+ guard let tpl, let render = tpl._render else {
732
+ region.set([])
733
+ return
734
+ }
735
+ let owner = Owner(parent: nil)
736
+ branchOwner = owner
737
+ jsReport { try owner.run { try render(region, ctx) } }
738
+ }
739
+ }
740
+ }
741
+ }
742
+
743
+ /// Scopes `TemplateRef.current()` and `ViewContainerRef.current()` for custom structural directives (`*myDir`).
744
+ public enum StructuralDirective {
745
+ public static func scope<T>(_ region: Region, _ template: TemplateRef, _ body: () throws -> T) rethrows -> T {
746
+ let vcr = ViewContainerRef()
747
+ vcr._clear = { region.set([]) }
748
+ vcr._mount = { tpl, ctx in
749
+ let owner = Owner(parent: nil)
750
+ jsReport { try owner.run { try tpl._render?(region, ctx) } }
751
+ return EmbeddedViewRef(ctx) {
752
+ owner.dispose()
753
+ region.set([])
754
+ }
755
+ }
756
+ return try TemplateRef.with(template) {
757
+ try ViewContainerRef.with(vcr, body)
758
+ }
759
+ }
760
+ }
@@ -160,6 +160,10 @@ public class RxSubject<T>: RxObservable<T> {
160
160
  for observer in observers { observer.next(value) }
161
161
  }
162
162
 
163
+ public func complete() {
164
+ observers.removeAll()
165
+ }
166
+
163
167
  public func asObservable() -> RxObservable<T> { RxObservable { next in self.subscribe(next) } }
164
168
  }
165
169