From fe3d8794f0de802368ace89fc7706c4dd5410512 Mon Sep 17 00:00:00 2001 From: yashim-deriv Date: Fri, 9 Feb 2024 16:34:31 +0800 Subject: [PATCH] feat: add request/subscribe blocking feature (#219) --- src/deriv_api/DerivAPIBasic.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/deriv_api/DerivAPIBasic.js b/src/deriv_api/DerivAPIBasic.js index 3171623..c7f2e9d 100644 --- a/src/deriv_api/DerivAPIBasic.js +++ b/src/deriv_api/DerivAPIBasic.js @@ -76,6 +76,7 @@ export default class DerivAPIBasic extends DerivAPICalls { this.subscription_manager = new SubscriptionManager(this); this.reconnect_timeout = false; this.keep_alive_interval = false; + this.is_request_blocked = false; if (storage) { this.storage = new Cache(this, storage); @@ -87,6 +88,10 @@ export default class DerivAPIBasic extends DerivAPICalls { this.connectionHandlers(); } + blockRequest(value) { + this.is_request_blocked = value; + } + connectionHandlers() { this.connection.onopen = this.openHandler.bind(this); this.connection.onclose = this.closeHandler.bind(this); @@ -145,6 +150,9 @@ export default class DerivAPIBasic extends DerivAPICalls { } async send(...args) { + const api_type = Object.keys(args[0])[0]; + if (this.is_request_blocked && api_type !== 'website_status') return new Promise((resolve) => { resolve(true); }); + const send_will_be_called = this.callMiddleware('sendWillBeCalled', { args }); if (send_will_be_called) return send_will_be_called; @@ -181,6 +189,8 @@ export default class DerivAPIBasic extends DerivAPICalls { } subscribe(parsed_request) { + if (this.is_request_blocked) return new Subject(); + const request = this.callMiddleware('requestDataTransformer', parsed_request) || parsed_request; return this.subscription_manager.subscribe(request); }