Skip to content

Instantly share code, notes, and snippets.

@ScalableJS
Last active December 11, 2015 11:19
Show Gist options
  • Save ScalableJS/4593183 to your computer and use it in GitHub Desktop.
Save ScalableJS/4593183 to your computer and use it in GitHub Desktop.
/**
* Possible options:
*
* once: will ensure the callback list can only be fired once in the triggered event.
*
*
* memory: will keep track of previous values and will call any callback functions added
* after the list has been fired right away with the latest 'memorized' values
* @param options {{once:boolean, memory: boolean}|undefined}
* @constructor
*
* after yuicompressor
* compression 453
* compression and gzip 296
*/
function Observer(options) {
options = options || {};
var
t = this,
// Last fire value (for non-forgettable lists)
memory,
// Boolean Flag to signify if the list has already been fired
fired,
//Blocked observer: if Observer is locked, it will not accept any parameters, and will only store the states.
locked = false,
// The list of callback functions
list = [];
/**
* Is a method for binding callback functions to the triggered event.
* @param fn {function} Is a callback function.
*/
function bind(fn) {
if (!locked) {
if (fired) {
fn.call({},memory);
} else {
list.push(fn);
}
}
}
/**
* Unbinding the specific callback function from the event
* @param fn {function}
*/
function unbind(fn) {
var i = 0;
while (i < list.length) {
if (list[i] === fn) {
list.splice(i,1);
} else {
i++;
}
}
}
/**
* Unbinding All callback functions from the event
*/
function unbindAll() {
list = [];
}
/**
* Blocking obsever and unbinding callback functions from the event
*/
function lock() {
locked = true;
unbindAll();
}
/**
* Checking if the state is locked or not:
* @return {Boolean}
*/
function isLocked() {
return locked;
}
/**
* Publishing the event with the parameters.
* @param data {*}
*/
function trigger(data) {
if (!locked) {
if (options.memory) {
memory = data;
}
if (options.once) {
fired = true;
}
var t_list = list.slice();
for (var i = 0; i < t_list.length; i++) {
t_list[i].call(t, data);
}
if (options.once) {
unbindAll();
}
}
}
return {
bind: bind,
unbind: unbind,
unbindAll: unbindAll,
lock: lock,
isLocked: isLocked,
trigger: trigger
}
}
/* Example EventManager is a collection of observers
* after yuicompressor
* compression 382
* compression and gzip 213
*/
function EventManager(options) {
var events = {};
function createObserver(name) {
return events[name] = events[name] || Observer(options);
}
function bind(name,fn) {
createObserver(name).bind(fn);
return this;
}
function unbind(name,fn) {
if (events[name]) {
if (fn) {
events[name].unbind(fn);
} else {
events[name].unbindAll();
}
}
return this;
}
function lock(name,data) {
createObserver(name).lock();
return this;
}
function isLocked(name,data) {
return createObserver(name).isLocked();
}
function trigger(name,data) {
createObserver(name).trigger(data);
return this;
}
return {
bind: bind,
unbind: unbind,
lock: lock,
isLocked: isLocked,
trigger: trigger
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment