Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created June 26, 2017 10:04
Show Gist options
  • Save kalda341/044ee1a008f9153528178416bb56de18 to your computer and use it in GitHub Desktop.
Save kalda341/044ee1a008f9153528178416bb56de18 to your computer and use it in GitHub Desktop.
// Seconds since call started, if a call is in progress
call_time: Ember.computed('user.activeLiveCallAgents.[]', '[email protected]', '_call_time', function() {
var self = this;
// Basically, we want a way of having call_time update every second,
// but only if a call is in progress.
// We achieve that by having a timer update a property we are listening on,
// which will start when the call time is requested, and stop when there
// is no call in progress. We enforce that there is only ever one timer
// running for an agent.
// This code may be better suited on a user, and moved later.
var timer = this.get('_timer') || {
started: false,
getCallTime() {
if (!this.started) {
this.tick();
}
// TODO: Deal with case where there are multiple calls
var agent = self.get('user.activeLiveCallAgents').find(agent => agent.state === CALL_STATE.IN_CALL);
var callTime = agent && (new Date().getTime() - agent.group.jsAnswerTime)/1000;
return callTime;
},
tick() {
this.started = true;
self.set('_call_time', this.getCallTime());
if (self.get('_call_time') === undefined) {
this.started = false;
} else {
setTimeout(() => this.tick(), 1000);
}
}
};
this.set('_timer', timer);
return timer.getCallTime();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment