Comparing version 0.1.5 to 0.1.6
@@ -1,2 +0,236 @@ | ||
var n="development"===process.env.NODE_ENV,o={effects:{},coeffects:{},events:{}},e=Object.assign({},o),t={};function c(o){j(o,"event");var e=function(n){return b(n)?{id:n}:n}(o);!function(o){var t=e.id,c=e.payload;n&&(console.groupCollapsed("Dispatching event: "+t),console.info("EventId:",t),c?console.info("Payload:",c):console.info("Payload:","The "+t+" event has no payload."),console.groupEnd())}();var c=e.id,f=e.payload;!function(o){o&&Object.entries(o).forEach(function(o){var e=o[0],t=o[1];!function(o,e){n&&(console.groupCollapsed("Applying effect: "+o),console.info("Effect id:",o),e?console.info("Effect data:",e):console.info("Effect data:","The "+o+" effect needs no data"),console.groupEnd())}(e,t),v(e)(t)})}(E(c)(t[c].reduce(function(o,e){return Object.assign({},o,function(o){j(o,"coeffect");var e=function(n){return b(n)?{id:n}:n}(o),t=p(e.id)(e.data);return function(o,t){var c=e.id,f=e.data;n&&(console.groupCollapsed("Extracting values of coeffect: "+c),console.info("Coeffect id:",c),f?console.info("Coeffect data:",f):console.info("Coeffect data:","The "+c+" coeffect needs no data"),console.info("Extracted value:",t),console.groupEnd())}(0,t),t}(e))},{}),f))}function f(n){n.forEach(function(n){c(n)})}function i(n,o,e){void 0===e&&(e=[]),l("events",n,o),t[n]=e}function a(n,o){l("coeffects",n,o)}function r(n,o){l("effects",n,o)}function u(n,o){n.forEach(function(n){i(n,function(n,e){return{dispatch:{id:o,payload:e}}})})}function d(n,o){var t=e[n][o];if(!t)throw new Error("There is no handler called '"+o+"'.");return t}function s(n,o){return o?{id:n,data:o}:n}function l(n,o,t){e[n][o]=t}function p(n){return d("coeffects",n)}function v(n){return d("effects",n)}function E(n){return d("events",n)}function h(){e=Object.assign({},o),t={}}function g(o){n=o}r("dispatch",function(n){c(n)}),r("dispatchMany",function(n){f(n)}),r("dispatchLater",function(n){!function(n){setTimeout(function(){c(n)},n.milliseconds)}(n)});var y=Object.prototype.toString;function b(n){var o=typeof n;return"string"===o||"object"===o&&null!=n&&!Array.isArray(n)&&"[object String]"===y.call(n)}function j(n,o){var e={coeffect:"It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'",event:"It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'"};n||function(n){throw new Error("Not defined "+o+".\n"+e[o])}(),b(n)||null!=n.id||function(n){throw new Error("Not valid "+o+".\n"+e[o])}()}export{c as dispatch,f as dispatchMany,i as registerEventHandler,a as registerCoeffectHandler,r as registerEffectHandler,u as registerEventsDelegation,s as coeffect,p as getCoeffectHandler,v as getEffectHandler,E as getEventHandler,h as clearHandlers,g as setVerbosity}; | ||
//# sourceMappingURL=reffects.es.js.map | ||
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
const initialHandlers = { | ||
effects: {}, | ||
coeffects: {}, | ||
events: {} | ||
}; | ||
let handlers = { ...initialHandlers }; | ||
let coeffectsByEvent = {}; | ||
function logEvent({ id, payload }) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Dispatching event: ${id}`); | ||
console.info('EventId:', id); | ||
if (!payload) { | ||
console.info('Payload:', `The ${id} event has no payload.`); | ||
} else { | ||
console.info('Payload:', payload); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function logCoeffect({id, data}, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
} else { | ||
console.info('Coeffect data:', data); | ||
} | ||
console.info('Extracted value:', value); | ||
console.groupEnd(); | ||
} | ||
} | ||
function logEffect(effectId, effectData) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Applying effect: ${effectId}`); | ||
console.info('Effect id:', effectId); | ||
if (!effectData) { | ||
console.info('Effect data:', `The ${effectId} effect needs no data`); | ||
} else { | ||
console.info('Effect data:', effectData); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function normalizeCoeffectDescription(coeffectDescription) { | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
} | ||
return coeffectDescription; | ||
} | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
logCoeffect(normalizedCoeffectDescription, value); | ||
return value; | ||
} | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
} | ||
function applyEffects(effects) { | ||
if (!effects) { | ||
return; | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
} | ||
function normalizeEvent(event) { | ||
if(isString(event)) { | ||
return {id: event}; | ||
} | ||
return event; | ||
} | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
const normalizedEvent = normalizeEvent(event); | ||
logEvent(normalizedEvent); | ||
const { id, payload } = normalizedEvent; | ||
const eventHandler = getEventHandler(id); | ||
const coeffectDescriptions = coeffectsByEvent[id]; | ||
const coeffects = extractCoeffectsValues(coeffectDescriptions); | ||
const effects = eventHandler(coeffects, payload); | ||
applyEffects(effects); | ||
} | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
} | ||
function dispatchLater(event) { | ||
setTimeout(function() { | ||
dispatch(event); | ||
}, event.milliseconds); | ||
} | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
setHandler('events', eventId, handler); | ||
coeffectsByEvent[eventId] = coeffectDescriptions; | ||
} | ||
function registerCoeffectHandler(coeffectId, handler) { | ||
setHandler('coeffects', coeffectId, handler); | ||
} | ||
function registerEffectHandler(effectId, handler) { | ||
setHandler('effects', effectId, handler); | ||
} | ||
function registerEventsDelegation(originalEvents, targetEvent) { | ||
originalEvents.forEach(function(id) { | ||
registerEventHandler(id, function(coeffects, payload) { | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
}; | ||
}); | ||
}); | ||
} | ||
registerEffectHandler('dispatch', function dispatchEffect(event) { | ||
dispatch(event); | ||
}); | ||
registerEffectHandler('dispatchMany', function dispatchManyEffect(events) { | ||
dispatchMany(events); | ||
}); | ||
registerEffectHandler('dispatchLater', function dispatchLaterEffect(event) { | ||
dispatchLater(event); | ||
}); | ||
function getHandler(handlerType, handlerId) { | ||
const handler = handlers[handlerType][handlerId]; | ||
if (!handler) { | ||
throw new Error(`There is no handler called '${handlerId}'.`); | ||
} | ||
return handler; | ||
} | ||
function coeffect(id, data) { | ||
if (!data) { | ||
return id; | ||
} | ||
return { id: id, data: data }; | ||
} | ||
function setHandler(handlerType, handlerId, handler) { | ||
handlers[handlerType][handlerId] = handler; | ||
} | ||
function getCoeffectHandler(coeffectId) { | ||
return getHandler('coeffects', coeffectId); | ||
} | ||
function getEffectHandler(effectId) { | ||
return getHandler('effects', effectId); | ||
} | ||
function getEventHandler(eventId) { | ||
return getHandler('events', eventId); | ||
} | ||
function clearHandlers() { | ||
handlers = { ...initialHandlers }; | ||
coeffectsByEvent = {}; | ||
} | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
} | ||
const toString = Object.prototype.toString; | ||
function isString(value) { | ||
const type = typeof value; | ||
return ( | ||
type === 'string' || | ||
(type === 'object' && | ||
value != null && | ||
!Array.isArray(value) && | ||
toString.call(value) === '[object String]') | ||
); | ||
} | ||
function checkElementValidity(element, elementType) { | ||
const shapeDescriptionsByElement = { | ||
"coeffect": "It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'", | ||
"event": "It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'", | ||
}; | ||
if (!element) { | ||
throwNotDefinedError(elementType); | ||
} | ||
if(!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
} | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
} | ||
export { clearHandlers, coeffect, dispatch, dispatchMany, getCoeffectHandler, getEffectHandler, getEventHandler, registerCoeffectHandler, registerEffectHandler, registerEventHandler, registerEventsDelegation, setVerbosity }; |
@@ -1,2 +0,251 @@ | ||
var e="development"===process.env.NODE_ENV,n={effects:{},coeffects:{},events:{}},t=Object.assign({},n),o={};function c(n){v(n,"event");var t=function(e){return E(e)?{id:e}:e}(n);!function(n){var o=t.id,c=t.payload;e&&(console.groupCollapsed("Dispatching event: "+o),console.info("EventId:",o),c?console.info("Payload:",c):console.info("Payload:","The "+o+" event has no payload."),console.groupEnd())}();var c=t.id,r=t.payload;!function(n){n&&Object.entries(n).forEach(function(n){var t=n[0],o=n[1];!function(n,t){e&&(console.groupCollapsed("Applying effect: "+n),console.info("Effect id:",n),t?console.info("Effect data:",t):console.info("Effect data:","The "+n+" effect needs no data"),console.groupEnd())}(t,o),u(t)(o)})}(l(c)(o[c].reduce(function(n,t){return Object.assign({},n,function(n){v(n,"coeffect");var t=function(e){return E(e)?{id:e}:e}(n),o=d(t.id)(t.data);return function(n,o){var c=t.id,r=t.data;e&&(console.groupCollapsed("Extracting values of coeffect: "+c),console.info("Coeffect id:",c),r?console.info("Coeffect data:",r):console.info("Coeffect data:","The "+c+" coeffect needs no data"),console.info("Extracted value:",o),console.groupEnd())}(0,o),o}(t))},{}),r))}function r(e){e.forEach(function(e){c(e)})}function f(e,n,t){void 0===t&&(t=[]),s("events",e,n),o[e]=t}function i(e,n){s("effects",e,n)}function a(e,n){var o=t[e][n];if(!o)throw new Error("There is no handler called '"+n+"'.");return o}function s(e,n,o){t[e][n]=o}function d(e){return a("coeffects",e)}function u(e){return a("effects",e)}function l(e){return a("events",e)}i("dispatch",function(e){c(e)}),i("dispatchMany",function(e){r(e)}),i("dispatchLater",function(e){!function(e){setTimeout(function(){c(e)},e.milliseconds)}(e)});var p=Object.prototype.toString;function E(e){var n=typeof e;return"string"===n||"object"===n&&null!=e&&!Array.isArray(e)&&"[object String]"===p.call(e)}function v(e,n){var t={coeffect:"It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'",event:"It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'"};e||function(e){throw new Error("Not defined "+n+".\n"+t[n])}(),E(e)||null!=e.id||function(e){throw new Error("Not valid "+n+".\n"+t[n])}()}exports.dispatch=c,exports.dispatchMany=r,exports.registerEventHandler=f,exports.registerCoeffectHandler=function(e,n){s("coeffects",e,n)},exports.registerEffectHandler=i,exports.registerEventsDelegation=function(e,n){e.forEach(function(e){f(e,function(e,t){return{dispatch:{id:n,payload:t}}})})},exports.coeffect=function(e,n){return n?{id:e,data:n}:e},exports.getCoeffectHandler=d,exports.getEffectHandler=u,exports.getEventHandler=l,exports.clearHandlers=function(){t=Object.assign({},n),o={}},exports.setVerbosity=function(n){e=n}; | ||
//# sourceMappingURL=reffects.js.map | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
const initialHandlers = { | ||
effects: {}, | ||
coeffects: {}, | ||
events: {} | ||
}; | ||
let handlers = { ...initialHandlers }; | ||
let coeffectsByEvent = {}; | ||
function logEvent({ id, payload }) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Dispatching event: ${id}`); | ||
console.info('EventId:', id); | ||
if (!payload) { | ||
console.info('Payload:', `The ${id} event has no payload.`); | ||
} else { | ||
console.info('Payload:', payload); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function logCoeffect({id, data}, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
} else { | ||
console.info('Coeffect data:', data); | ||
} | ||
console.info('Extracted value:', value); | ||
console.groupEnd(); | ||
} | ||
} | ||
function logEffect(effectId, effectData) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Applying effect: ${effectId}`); | ||
console.info('Effect id:', effectId); | ||
if (!effectData) { | ||
console.info('Effect data:', `The ${effectId} effect needs no data`); | ||
} else { | ||
console.info('Effect data:', effectData); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function normalizeCoeffectDescription(coeffectDescription) { | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
} | ||
return coeffectDescription; | ||
} | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
logCoeffect(normalizedCoeffectDescription, value); | ||
return value; | ||
} | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
} | ||
function applyEffects(effects) { | ||
if (!effects) { | ||
return; | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
} | ||
function normalizeEvent(event) { | ||
if(isString(event)) { | ||
return {id: event}; | ||
} | ||
return event; | ||
} | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
const normalizedEvent = normalizeEvent(event); | ||
logEvent(normalizedEvent); | ||
const { id, payload } = normalizedEvent; | ||
const eventHandler = getEventHandler(id); | ||
const coeffectDescriptions = coeffectsByEvent[id]; | ||
const coeffects = extractCoeffectsValues(coeffectDescriptions); | ||
const effects = eventHandler(coeffects, payload); | ||
applyEffects(effects); | ||
} | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
} | ||
function dispatchLater(event) { | ||
setTimeout(function() { | ||
dispatch(event); | ||
}, event.milliseconds); | ||
} | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
setHandler('events', eventId, handler); | ||
coeffectsByEvent[eventId] = coeffectDescriptions; | ||
} | ||
function registerCoeffectHandler(coeffectId, handler) { | ||
setHandler('coeffects', coeffectId, handler); | ||
} | ||
function registerEffectHandler(effectId, handler) { | ||
setHandler('effects', effectId, handler); | ||
} | ||
function registerEventsDelegation(originalEvents, targetEvent) { | ||
originalEvents.forEach(function(id) { | ||
registerEventHandler(id, function(coeffects, payload) { | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
}; | ||
}); | ||
}); | ||
} | ||
registerEffectHandler('dispatch', function dispatchEffect(event) { | ||
dispatch(event); | ||
}); | ||
registerEffectHandler('dispatchMany', function dispatchManyEffect(events) { | ||
dispatchMany(events); | ||
}); | ||
registerEffectHandler('dispatchLater', function dispatchLaterEffect(event) { | ||
dispatchLater(event); | ||
}); | ||
function getHandler(handlerType, handlerId) { | ||
const handler = handlers[handlerType][handlerId]; | ||
if (!handler) { | ||
throw new Error(`There is no handler called '${handlerId}'.`); | ||
} | ||
return handler; | ||
} | ||
function coeffect(id, data) { | ||
if (!data) { | ||
return id; | ||
} | ||
return { id: id, data: data }; | ||
} | ||
function setHandler(handlerType, handlerId, handler) { | ||
handlers[handlerType][handlerId] = handler; | ||
} | ||
function getCoeffectHandler(coeffectId) { | ||
return getHandler('coeffects', coeffectId); | ||
} | ||
function getEffectHandler(effectId) { | ||
return getHandler('effects', effectId); | ||
} | ||
function getEventHandler(eventId) { | ||
return getHandler('events', eventId); | ||
} | ||
function clearHandlers() { | ||
handlers = { ...initialHandlers }; | ||
coeffectsByEvent = {}; | ||
} | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
} | ||
const toString = Object.prototype.toString; | ||
function isString(value) { | ||
const type = typeof value; | ||
return ( | ||
type === 'string' || | ||
(type === 'object' && | ||
value != null && | ||
!Array.isArray(value) && | ||
toString.call(value) === '[object String]') | ||
); | ||
} | ||
function checkElementValidity(element, elementType) { | ||
const shapeDescriptionsByElement = { | ||
"coeffect": "It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'", | ||
"event": "It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'", | ||
}; | ||
if (!element) { | ||
throwNotDefinedError(elementType); | ||
} | ||
if(!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
} | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
} | ||
exports.clearHandlers = clearHandlers; | ||
exports.coeffect = coeffect; | ||
exports.dispatch = dispatch; | ||
exports.dispatchMany = dispatchMany; | ||
exports.getCoeffectHandler = getCoeffectHandler; | ||
exports.getEffectHandler = getEffectHandler; | ||
exports.getEventHandler = getEventHandler; | ||
exports.registerCoeffectHandler = registerCoeffectHandler; | ||
exports.registerEffectHandler = registerEffectHandler; | ||
exports.registerEventHandler = registerEventHandler; | ||
exports.registerEventsDelegation = registerEventsDelegation; | ||
exports.setVerbosity = setVerbosity; |
@@ -1,2 +0,257 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.reffects={})}(this,function(e){var n="development"===process.env.NODE_ENV,t={effects:{},coeffects:{},events:{}},o=Object.assign({},t),c={};function f(e){g(e,"event");var t=function(e){return v(e)?{id:e}:e}(e);!function(e){var o=t.id,c=t.payload;n&&(console.groupCollapsed("Dispatching event: "+o),console.info("EventId:",o),c?console.info("Payload:",c):console.info("Payload:","The "+o+" event has no payload."),console.groupEnd())}();var o=t.id,f=t.payload;!function(e){e&&Object.entries(e).forEach(function(e){var t=e[0],o=e[1];!function(e,t){n&&(console.groupCollapsed("Applying effect: "+e),console.info("Effect id:",e),t?console.info("Effect data:",t):console.info("Effect data:","The "+e+" effect needs no data"),console.groupEnd())}(t,o),l(t)(o)})}(p(o)(c[o].reduce(function(e,t){return Object.assign({},e,function(e){g(e,"coeffect");var t=function(e){return v(e)?{id:e}:e}(e),o=u(t.id)(t.data);return function(e,o){var c=t.id,f=t.data;n&&(console.groupCollapsed("Extracting values of coeffect: "+c),console.info("Coeffect id:",c),f?console.info("Coeffect data:",f):console.info("Coeffect data:","The "+c+" coeffect needs no data"),console.info("Extracted value:",o),console.groupEnd())}(0,o),o}(t))},{}),f))}function i(e){e.forEach(function(e){f(e)})}function a(e,n,t){void 0===t&&(t=[]),s("events",e,n),c[e]=t}function r(e,n){s("effects",e,n)}function d(e,n){var t=o[e][n];if(!t)throw new Error("There is no handler called '"+n+"'.");return t}function s(e,n,t){o[e][n]=t}function u(e){return d("coeffects",e)}function l(e){return d("effects",e)}function p(e){return d("events",e)}r("dispatch",function(e){f(e)}),r("dispatchMany",function(e){i(e)}),r("dispatchLater",function(e){!function(e){setTimeout(function(){f(e)},e.milliseconds)}(e)});var E=Object.prototype.toString;function v(e){var n=typeof e;return"string"===n||"object"===n&&null!=e&&!Array.isArray(e)&&"[object String]"===E.call(e)}function g(e,n){var t={coeffect:"It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'",event:"It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'"};e||function(e){throw new Error("Not defined "+n+".\n"+t[n])}(),v(e)||null!=e.id||function(e){throw new Error("Not valid "+n+".\n"+t[n])}()}e.dispatch=f,e.dispatchMany=i,e.registerEventHandler=a,e.registerCoeffectHandler=function(e,n){s("coeffects",e,n)},e.registerEffectHandler=r,e.registerEventsDelegation=function(e,n){e.forEach(function(e){a(e,function(e,t){return{dispatch:{id:n,payload:t}}})})},e.coeffect=function(e,n){return n?{id:e,data:n}:e},e.getCoeffectHandler=u,e.getEffectHandler=l,e.getEventHandler=p,e.clearHandlers=function(){o=Object.assign({},t),c={}},e.setVerbosity=function(e){n=e}}); | ||
//# sourceMappingURL=reffects.umd.js.map | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.reffects = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
const initialHandlers = { | ||
effects: {}, | ||
coeffects: {}, | ||
events: {} | ||
}; | ||
let handlers = { ...initialHandlers }; | ||
let coeffectsByEvent = {}; | ||
function logEvent({ id, payload }) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Dispatching event: ${id}`); | ||
console.info('EventId:', id); | ||
if (!payload) { | ||
console.info('Payload:', `The ${id} event has no payload.`); | ||
} else { | ||
console.info('Payload:', payload); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function logCoeffect({id, data}, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
} else { | ||
console.info('Coeffect data:', data); | ||
} | ||
console.info('Extracted value:', value); | ||
console.groupEnd(); | ||
} | ||
} | ||
function logEffect(effectId, effectData) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Applying effect: ${effectId}`); | ||
console.info('Effect id:', effectId); | ||
if (!effectData) { | ||
console.info('Effect data:', `The ${effectId} effect needs no data`); | ||
} else { | ||
console.info('Effect data:', effectData); | ||
} | ||
console.groupEnd(); | ||
} | ||
} | ||
function normalizeCoeffectDescription(coeffectDescription) { | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
} | ||
return coeffectDescription; | ||
} | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
logCoeffect(normalizedCoeffectDescription, value); | ||
return value; | ||
} | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
} | ||
function applyEffects(effects) { | ||
if (!effects) { | ||
return; | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
} | ||
function normalizeEvent(event) { | ||
if(isString(event)) { | ||
return {id: event}; | ||
} | ||
return event; | ||
} | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
const normalizedEvent = normalizeEvent(event); | ||
logEvent(normalizedEvent); | ||
const { id, payload } = normalizedEvent; | ||
const eventHandler = getEventHandler(id); | ||
const coeffectDescriptions = coeffectsByEvent[id]; | ||
const coeffects = extractCoeffectsValues(coeffectDescriptions); | ||
const effects = eventHandler(coeffects, payload); | ||
applyEffects(effects); | ||
} | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
} | ||
function dispatchLater(event) { | ||
setTimeout(function() { | ||
dispatch(event); | ||
}, event.milliseconds); | ||
} | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
setHandler('events', eventId, handler); | ||
coeffectsByEvent[eventId] = coeffectDescriptions; | ||
} | ||
function registerCoeffectHandler(coeffectId, handler) { | ||
setHandler('coeffects', coeffectId, handler); | ||
} | ||
function registerEffectHandler(effectId, handler) { | ||
setHandler('effects', effectId, handler); | ||
} | ||
function registerEventsDelegation(originalEvents, targetEvent) { | ||
originalEvents.forEach(function(id) { | ||
registerEventHandler(id, function(coeffects, payload) { | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
}; | ||
}); | ||
}); | ||
} | ||
registerEffectHandler('dispatch', function dispatchEffect(event) { | ||
dispatch(event); | ||
}); | ||
registerEffectHandler('dispatchMany', function dispatchManyEffect(events) { | ||
dispatchMany(events); | ||
}); | ||
registerEffectHandler('dispatchLater', function dispatchLaterEffect(event) { | ||
dispatchLater(event); | ||
}); | ||
function getHandler(handlerType, handlerId) { | ||
const handler = handlers[handlerType][handlerId]; | ||
if (!handler) { | ||
throw new Error(`There is no handler called '${handlerId}'.`); | ||
} | ||
return handler; | ||
} | ||
function coeffect(id, data) { | ||
if (!data) { | ||
return id; | ||
} | ||
return { id: id, data: data }; | ||
} | ||
function setHandler(handlerType, handlerId, handler) { | ||
handlers[handlerType][handlerId] = handler; | ||
} | ||
function getCoeffectHandler(coeffectId) { | ||
return getHandler('coeffects', coeffectId); | ||
} | ||
function getEffectHandler(effectId) { | ||
return getHandler('effects', effectId); | ||
} | ||
function getEventHandler(eventId) { | ||
return getHandler('events', eventId); | ||
} | ||
function clearHandlers() { | ||
handlers = { ...initialHandlers }; | ||
coeffectsByEvent = {}; | ||
} | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
} | ||
const toString = Object.prototype.toString; | ||
function isString(value) { | ||
const type = typeof value; | ||
return ( | ||
type === 'string' || | ||
(type === 'object' && | ||
value != null && | ||
!Array.isArray(value) && | ||
toString.call(value) === '[object String]') | ||
); | ||
} | ||
function checkElementValidity(element, elementType) { | ||
const shapeDescriptionsByElement = { | ||
"coeffect": "It must be an object with the following format `{ id: 'COEFFECT_ID', data: <Object | any> }`, or a string if no data is needed: 'EVENT_ID'", | ||
"event": "It must be an object with the following format `{ id: 'EVENT_ID', payload: <Object | any> }`, or a string if no payload is needed: 'EVENT_ID'", | ||
}; | ||
if (!element) { | ||
throwNotDefinedError(elementType); | ||
} | ||
if(!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
} | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
} | ||
} | ||
exports.clearHandlers = clearHandlers; | ||
exports.coeffect = coeffect; | ||
exports.dispatch = dispatch; | ||
exports.dispatchMany = dispatchMany; | ||
exports.getCoeffectHandler = getCoeffectHandler; | ||
exports.getEffectHandler = getEffectHandler; | ||
exports.getEventHandler = getEventHandler; | ||
exports.registerCoeffectHandler = registerCoeffectHandler; | ||
exports.registerEffectHandler = registerEffectHandler; | ||
exports.registerEventHandler = registerEventHandler; | ||
exports.registerEventsDelegation = registerEventsDelegation; | ||
exports.setVerbosity = setVerbosity; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
# Events and event handlers | ||
reffects is a synchronous event bus with [effects and coeffects](https://github.com/trovit/reffects/blob/master/docs/effects-and-coeffects.md). | ||
reffects is a synchronous event bus with [effects and coeffects](https://github.com/trovit/reffects/blob/master/docs/effects_and_coeffects.md). | ||
Any application using reffects has a an [event-driven architecture](https://en.wikipedia.org/wiki/Event-driven_architecture). | ||
Thanks to the [declarative effects pattern](https://github.com/trovit/reffects/blob/master/docs/effects-and-coeffects.md#declarative-effects-pattern), all the event handlers in an application using reffects can be pure function. | ||
Thanks to the [declarative effects pattern](https://github.com/trovit/reffects/blob/master/docs/effects_and_coeffects.md#declarative-effects-pattern), all the event handlers in an application using reffects can be pure function. | ||
@@ -9,0 +9,0 @@ Pure functions offer great advantages over effectful functions because they are |
{ | ||
"name": "reffects", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "", | ||
@@ -15,10 +15,13 @@ "source": "src/index.js", | ||
"babel-jest": "^24.7.1", | ||
"coveralls": "^3.0.7", | ||
"jest": "^24.7.1", | ||
"microbundle": "^0.11.0" | ||
"rollup": "^1.25.2", | ||
"rollup-plugin-terser": "^5.1.2" | ||
}, | ||
"scripts": { | ||
"build": "microbundle", | ||
"build": "rollup -c", | ||
"release": "npm run build && npm t && git push && npm publish", | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
"test:watch": "jest --watch", | ||
"test:ci": "jest --coverage --coverageReporters=text-lcov | coveralls" | ||
}, | ||
@@ -25,0 +28,0 @@ "jest": { |
@@ -1,15 +0,36 @@ | ||
# reffects | ||
<p align="center"> | ||
<img alt="reffects" src="./logo.png" height="85"> | ||
</p> | ||
<h1 align="center">Reffects</h1> | ||
<p align="center">Synchronous event bus with effects and coeffects in JS</p> | ||
Synchronous event bus with effects and coeffects in JS | ||
<p align="center"> | ||
<a href="https://img.shields.io/npm/v/reffects"> | ||
<img src="https://badgen.net/npm/v/reffects"> | ||
</a> | ||
<a href="https://travis-ci.com/trovit/reffects"> | ||
<img src="https://travis-ci.com/trovit/reffects.svg?branch=master"> | ||
</a> | ||
<a href="https://coveralls.io/github/trovit/reffects?branch=master"> | ||
<img src="https://coveralls.io/repos/github/trovit/reffects/badge.svg?branch=master"> | ||
</a> | ||
<a href="https://bundlephobia.com/result?p=reffects"> | ||
<img src="https://badgen.net/bundlephobia/min/reffects"> | ||
</a> | ||
<a href="https://bundlephobia.com/result?p=reffects"> | ||
<img src="https://badgen.net/bundlephobia/minzip/reffects"> | ||
</a> | ||
</p> | ||
## Documentation | ||
* [Events & event handlers](https://github.com/trovit/reffects/blob/master/docs/events_and_event_handlers.md) | ||
* [Registering event handlers](https://github.com/trovit/reffects/blob/master/docs/event_handlers.md). | ||
* [Testing event handlers](https://github.com/trovit/reffects/blob/master/docs/testing_event_handlers.md). | ||
* [Effects & Coeffects](https://github.com/trikitrok/reffects/blob/master/docs/effects-and-coeffects.md) | ||
* [Built-in effects](https://github.com/trikitrok/reffects/blob/master/docs/built-in-effects.md) | ||
* [Registering custom effect handlers](https://github.com/trikitrok/reffects/blob/master/docs/custom-effects.md) | ||
* [Registering custom coeffect handlers](https://github.com/trikitrok/reffects/blob/master/docs/custom-coeffects.md) | ||
* [API](https://github.com/trikitrok/reffects/blob/master/docs/api.md) | ||
- [Events & event handlers](https://github.com/trovit/reffects/blob/master/docs/events_and_event_handlers.md) | ||
- [Registering event handlers](https://github.com/trovit/reffects/blob/master/docs/event_handlers.md). | ||
- [Testing event handlers](https://github.com/trovit/reffects/blob/master/docs/testing_event_handlers.md). | ||
- [Effects & Coeffects](https://github.com/trikitrok/reffects/blob/master/docs/effects_and_coeffects.md) | ||
- [Built-in effects](https://github.com/trikitrok/reffects/blob/master/docs/built-in-effects.md) | ||
- [Registering custom effect handlers](https://github.com/trikitrok/reffects/blob/master/docs/custom_effects.md) | ||
- [Registering custom coeffect handlers](https://github.com/trikitrok/reffects/blob/master/docs/custom_coeffects.md) | ||
- [API](https://github.com/trikitrok/reffects/blob/master/docs/api.md) | ||
## Tests | ||
@@ -16,0 +37,0 @@ |
Sorry, the diff of this file is not supported yet
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
973627
28
0
1200
41
7