This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
2021-04-02 02:24:13 +03:00

1 line
2.0 KiB
Plaintext

{"version":3,"sources":["useCode.js"],"names":["useCode","nodeFactory","dependencies","React","useEffect","Function","console","warn","node","Array","animatedAlways","__attach","__detach"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEA;;;;;;AAMA;;;;;AAKe,SAASA,OAAT,CAAiBC,WAAjB,EAA8BC,YAA9B,EAA4C;AACzD,MAAI,EAAEC,eAAMC,SAAN,YAA2BC,QAA7B,CAAJ,EAA4C;;AAE5CF,iBAAMC,SAAN,CAAgB,MAAM;AACpB;AACA,QAAI,EAAEH,WAAW,YAAYI,QAAzB,CAAJ,EAAwC;AACtCC,MAAAA,OAAO,CAACC,IAAR,CACE,+EADF;AAIA,YAAMC,IAAI,GAAGP,WAAb;;AACAA,MAAAA,WAAW,GAAG,MAAMO,IAApB;AACD;;AAED,QAAIA,IAAI,GAAGP,WAAW,EAAtB;;AACA,QAAIO,IAAJ,EAAU;AACR;AACA,UAAIA,IAAI,YAAYC,KAApB,EAA2BD,IAAI,GAAG,iBAAMA,IAAN,CAAP;AAE3B,YAAME,cAAc,GAAG,kBAAOF,IAAP,CAAvB;;AACAE,MAAAA,cAAc,CAACC,QAAf,GALQ,CAOR;;;AACA,aAAO,MAAMD,cAAc,CAACE,QAAf,EAAb;AACD;AACF,GAtBD,EAsBGV,YAtBH;AAuBD","sourcesContent":["import React from 'react';\nimport { always, block } from '../base';\n\n/**\n * @callback NodeFactory\n * Function to create a node or an array of nodes.\n * @returns {(Node[] | Node | null | undefined | Boolean)}\n */\n\n/**\n * React hook to run a node.\n * @param {NodeFactory} nodeFactory Function to build the node to run.\n * @param dependencies Array of dependencies. Refresh the node on changes.\n */\nexport default function useCode(nodeFactory, dependencies) {\n if (!(React.useEffect instanceof Function)) return;\n\n React.useEffect(() => {\n // check and correct 1st parameter\n if (!(nodeFactory instanceof Function)) {\n console.warn(\n 'useCode() first argument should be a function that returns an animation node.'\n );\n\n const node = nodeFactory;\n nodeFactory = () => node;\n }\n\n let node = nodeFactory();\n if (node) {\n // allow factory to return array\n if (node instanceof Array) node = block(node);\n\n const animatedAlways = always(node);\n animatedAlways.__attach();\n\n // return undo function\n return () => animatedAlways.__detach();\n }\n }, dependencies);\n}\n"]}