mongodb-livedata-server 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/livedata_server.d.ts +4 -4
- package/dist/livedata_server.js +11 -11
- package/dist/meteor/binary-heap/max_heap.d.ts +31 -31
- package/dist/meteor/binary-heap/max_heap.js +186 -186
- package/dist/meteor/binary-heap/min_heap.d.ts +6 -6
- package/dist/meteor/binary-heap/min_heap.js +17 -17
- package/dist/meteor/binary-heap/min_max_heap.d.ts +11 -11
- package/dist/meteor/binary-heap/min_max_heap.js +48 -48
- package/dist/meteor/callback-hook/hook.d.ts +11 -11
- package/dist/meteor/callback-hook/hook.js +78 -78
- package/dist/meteor/ddp/crossbar.d.ts +15 -15
- package/dist/meteor/ddp/crossbar.js +136 -136
- package/dist/meteor/ddp/heartbeat.d.ts +19 -19
- package/dist/meteor/ddp/heartbeat.js +77 -77
- package/dist/meteor/ddp/livedata_server.d.ts +141 -142
- package/dist/meteor/ddp/livedata_server.js +403 -403
- package/dist/meteor/ddp/method-invocation.d.ts +35 -35
- package/dist/meteor/ddp/method-invocation.js +72 -72
- package/dist/meteor/ddp/random-stream.d.ts +8 -8
- package/dist/meteor/ddp/random-stream.js +100 -100
- package/dist/meteor/ddp/session-collection-view.d.ts +20 -20
- package/dist/meteor/ddp/session-collection-view.js +106 -106
- package/dist/meteor/ddp/session-document-view.d.ts +8 -8
- package/dist/meteor/ddp/session-document-view.js +82 -82
- package/dist/meteor/ddp/session.d.ts +75 -74
- package/dist/meteor/ddp/session.js +590 -589
- package/dist/meteor/ddp/stream_server.d.ts +20 -21
- package/dist/meteor/ddp/stream_server.js +181 -181
- package/dist/meteor/ddp/subscription.d.ts +94 -94
- package/dist/meteor/ddp/subscription.js +370 -370
- package/dist/meteor/ddp/utils.d.ts +8 -8
- package/dist/meteor/ddp/utils.js +104 -104
- package/dist/meteor/ddp/writefence.d.ts +20 -20
- package/dist/meteor/ddp/writefence.js +111 -111
- package/dist/meteor/diff-sequence/diff.d.ts +17 -17
- package/dist/meteor/diff-sequence/diff.js +257 -257
- package/dist/meteor/ejson/ejson.d.ts +82 -82
- package/dist/meteor/ejson/ejson.js +568 -569
- package/dist/meteor/ejson/stringify.d.ts +2 -2
- package/dist/meteor/ejson/stringify.js +119 -119
- package/dist/meteor/ejson/utils.d.ts +12 -12
- package/dist/meteor/ejson/utils.js +42 -42
- package/dist/meteor/mongo/caching_change_observer.d.ts +16 -16
- package/dist/meteor/mongo/caching_change_observer.js +63 -63
- package/dist/meteor/mongo/doc_fetcher.d.ts +7 -7
- package/dist/meteor/mongo/doc_fetcher.js +53 -53
- package/dist/meteor/mongo/geojson_utils.d.ts +3 -3
- package/dist/meteor/mongo/geojson_utils.js +40 -41
- package/dist/meteor/mongo/live_connection.d.ts +28 -28
- package/dist/meteor/mongo/live_connection.js +264 -264
- package/dist/meteor/mongo/live_cursor.d.ts +25 -25
- package/dist/meteor/mongo/live_cursor.js +60 -60
- package/dist/meteor/mongo/minimongo_common.d.ts +84 -84
- package/dist/meteor/mongo/minimongo_common.js +1998 -2002
- package/dist/meteor/mongo/minimongo_matcher.d.ts +23 -23
- package/dist/meteor/mongo/minimongo_matcher.js +283 -283
- package/dist/meteor/mongo/minimongo_sorter.d.ts +16 -16
- package/dist/meteor/mongo/minimongo_sorter.js +268 -268
- package/dist/meteor/mongo/observe_driver_utils.d.ts +9 -9
- package/dist/meteor/mongo/observe_driver_utils.js +72 -73
- package/dist/meteor/mongo/observe_multiplexer.d.ts +46 -46
- package/dist/meteor/mongo/observe_multiplexer.js +203 -203
- package/dist/meteor/mongo/oplog-observe-driver.d.ts +68 -68
- package/dist/meteor/mongo/oplog-observe-driver.js +918 -918
- package/dist/meteor/mongo/oplog_tailing.d.ts +35 -35
- package/dist/meteor/mongo/oplog_tailing.js +352 -352
- package/dist/meteor/mongo/oplog_v2_converter.d.ts +1 -1
- package/dist/meteor/mongo/oplog_v2_converter.js +125 -126
- package/dist/meteor/mongo/polling_observe_driver.d.ts +30 -30
- package/dist/meteor/mongo/polling_observe_driver.js +216 -221
- package/dist/meteor/mongo/synchronous-cursor.d.ts +17 -17
- package/dist/meteor/mongo/synchronous-cursor.js +261 -261
- package/dist/meteor/mongo/synchronous-queue.d.ts +13 -13
- package/dist/meteor/mongo/synchronous-queue.js +110 -110
- package/dist/meteor/ordered-dict/ordered_dict.d.ts +31 -31
- package/dist/meteor/ordered-dict/ordered_dict.js +198 -198
- package/dist/meteor/random/AbstractRandomGenerator.d.ts +42 -42
- package/dist/meteor/random/AbstractRandomGenerator.js +92 -92
- package/dist/meteor/random/AleaRandomGenerator.d.ts +13 -13
- package/dist/meteor/random/AleaRandomGenerator.js +90 -90
- package/dist/meteor/random/NodeRandomGenerator.d.ts +16 -16
- package/dist/meteor/random/NodeRandomGenerator.js +42 -42
- package/dist/meteor/random/createAleaGenerator.d.ts +2 -2
- package/dist/meteor/random/createAleaGenerator.js +32 -32
- package/dist/meteor/random/createRandom.d.ts +1 -1
- package/dist/meteor/random/createRandom.js +22 -22
- package/dist/meteor/random/main.d.ts +1 -1
- package/dist/meteor/random/main.js +12 -12
- package/dist/meteor/types.d.ts +1 -1
- package/dist/meteor/types.js +2 -2
- package/package.json +6 -5
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SynchronousCursor =
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
// only works
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
;
|
|
50
|
-
class SynchronousCursor {
|
|
51
|
-
constructor(_dbCursor, _cursorDescription, options) {
|
|
52
|
-
this._dbCursor = _dbCursor;
|
|
53
|
-
this._cursorDescription = _cursorDescription;
|
|
54
|
-
var self = this;
|
|
55
|
-
if (options.useTransform && _cursorDescription.options.transform) {
|
|
56
|
-
self._transform = wrapTransform(_cursorDescription.options.transform);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
self._transform = null;
|
|
60
|
-
}
|
|
61
|
-
self._visitedIds = new Set();
|
|
62
|
-
}
|
|
63
|
-
// Returns a Promise for the next object from the cursor, skipping those whose
|
|
64
|
-
// IDs we've already seen and replacing Mongo atoms with Meteor atoms.
|
|
65
|
-
async _nextObjectPromise() {
|
|
66
|
-
var self = this;
|
|
67
|
-
while (true) {
|
|
68
|
-
var doc = await this._dbCursor.next();
|
|
69
|
-
if (!doc)
|
|
70
|
-
return null;
|
|
71
|
-
if (!self._cursorDescription.options.tailable && doc.hasOwnProperty('_id')) {
|
|
72
|
-
// Did Mongo give us duplicate documents in the same cursor? If so,
|
|
73
|
-
// ignore this one. (Do this before the transform, since transform might
|
|
74
|
-
// return some unrelated value.) We don't do this for tailable cursors,
|
|
75
|
-
// because we want to maintain O(1) memory usage. And if there isn't _id
|
|
76
|
-
// for some reason (maybe it's the oplog), then we don't do this either.
|
|
77
|
-
// (Be careful to do this for falsey but existing _id, though.)
|
|
78
|
-
if (self._visitedIds.has(doc._id))
|
|
79
|
-
continue;
|
|
80
|
-
self._visitedIds.add(doc._id);
|
|
81
|
-
}
|
|
82
|
-
if (self._transform)
|
|
83
|
-
doc = self._transform(doc);
|
|
84
|
-
return doc;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// Returns a promise which is resolved with the next object (like with
|
|
88
|
-
// _nextObjectPromise) or rejected if the cursor doesn't return within
|
|
89
|
-
// timeoutMS ms.
|
|
90
|
-
async _nextObjectPromiseWithTimeout(timeoutMS) {
|
|
91
|
-
if (!timeoutMS) {
|
|
92
|
-
return this._nextObjectPromise();
|
|
93
|
-
}
|
|
94
|
-
const nextObjectPromise = this._nextObjectPromise();
|
|
95
|
-
const timeoutErr = new Error('Client-side timeout waiting for next object');
|
|
96
|
-
const timeoutPromise = new Promise((_resolve, reject) => {
|
|
97
|
-
setTimeout(() => {
|
|
98
|
-
reject(timeoutErr);
|
|
99
|
-
}, timeoutMS);
|
|
100
|
-
});
|
|
101
|
-
return Promise.race([nextObjectPromise, timeoutPromise])
|
|
102
|
-
.catch((err) => {
|
|
103
|
-
if (err === timeoutErr) {
|
|
104
|
-
this._dbCursor.close();
|
|
105
|
-
}
|
|
106
|
-
throw err;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
close() {
|
|
110
|
-
this._dbCursor.close();
|
|
111
|
-
}
|
|
112
|
-
async forEach(callback, thisArg) {
|
|
113
|
-
var self = this;
|
|
114
|
-
// Get back to the beginning.
|
|
115
|
-
self._rewind();
|
|
116
|
-
// We implement the loop ourself instead of using self._dbCursor.each,
|
|
117
|
-
// because "each" will call its callback outside of a fiber which makes it
|
|
118
|
-
// much more complex to make this function synchronous.
|
|
119
|
-
var index = 0;
|
|
120
|
-
while (true) {
|
|
121
|
-
var doc = await self._nextObjectPromise();
|
|
122
|
-
if (!doc)
|
|
123
|
-
return;
|
|
124
|
-
callback.call(thisArg, doc, index++, self);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
_rewind() {
|
|
128
|
-
var self = this;
|
|
129
|
-
// known to be synchronous
|
|
130
|
-
self._dbCursor.rewind();
|
|
131
|
-
self._visitedIds = new Set();
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
exports.SynchronousCursor = SynchronousCursor;
|
|
135
|
-
// Wrap a transform function to return objects that have the _id field
|
|
136
|
-
// of the untransformed document. This ensures that subsystems such as
|
|
137
|
-
// the observe-sequence package that call `observe` can keep track of
|
|
138
|
-
// the documents identities.
|
|
139
|
-
//
|
|
140
|
-
// - Require that it returns objects
|
|
141
|
-
// - If the return value has an _id field, verify that it matches the
|
|
142
|
-
// original _id field
|
|
143
|
-
// - If the return value doesn't have an _id field, add it back.
|
|
144
|
-
function wrapTransform(transform) {
|
|
145
|
-
if (!transform)
|
|
146
|
-
return null;
|
|
147
|
-
// No need to doubly-wrap transforms.
|
|
148
|
-
if (transform.__wrappedTransform__)
|
|
149
|
-
return transform;
|
|
150
|
-
const wrapped = doc => {
|
|
151
|
-
if (!doc.hasOwnProperty('_id')) {
|
|
152
|
-
// XXX do we ever have a transform on the oplog's collection? because that
|
|
153
|
-
// collection has no _id.
|
|
154
|
-
throw new Error('can only transform documents with _id');
|
|
155
|
-
}
|
|
156
|
-
const id = doc._id;
|
|
157
|
-
const transformed = transform(doc);
|
|
158
|
-
if (transformed.hasOwnProperty('_id')) {
|
|
159
|
-
if (!(0, ejson_1.equals)(transformed._id, id)) {
|
|
160
|
-
throw new Error('transformed document can\'t have different _id');
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
transformed._id = id;
|
|
165
|
-
}
|
|
166
|
-
return transformed;
|
|
167
|
-
};
|
|
168
|
-
wrapped.__wrappedTransform__ = true;
|
|
169
|
-
return wrapped;
|
|
170
|
-
}
|
|
171
|
-
;
|
|
172
|
-
/*
|
|
173
|
-
forEach(callback, thisArg) {
|
|
174
|
-
var self = this;
|
|
175
|
-
|
|
176
|
-
// Get back to the beginning.
|
|
177
|
-
self._rewind();
|
|
178
|
-
|
|
179
|
-
// We implement the loop ourself instead of using self._dbCursor.each,
|
|
180
|
-
// because "each" will call its callback outside of a fiber which makes it
|
|
181
|
-
// much more complex to make this function synchronous.
|
|
182
|
-
var index = 0;
|
|
183
|
-
while (true) {
|
|
184
|
-
var doc = self._nextObject();
|
|
185
|
-
if (!doc) return;
|
|
186
|
-
callback.call(thisArg, doc, index++, self._selfForIteration);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// XXX Allow overlapping callback executions if callback yields.
|
|
191
|
-
map(callback, thisArg) {
|
|
192
|
-
var self = this;
|
|
193
|
-
var res = [];
|
|
194
|
-
self.forEach(function (doc, index) {
|
|
195
|
-
res.push(callback.call(thisArg, doc, index, self._selfForIteration));
|
|
196
|
-
});
|
|
197
|
-
return res;
|
|
198
|
-
},
|
|
199
|
-
|
|
200
|
-
_rewind() {
|
|
201
|
-
var self = this;
|
|
202
|
-
|
|
203
|
-
// known to be synchronous
|
|
204
|
-
self._dbCursor.rewind();
|
|
205
|
-
|
|
206
|
-
self._visitedIds = new LocalCollection._IdMap;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Mostly usable for tailable cursors.
|
|
210
|
-
close() {
|
|
211
|
-
var self = this;
|
|
212
|
-
|
|
213
|
-
self._dbCursor.close();
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
fetch() {
|
|
217
|
-
var self = this;
|
|
218
|
-
return self.map(_.identity);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// This method is NOT wrapped in Cursor.
|
|
222
|
-
getRawObjects(ordered) {
|
|
223
|
-
var self = this;
|
|
224
|
-
if (ordered) {
|
|
225
|
-
return self.fetch();
|
|
226
|
-
} else {
|
|
227
|
-
var results = new LocalCollection._IdMap;
|
|
228
|
-
self.forEach(function (doc) {
|
|
229
|
-
results.set(doc._id, doc);
|
|
230
|
-
});
|
|
231
|
-
return results;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
[Symbol.iterator]() {
|
|
236
|
-
var self = this;
|
|
237
|
-
|
|
238
|
-
// Get back to the beginning.
|
|
239
|
-
self._rewind();
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
next() {
|
|
243
|
-
const doc = self._nextObject();
|
|
244
|
-
return doc ? {
|
|
245
|
-
value: doc
|
|
246
|
-
} : {
|
|
247
|
-
done: true
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
[Symbol.asyncIterator]() {
|
|
254
|
-
const syncResult = this[Symbol.iterator]();
|
|
255
|
-
return {
|
|
256
|
-
async next() {
|
|
257
|
-
return Promise.resolve(syncResult.next());
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SynchronousCursor = void 0;
|
|
4
|
+
exports._createSynchronousCursor = _createSynchronousCursor;
|
|
5
|
+
const ejson_1 = require("../ejson/ejson");
|
|
6
|
+
const oplog_tailing_1 = require("./oplog_tailing");
|
|
7
|
+
function _createSynchronousCursor(db, cursorDescription, options) {
|
|
8
|
+
const { useTransform } = options || {};
|
|
9
|
+
var collection = db.collection(cursorDescription.collectionName);
|
|
10
|
+
var cursorOptions = cursorDescription.options;
|
|
11
|
+
var mongoOptions = {
|
|
12
|
+
sort: cursorOptions.sort,
|
|
13
|
+
limit: cursorOptions.limit,
|
|
14
|
+
skip: cursorOptions.skip,
|
|
15
|
+
projection: cursorOptions.projection,
|
|
16
|
+
readPreference: cursorOptions.readPreference,
|
|
17
|
+
numberOfRetries: undefined
|
|
18
|
+
};
|
|
19
|
+
// Do we want a tailable cursor (which only works on capped collections)?
|
|
20
|
+
if (cursorOptions.tailable) {
|
|
21
|
+
mongoOptions.numberOfRetries = -1;
|
|
22
|
+
}
|
|
23
|
+
var dbCursor = collection.find(cursorDescription.selector, mongoOptions);
|
|
24
|
+
// Do we want a tailable cursor (which only works on capped collections)?
|
|
25
|
+
if (cursorOptions.tailable) {
|
|
26
|
+
// We want a tailable cursor...
|
|
27
|
+
dbCursor.addCursorFlag("tailable", true);
|
|
28
|
+
// ... and for the server to wait a bit if any getMore has no data (rather
|
|
29
|
+
// than making us put the relevant sleeps in the client)...
|
|
30
|
+
dbCursor.addCursorFlag("awaitData", true);
|
|
31
|
+
// And if this is on the oplog collection and the cursor specifies a 'ts',
|
|
32
|
+
// then set the undocumented oplog replay flag, which does a special scan to
|
|
33
|
+
// find the first document (instead of creating an index on ts). This is a
|
|
34
|
+
// very hard-coded Mongo flag which only works on the oplog collection and
|
|
35
|
+
// only works with the ts field.
|
|
36
|
+
if (cursorDescription.collectionName === oplog_tailing_1.OPLOG_COLLECTION &&
|
|
37
|
+
cursorDescription.selector.ts) {
|
|
38
|
+
dbCursor.addCursorFlag("oplogReplay", true);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (typeof cursorOptions.maxTimeMs !== 'undefined') {
|
|
42
|
+
dbCursor = dbCursor.maxTimeMS(cursorOptions.maxTimeMs);
|
|
43
|
+
}
|
|
44
|
+
if (typeof cursorOptions.hint !== 'undefined') {
|
|
45
|
+
dbCursor = dbCursor.hint(cursorOptions.hint);
|
|
46
|
+
}
|
|
47
|
+
return new SynchronousCursor(dbCursor, cursorDescription, { useTransform });
|
|
48
|
+
}
|
|
49
|
+
;
|
|
50
|
+
class SynchronousCursor {
|
|
51
|
+
constructor(_dbCursor, _cursorDescription, options) {
|
|
52
|
+
this._dbCursor = _dbCursor;
|
|
53
|
+
this._cursorDescription = _cursorDescription;
|
|
54
|
+
var self = this;
|
|
55
|
+
if (options.useTransform && _cursorDescription.options.transform) {
|
|
56
|
+
self._transform = wrapTransform(_cursorDescription.options.transform);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
self._transform = null;
|
|
60
|
+
}
|
|
61
|
+
self._visitedIds = new Set();
|
|
62
|
+
}
|
|
63
|
+
// Returns a Promise for the next object from the cursor, skipping those whose
|
|
64
|
+
// IDs we've already seen and replacing Mongo atoms with Meteor atoms.
|
|
65
|
+
async _nextObjectPromise() {
|
|
66
|
+
var self = this;
|
|
67
|
+
while (true) {
|
|
68
|
+
var doc = await this._dbCursor.next();
|
|
69
|
+
if (!doc)
|
|
70
|
+
return null;
|
|
71
|
+
if (!self._cursorDescription.options.tailable && doc.hasOwnProperty('_id')) {
|
|
72
|
+
// Did Mongo give us duplicate documents in the same cursor? If so,
|
|
73
|
+
// ignore this one. (Do this before the transform, since transform might
|
|
74
|
+
// return some unrelated value.) We don't do this for tailable cursors,
|
|
75
|
+
// because we want to maintain O(1) memory usage. And if there isn't _id
|
|
76
|
+
// for some reason (maybe it's the oplog), then we don't do this either.
|
|
77
|
+
// (Be careful to do this for falsey but existing _id, though.)
|
|
78
|
+
if (self._visitedIds.has(doc._id))
|
|
79
|
+
continue;
|
|
80
|
+
self._visitedIds.add(doc._id);
|
|
81
|
+
}
|
|
82
|
+
if (self._transform)
|
|
83
|
+
doc = self._transform(doc);
|
|
84
|
+
return doc;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Returns a promise which is resolved with the next object (like with
|
|
88
|
+
// _nextObjectPromise) or rejected if the cursor doesn't return within
|
|
89
|
+
// timeoutMS ms.
|
|
90
|
+
async _nextObjectPromiseWithTimeout(timeoutMS) {
|
|
91
|
+
if (!timeoutMS) {
|
|
92
|
+
return this._nextObjectPromise();
|
|
93
|
+
}
|
|
94
|
+
const nextObjectPromise = this._nextObjectPromise();
|
|
95
|
+
const timeoutErr = new Error('Client-side timeout waiting for next object');
|
|
96
|
+
const timeoutPromise = new Promise((_resolve, reject) => {
|
|
97
|
+
setTimeout(() => {
|
|
98
|
+
reject(timeoutErr);
|
|
99
|
+
}, timeoutMS);
|
|
100
|
+
});
|
|
101
|
+
return Promise.race([nextObjectPromise, timeoutPromise])
|
|
102
|
+
.catch((err) => {
|
|
103
|
+
if (err === timeoutErr) {
|
|
104
|
+
this._dbCursor.close();
|
|
105
|
+
}
|
|
106
|
+
throw err;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
close() {
|
|
110
|
+
this._dbCursor.close();
|
|
111
|
+
}
|
|
112
|
+
async forEach(callback, thisArg) {
|
|
113
|
+
var self = this;
|
|
114
|
+
// Get back to the beginning.
|
|
115
|
+
self._rewind();
|
|
116
|
+
// We implement the loop ourself instead of using self._dbCursor.each,
|
|
117
|
+
// because "each" will call its callback outside of a fiber which makes it
|
|
118
|
+
// much more complex to make this function synchronous.
|
|
119
|
+
var index = 0;
|
|
120
|
+
while (true) {
|
|
121
|
+
var doc = await self._nextObjectPromise();
|
|
122
|
+
if (!doc)
|
|
123
|
+
return;
|
|
124
|
+
callback.call(thisArg, doc, index++, self);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
_rewind() {
|
|
128
|
+
var self = this;
|
|
129
|
+
// known to be synchronous
|
|
130
|
+
self._dbCursor.rewind();
|
|
131
|
+
self._visitedIds = new Set();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.SynchronousCursor = SynchronousCursor;
|
|
135
|
+
// Wrap a transform function to return objects that have the _id field
|
|
136
|
+
// of the untransformed document. This ensures that subsystems such as
|
|
137
|
+
// the observe-sequence package that call `observe` can keep track of
|
|
138
|
+
// the documents identities.
|
|
139
|
+
//
|
|
140
|
+
// - Require that it returns objects
|
|
141
|
+
// - If the return value has an _id field, verify that it matches the
|
|
142
|
+
// original _id field
|
|
143
|
+
// - If the return value doesn't have an _id field, add it back.
|
|
144
|
+
function wrapTransform(transform) {
|
|
145
|
+
if (!transform)
|
|
146
|
+
return null;
|
|
147
|
+
// No need to doubly-wrap transforms.
|
|
148
|
+
if (transform.__wrappedTransform__)
|
|
149
|
+
return transform;
|
|
150
|
+
const wrapped = doc => {
|
|
151
|
+
if (!doc.hasOwnProperty('_id')) {
|
|
152
|
+
// XXX do we ever have a transform on the oplog's collection? because that
|
|
153
|
+
// collection has no _id.
|
|
154
|
+
throw new Error('can only transform documents with _id');
|
|
155
|
+
}
|
|
156
|
+
const id = doc._id;
|
|
157
|
+
const transformed = transform(doc);
|
|
158
|
+
if (transformed.hasOwnProperty('_id')) {
|
|
159
|
+
if (!(0, ejson_1.equals)(transformed._id, id)) {
|
|
160
|
+
throw new Error('transformed document can\'t have different _id');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
transformed._id = id;
|
|
165
|
+
}
|
|
166
|
+
return transformed;
|
|
167
|
+
};
|
|
168
|
+
wrapped.__wrappedTransform__ = true;
|
|
169
|
+
return wrapped;
|
|
170
|
+
}
|
|
171
|
+
;
|
|
172
|
+
/*
|
|
173
|
+
forEach(callback, thisArg) {
|
|
174
|
+
var self = this;
|
|
175
|
+
|
|
176
|
+
// Get back to the beginning.
|
|
177
|
+
self._rewind();
|
|
178
|
+
|
|
179
|
+
// We implement the loop ourself instead of using self._dbCursor.each,
|
|
180
|
+
// because "each" will call its callback outside of a fiber which makes it
|
|
181
|
+
// much more complex to make this function synchronous.
|
|
182
|
+
var index = 0;
|
|
183
|
+
while (true) {
|
|
184
|
+
var doc = self._nextObject();
|
|
185
|
+
if (!doc) return;
|
|
186
|
+
callback.call(thisArg, doc, index++, self._selfForIteration);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// XXX Allow overlapping callback executions if callback yields.
|
|
191
|
+
map(callback, thisArg) {
|
|
192
|
+
var self = this;
|
|
193
|
+
var res = [];
|
|
194
|
+
self.forEach(function (doc, index) {
|
|
195
|
+
res.push(callback.call(thisArg, doc, index, self._selfForIteration));
|
|
196
|
+
});
|
|
197
|
+
return res;
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
_rewind() {
|
|
201
|
+
var self = this;
|
|
202
|
+
|
|
203
|
+
// known to be synchronous
|
|
204
|
+
self._dbCursor.rewind();
|
|
205
|
+
|
|
206
|
+
self._visitedIds = new LocalCollection._IdMap;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Mostly usable for tailable cursors.
|
|
210
|
+
close() {
|
|
211
|
+
var self = this;
|
|
212
|
+
|
|
213
|
+
self._dbCursor.close();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
fetch() {
|
|
217
|
+
var self = this;
|
|
218
|
+
return self.map(_.identity);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// This method is NOT wrapped in Cursor.
|
|
222
|
+
getRawObjects(ordered) {
|
|
223
|
+
var self = this;
|
|
224
|
+
if (ordered) {
|
|
225
|
+
return self.fetch();
|
|
226
|
+
} else {
|
|
227
|
+
var results = new LocalCollection._IdMap;
|
|
228
|
+
self.forEach(function (doc) {
|
|
229
|
+
results.set(doc._id, doc);
|
|
230
|
+
});
|
|
231
|
+
return results;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
[Symbol.iterator]() {
|
|
236
|
+
var self = this;
|
|
237
|
+
|
|
238
|
+
// Get back to the beginning.
|
|
239
|
+
self._rewind();
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
next() {
|
|
243
|
+
const doc = self._nextObject();
|
|
244
|
+
return doc ? {
|
|
245
|
+
value: doc
|
|
246
|
+
} : {
|
|
247
|
+
done: true
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
[Symbol.asyncIterator]() {
|
|
254
|
+
const syncResult = this[Symbol.iterator]();
|
|
255
|
+
return {
|
|
256
|
+
async next() {
|
|
257
|
+
return Promise.resolve(syncResult.next());
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
*/
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { AsyncFunction } from "../types";
|
|
2
|
-
export declare class _SynchronousQueue {
|
|
3
|
-
private _taskHandles;
|
|
4
|
-
private _runningOrRunScheduled;
|
|
5
|
-
private _draining;
|
|
6
|
-
constructor();
|
|
7
|
-
runTask(task: AsyncFunction): Promise<unknown>;
|
|
8
|
-
queueTask(task: AsyncFunction): void;
|
|
9
|
-
flush(): Promise<void>;
|
|
10
|
-
drain(): Promise<void>;
|
|
11
|
-
_scheduleRun(): void;
|
|
12
|
-
_run(): Promise<void>;
|
|
13
|
-
}
|
|
1
|
+
import { AsyncFunction } from "../types";
|
|
2
|
+
export declare class _SynchronousQueue {
|
|
3
|
+
private _taskHandles;
|
|
4
|
+
private _runningOrRunScheduled;
|
|
5
|
+
private _draining;
|
|
6
|
+
constructor();
|
|
7
|
+
runTask(task: AsyncFunction): Promise<unknown>;
|
|
8
|
+
queueTask(task: AsyncFunction): void;
|
|
9
|
+
flush(): Promise<void>;
|
|
10
|
+
drain(): Promise<void>;
|
|
11
|
+
_scheduleRun(): void;
|
|
12
|
+
_run(): Promise<void>;
|
|
13
|
+
}
|