Last active
August 23, 2016 13:05
-
-
Save yonatanmn/49e5b4d912f6cb7be467de2ca7ec7444 to your computer and use it in GitHub Desktop.
event emitter without 'on' and 'emit'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function emitize(obj, eventName) { | |
var _subscriptions = new Set(); | |
Object.defineProperty(obj, eventName, { | |
set(func) { _subscriptions.add(func); }, | |
get() { | |
var emit = (...args)=>{ | |
_subscriptions.forEach(f=>f(...args)); | |
}; | |
Object.defineProperty(emit, 'off', { | |
set(func) { _subscriptions.delete(func);}, | |
get() { _subscriptions = new Set(); } | |
}); | |
return emit; | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function emitize(obj, eventName) { | |
var _subscriptions = []; | |
Object.defineProperty(obj, eventName, { | |
set: function set(func) { | |
_subscriptions.push(func); | |
}, | |
get: function get() { | |
var emit = function emit() { | |
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | |
args[_key] = arguments[_key]; | |
} | |
if (!_subscriptions) { | |
return function () {}; | |
} | |
_subscriptions.forEach(function (f) { | |
return f.apply(undefined, args); | |
}); | |
}; | |
Object.defineProperty(emit, 'off', { | |
set: function set(func) { | |
_subscriptions = _subscriptions.filter(function (f) { | |
return f !== func; | |
}); | |
}, | |
get: function get() { | |
_subscriptions = []; | |
} | |
}); | |
return emit; | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createEmitter() { | |
var target = {} | |
return new Proxy(target, { | |
get(target, property, receiver){ | |
var emit = (...args)=>{ | |
target._subscriptions.forEach(f=>f(...args)); | |
}; | |
return emit | |
}, | |
set(target, prop, value, receiver) { | |
if(prop === '_subscriptions'){throw new Error('dont subscribe to _subscriptions!'); } | |
if(typeof value !== 'function'){throw new Error('subscribe only functions!'); } | |
if(!target._subscriptions){ target._subscriptions = [] } | |
target._subscriptions.push(value); | |
return true; | |
} | |
}); | |
} | |
// ** usage ** // | |
function log(v) { | |
console.log(v); | |
} | |
function logPlus1(v) { | |
console.log(v + 1); | |
} | |
const emitter = createEmitter(); | |
emitter.x = log; //subscribe to events ('x') with cb (log) | |
// emitter._subscriptions = log; | |
// emitter.x = '2' | |
emitter.x = logPlus1; | |
emitter.x('x'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function log(v) { | |
console.log(v); | |
} | |
function logPlus1(v) { | |
console.log(v + 1); | |
} | |
var obj = {}; | |
emitize(obj, 'x'); | |
emitize(obj, 'y'); | |
emitize(obj, 'z'); | |
obj.x = log; //subscribe to events ('x') with cb (log) | |
obj.x = logPlus1; //another subscription, it won't override the previous one | |
obj.y = log; | |
obj.y = logPlus1; | |
obj.y.off = logPlus1; //unsbscribe logPlus1 | |
obj.z = log; | |
obj.z = logPlus1; | |
obj.z.off; //unscbscribe all | |
obj.x('x'); //emits 'x' to all listeners; | |
//logs=> | |
//x | |
//x1 | |
obj.y('y'); | |
//logs=> | |
//y | |
obj.z('z'); | |
// no logs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const emitter = { | |
on(name, func){ | |
this._subscriptions[name] = this._subscriptions[name] || []; | |
this._subscriptions[name].push(func) | |
}, | |
off(name, func){ | |
this._subscriptions[name] = this._subscriptions[name].filter(f=>f!==func) | |
}, | |
_subscriptions:{}, | |
emit(name, ...args){ | |
if(!this._subscriptions[name]){return; } | |
this._subscriptions[name].forEach(f=>f(...args)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function(){ | |
emitter.emit('yyy', 5); | |
}, 100) | |
setTimeout(function(){ | |
emitter.emit('yyy', 9); | |
}, 50) | |
//emitter.on('yyy', function(v){console.log(v - 1)}) | |
function log(v){ | |
console.log(v) | |
} | |
emitter.on('yyy', function(v){console.log(v + 1)}) | |
emitter.on('yyy', log); | |
setTimeout(function(){ | |
emitter.off('yyy', log); | |
}, 70) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment