0

I have a continuous parent signal that I derive various other signals from. Most of the subscribers are fine with the “continuous” semantics, meaning they do want to receive the last value upon subscribing. But with some subscribers I just want to receive the future values, no initial ones. Example:

import CwlSignal

let (input, signal) = Signal<Int>.create { s in s.continuous() }

input.send(value: 1)
input.send(value: 2)

let e1 = signal.subscribeValues { val in
    print("A: \(val)")
}

let e2 = signal.subscribeValues { val in
    print("B: \(val)")
}

input.send(value: 3)
input.close()

This prints:

A: 2
B: 2
A: 3
B: 3

Now I would like the second subscription not to receive the initial value, ie. the output should look like this:

A: 2
A: 3
B: 3

Is it possible to get that kind of behaviour without tweaking the parent signal?

zoul
  • 102,279
  • 44
  • 260
  • 354

2 Answers2

0

I've never used CwlSignal, but it looks like it has a skip operator like most reactive frameworks. Does this work?

let e2 = signal.skip(1).subscribeValues { val in
    print("B: \(val)")
}
jjoelson
  • 5,771
  • 5
  • 31
  • 51
  • Thank you! Unfortunately that would only work for signals that already received a value, for empty signals it would skip the first value received _after subscribing_. – zoul Jun 22 '17 at 05:43
  • Maybe you could provide an initial value when you call `continuous` to ensure you never have an empty signal? Otherwise, you might be out of luck. I haven't used CwlSignal, but Matt's introductory blog post makes it sound like it's a foundational feature of the framework that a signal can't be different things to different subscribers: the handling of old values must be defined in order to get a `SignalMulti`. And since operators have no way of knowing if incoming values are actually old buffered values, I'm not sure you can work around it. Anyway, good luck! – jjoelson Jun 22 '17 at 14:30
0

This can be solved using multicast signals, quote reply from the library author:

let (input, multicastSignal) = Signal<Int>.create { s in s.multicast() }
let continuousSignal = multicastSignal.continuous()

input.send(value: 1)
input.send(value: 2)

let e1 = continuousSignal.subscribeValues { val in
    print("A: \(val)")
}

let e2 = multicastSignal.subscribeValues { val in
    print("B: \(val)")
}

input.send(value: 3)
zoul
  • 102,279
  • 44
  • 260
  • 354