Comparing version 0.1.11 to 0.1.13
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
var devToolsOn = | ||
process.env.NODE_ENV === 'development' && typeof window !== 'undefined'; | ||
@@ -6,3 +8,3 @@ const initialHandlers = { | ||
coeffects: {}, | ||
events: {} | ||
events: {}, | ||
}; | ||
@@ -26,7 +28,7 @@ | ||
function logCoeffect({id, data}, value) { | ||
function logCoeffect({ id, data }, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
if (!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
@@ -48,3 +50,3 @@ } else { | ||
} else { | ||
console.info('Effect data:', effectData); | ||
console.info('Effect data:', effectData); | ||
} | ||
@@ -57,3 +59,3 @@ console.groupEnd(); | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
return { id: coeffectDescription }; | ||
} | ||
@@ -64,4 +66,6 @@ return coeffectDescription; | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
checkElementValidity(coeffectDescription, 'coeffect'); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription( | ||
coeffectDescription | ||
); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
@@ -74,8 +78,5 @@ const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
return coeffectDescriptions.reduce(function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, {}); | ||
} | ||
@@ -87,14 +88,12 @@ | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
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}; | ||
if (isString(event)) { | ||
return { id: event }; | ||
} | ||
@@ -105,3 +104,3 @@ return event; | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
checkElementValidity(event, 'event'); | ||
const normalizedEvent = normalizeEvent(event); | ||
@@ -118,5 +117,3 @@ logEvent(normalizedEvent); | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
events.forEach(dispatch); | ||
} | ||
@@ -130,7 +127,3 @@ | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
function registerEventHandler(eventId, handler, coeffectDescriptions = []) { | ||
setHandler('events', eventId, handler); | ||
@@ -152,3 +145,3 @@ coeffectsByEvent[eventId] = coeffectDescriptions; | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
dispatch: { id: targetEvent, payload: payload }, | ||
}; | ||
@@ -207,4 +200,4 @@ }); | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
function disableVerbosity() { | ||
verbosityOn = false; | ||
} | ||
@@ -227,4 +220,6 @@ | ||
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'", | ||
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'", | ||
}; | ||
@@ -236,3 +231,3 @@ | ||
if(!isString(element) && element.id == null) { | ||
if (!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
@@ -242,10 +237,22 @@ } | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not defined ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not valid ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
} | ||
export { clearHandlers, coeffect, dispatch, dispatchMany, getCoeffectHandler, getEffectHandler, getEventHandler, registerCoeffectHandler, registerEffectHandler, registerEventHandler, registerEventsDelegation, setVerbosity }; | ||
if (devToolsOn) { | ||
window['__REFFECTS_DEV_TOOLS__'] = { | ||
...window['__REFFECTS_DEV_TOOLS__'], | ||
dispatch, | ||
dispatchMany, | ||
}; | ||
} | ||
export { clearHandlers, coeffect, disableVerbosity, dispatch, dispatchLater, dispatchMany, getCoeffectHandler, getEffectHandler, getEventHandler, registerCoeffectHandler, registerEffectHandler, registerEventHandler, registerEventsDelegation }; |
@@ -6,2 +6,4 @@ 'use strict'; | ||
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
var devToolsOn = | ||
process.env.NODE_ENV === 'development' && typeof window !== 'undefined'; | ||
@@ -11,3 +13,3 @@ const initialHandlers = { | ||
coeffects: {}, | ||
events: {} | ||
events: {}, | ||
}; | ||
@@ -31,7 +33,7 @@ | ||
function logCoeffect({id, data}, value) { | ||
function logCoeffect({ id, data }, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
if (!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
@@ -53,3 +55,3 @@ } else { | ||
} else { | ||
console.info('Effect data:', effectData); | ||
console.info('Effect data:', effectData); | ||
} | ||
@@ -62,3 +64,3 @@ console.groupEnd(); | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
return { id: coeffectDescription }; | ||
} | ||
@@ -69,4 +71,6 @@ return coeffectDescription; | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
checkElementValidity(coeffectDescription, 'coeffect'); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription( | ||
coeffectDescription | ||
); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
@@ -79,8 +83,5 @@ const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
return coeffectDescriptions.reduce(function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, {}); | ||
} | ||
@@ -92,14 +93,12 @@ | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
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}; | ||
if (isString(event)) { | ||
return { id: event }; | ||
} | ||
@@ -110,3 +109,3 @@ return event; | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
checkElementValidity(event, 'event'); | ||
const normalizedEvent = normalizeEvent(event); | ||
@@ -123,5 +122,3 @@ logEvent(normalizedEvent); | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
events.forEach(dispatch); | ||
} | ||
@@ -135,7 +132,3 @@ | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
function registerEventHandler(eventId, handler, coeffectDescriptions = []) { | ||
setHandler('events', eventId, handler); | ||
@@ -157,3 +150,3 @@ coeffectsByEvent[eventId] = coeffectDescriptions; | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
dispatch: { id: targetEvent, payload: payload }, | ||
}; | ||
@@ -212,4 +205,4 @@ }); | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
function disableVerbosity() { | ||
verbosityOn = false; | ||
} | ||
@@ -232,4 +225,6 @@ | ||
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'", | ||
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'", | ||
}; | ||
@@ -241,3 +236,3 @@ | ||
if(!isString(element) && element.id == null) { | ||
if (!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
@@ -247,13 +242,27 @@ } | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not defined ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not valid ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
} | ||
if (devToolsOn) { | ||
window['__REFFECTS_DEV_TOOLS__'] = { | ||
...window['__REFFECTS_DEV_TOOLS__'], | ||
dispatch, | ||
dispatchMany, | ||
}; | ||
} | ||
exports.clearHandlers = clearHandlers; | ||
exports.coeffect = coeffect; | ||
exports.disableVerbosity = disableVerbosity; | ||
exports.dispatch = dispatch; | ||
exports.dispatchLater = dispatchLater; | ||
exports.dispatchMany = dispatchMany; | ||
@@ -267,2 +276,1 @@ exports.getCoeffectHandler = getCoeffectHandler; | ||
exports.registerEventsDelegation = registerEventsDelegation; | ||
exports.setVerbosity = setVerbosity; |
@@ -8,2 +8,4 @@ (function (global, factory) { | ||
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
var devToolsOn = | ||
process.env.NODE_ENV === 'development' && typeof window !== 'undefined'; | ||
@@ -13,3 +15,3 @@ const initialHandlers = { | ||
coeffects: {}, | ||
events: {} | ||
events: {}, | ||
}; | ||
@@ -33,7 +35,7 @@ | ||
function logCoeffect({id, data}, value) { | ||
function logCoeffect({ id, data }, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
if (!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
@@ -55,3 +57,3 @@ } else { | ||
} else { | ||
console.info('Effect data:', effectData); | ||
console.info('Effect data:', effectData); | ||
} | ||
@@ -64,3 +66,3 @@ console.groupEnd(); | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
return { id: coeffectDescription }; | ||
} | ||
@@ -71,4 +73,6 @@ return coeffectDescription; | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
checkElementValidity(coeffectDescription, 'coeffect'); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription( | ||
coeffectDescription | ||
); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
@@ -81,8 +85,5 @@ const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
return coeffectDescriptions.reduce(function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, {}); | ||
} | ||
@@ -94,14 +95,12 @@ | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
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}; | ||
if (isString(event)) { | ||
return { id: event }; | ||
} | ||
@@ -112,3 +111,3 @@ return event; | ||
function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
checkElementValidity(event, 'event'); | ||
const normalizedEvent = normalizeEvent(event); | ||
@@ -125,5 +124,3 @@ logEvent(normalizedEvent); | ||
function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
events.forEach(dispatch); | ||
} | ||
@@ -137,7 +134,3 @@ | ||
function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
function registerEventHandler(eventId, handler, coeffectDescriptions = []) { | ||
setHandler('events', eventId, handler); | ||
@@ -159,3 +152,3 @@ coeffectsByEvent[eventId] = coeffectDescriptions; | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
dispatch: { id: targetEvent, payload: payload }, | ||
}; | ||
@@ -214,4 +207,4 @@ }); | ||
function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
function disableVerbosity() { | ||
verbosityOn = false; | ||
} | ||
@@ -234,4 +227,6 @@ | ||
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'", | ||
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'", | ||
}; | ||
@@ -243,3 +238,3 @@ | ||
if(!isString(element) && element.id == null) { | ||
if (!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
@@ -249,13 +244,27 @@ } | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not defined ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not valid ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
} | ||
if (devToolsOn) { | ||
window['__REFFECTS_DEV_TOOLS__'] = { | ||
...window['__REFFECTS_DEV_TOOLS__'], | ||
dispatch, | ||
dispatchMany, | ||
}; | ||
} | ||
exports.clearHandlers = clearHandlers; | ||
exports.coeffect = coeffect; | ||
exports.disableVerbosity = disableVerbosity; | ||
exports.dispatch = dispatch; | ||
exports.dispatchLater = dispatchLater; | ||
exports.dispatchMany = dispatchMany; | ||
@@ -269,3 +278,2 @@ exports.getCoeffectHandler = getCoeffectHandler; | ||
exports.registerEventsDelegation = registerEventsDelegation; | ||
exports.setVerbosity = setVerbosity; | ||
@@ -272,0 +280,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).reffects={})}(this,(function(e){"use strict";var n="development"===process.env.NODE_ENV;const t={effects:{},coeffects:{},events:{}};let o={...t},c={};function f(e){v(e,"coeffect");const t=function(e){return y(e)?{id:e}:e}(e),o=p(t.id)(t.data);return function({id:e,data:t},o){n&&(console.groupCollapsed(`Extracting values of coeffect: ${e}`),console.info("Coeffect id:",e),console.info("Coeffect data:",t||`The ${e} coeffect needs no data`),console.info("Extracted value:",o),console.groupEnd())}(t,o),o}function i(e){e&&Object.entries(e).forEach((function([e,t]){!function(e,t){n&&(console.groupCollapsed(`Applying effect: ${e}`),console.info("Effect id:",e),console.info("Effect data:",t||`The ${e} effect needs no data`),console.groupEnd())}(e,t),E(e)(t)}))}function r(e){v(e,"event");const t=function(e){return y(e)?{id:e}:e}(e);!function({id:e,payload:t}){n&&(console.groupCollapsed(`Dispatching event: ${e}`),console.info("EventId:",e),console.info("Payload:",t||`The ${e} event has no payload.`),console.groupEnd())}(t);const{id:o,payload:r}=t;i(g(o)(function(e){return e.reduce((function(e,n){return Object.assign({},e,f(n))}),{})}(c[o]),r))}function a(e){e.forEach((function(e){r(e)}))}function s(e,n,t=[]){l("events",e,n),c[e]=t}function d(e,n){l("effects",e,n)}function u(e,n){const t=o[e][n];if(!t)throw Error(`There is no handler called '${n}'.`);return t}function l(e,n,t){o[e][n]=t}function p(e){return u("coeffects",e)}function E(e){return u("effects",e)}function g(e){return u("events",e)}d("dispatch",(function(e){r(e)})),d("dispatchMany",(function(e){a(e)})),d("dispatchLater",(function(e){!function(e){setTimeout((function(){r(e)}),e.milliseconds)}(e)}));const h=Object.prototype.toString;function y(e){const n=typeof e;return"string"===n||"object"===n&&null!=e&&!Array.isArray(e)&&"[object String]"===h.call(e)}function v(e,n){const 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 Error("Not defined "+e+".\n"+t[e])}(n),y(e)||null!=e.id||function(e){throw Error("Not valid "+e+".\n"+t[e])}(n)}e.clearHandlers=function(){o={...t},c={}},e.coeffect=function(e,n){return n?{id:e,data:n}:e},e.dispatch=r,e.dispatchMany=a,e.getCoeffectHandler=p,e.getEffectHandler=E,e.getEventHandler=g,e.registerCoeffectHandler=function(e,n){l("coeffects",e,n)},e.registerEffectHandler=d,e.registerEventHandler=s,e.registerEventsDelegation=function(e,n){e.forEach((function(e){s(e,(function(e,t){return{dispatch:{id:n,payload:t}}}))}))},e.setVerbosity=function(e){n=e},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).reffects={})}(this,(function(e){"use strict";var n="development"===process.env.NODE_ENV,t="development"===process.env.NODE_ENV&&"undefined"!=typeof window;const o={effects:{},coeffects:{},events:{}};let c={...o},f={};function i(e){b(e,"coeffect");const t=function(e){return _(e)?{id:e}:e}(e),o=h(t.id)(t.data);return function({id:e,data:t},o){n&&(console.groupCollapsed(`Extracting values of coeffect: ${e}`),console.info("Coeffect id:",e),console.info("Coeffect data:",t||`The ${e} coeffect needs no data`),console.info("Extracted value:",o),console.groupEnd())}(t,o),o}function r(e){e&&Object.entries(e).forEach((function([e,t]){!function(e,t){n&&(console.groupCollapsed(`Applying effect: ${e}`),console.info("Effect id:",e),console.info("Effect data:",t||`The ${e} effect needs no data`),console.groupEnd())}(e,t),g(e)(t)}))}function a(e){b(e,"event");const t=function(e){return _(e)?{id:e}:e}(e);!function({id:e,payload:t}){n&&(console.groupCollapsed(`Dispatching event: ${e}`),console.info("EventId:",e),console.info("Payload:",t||`The ${e} event has no payload.`),console.groupEnd())}(t);const{id:o,payload:c}=t;r(y(o)(function(e){return e.reduce((function(e,n){return Object.assign({},e,i(n))}),{})}(f[o]),c))}function d(e){e.forEach(a)}function s(e){setTimeout((function(){a(e)}),e.milliseconds)}function u(e,n,t=[]){E("events",e,n),f[e]=t}function l(e,n){E("effects",e,n)}function p(e,n){const t=c[e][n];if(!t)throw Error(`There is no handler called '${n}'.`);return t}function E(e,n,t){c[e][n]=t}function h(e){return p("coeffects",e)}function g(e){return p("effects",e)}function y(e){return p("events",e)}l("dispatch",(function(e){a(e)})),l("dispatchMany",(function(e){d(e)})),l("dispatchLater",(function(e){s(e)}));const v=Object.prototype.toString;function _(e){const n=typeof e;return"string"===n||"object"===n&&null!=e&&!Array.isArray(e)&&"[object String]"===v.call(e)}function b(e,n){const 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 Error("Not defined "+e+".\n"+t[e])}(n),_(e)||null!=e.id||function(e){throw Error("Not valid "+e+".\n"+t[e])}(n)}t&&(window.__REFFECTS_DEV_TOOLS__={...window.__REFFECTS_DEV_TOOLS__,dispatch:a,dispatchMany:d}),e.clearHandlers=function(){c={...o},f={}},e.coeffect=function(e,n){return n?{id:e,data:n}:e},e.disableVerbosity=function(){n=!1},e.dispatch=a,e.dispatchLater=s,e.dispatchMany=d,e.getCoeffectHandler=h,e.getEffectHandler=g,e.getEventHandler=y,e.registerCoeffectHandler=function(e,n){E("coeffects",e,n)},e.registerEffectHandler=l,e.registerEventHandler=u,e.registerEventsDelegation=function(e,n){e.forEach((function(e){u(e,(function(e,t){return{dispatch:{id:n,payload:t}}}))}))},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "reffects", | ||
"version": "0.1.11", | ||
"version": "0.1.13", | ||
"description": "", | ||
@@ -31,3 +31,3 @@ "source": "src/index.js", | ||
}, | ||
"gitHead": "fd757172babcaa90cfdd49705f1897edd5ad8fd5" | ||
"gitHead": "1fbde15bea55022905be4956d47d767ca365f71f" | ||
} |
121
src/index.js
var verbosityOn = process.env.NODE_ENV === 'development'; | ||
var devToolsOn = | ||
process.env.NODE_ENV === 'development' && typeof window !== 'undefined'; | ||
@@ -6,3 +8,3 @@ const initialHandlers = { | ||
coeffects: {}, | ||
events: {} | ||
events: {}, | ||
}; | ||
@@ -26,7 +28,7 @@ | ||
function logCoeffect({id, data}, value) { | ||
function logCoeffect({ id, data }, value) { | ||
if (verbosityOn) { | ||
console.groupCollapsed(`Extracting values of coeffect: ${id}`); | ||
console.info('Coeffect id:', id); | ||
if(!data) { | ||
if (!data) { | ||
console.info('Coeffect data:', `The ${id} coeffect needs no data`); | ||
@@ -48,3 +50,3 @@ } else { | ||
} else { | ||
console.info('Effect data:', effectData); | ||
console.info('Effect data:', effectData); | ||
} | ||
@@ -57,3 +59,3 @@ console.groupEnd(); | ||
if (isString(coeffectDescription)) { | ||
return {id:coeffectDescription}; | ||
return { id: coeffectDescription }; | ||
} | ||
@@ -64,4 +66,6 @@ return coeffectDescription; | ||
function extractCoeffectValue(coeffectDescription) { | ||
checkElementValidity(coeffectDescription, "coeffect"); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription(coeffectDescription); | ||
checkElementValidity(coeffectDescription, 'coeffect'); | ||
const normalizedCoeffectDescription = normalizeCoeffectDescription( | ||
coeffectDescription | ||
); | ||
const coeffectHandler = getCoeffectHandler(normalizedCoeffectDescription.id); | ||
@@ -74,8 +78,5 @@ const value = coeffectHandler(normalizedCoeffectDescription.data); | ||
function extractCoeffectsValues(coeffectDescriptions) { | ||
return coeffectDescriptions.reduce( | ||
function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, | ||
{} | ||
); | ||
return coeffectDescriptions.reduce(function(acc, coeffectDescription) { | ||
return Object.assign({}, acc, extractCoeffectValue(coeffectDescription)); | ||
}, {}); | ||
} | ||
@@ -87,14 +88,12 @@ | ||
} | ||
Object.entries(effects).forEach( | ||
function([effectId, effectData]) { | ||
logEffect(effectId, effectData); | ||
const effectHandler = getEffectHandler(effectId); | ||
effectHandler(effectData); | ||
} | ||
); | ||
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}; | ||
if (isString(event)) { | ||
return { id: event }; | ||
} | ||
@@ -104,4 +103,4 @@ return event; | ||
export function dispatch(event) { | ||
checkElementValidity(event, "event"); | ||
function dispatch(event) { | ||
checkElementValidity(event, 'event'); | ||
const normalizedEvent = normalizeEvent(event); | ||
@@ -117,6 +116,4 @@ logEvent(normalizedEvent); | ||
export function dispatchMany(events) { | ||
events.forEach(function(event) { | ||
dispatch(event); | ||
}); | ||
function dispatchMany(events) { | ||
events.forEach(dispatch); | ||
} | ||
@@ -130,7 +127,3 @@ | ||
export function registerEventHandler( | ||
eventId, | ||
handler, | ||
coeffectDescriptions = [] | ||
) { | ||
function registerEventHandler(eventId, handler, coeffectDescriptions = []) { | ||
setHandler('events', eventId, handler); | ||
@@ -140,15 +133,15 @@ coeffectsByEvent[eventId] = coeffectDescriptions; | ||
export function registerCoeffectHandler(coeffectId, handler) { | ||
function registerCoeffectHandler(coeffectId, handler) { | ||
setHandler('coeffects', coeffectId, handler); | ||
} | ||
export function registerEffectHandler(effectId, handler) { | ||
function registerEffectHandler(effectId, handler) { | ||
setHandler('effects', effectId, handler); | ||
} | ||
export function registerEventsDelegation(originalEvents, targetEvent) { | ||
function registerEventsDelegation(originalEvents, targetEvent) { | ||
originalEvents.forEach(function(id) { | ||
registerEventHandler(id, function(coeffects, payload) { | ||
return { | ||
dispatch: { id: targetEvent, payload: payload } | ||
dispatch: { id: targetEvent, payload: payload }, | ||
}; | ||
@@ -179,3 +172,3 @@ }); | ||
export function coeffect(id, data) { | ||
function coeffect(id, data) { | ||
if (!data) { | ||
@@ -191,15 +184,15 @@ return id; | ||
export function getCoeffectHandler(coeffectId) { | ||
function getCoeffectHandler(coeffectId) { | ||
return getHandler('coeffects', coeffectId); | ||
} | ||
export function getEffectHandler(effectId) { | ||
function getEffectHandler(effectId) { | ||
return getHandler('effects', effectId); | ||
} | ||
export function getEventHandler(eventId) { | ||
function getEventHandler(eventId) { | ||
return getHandler('events', eventId); | ||
} | ||
export function clearHandlers() { | ||
function clearHandlers() { | ||
handlers = { ...initialHandlers }; | ||
@@ -209,4 +202,4 @@ coeffectsByEvent = {}; | ||
export function setVerbosity(newValue) { | ||
verbosityOn = newValue; | ||
function disableVerbosity() { | ||
verbosityOn = false; | ||
} | ||
@@ -229,4 +222,6 @@ | ||
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'", | ||
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'", | ||
}; | ||
@@ -238,3 +233,3 @@ | ||
if(!isString(element) && element.id == null) { | ||
if (!isString(element) && element.id == null) { | ||
throwNotValidError(elementType); | ||
@@ -244,8 +239,36 @@ } | ||
function throwNotDefinedError(element) { | ||
throw new Error("Not defined " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not defined ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
function throwNotValidError(element) { | ||
throw new Error("Not valid " + element + ".\n" + shapeDescriptionsByElement[element]); | ||
throw new Error( | ||
'Not valid ' + element + '.\n' + shapeDescriptionsByElement[element] | ||
); | ||
} | ||
} | ||
if (devToolsOn) { | ||
window['__REFFECTS_DEV_TOOLS__'] = { | ||
...window['__REFFECTS_DEV_TOOLS__'], | ||
dispatch, | ||
dispatchMany, | ||
}; | ||
} | ||
export { | ||
dispatch, | ||
dispatchMany, | ||
dispatchLater, | ||
registerEventHandler, | ||
registerCoeffectHandler, | ||
registerEffectHandler, | ||
registerEventsDelegation, | ||
coeffect, | ||
getEffectHandler, | ||
getCoeffectHandler, | ||
getEventHandler, | ||
clearHandlers, | ||
disableVerbosity, | ||
}; |
@@ -1,5 +0,5 @@ | ||
import * as reffects from "."; | ||
import { destroyAllMocks } from "../test-helpers/fixtures"; | ||
import * as reffects from '.'; | ||
import { destroyAllMocks } from '../test-helpers/fixtures'; | ||
reffects.setVerbosity(false); | ||
reffects.disableVerbosity(); | ||
@@ -11,13 +11,11 @@ afterEach(() => { | ||
test("dispatching an event and receiving its payload", () => { | ||
test('dispatching an event and receiving its payload', () => { | ||
var callsCounter = 0; | ||
const passedPayload = "somePayload"; | ||
const eventId = "anEventHandlerThatWillBeCalled"; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'anEventHandlerThatWillBeCalled'; | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(passedPayload); | ||
}); | ||
reffects.registerEventHandler(eventId, function(coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(passedPayload); | ||
}); | ||
@@ -29,12 +27,10 @@ reffects.dispatch({ id: eventId, payload: passedPayload }); | ||
test("dispatching an event represented as a string because it does not need payload", () => { | ||
test('dispatching an event represented as a string because it does not need payload', () => { | ||
var callsCounter = 0; | ||
const eventId = "anEventHandlerThatWillBeCalled"; | ||
const eventId = 'anEventHandlerThatWillBeCalled'; | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (coeffects, payload) { | ||
callsCounter++; | ||
}); | ||
reffects.registerEventHandler(eventId, function(coeffects, payload) { | ||
callsCounter++; | ||
}); | ||
@@ -46,37 +42,34 @@ reffects.dispatch(eventId); | ||
test("when dispatching an event, coeffect values, whose handlers do not receive parameters, are injected into event handler", () => { | ||
test('when dispatching an event, coeffect values, whose handlers do not receive parameters, are injected into event handler', () => { | ||
var callsCounter = 0; | ||
const datetimeCoeffectId = "datetime"; | ||
const expectedDateTime = "anyDate"; | ||
const apiUrlCoeffectId = "apiUrl"; | ||
const expectedApiUrl = "anyUrl"; | ||
const expectedCoeffects = { [datetimeCoeffectId]: expectedDateTime, [apiUrlCoeffectId]: expectedApiUrl }; | ||
const passedPayload = "somePayload"; | ||
const eventId = "eventHandlerInWhichCoeffectsValuesAreInjected"; | ||
const datetimeCoeffectId = 'datetime'; | ||
const expectedDateTime = 'anyDate'; | ||
const apiUrlCoeffectId = 'apiUrl'; | ||
const expectedApiUrl = 'anyUrl'; | ||
const expectedCoeffects = { | ||
[datetimeCoeffectId]: expectedDateTime, | ||
[apiUrlCoeffectId]: expectedApiUrl, | ||
}; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'eventHandlerInWhichCoeffectsValuesAreInjected'; | ||
reffects.registerCoeffectHandler( | ||
datetimeCoeffectId, | ||
function () { | ||
return { [datetimeCoeffectId]: expectedDateTime }; | ||
} | ||
); | ||
reffects.registerCoeffectHandler(datetimeCoeffectId, function() { | ||
return { [datetimeCoeffectId]: expectedDateTime }; | ||
}); | ||
reffects.registerCoeffectHandler( | ||
apiUrlCoeffectId, | ||
function () { | ||
return { [apiUrlCoeffectId]: expectedApiUrl }; | ||
} | ||
); | ||
reffects.registerCoeffectHandler(apiUrlCoeffectId, function() { | ||
return { [apiUrlCoeffectId]: expectedApiUrl }; | ||
}); | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (coeffects, payload) { | ||
function(coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(passedPayload); | ||
Object.keys(expectedCoeffects).forEach( | ||
function (coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
Object.keys(expectedCoeffects).forEach(function(coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
}, | ||
[datetimeCoeffectId, apiUrlCoeffectId]); | ||
[datetimeCoeffectId, apiUrlCoeffectId] | ||
); | ||
@@ -88,28 +81,25 @@ reffects.dispatch({ id: eventId, payload: passedPayload }); | ||
test("when dispatching an event, a coeffect value, whose handler receives parameters, is injected into event handler", () => { | ||
test('when dispatching an event, a coeffect value, whose handler receives parameters, is injected into event handler', () => { | ||
var callsCounter = 0; | ||
const expectedData = "koko"; | ||
const mokoCoeffectDescription = { id: "moko", data: expectedData }; | ||
const expectedData = 'koko'; | ||
const mokoCoeffectDescription = { id: 'moko', data: expectedData }; | ||
const expectedCoeffects = { [mokoCoeffectDescription.id]: expectedData }; | ||
const passedPayload = "somePayload"; | ||
const eventId = "eventHandlerInWhichCoeffectsValuesAreInjected"; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'eventHandlerInWhichCoeffectsValuesAreInjected'; | ||
reffects.registerCoeffectHandler( | ||
mokoCoeffectDescription.id, | ||
function () { | ||
return { [mokoCoeffectDescription.id]: expectedData }; | ||
} | ||
); | ||
reffects.registerCoeffectHandler(mokoCoeffectDescription.id, function() { | ||
return { [mokoCoeffectDescription.id]: expectedData }; | ||
}); | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (coeffects, payload) { | ||
function(coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(passedPayload); | ||
Object.keys(expectedCoeffects).forEach( | ||
function (coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
Object.keys(expectedCoeffects).forEach(function(coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
}, | ||
[mokoCoeffectDescription]); | ||
[mokoCoeffectDescription] | ||
); | ||
@@ -121,28 +111,25 @@ reffects.dispatch({ id: eventId, payload: passedPayload }); | ||
test("when dispatching an event, a coeffect description as string, whose handler receives parameters, is injected into event handler", () => { | ||
test('when dispatching an event, a coeffect description as string, whose handler receives parameters, is injected into event handler', () => { | ||
var callsCounter = 0; | ||
const expectedData = "koko"; | ||
const mokoCoeffectDescription = "moko"; | ||
const expectedData = 'koko'; | ||
const mokoCoeffectDescription = 'moko'; | ||
const expectedCoeffects = { [mokoCoeffectDescription]: expectedData }; | ||
const passedPayload = "somePayload"; | ||
const eventId = "eventHandlerInWhichCoeffectsValuesAreInjected"; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'eventHandlerInWhichCoeffectsValuesAreInjected'; | ||
reffects.registerCoeffectHandler( | ||
mokoCoeffectDescription, | ||
function () { | ||
return { [mokoCoeffectDescription]: expectedData }; | ||
} | ||
); | ||
reffects.registerCoeffectHandler(mokoCoeffectDescription, function() { | ||
return { [mokoCoeffectDescription]: expectedData }; | ||
}); | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (coeffects, payload) { | ||
function(coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(passedPayload); | ||
Object.keys(expectedCoeffects).forEach( | ||
function (coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
Object.keys(expectedCoeffects).forEach(function(coeffectId) { | ||
expect(coeffects[coeffectId]).toEqual(expectedCoeffects[coeffectId]); | ||
}); | ||
}, | ||
[mokoCoeffectDescription]); | ||
[mokoCoeffectDescription] | ||
); | ||
@@ -154,28 +141,25 @@ reffects.dispatch({ id: eventId, payload: passedPayload }); | ||
test("a not registered handler id throws an error", () => { | ||
const passedPayload = "somePayload"; | ||
const notRegisteredHandler = "noopEventHandler"; | ||
test('a not registered handler id throws an error', () => { | ||
const passedPayload = 'somePayload'; | ||
const notRegisteredHandler = 'noopEventHandler'; | ||
expect(() => reffects.dispatch({ | ||
id: notRegisteredHandler, | ||
payload: passedPayload | ||
})).toThrowError(`There is no handler called '${notRegisteredHandler}'.`); | ||
expect(() => | ||
reffects.dispatch({ | ||
id: notRegisteredHandler, | ||
payload: passedPayload, | ||
}) | ||
).toThrowError(`There is no handler called '${notRegisteredHandler}'.`); | ||
}); | ||
test("applying effects after executing the eventHandler", () => { | ||
const eventId = "eventHandlerAfterWhichAnEffectIsApplied"; | ||
const effectId = "someEffectId"; | ||
test('applying effects after executing the eventHandler', () => { | ||
const eventId = 'eventHandlerAfterWhichAnEffectIsApplied'; | ||
const effectId = 'someEffectId'; | ||
const dummyEffect = jest.fn(); | ||
const fakeEventPayload = 1; | ||
reffects.registerEffectHandler( | ||
effectId, | ||
(payload) => dummyEffect(payload) | ||
); | ||
reffects.registerEffectHandler(effectId, payload => dummyEffect(payload)); | ||
reffects.registerEventHandler( | ||
eventId, | ||
function (cofx, payload) { | ||
return { [effectId]: payload }; | ||
}); | ||
reffects.registerEventHandler(eventId, function(cofx, payload) { | ||
return { [effectId]: payload }; | ||
}); | ||
@@ -187,22 +171,27 @@ reffects.dispatch({ id: eventId, payload: fakeEventPayload }); | ||
test("dispatch effect", () => { | ||
test('dispatch effect', () => { | ||
var callsCounter = 0; | ||
const expectedPayload = ["arg1", "arg2"]; | ||
const expectedPayload = ['arg1', 'arg2']; | ||
reffects.registerEventHandler( | ||
"eventDispatchedUsingDispatchEffect", | ||
function (coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(expectedPayload); | ||
} | ||
); | ||
reffects.registerEventHandler('eventDispatchedUsingDispatchEffect', function( | ||
coeffects, | ||
payload | ||
) { | ||
callsCounter++; | ||
expect(payload).toEqual(expectedPayload); | ||
}); | ||
reffects.registerEventHandler( | ||
"eventReturningDispatchEffect", | ||
function (coeffects, payload) { | ||
return { "dispatch": { id: "eventDispatchedUsingDispatchEffect", payload: ["arg1", "arg2"] } }; | ||
} | ||
); | ||
reffects.registerEventHandler('eventReturningDispatchEffect', function( | ||
coeffects, | ||
payload | ||
) { | ||
return { | ||
dispatch: { | ||
id: 'eventDispatchedUsingDispatchEffect', | ||
payload: ['arg1', 'arg2'], | ||
}, | ||
}; | ||
}); | ||
reffects.dispatch({ id: "eventReturningDispatchEffect" }); | ||
reffects.dispatch({ id: 'eventReturningDispatchEffect' }); | ||
@@ -212,33 +201,43 @@ expect(callsCounter).toEqual(1); | ||
test("dispatchMany effect", () => { | ||
test('dispatchMany effect', () => { | ||
var firstEventCallsCounter = 0, | ||
secondEventCallsCounter = 0; | ||
const expectedPayloadForFirstEvent = ["args1-1", ":args1-2"]; | ||
const expectedPayloadForSecondEvent = [":args2-1", "args2-2"]; | ||
const expectedPayloadForFirstEvent = ['args1-1', ':args1-2']; | ||
const expectedPayloadForSecondEvent = [':args2-1', 'args2-2']; | ||
reffects.registerEventHandler( | ||
"firstEventDispatchedUsingDispatchMany", | ||
function (coeffects, payload) { | ||
'firstEventDispatchedUsingDispatchMany', | ||
function(coeffects, payload) { | ||
firstEventCallsCounter++; | ||
expect(payload).toEqual(expectedPayloadForFirstEvent); | ||
}); | ||
} | ||
); | ||
reffects.registerEventHandler( | ||
"secondEventDispatchedUsingDispatchMany", | ||
function (coeffects, payload) { | ||
'secondEventDispatchedUsingDispatchMany', | ||
function(coeffects, payload) { | ||
secondEventCallsCounter++; | ||
expect(payload).toEqual(expectedPayloadForSecondEvent); | ||
}); | ||
} | ||
); | ||
reffects.registerEventHandler( | ||
"eventReturningDispatchManyEffect", | ||
function (coeffects, payload) { | ||
return { | ||
"dispatchMany": [ | ||
{ id: "firstEventDispatchedUsingDispatchMany", payload: expectedPayloadForFirstEvent }, | ||
{ id: "secondEventDispatchedUsingDispatchMany", payload: expectedPayloadForSecondEvent }] | ||
} | ||
}); | ||
reffects.registerEventHandler('eventReturningDispatchManyEffect', function( | ||
coeffects, | ||
payload | ||
) { | ||
return { | ||
dispatchMany: [ | ||
{ | ||
id: 'firstEventDispatchedUsingDispatchMany', | ||
payload: expectedPayloadForFirstEvent, | ||
}, | ||
{ | ||
id: 'secondEventDispatchedUsingDispatchMany', | ||
payload: expectedPayloadForSecondEvent, | ||
}, | ||
], | ||
}; | ||
}); | ||
reffects.dispatch({ id: "eventReturningDispatchManyEffect" }); | ||
reffects.dispatch({ id: 'eventReturningDispatchManyEffect' }); | ||
@@ -249,11 +248,11 @@ expect(firstEventCallsCounter).toEqual(1); | ||
test("dispatchLater effect", async () => { | ||
test('dispatchLater effect', async () => { | ||
jest.useFakeTimers(); | ||
var callsCounter = 0; | ||
const expectedPayload = ["arg1", "arg2"]; | ||
const expectedPayload = ['arg1', 'arg2']; | ||
reffects.registerEventHandler( | ||
"eventDispatchedUsingDispatchLaterEffect", | ||
function (coeffects, payload) { | ||
'eventDispatchedUsingDispatchLaterEffect', | ||
function(coeffects, payload) { | ||
callsCounter++; | ||
@@ -264,16 +263,16 @@ expect(payload).toEqual(expectedPayload); | ||
reffects.registerEventHandler( | ||
"eventReturningDispatchLaterEffect", | ||
function (coeffects, payload) { | ||
return { | ||
dispatchLater: { | ||
id: "eventDispatchedUsingDispatchLaterEffect", | ||
payload: ["arg1", "arg2"], | ||
milliseconds: 1000 | ||
} | ||
}; | ||
} | ||
); | ||
reffects.registerEventHandler('eventReturningDispatchLaterEffect', function( | ||
coeffects, | ||
payload | ||
) { | ||
return { | ||
dispatchLater: { | ||
id: 'eventDispatchedUsingDispatchLaterEffect', | ||
payload: ['arg1', 'arg2'], | ||
milliseconds: 1000, | ||
}, | ||
}; | ||
}); | ||
reffects.dispatch({ id: "eventReturningDispatchLaterEffect" }); | ||
reffects.dispatch({ id: 'eventReturningDispatchLaterEffect' }); | ||
@@ -285,17 +284,17 @@ jest.runAllTimers(); | ||
test("delegating events", () => { | ||
test('delegating events', () => { | ||
var callsCounter = 0; | ||
const expectedPayload = ["arg1", "arg2"]; | ||
const eventReceivingDelegationId = "eventReceivingDelegation"; | ||
const delegatedEventId1 = "delegatedEventId1"; | ||
const delegatedEventId2 = "delegatedEventId2"; | ||
const delegatedEventId3 = "delegatedEventId3"; | ||
const expectedPayload = ['arg1', 'arg2']; | ||
const eventReceivingDelegationId = 'eventReceivingDelegation'; | ||
const delegatedEventId1 = 'delegatedEventId1'; | ||
const delegatedEventId2 = 'delegatedEventId2'; | ||
const delegatedEventId3 = 'delegatedEventId3'; | ||
reffects.registerEventHandler( | ||
eventReceivingDelegationId, | ||
function (coeffects, payload) { | ||
callsCounter++; | ||
expect(payload).toEqual(expectedPayload); | ||
} | ||
); | ||
reffects.registerEventHandler(eventReceivingDelegationId, function( | ||
coeffects, | ||
payload | ||
) { | ||
callsCounter++; | ||
expect(payload).toEqual(expectedPayload); | ||
}); | ||
@@ -314,47 +313,52 @@ reffects.registerEventsDelegation( | ||
test("creating coeffects sugar", () => { | ||
expect(reffects.coeffect("apiUrl")).toEqual("apiUrl"); | ||
expect(reffects.coeffect("apiUrl", {someData: "koko"})).toEqual({"id": "apiUrl", "data": {"someData": "koko"}}); | ||
test('creating coeffects sugar', () => { | ||
expect(reffects.coeffect('apiUrl')).toEqual('apiUrl'); | ||
expect(reffects.coeffect('apiUrl', { someData: 'koko' })).toEqual({ | ||
id: 'apiUrl', | ||
data: { someData: 'koko' }, | ||
}); | ||
}); | ||
test("an exception is thrown, when a coeffect is not defined", () => { | ||
test('an exception is thrown, when a coeffect is not defined', () => { | ||
const mokoCoeffectDescription = undefined; | ||
const passedPayload = "somePayload"; | ||
const eventId = "eventHandlerInWhichCoeffectsValuesAreInjected"; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'eventHandlerInWhichCoeffectsValuesAreInjected'; | ||
const dummyEventHandler = jest.fn(); | ||
reffects.registerEventHandler( | ||
eventId, | ||
dummyEventHandler, | ||
[mokoCoeffectDescription]); | ||
reffects.registerEventHandler(eventId, dummyEventHandler, [ | ||
mokoCoeffectDescription, | ||
]); | ||
expect(() => reffects.dispatch({ | ||
id: eventId, | ||
payload: passedPayload | ||
})).toThrowError("Not defined coeffect."); | ||
expect(() => | ||
reffects.dispatch({ | ||
id: eventId, | ||
payload: passedPayload, | ||
}) | ||
).toThrowError('Not defined coeffect.'); | ||
}); | ||
test("an exception is thrown, when a coeffect description is not valid", () => { | ||
const mokoCoeffectDescription = {a: "lala"}; | ||
const passedPayload = "somePayload"; | ||
const eventId = "eventHandlerInWhichCoeffectsValuesAreInjected"; | ||
test('an exception is thrown, when a coeffect description is not valid', () => { | ||
const mokoCoeffectDescription = { a: 'lala' }; | ||
const passedPayload = 'somePayload'; | ||
const eventId = 'eventHandlerInWhichCoeffectsValuesAreInjected'; | ||
const dummyEventHandler = jest.fn(); | ||
reffects.registerEventHandler( | ||
eventId, | ||
dummyEventHandler, | ||
[mokoCoeffectDescription]); | ||
reffects.registerEventHandler(eventId, dummyEventHandler, [ | ||
mokoCoeffectDescription, | ||
]); | ||
expect(() => reffects.dispatch({ | ||
id: eventId, | ||
payload: passedPayload | ||
})).toThrowError("Not valid coeffect."); | ||
expect(() => | ||
reffects.dispatch({ | ||
id: eventId, | ||
payload: passedPayload, | ||
}) | ||
).toThrowError('Not valid coeffect.'); | ||
}); | ||
test("an exception is thrown, when an event is not defined", () => { | ||
test('an exception is thrown, when an event is not defined', () => { | ||
const event = undefined; | ||
expect(() => reffects.dispatch(event)).toThrowError("Not defined event."); | ||
expect(() => reffects.dispatch(event)).toThrowError('Not defined event.'); | ||
}); | ||
test("an exception is thrown, when an event is not valid", () => { | ||
const event = {oh: "lala"}; | ||
expect(() => reffects.dispatch(event)).toThrowError("Not valid event."); | ||
test('an exception is thrown, when an event is not valid', () => { | ||
const event = { oh: 'lala' }; | ||
expect(() => reffects.dispatch(event)).toThrowError('Not valid event.'); | ||
}); |
export function destroyAllMocks() { | ||
jest.restoreAllMocks(); | ||
jest.clearAllMocks(); | ||
} | ||
} |
export function callsTo(spyFn) { | ||
return spyFn.mock.calls; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
46801
1247