From 7076343338ae3439f3c86f01144818abe8c31978 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Sun, 12 Mar 2023 07:16:23 +0200 Subject: add container helpers; constuct public directory-tree --- html/content/lib/emitter/dist/emitter.js | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 html/content/lib/emitter/dist/emitter.js (limited to 'html/content/lib/emitter/dist') diff --git a/html/content/lib/emitter/dist/emitter.js b/html/content/lib/emitter/dist/emitter.js new file mode 100644 index 0000000..6b2a6fa --- /dev/null +++ b/html/content/lib/emitter/dist/emitter.js @@ -0,0 +1,88 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.emitter = factory()); +}(this, (function () { 'use strict'; + + function emitter(context, options) { + var _events = {}, o = options || {}, _constantEvents = {}, noop = function(){}; + + function invoke(handler, args){ + window.setTimeout(function () { + handler.apply(context, args); + }); + } + + function emit(event) { + var handlers = (_events[event] || (_events[event] = [])); + if (!handlers){ + return; + } + var args = Array.prototype.slice.call(arguments, 1); + if (o.emitting) { + o.emitting({ event: event, data: args, context: context }); + } + for (var i = 0, l = handlers.length; i < l; i++) { + invoke(handlers[i], args); + } + } + + function replay (event) { + var args = Array.prototype.slice.call(arguments, 1); + _constantEvents[event] = args; + emit.apply(null, arguments); + } + + function on(event, fn) { + if (_constantEvents[event]){ + invoke(fn, _constantEvents[event]); + return noop; + } + var handlers = (_events[event] || (_events[event] = [])); + handlers.push(fn); + return function () { + off(event, fn); + }; + } + + function off(event, fn) { + var handlers = _events[event], i, h; + if (!handlers) { + return; + } + if (Array.isArray(event)) { + for (i = 0, l = event.length; i < l; i++) { + off(event[i], fn); + } + return; + } + i = handlers.length; + while(i--) { + h = handlers[i]; + if (fn === h){ + handlers.splice(i, 1); + break; + } + } + } + + function once(event, fn) { + var unsub = on(event, function () { + fn.apply(context, arguments); + unsub(); + }); + return unsub; + } + + return { + on: on, + off: off, + emit: emit, + once: once, + replay: replay + }; + } + + return emitter; + +}))); -- cgit v1.2.3