Skip to content

Commit b081d35

Browse files
committed
Remove global subscription store in place of per-isolate stores.
1 parent 92a8c99 commit b081d35

File tree

6 files changed

+21
-39
lines changed

6 files changed

+21
-39
lines changed

src/main/scala/scala/reactive/EventSink.scala

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package scala.reactive
22

33

44

5-
import scala.collection._
65

76

87

@@ -12,27 +11,17 @@ import scala.collection._
1211
*/
1312
trait EventSink {
1413

15-
def registerEventSink() {
16-
Iso.selfIso.get match {
17-
case null =>
18-
EventSink.globalEventSinks += this
19-
case iso =>
20-
iso.eventSinks += this
21-
}
14+
def registerEventSink(canLeak: CanLeak) {
15+
canLeak.eventSinks += this
2216
}
2317

24-
def unregisterEventSink() {
25-
Iso.selfIso.get match {
26-
case null =>
27-
EventSink.globalEventSinks -= this
28-
case iso =>
29-
iso.eventSinks -= this
30-
}
18+
def unregisterEventSink(canLeak: CanLeak) {
19+
canLeak.eventSinks -= this
3120
}
3221

33-
def liftSubscription(s: Reactive.Subscription) = {
22+
def liftSubscription(s: Reactive.Subscription, canLeak: CanLeak) = {
3423
Reactive.Subscription {
35-
unregisterEventSink()
24+
unregisterEventSink(canLeak)
3625
s.unsubscribe()
3726
}
3827
}
@@ -42,7 +31,4 @@ trait EventSink {
4231

4332
object EventSink {
4433

45-
/** A set of global event sinks. */
46-
private[reactive] val globalEventSinks = mutable.Set[EventSink]()
47-
4834
}

src/main/scala/scala/reactive/Iso.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ trait Iso[@spec(Int, Long, Double) T] extends ReactRecord {
7171

7272
@volatile private[reactive] var eventSources: mutable.Set[EventSource] = _
7373

74-
@volatile private[reactive] var eventSinks: mutable.Set[EventSink] = _
75-
7674
@volatile private[reactive] var systemEmitter: Reactive.Emitter[SysEvent] = _
7775

7876
@volatile private[reactive] var failureEmitter:
7977
Reactive.Emitter[Throwable] = _
8078

81-
val implicits = Iso.Implicits
79+
val implicits = new Iso.Implicits
8280

8381
private def illegal() =
8482
throw new IllegalStateException("Only isolate systems can create isolates.")
@@ -91,7 +89,6 @@ trait Iso[@spec(Int, Long, Double) T] extends ReactRecord {
9189
case eq => eq.asInstanceOf[IsoFrame]
9290
}
9391
eventSources = mutable.Set[EventSource]()
94-
eventSinks = mutable.Set[EventSink]()
9592
systemEmitter = new Reactive.Emitter[SysEvent]
9693
failureEmitter = new Reactive.Emitter[Throwable]
9794

@@ -191,8 +188,8 @@ object Iso {
191188
*/
192189
def of[@specialized(Int, Long, Double) T]: Iso[T] = Iso.self[Iso[T]]
193190

194-
object Implicits {
195-
implicit def canLeak: CanLeak = CanLeak.isoCanLeak
191+
class Implicits {
192+
implicit val canLeak: CanLeak = CanLeak.newCanLeak
196193
}
197194

198195
}

src/main/scala/scala/reactive/Reactive.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ trait Reactive[@spec(Int, Long, Double) +T] {
8383
*/
8484
def onReaction(reactor: Reactor[T])(implicit canLeak: CanLeak):
8585
Reactive.Subscription = {
86-
val eventSink = new Reactor.EventSink(reactor)
86+
val eventSink = new Reactor.EventSink(reactor, canLeak)
8787
val subscription = observe(eventSink)
88-
eventSink.liftSubscription(subscription)
88+
eventSink.liftSubscription(subscription, canLeak)
8989
}
9090

9191
/** A shorthand for `onReaction` -- the specified functions are invoked

src/main/scala/scala/reactive/Reactor.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ trait Reactor[@spec(Int, Long, Double) -T] {
2828

2929
object Reactor {
3030

31-
class EventSink[@spec(Int, Long, Double) T](val underlying: Reactor[T])
31+
class EventSink[@spec(Int, Long, Double) T]
32+
(val underlying: Reactor[T], val canLeak: CanLeak)
3233
extends Reactor[T] with scala.reactive.EventSink {
3334
def init(dummy: EventSink[T]) {
34-
registerEventSink()
35+
registerEventSink(canLeak)
3536
}
3637

3738
init(this)
3839

3940
def react(value: T) = underlying.react(value)
4041

4142
def unreact() = {
42-
unregisterEventSink()
43+
unregisterEventSink(canLeak)
4344
underlying.unreact()
4445
}
4546
}

src/main/scala/scala/reactive/package.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scala
33

44

55
import scala.annotation.implicitNotFound
6+
import scala.collection._
67
import scala.reflect.ClassTag
78

89

@@ -248,11 +249,12 @@ package object reactive {
248249
"If you are sure you want to risk this, import " +
249250
"scala.reactive.Implicits.canLeak. " +
250251
"Otherwise, consider calling observe or foreach.")
251-
sealed trait CanLeak
252+
sealed trait CanLeak {
253+
val eventSinks = mutable.Set[EventSink]()
254+
}
252255

253256
object CanLeak {
254257
private[reactive] def newCanLeak: CanLeak = new CanLeak {}
255-
private[reactive] val isoCanLeak: CanLeak = new CanLeak {}
256258
}
257259

258260
/* system events */

src/test/scala/scala/reactive/test/signal/ReactiveGCSpec.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ class ReactiveGCSpec extends FlatSpec with ShouldMatchers {
5757

5858
emitter += 1
5959

60-
try {
61-
for (i <- 0 until num) signsOfLife(i) should equal (true)
62-
afterCheck(emitter) should equal (true)
63-
} finally {
64-
EventSink.globalEventSinks.clear()
65-
}
60+
for (i <- 0 until num) signsOfLife(i) should equal (true)
61+
afterCheck(emitter) should equal (true)
6662
}
6763

6864
it should "not GC 1 onX dependency" in {

0 commit comments

Comments
 (0)