Skip to content

Commit

Permalink
Make sure to cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Feb 25, 2018
1 parent 480124e commit c8ba191
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions server.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Fiber = Npm.require 'fibers'

checkNames = (publish, collectionNames, computation, result) ->
getCollectionNames = (result) ->
if result and _.isArray result
resultNames = (cursor._getCollectionName() for cursor in result when _.isObject(cursor) and '_getCollectionName' of cursor)
else if result and _.isObject(result) and '_getCollectionName' of result
resultNames = [result._getCollectionName()]
else
resultNames = []

collectionNames[computation._id] = resultNames if computation
resultNames

for computationId, names of collectionNames when not computation or computationId isnt "#{computation._id}"
for collectionName in names when collectionName in resultNames
checkNames = (publish, allCollectionNames, id, collectionNames) ->
for computationId, names of allCollectionNames when computationId isnt id
for collectionName in names when collectionName in collectionNames
publish.error new Error "Multiple cursors for collection '#{collectionName}'"
return false

Expand Down Expand Up @@ -68,7 +69,7 @@ extendPublish (name, publishFunction, options) ->
oldDocuments = {}
documents = {}

collectionNames = {}
allCollectionNames = {}

publish._currentComputation = ->
if Tracker.active
Expand Down Expand Up @@ -167,7 +168,13 @@ extendPublish (name, publishFunction, options) ->
handle = Tracker.autorun (computation) ->
result = runFunc.call publish, computation

unless checkNames publish, collectionNames, computation, result
collectionNames = getCollectionNames result
allCollectionNames[computation._id] = collectionNames

computation.onInvalidate ->
delete allCollectionNames[computation._id]

unless checkNames publish, allCollectionNames, "#{computation._id}", collectionNames
computation.stop()
return

Expand All @@ -190,7 +197,9 @@ extendPublish (name, publishFunction, options) ->

result = publishFunction.apply publish, args

return unless checkNames publish, collectionNames, null, result
collectionNames = getCollectionNames result
allCollectionNames[''] = collectionNames
return unless checkNames publish, allCollectionNames, '', collectionNames

# Specially handle if computation has been returned.
if result instanceof Tracker.Computation
Expand Down

0 comments on commit c8ba191

Please sign in to comment.