Skip to content

Commit

Permalink
feat: add request/subscribe blocking feature (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashim-deriv authored Feb 9, 2024
1 parent 7033684 commit fe3d879
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/deriv_api/DerivAPIBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit fe3d879

Please sign in to comment.