Skip to content

Instantly share code, notes, and snippets.

@bdmac
Last active January 2, 2017 22:55
Show Gist options
  • Save bdmac/68842a305aa0b1add201802feb68af06 to your computer and use it in GitHub Desktop.
Save bdmac/68842a305aa0b1add201802feb68af06 to your computer and use it in GitHub Desktop.
controller.js
import Ember from 'ember';
import { Presence } from 'pyro/utils/phoenix'; // Import the Presence module from Phoenix...
const { Controller, inject, isPresent, computed, getOwner, Logger } = Ember;
export default Controller.extend({
channelManager: inject.service(), // Our existing custom ChannelManager service
init() {
this._super(...arguments);
this.set('presences', {});
let channelManager = this.get('channelManager');
// Add callbacks for 'presence_state' and 'presence_diff'
channelManager.subscribe(this, 'businessChannelName')
.onEvent('team-updated', this.cacheData.bind(this))
.onEvent('business-updated', this.cacheData.bind(this))
.onEvent('presence_state', this.presenceState.bind(this)) // Handle Presence event
.onEvent('presence_diff', this.presenceDiff.bind(this)) // Handle Presence event
.join();
},
presenceState(state) {
let presences = this.get('presences');
presences = Presence.syncState(presences,
state,
this.onJoin.bind(this),
this.onLeave.bind(this));
this.set('presences', presences);
},
presenceDiff(diff) {
let presences = this.get('presences');
presences = Presence.syncDiff(presences,
diff,
this.onJoin.bind(this),
this.onLeave.bind(this));
this.set('presences', presences);
},
onJoin(id, current, newPres) {
if (!current) {
Logger.debug('Presence: User has entered for the first time', newPres);
this.get('store').findRecord('user', id).then(function(user) {
user.set('online', true);
});
} else {
Logger.debug('Presence: User additional presence', newPres);
}
},
onLeave(id, current, leftPres) {
if (current.metas.length === 0) {
Logger.debug('Presence: User has left from all devices', leftPres);
this.get('store').findRecord('user', id).then(function(user) {
user.set('online', false);
});
} else {
Logger.debug('Presence: User has left from a device', leftPres);
}
}
....
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment