From 17d026f8fdad678d3ffb080f7d709b50084d8524 Mon Sep 17 00:00:00 2001 From: zodern Date: Wed, 29 Nov 2023 10:59:44 -0600 Subject: [PATCH] Queue subscriptions --- .../ddp-client/client/client_convenience.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/ddp-client/client/client_convenience.js b/packages/ddp-client/client/client_convenience.js index c747ad9587..ba4f791241 100644 --- a/packages/ddp-client/client/client_convenience.js +++ b/packages/ddp-client/client/client_convenience.js @@ -214,6 +214,35 @@ function queueMethodInvoker(methodInvoker, wait) { }); } +/** + * Queue subscriptions in case they rely on previous method calls + */ +let queueSend = false; +let oldSubscribe = Meteor.connection.subscribe; +Meteor.connection.subscribe = function () { + queueSend = true; + try { + return oldSubscribe.apply(this, arguments); + } finally { + queueSend = false; + } +}; + +let oldSend = Meteor.connection._send; +Meteor.connection._send = function () { + if (!queueSend) { + return oldSend.apply(this, arguments); + } + + queueSend = false; + queueFunction((resolve) => { + try { + oldSend.apply(this, arguments); + } finally { + resolve(); + } + }); +}; // Proxy the public methods of Meteor.connection so they can // be called directly on Meteor.