Skip to content

Instantly share code, notes, and snippets.

@idettman
Created January 31, 2017 02:20
Show Gist options
  • Select an option

  • Save idettman/73c98d05664fa91e289bb172460a304f to your computer and use it in GitHub Desktop.

Select an option

Save idettman/73c98d05664fa91e289bb172460a304f to your computer and use it in GitHub Desktop.
ES6 utils
'use strict';
/**
* @param obj {Object}
* @returns {string} Object's type {string} value
*/
function getType (obj) {
return Object.prototype.toString.call(obj);
};
/**
* @param arrayLikeObject {NodeList|Arguments}
* @return {Array}
*/
function convertToArray (arrayLikeObject) {
return Array.prototype.slice.call(arrayLikeObject);
};
/**
* @param node {Node}
* @return {Node}
*/
function removeFromParent (node) {
node.parentNode.removeChild(node);
};
/**
* @param node {HTMLElement} element to remove children from
*/
function removeChildren (node) {
if (node.hasChildNodes()) {
let child = node.firstElementChild;
while (child) {
removeFromParent(child);
child = node.firstElementChild;
}
}
};
/**
* @param element {HTMLElement}
*/
function removeChildrenV2 (element) {
if (element.hasChildNodes()) {
while (element.children.length) {
removeFromParent(element.children[0]);
}
}
};
function FunctionCache() {
this.commands = new Array(3);
this.init();
}
FunctionCache.prototype = {
constructor: FunctionCache,
/**
* @type {Function[]|undefined[]}
*/
commands: null,
/**
* @type {Number}
*/
total: 0,
init: function () {
this.commands.fill(undefined);
this.total = 0;
},
destroy: function () {
this.commands.fill(undefined);
this.total = 0;
},
add: function (command) {
let index = this.commands.indexOf(undefined);
this.commands[index] = command;
this.total = this.count();
},
remove: function (command) {
let index = this.commands.indexOf(command);
console.log('function cache : remove -> %O', command);
if (index !== -1) {
console.log('found command : clear entry');
this.commands[index] = undefined;
this.total = this.count();
}
},
count: function () {
return this.commands.reduce((returnValue, current, index, array) => {
return (current) ? returnValue + 1: returnValue;
}, 0);
},
execute: function () {
const total = this.count();
for (let i = 0; i < total; i++) {
this.commands[i]();
}
}
};
/**
* @function
*/
function getListener() {
/**
* @type {string[]?}
*/
const events = new Array(12);
events.fill(null);
/**
* @type {FunctionCache[]}
*/
const handlers = new Array(12);
handlers.fill(new FunctionCache());
return {
/**
* @param eventId {string}
*/
fire: function (eventId) {
let index = events.indexOf(eventId);
if (index !== -1) {
handlers[index].execute();
}
},
/**
* @param eventId {string}
* @param eventHandler {Function}
*/
on: function (eventName, eventHandler) {
let index = events.indexOf(null);
events[index] = eventName;
handlers[index].add(eventHandler);
},
/**
* @param eventId {string}
*/
off: function(eventId) {
let index = events.indexOf(eventId);
if (index !== -1) {
let functionCache = handlers[index];
functionCache.destroy();
events[index] = undefined;
}
},
/**
* @param eventId {string}
* @param handler {Function}
*/
removeHandlerForEventId: function(eventId, handler) {
let index = events.indexOf(eventId);
if (index !== -1) {
let functionCache = handlers[index];
functionCache.remove(handler);
if (functionCache.total < 1) {
events[index] = undefined;
}
}
},
/**
* clear all events on listener {{}}
*/
removeAll: function () {
}
};
};
/**
* @param obj {*}
* @param callback {function}
*/
function maybe (obj, callback) {
if (obj) callback();
};
@idettman
Copy link
Author

(function (window, document)
{
const listener = getListener();

function test1() {
	console.log('test handler 1');
}
function test2() {
	console.log('test handler 2');
}

listener.on('test', test1);

listener.on('test', test2);

listener.fire('test');

listener.removeHandlerForEventId('test', test1);
listener.removeHandlerForEventId('test', test2);

//listener.off('test');

})(this, this.document);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment