Skip to content

Commit

Permalink
Merge pull request #157 from negarn/negar/forget_poc
Browse files Browse the repository at this point in the history
negar/forget_poc
  • Loading branch information
negarn authored Jun 27, 2019
2 parents 6af99dd + 826057f commit 7a5d282
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/javascript/_common/base/subscription_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,20 @@ const SubscriptionManager = (() => {
throw new Error(`Missing callback function. To forget all subscriptions of msg_type: ${msg_type}, please call forgetAll().`);
}

// find corresponding id(s)
const sub_ids = Object.keys(subscriptions).filter(id => (
subscriptions[id].msg_type === msg_type &&
hasCallbackFunction(id, fncCallback)
));

const forgets_list = [];
sub_ids.forEach((id) => {
if (match_values && !hasValues(subscriptions[id].request, match_values)) {
return;
}
const stream_id = subscriptions[id].stream_id;
if (stream_id && subscriptions[id].subscribers.length === 1) {
delete subscriptions[id];
forgets_list.push(forgetStream(stream_id));
} else {
// there are other subscribers, or for some reason there is no stream_id:
// (i.e. returned an error, or forget() being called before the first response)
subscriptions[id].subscribers.splice(subscriptions[id].subscribers.indexOf(fncCallback), 1);
Object.keys(subscriptions).forEach((id) => {
if (subscriptions[id].msg_type === msg_type) { // it's the msg_type we are looking for
if (!match_values || hasValues(subscriptions[id].request, match_values)) { // the value matches as well
const stream_id = subscriptions[id].stream_id;
if (stream_id && subscriptions[id].subscribers.length === 1) { // there is only one subscriber, so we can forget the call
delete subscriptions[id];
forgets_list.push(forgetStream(stream_id));
} else if (hasCallbackFunction(id, fncCallback)) {
// there are other subscribers, or for some reason there is no stream_id:
// (i.e. returned an error, or forget() being called before the first response)
subscriptions[id].subscribers.splice(subscriptions[id].subscribers.indexOf(fncCallback), 1);
}
}
}
});
return Promise.all(forgets_list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
getDisplayStatus,
getEndTime,
isEnded,
isUserSold,
isValidToSell } from '../Contract/Helpers/logic';
import BaseStore from '../../base-store';
Expand Down Expand Up @@ -183,6 +184,13 @@ export default class PortfolioStore extends BaseStore {
if (isUserSold(contract_response)) this.positions[i].exit_spot = '-';
this.positions[i].is_loading = false;
if (isEnded(contract_response)) {
// also forget for buy
[this.populateResultDetails, this.proposalOpenContractHandler].forEach(cb => {
WS.forget('proposal_open_contract', cb, { contract_id: contract_response.contract_id });
});
}
};
@action.bound
Expand Down

0 comments on commit 7a5d282

Please sign in to comment.