|
1 | 1 | MeteorCursor = Object.getPrototypeOf(MongoInternals.defaultRemoteCollectionDriver().mongo.find()).constructor |
2 | 2 |
|
3 | 3 | originalObserveChanges = MeteorCursor::observeChanges |
4 | | -originalForEach = MeteorCursor::forEach |
5 | 4 | originalCount = MeteorCursor::count |
6 | 5 |
|
| 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 | + |
7 | 11 | MeteorCursor::_isReactive = -> |
8 | 12 | # By default we make all cursors reactive. But you can |
9 | 13 | # still disable that, the same as on the client. |
@@ -37,20 +41,32 @@ MeteorCursor::observeChanges = (options) -> |
37 | 41 | handle.stop() |
38 | 42 | handle |
39 | 43 |
|
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 |
47 | 54 |
|
48 | | - originalForEach.apply @, args |
| 55 | + originalMethod.apply @, args |
49 | 56 |
|
50 | 57 | MeteorCursor::count = (args...) -> |
51 | 58 | if @_isReactive() |
52 | 59 | @_depend |
53 | 60 | added: true |
54 | 61 | removed: true |
55 | 62 |
|
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