matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
32 lines • 1.02 kB
JavaScript
const DEFAULT_TIMEOUT_MS = 6000;
class PushCaptureListener {
duid;
parser;
name = 'PushCaptureListener';
requiresBody = false;
resolve;
promise;
constructor(duid, parser) {
this.duid = duid;
this.parser = parser;
this.promise = new Promise((res) => {
this.resolve = res;
});
}
async onMessage(message) {
if (message.duid !== this.duid || !this.resolve)
return;
const result = this.parser(message);
if (result !== undefined) {
this.resolve(result);
this.resolve = undefined;
}
}
}
export async function waitForPush(clientRouter, duid, parser, timeoutMs = DEFAULT_TIMEOUT_MS) {
const listener = new PushCaptureListener(duid, parser);
clientRouter.registerMessageListener(listener);
const timeout = new Promise((res) => setTimeout(() => res(undefined), timeoutMs));
return Promise.race([listener.promise, timeout]);
}
//# sourceMappingURL=waitForPush.js.map