-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.coffee
83 lines (65 loc) · 2.37 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
MeteorCursor = Object.getPrototypeOf(MongoInternals.defaultRemoteCollectionDriver().mongo.find()).constructor
originalObserveChanges = MeteorCursor::observeChanges
originalCount = MeteorCursor::count
# This is a PeerDB extension. It might not exist if the package is used without PeerDB.
# But we defined a week dependency on PeerDB so that it is loaded before this package
# to that PeerDB adds this extension before we get here.
originalExists = MeteorCursor::exists
MeteorCursor::_isReactive = ->
# By default we make all cursors reactive. But you can
# still disable that, the same as on the client.
@_cursorDescription.options.reactive ? true
MeteorCursor::_depend = (changers) ->
return unless Tracker.active
dependency = new Tracker.Dependency()
dependency.depend()
# On server side observe does not have _suppress_initial,
# so we are skipping initial documents manually.
initializing = true
callback = {}
for fnName in ['added', 'changed', 'removed', 'addedBefore', 'movedBefore'] when changers[fnName]
callback[fnName] = ->
dependency.changed() unless initializing
# observeChanges will stop() when this computation is invalidated.
@observeChanges callback, {nonMutatingCallbacks: true}
initializing = false
MeteorCursor::observeChanges = (callbacks, options = {}) ->
handle = originalObserveChanges.call @, callbacks, options
if Tracker.active and @_isReactive()
Tracker.onInvalidate =>
handle.stop()
handle
callbacksOrdered =
addedBefore: true
removed: true
changed: true
movedBefore: true
callbacksUnordered =
added: true
changed: true
removed: true
for method in ['forEach', 'map', 'fetch']
do (method) ->
originalMethod = MeteorCursor::[method]
MeteorCursor::[method] = (args...) ->
if @_isReactive()
{sort, ordered} = @_cursorDescription.options
if ordered?
callbacks = if ordered then callbacksOrdered else callbacksUnordered
else
callbacks = if !!sort then callbacksOrdered else callbacksUnordered
@_depend callbacks
originalMethod.apply @, args
MeteorCursor::count = (args...) ->
if @_isReactive()
@_depend
added: true
removed: true
originalCount.apply @, args
if originalExists
MeteorCursor::exists = (args...) ->
if @_isReactive()
@_depend
added: true
removed: true
originalExists.apply @, args