From 175c212290565a03c6f7a2fcfcc38635e9db1418 Mon Sep 17 00:00:00 2001 From: Edimar Cardoso Date: Thu, 26 Jan 2023 10:05:44 -0300 Subject: [PATCH] Refectory mongo packages - Using isomorphy async methods with the suffix Async; - Changing tests to use async methods on client and server - Fix tests --- packages/ddp-client/common/livedata_connection.js | 15 ++++++++------- .../ddp-client/test/livedata_connection_tests.js | 2 +- packages/mongo/observe_changes_tests.js | 2 +- packages/mongo/polling_observe_driver.js | 2 +- packages/mongo/upsert_compatibility_test.js | 4 ++-- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/ddp-client/common/livedata_connection.js b/packages/ddp-client/common/livedata_connection.js index 0e77b4e2fb..7b4ded20e6 100644 --- a/packages/ddp-client/common/livedata_connection.js +++ b/packages/ddp-client/common/livedata_connection.js @@ -311,9 +311,9 @@ export class Connection { const queued = self._updatesForUnknownStores[name]; if (Array.isArray(queued)) { await store.beginUpdate(queued.length, false); - queued.forEach(msg => { - store.update(msg); - }); + for (const msg of queued) { + await store.update(msg); + } await store.endUpdate(); delete self._updatesForUnknownStores[name]; } @@ -600,7 +600,7 @@ export class Connection { * @param {Boolean} options.returnStubValue (Client only) If true then in cases where we would have otherwise discarded the stub's return value and returned undefined, instead we go ahead and return it. Specifically, this is any time other than when (a) we are already inside a stub or (b) we are in Node and no callback was provided. Currently we require this flag to be explicitly passed to reduce the likelihood that stub return values will be confused with server return values; we may improve this in future. * @param {Function} [asyncCallback] Optional callback; same semantics as in [`Meteor.call`](#meteor_call). */ - apply(name, args, options, callback) { + async apply(name, args, options, callback) { const { stubInvocation, invocation, ...stubOptions } = this._stubCall(name, EJSON.clone(args)); if (stubOptions.hasStub) { @@ -610,7 +610,7 @@ export class Connection { isFromCallAsync: stubOptions.isFromCallAsync, }) ) { - this._saveOriginals(); + await this._saveOriginals(); } try { stubOptions.stubReturnValue = DDP._CurrentMethodInvocation @@ -1171,8 +1171,9 @@ export class Connection { if (! self._waitingForQuiescence()) { if (self._resetStores) { Object.values(self._stores).forEach((store) => { - store.beginUpdate(0, true); - store.endUpdate(); + store.beginUpdate(0, true).finally(async () => { + await store.endUpdate(); + }); }); self._resetStores = false; } diff --git a/packages/ddp-client/test/livedata_connection_tests.js b/packages/ddp-client/test/livedata_connection_tests.js index 59f068ae24..04559be9c6 100644 --- a/packages/ddp-client/test/livedata_connection_tests.js +++ b/packages/ddp-client/test/livedata_connection_tests.js @@ -2478,7 +2478,7 @@ if (Meteor.isClient) { // Buffer should already be flushed because of a non-update message. // And after a flush we really want subscription field to be 112. - conn._flushBufferedWrites(); + await conn._flushBufferedWrites(); test.equal((await coll.findOneAsync('aaa')).method, 222); test.equal((await coll.findOneAsync('aaa')).subscription, 112); }); diff --git a/packages/mongo/observe_changes_tests.js b/packages/mongo/observe_changes_tests.js index 2d2f034bf8..574d20b25d 100644 --- a/packages/mongo/observe_changes_tests.js +++ b/packages/mongo/observe_changes_tests.js @@ -302,7 +302,7 @@ if (Meteor.isServer) { // Tailable observe shouldn't show things that are in the initial // contents. - self.insertAsync({x: 1}); + await self.insertAsync({x: 1}); // Wait for one added call before going to the next test function. self.expects.push(expect()); diff --git a/packages/mongo/polling_observe_driver.js b/packages/mongo/polling_observe_driver.js index c9cee7b350..839e96d679 100644 --- a/packages/mongo/polling_observe_driver.js +++ b/packages/mongo/polling_observe_driver.js @@ -202,7 +202,7 @@ _.extend(PollingObserveDriver.prototype, { // round, mark all the writes which existed before this call as // commmitted. (If new writes have shown up in the meantime, there'll // already be another _pollMongo task scheduled.) - self._multiplexer.onFlush(function () { + await self._multiplexer.onFlush(function () { _.each(writesForCycle, function (w) { w.committed(); }); diff --git a/packages/mongo/upsert_compatibility_test.js b/packages/mongo/upsert_compatibility_test.js index dc0698bba1..4b3ba448d9 100644 --- a/packages/mongo/upsert_compatibility_test.js +++ b/packages/mongo/upsert_compatibility_test.js @@ -2,7 +2,7 @@ Tinytest.addAsync('mongo livedata - native upsert - id type MONGO with MODIFIERS var collName = Random.id(); var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); - coll.insertAsync({foo: 1}); + await coll.insertAsync({foo: 1}); var result = await coll.upsertAsync({foo: 1}, {$set: {foo:2}}); var updated = await coll.findOneAsync({foo: 2}); @@ -36,7 +36,7 @@ Tinytest.addAsync('mongo livedata - native upsert - id type MONGO PLAIN OBJECT u var collName = Random.id(); var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); - coll.insertAsync({foo: 1, baz: 42}); + await coll.insertAsync({foo: 1, baz: 42}); var result = await coll.upsertAsync({foo: 1}, {bar:2}); var updated = await coll.findOneAsync({bar: 2});