Skip to content

Commit

Permalink
Track computations from autoruns and ensure they're all stopped on pu…
Browse files Browse the repository at this point in the history
…blish stop
  • Loading branch information
etyp committed Sep 7, 2018
1 parent a636f0f commit aff9063
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ extendPublish (name, publishFunction, options) ->
return

handles = []
trackedComputations = []
# This autorun is nothing special, just that it makes sure handles are stopped when publish stops,
# and that you can return cursors from the function which would be automatically published.
publish.autorun = (runFunc) ->
handle = Tracker.autorun (computation) ->
trackedComputations.push computation
result = runFunc.call publish, computation

collectionNames = getCollectionNames result
Expand Down Expand Up @@ -194,6 +196,12 @@ extendPublish (name, publishFunction, options) ->
while handles.length
handle = handles.shift()
handle?.stop()
# Stop computations in case a subscription is stopped before ready on the client and
# the publish is using meteorhacks:unblock's "this.unblock()". Without this, observers
# created from cursors inside the autorun will exist forever.
while trackedComputations.length
computation = trackedComputations.shift()
computation?.stop()

result = publishFunction.apply publish, args

Expand Down

0 comments on commit aff9063

Please sign in to comment.