{"_id":"@allnulled/triggers-class","name":"@allnulled/triggers-class","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@allnulled/triggers-class","version":"1.0.0","description":"Register and emit async triggers using asterisks, names, priority and parameters through a less than 100 lines script.","main":"triggers-class.js","scripts":{"test":"npx http-server -c-1 . -o test.html"},"keywords":["triggers","event-emitter"],"author":{"name":"allnulled"},"license":"WTFPL","gitHead":"bdda43be21312485b42073bded277455d951a182","_id":"@allnulled/triggers-class@1.0.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-4t6PPIkAiS7OUTdzE022ZBTvCMGn0YVstkru0QD+F4+CTJaUaU7ecVe9AxW5HowH+Pg08VgxoCoH8yhY3K+ORQ==","shasum":"2763f8bc3ba5677af4d85d54159b3095927dc9d7","tarball":"https://registry.npmjs.org/@allnulled/triggers-class/-/triggers-class-1.0.0.tgz","fileCount":4,"unpackedSize":8177,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDCvZTfkmaBpkpPwZWFAXMXQKwGKOTUL+eeoGneTClrbQIgTKBRqlclapeK6siED+bxISLlHzYZ94FQT12i9rpbXdU="}]},"_npmUser":{"name":"allnulled","email":"todosanulados@gmail.com"},"directories":{},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/triggers-class_1.0.0_1737496880525_0.313976052872482"},"_hasShrinkwrap":false}},"time":{"created":"2025-01-21T22:01:20.409Z","1.0.0":"2025-01-21T22:01:20.836Z","modified":"2025-01-21T22:01:21.128Z"},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"description":"Register and emit async triggers using asterisks, names, priority and parameters through a less than 100 lines script.","keywords":["triggers","event-emitter"],"author":{"name":"allnulled"},"license":"WTFPL","readme":"# triggers-class\n\nRegister and emit async triggers using asterisks, names, priority and parameters. Less than 100 lines script.\n\n## Installation\n\n```sh\nnpm i -s @allnulled/triggers-class\n```\n\n## Import\n\nIn node.js:\n\n```js\nrequire(\"@allnulled/triggers-class\");\n```\n\nIn html:\n\n```html\n<script src=\"node_modules/@allnulled/triggers-class/triggers-class.js\"></script>\n```\n\nThen you can use `window.TriggersClass` or `global.TriggersClass`.\n\n## API\n\nThese are the signatures of the methods:\n\n```js\nTriggersClass.globMatch(patterns, list);\nconst triggersClass = new TriggersClass();\ntriggersClass.all = {};\ntriggersClass.register(triggerNamePattern, triggerIdentifier, triggerCallback, triggerConfigurations = {});\nawait triggersClass.emit(triggerName, parameters = {});\ntriggersClass.unregister(triggerIdentifier);\n```\n\nThe `globMatch` static method accepts a patterns list, and a texts list. It will return the texts that match at least one pattern.\n\nThe `register` method requires `name:string`, `identifier:string` and `callback:function`. Optionally, `configurations:object`. \n  - `name` is the name of the event. This one accepts `*` as any character/s, like glob patterns.\n  - `identifier` is the name of this registration.\n  - `callback` is the function triggered.\n  - `configurations` is an object. It can have `priority` as a number. The higher, the sooner. Passed to the callback too.\n\nThe `emit` method requires `name:string` with the name of the event (no `*` accepted here). Optionally, `parameters:object`, passed to the callback too.\n\nThe `unregister` method requires only `identifier:string`, the same used on `register` call previously.\n\n## Example\n\nThis is the current test of the library.\n\n```js\nconsole.log(TriggersClass.globMatch([\n    \"crud.insert.*.users\",\n    \"crud.*.many.users\"\n], [\n    \"crud.select.one.users\",\n    \"crud.select.many.users\",\n    \"crud.insert.one.users\",\n    \"crud.insert.many.users\",\n    \"crud.update.one.users\",\n    \"crud.update.many.users\",\n    \"crud.delete.one.users\",\n    \"crud.delete.many.users\",\n]));\nconst triggers = new TriggersClass();\nlet counter = 0;\ntriggers.register(\"crud.insert.one.tabla1\", \"temp1\", function () {\n    console.log(\"triggering temp1\");\n    counter -= 2;\n    return 100;\n}, {\n    priority: 20\n});\ntriggers.register(\"crud.insert.one.tabla1\", \"temp2\", function () {\n    console.log(\"triggering temp2\");\n    counter *= 10;\n    return 10;\n}, {\n    priority: 10\n});\ntriggers.register(\"crud.insert.one.tabla1\", \"temp3\", function () {\n    console.log(\"triggering temp3\");\n    counter += 5;\n    return 1;\n}, {\n    priority: 30\n});\nconst output = await triggers.emit(\"crud.insert.one.tabla1\", { in: 500 });\nconst outputSum = output.reduce((o,i) => {\n    o += i;\n    return o;\n}, 0);\nsetTimeout(() => {\n    console.log(counter);\n    console.log(outputSum);\n    if(counter !== 30) {\n        throw new Error(\"Triggers order is not as expected\");\n    }\n    if(outputSum !== 111) {\n        throw new Error(\"Emit is not working as expected\");\n    }\n}, 1000);\n```","readmeFilename":"README.md"}