From acdf6b23cc99103cf8ff41de51205b4273592762 Mon Sep 17 00:00:00 2001 From: Srdan Rasic Date: Sat, 27 Apr 2019 11:13:35 +0200 Subject: [PATCH] Add deferring initializer. --- Sources/Signal.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/Signal.swift b/Sources/Signal.swift index 33a0ec4..c50f75e 100644 --- a/Sources/Signal.swift +++ b/Sources/Signal.swift @@ -113,6 +113,16 @@ extension Signal { self = Signal(evaluating: { result }) } + /// Defer the signal creation until an observer stars observing it. + /// A new signal is created for each observer. + /// + /// - Parameter makeSignal: A closure to creates the signal. + public init(deferring makeSignal: @escaping () -> Other) where Other.Element == Element, Other.Error == Error { + self.init { observer in + return makeSignal().observe(with: observer) + } + } + /// Create a signal by evaluating a closure that returns result, propagating the success element as a /// next event and completing immediately, or propagating the failure error as a failed event. ///