UNPKG

@xailabs/altx

Version:

Flux flavor based on alt.js

189 lines (162 loc) 11.9 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/decorators/bindCalls.js | API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/ImmutableStore.js~ImmutableStore.html">ImmutableStore</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-BindHandlers">BindHandlers</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-resetStores">resetStores</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getAltInstance">getAltInstance</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-setAltInstance">setAltInstance</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-callFactory">callFactory</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-callSeries">callSeries</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createActions">createActions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createStore">createStore</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSources">getSources</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-handlerFactory">handlerFactory</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">decorators</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-bindActions">bindActions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-bindCalls">bindCalls</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-bindHandlers">bindHandlers</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-connect">connect</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-connectAlternative">connectAlternative</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">utils</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-flatten">flatten</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createLogger">createLogger</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getLevel">getLevel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-setLevel">setLevel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-logLevel">logLevel</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">utils/validate</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-creatorConstraints">creatorConstraints</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validateCreator">validateCreator</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-definitionConstraints">definitionConstraints</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validateDefinition">validateDefinition</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-handlerConstraints">handlerConstraints</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validateHandler">validateHandler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-loggerConstraints">loggerConstraints</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/decorators/bindCalls.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import {bind} from &apos;alt-utils/lib/decorators&apos;; import flatten from &apos;../utils/flatten&apos;; import {getLevel as getLogLevel, logLevel} from &apos;../utils/logging&apos;; /** * Decorates a store with any number of call definitions. */ export default function bindCalls(...args) { return function decorate(storeClass) { const calls = flatten(args); calls.forEach(call =&gt; decorateStoreWithCall(storeClass, call)); return storeClass; }; } /** * Decorates a store with a single call definition. * Attaches the dataSource specified in the call definition using alt&apos;s datasource decorator. * Creates and binds a handler function for all reducers and actions specified in the call definition. */ function decorateStoreWithCall(storeClass, callDefinition) { const actionNames = Object.keys(callDefinition.actions).reduce((result, key) =&gt; { // remove ACTION_CONSTANT variants generated by alt if (result.indexOf(key.toLowerCase()) === -1) { result.push(key); } return result; }, []); actionNames.forEach(reducerName =&gt; { bindReducerHandler(reducerName, storeClass, callDefinition); }); } /** * Attaches a single reducer handling to the store. * A new handler method will be created on the store for each action associated * with a reducer (defaults to the action names: loading, error, success). Each handler will pass * the current state and the action payload to the reducer with the same name * and mutate the store with the new state returned by the reducer. * Any sideEffects defined in the call will be executed with a ({state, prevState, payload}) signature. */ function bindReducerHandler(reducerName, storeClass, callDefinition) { const handlerName = `_${callDefinition.name}_${reducerName}`; if (storeClass.prototype[handlerName]) throw new Error(`Duplicate handler &quot;${handlerName}&quot;`); storeClass.prototype[handlerName] = function handleCallAction(payload) { const reducer = callDefinition.hasOwnProperty(&apos;reducers&apos;) &amp;&amp; callDefinition.reducers[reducerName]; const sideEffect = callDefinition.hasOwnProperty(&apos;sideEffects&apos;) &amp;&amp; callDefinition.sideEffects[reducerName]; const logging = callDefinition.hasOwnProperty(&apos;logging&apos;) &amp;&amp; callDefinition.logging; const logger = callDefinition.hasOwnProperty(&apos;logger&apos;) &amp;&amp; callDefinition.logger; const isError = payload instanceof Error; if (isError) { if (payload.response &amp;&amp; payload.response.body &amp;&amp; payload.response.body.message) { logger.error(reducerName, payload.response.body.message); } // logger.debug(payload); } else if (logging || getLogLevel() === logLevel.FORCE) { logger[isError ? &apos;error&apos; : &apos;log&apos;](reducerName, payload &amp;&amp; payload.toJS ? payload.toJS() : payload); } const currentState = this.state; let nextState = currentState; if (reducer) { //console.log(`[${handlerName}]`, payload, callDefinition); try { nextState = reducer(currentState, payload); } catch (error) { console.error(`Error in reducer (${callDefinition.name}, ${reducerName})`, error); } } if (reducer &amp;&amp; !nextState) console.warn(`reducer &quot;${reducerName}&quot; in call &quot;${callDefinition.name}&quot; did not return a new state. Either you forgot to return it, or you should consider using a sideEffect instead of a reducer if no return value is needed.`); if (nextState) { this.setState(nextState); } if (sideEffect) { setTimeout(() =&gt; { try { sideEffect({state: nextState, prevState: currentState, payload}); } catch (error) { console.error(`Error in sideEffect (${callDefinition.name}, ${reducerName})`, error); } }); } }; const action = callDefinition.actions[reducerName]; const bindActionHandler = bind(action); bindActionHandler( storeClass, handlerName, Object.getOwnPropertyDescriptor(storeClass.prototype, handlerName) ); }; </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.5.2)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>