Skip to content

Instantly share code, notes, and snippets.

@schontz
Last active May 9, 2019 02:12
Show Gist options
  • Save schontz/e81963907c5f89e2169a66f5bb9e6092 to your computer and use it in GitHub Desktop.
Save schontz/e81963907c5f89e2169a66f5bb9e6092 to your computer and use it in GitHub Desktop.
function Evented() {
var events = {};
this.on = function (name, callback) {
if (!events[name]) events[name] = [];
var index = events[name].push(callback);
return { remove: function () { events[name].splice(index - 1, 1); }
}
this.emit = function (name, event) {
var cbs = events[name];
if (cbs) cbs.forEach(cb => cb(event));
}
}
class Foo extends Evented {
bar() {
this.emit('channel', 'bar was called');
}
}
x = new Foo();
y = new RetrieveMessages();
const handle = x.on('channel', (channel) => y.goGetMessages(channel))
handle.remove();
x = new TwilioClient();
UI.setServer(x)
setServer (x) {
x.on('ready', channel => updateScreen())
x.on('message', message => addTheMessage(message))
x.on('close', () => reconnect())
)
// Deferred
class StartChannel {
constructor() {
this.ready = new Deferred();
this.findThatChannel();
}
findThatChannel() { ... }
gotIt() {
this.ready.resolve(channelName);
}
}
x = new StartChannel();
channel = await x.ready;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment