{"id":24096369,"url":"https://github.com/nihilor/hooks-class-mixin","last_synced_at":"2026-06-06T21:31:12.488Z","repository":{"id":57266570,"uuid":"258928803","full_name":"nihilor/hooks-class-mixin","owner":"nihilor","description":"A class mixin for a simple Hooks implementation in Javascript.","archived":false,"fork":false,"pushed_at":"2020-06-29T04:12:07.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T17:44:16.941Z","etag":null,"topics":["hooks","latches","mixin","pattern"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nihilor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-26T03:18:55.000Z","updated_at":"2020-06-29T04:12:09.000Z","dependencies_parsed_at":"2022-08-25T03:41:09.426Z","dependency_job_id":null,"html_url":"https://github.com/nihilor/hooks-class-mixin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nihilor%2Fhooks-class-mixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nihilor%2Fhooks-class-mixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nihilor%2Fhooks-class-mixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nihilor%2Fhooks-class-mixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nihilor","download_url":"https://codeload.github.com/nihilor/hooks-class-mixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241020101,"owners_count":19895369,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","latches","mixin","pattern"],"created_at":"2025-01-10T12:31:38.634Z","updated_at":"2026-06-06T21:31:11.998Z","avatar_url":"https://github.com/nihilor.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hooks-class-mixin\n\nA mixin for a simple, resusable Hooks implementation in Javascript.\n\n## What?\n\nHooks are a concept of modifying the behaviour of a program by adding code without changing the original code. The additional code is provided via latches. At certain situations during runtime the program actively collects the latches, provides the current result and takes the processing result from the latches. Latches can just provide and run their own code, or they can change what the program is doing or outputting by default.\n\n## How?\n\nThis simplified hook implementation is designed as an universally as possible applicable mixin. Just add the mixin to your existing project. Then extend your class or assign the additional properties by composition.\n\nExtending a class from `HooksClassMixin`:\n\n```javascript\nconst HooksClassMixin = require('hooks-class-mixin/index.class')\nclass AClass extends HooksClassMixin {}\nlet   anObject = new AClass()\n```\n\nCompose a class by assigning the object properties:\n\n```javascript\nconst hooksMixin = require('hooks-class-mixin/index.object')\nclass AClass {}\nObject.assign(AClass.prototype, hooksMixin)\nlet   anObject = new AClass()\n```\n\nCompose an object by assigning the object properties:\n\n```javascript\nconst hooksMixin = require('hooks-class-mixin/index.object')\nlet   anObject = {}\nObject.assign(anObject, hooksMixin)\n```\n\nPlease note the difference between assigning the hooks mixin to a class or to an object. For classes you have to add the properties to `prototype`.\n\n```javascript\nclass AClass {}\nObject.assign(AClass.prototype, hooksMixin)\n\nlet   anObject = {}\nObject.assign(anObject, hooksMixin)\n```\n\n## Install\n\nThe mixin is available, installable and manageable via NPM.\n\n```shell\nnpm install hooks-class-mixin --save\n```\n\n## API\n\n### Create a latch\n\nTo create a latch, call `latch` or its shortform `at`, specifiy the hooks name and provide a callback:\n\n```javascript\nlatch (hookName, latchCallback, [...latchParams]): latchId\nat    (hookName, latchCallback, [...latchParams]): latchId\n```\n\n`latch()` returns the latch id to explicitly identify the latch. This id is a Javascript Symbol and must be stored in a variable.\n\n*Optional:* You may provide additional parameters for the latch that will be passed through the callback of the latch.\n\n```javascript\nlet latchId = anObject.latch('userinput', (result) =\u003e `the user typed '${result}'`)\n```\n\n### Collect latches\n\nTo collect the latches, call `hook`. Provide the hooks name and the origin result. The function will call every registered callback, passing the origin and modified result through all callbacks.\n\n```javascript\nhook (hookName, result, [...hookParams]): result\n```\n\n`hook()` returns the final result.\n\n*Optional:* You may provide additional parameters for the hook that will be passed through the callbacks of the latches.\n\n```javascript\nconsole.log(\n    anObject.hook('userinput', 'Hello world!')\n)\n// log output: the user typed 'Hello world!'\n```\n\n### Remove a latch\n\nIt's also possible to remove a previously created latch. Call `unlatch`, provide the latch id returned by `latch` or `at` and also provide the hooks name. The hooks name wouldn't be necessary, but serves as an additional locking mechanism to prevent accidental deletion. \n\n```javascript\nunlatch (latchId, hookName)\n```\n\n`unlatch()` returns nothing.\n\n```javascript\nanObject.unlatch(latchId, 'userinput')\n```\n\n## LongShort Example\n\nIn `./examples/longshort` you will find a very simple example of a hooks application. The example is not very representative, but explains the functionality of Hooks very clearly. `LongShort` shortens or lengthens strings. The hooks just handover the current result, the latches check the current result and will change it, if it matches a specific condition.\n\n`LongShort` will change the result `Read-eval-print loop` to `REPL` and vice versa. It will also change the result `Javascript` to `JavaS.` and `JavaS.` to `JS`.\n\n```javascript\nconst hooksMixin = require('../index.object.js')\nclass LongShort {}\nObject.assign(LongShort.prototype, hooksMixin)\nlet   ls = new LongShort()\n\n//  a. shorten Read-eval-print loop to REPL\nls.latch(\n    'shorten',\n    result =\u003e { return result === 'Read-eval-print loop' ? 'REPL' : result }\n)\n//  b. shorten Javascript to JavaS.\nls.latch(\n    'shorten',\n    result =\u003e { return result === 'Javascript' ? 'JavaS.' : result }\n)\n//  c. shorten JavaS. to JS\nls.latch(\n    'shorten',\n    result =\u003e { return result === 'JavaS.' ? 'JS' : result }\n)\n//  d. lengthens REPL to Read-eval-print loop\nls.latch(\n    'lengthen',\n    result =\u003e { return result === 'REPL' ? 'Read-eval-print loop' : result }\n)\n//  e. lengthens JS to Javascript\nls.latch(\n    'lengthen',\nresult =\u003e { return result === 'JS' ? 'Javascript' : result }\n)\n\n//  nothing changes\nconsole.log('1. Test =\u003e', ls.hook('shorten', 'Test'))\n//  Read-eval-print loop will be shortened to REPL (a)\nconsole.log('2. Read-eval-print loop =\u003e', ls.hook('shorten', 'Read-eval-print loop'))\n//  Javascript will be shortened to JavaS. and then to JS (b, c)\nconsole.log('3. Javascript =\u003e', ls.hook('shorten', 'Javascript'))\n\n//  nothing changes\nconsole.log('4. Test =\u003e', ls.hook('lengthen', 'Test'))\n//  JS will be lengthened to Javascript (e)\nconsole.log('5. JS =\u003e', ls.hook('lengthen', 'JS'))\n//  REPL will be lengthened to Read-eval-print loop (d)\nconsole.log('6. REPL =\u003e', ls.hook('lengthen', 'REPL'))\n```\n\n## Origin\n\nThis work is based on [Microkernel for Server Applications](https://github.com/rse/microkernel/blob/master/src/microkernel-5-hook.js) by [Ralf S. Engelschall](https://github.com/rse), but reimplemented and modified to be as universally applicable as possible.\n\n## LICENSE\n\nMIT License\n\nCopyright (c) 2020 Mark Lubkowitz\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnihilor%2Fhooks-class-mixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnihilor%2Fhooks-class-mixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnihilor%2Fhooks-class-mixin/lists"}