Skip to content

Commit 69f756a

Browse files
committed
Properly create reactive cursor.
We should be patching SynchronousCursor's forEach and count only, but from SynchronousCursor one cannot access observeChanges. So we have to patch Cursor itself.
1 parent ef62d11 commit 69f756a

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

package.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ Package.onUse(function (api) {
2020
'peerlibrary:server-autorun@0.2.2'
2121
], 'server');
2222

23+
// Package can be used without PeerDB. But if PeerDB is available, make
24+
// sure it is loaded before this package so that PeerDB adds "exists"
25+
// to the cursor before we make it reactive.
26+
api.use([
27+
'peerlibrary:peerdb@0.19.1'
28+
], 'server', {'weak': true});
29+
2330
api.addFiles([
2431
'server.coffee'
2532
], 'server');

server.coffee

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
MeteorCursor = Object.getPrototypeOf(MongoInternals.defaultRemoteCollectionDriver().mongo.find()).constructor
22

33
originalObserveChanges = MeteorCursor::observeChanges
4-
originalForEach = MeteorCursor::forEach
54
originalCount = MeteorCursor::count
65

6+
# This is a PeerDB extension. It might not exist if the package is used without PeerDB.
7+
# But we defined a week dependency on PeerDB so that it is loaded before this package
8+
# to that PeerDB adds this extension before we get here.
9+
originalExists = MeteorCursor::exists
10+
711
MeteorCursor::_isReactive = ->
812
# By default we make all cursors reactive. But you can
913
# still disable that, the same as on the client.
@@ -37,20 +41,32 @@ MeteorCursor::observeChanges = (options) ->
3741
handle.stop()
3842
handle
3943

40-
MeteorCursor::forEach = (args...) ->
41-
if @_isReactive()
42-
@_depend
43-
addedBefore: true
44-
removed: true
45-
changed: true
46-
movedBefore: true
44+
for method in ['forEach', 'map', 'fetch']
45+
do (method) ->
46+
originalMethod = MeteorCursor::[method]
47+
MeteorCursor::[method] = (args...) ->
48+
if @_isReactive()
49+
@_depend
50+
addedBefore: true
51+
removed: true
52+
changed: true
53+
movedBefore: true
4754

48-
originalForEach.apply @, args
55+
originalMethod.apply @, args
4956

5057
MeteorCursor::count = (args...) ->
5158
if @_isReactive()
5259
@_depend
5360
added: true
5461
removed: true
5562

56-
originalCount.apply @, args
63+
originalCount.apply @, args
64+
65+
if originalExists
66+
MeteorCursor::exists = (args...) ->
67+
if @_isReactive()
68+
@_depend
69+
added: true
70+
removed: true
71+
72+
originalExists.apply @, args

0 commit comments

Comments
 (0)