{"id":21615824,"url":"https://github.com/valango/eventist","last_synced_at":"2026-05-21T07:39:24.759Z","repository":{"id":34739995,"uuid":"38718819","full_name":"valango/eventist","owner":"valango","description":"Event emitter for modular designs.","archived":false,"fork":false,"pushed_at":"2016-06-09T12:00:00.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-12T11:17:36.234Z","etag":null,"topics":[],"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/valango.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":"2015-07-07T22:45:13.000Z","updated_at":"2016-01-12T01:17:50.000Z","dependencies_parsed_at":"2022-09-11T17:11:35.832Z","dependency_job_id":null,"html_url":"https://github.com/valango/eventist","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Feventist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Feventist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Feventist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Feventist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valango","download_url":"https://codeload.github.com/valango/eventist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244267475,"owners_count":20425835,"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":[],"created_at":"2024-11-24T22:12:56.550Z","updated_at":"2026-05-21T07:39:24.716Z","avatar_url":"https://github.com/valango.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eventist\n\n[![Build Status](https://travis-ci.org/valango/eventist.svg?branch=master)](https://travis-ci.org/valango/eventist)\n[![Coverage Status](https://coveralls.io/repos/valango/eventist/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/valango/eventist?branch=master)\n\nEvent emitter for modular designs.\n\n**Design goals:**\n\n* minimalistic extendable API;\n* support for application profiling and testing on both front- and back-end;\n* implement all this with minimum amount of code.\n\n**NB:** The release 1.0 conveniently supports using instance methods as event\nhandlers. Enhanced API makes [preventing memory leaks](#prevent-leaks) much\neasier.\n\n## Installation\n\n```\n \u003e npm install eventist\n```\nor\n```\n \u003e bower install eventist\n```\n\n## Usage\nTake a look at and play with [code samples](examples/README.md) and\n*Special Patterns* below.\n\n## Main API\n\n### emit ( event {, arguments} )\nCall *synchronously* the handlers registered for the *event*.\nWhen a handler returns anything else than `undefined`, *emit()* will return\nthe same value immediately, perhaps leaving some handlers untouched.\n\nHandlers will be invoked starting from the most recently registered one.\nSetting or removing handlers during the emit loop has no effect\non handling the current event.\n\n* ***event*** string.\n* ***arguments*** are optional and can be of any type.\n* ***Returns:*** `undefined` or the value returned by any handler.\n\n### send ( [callback ,] event {, arguments} )\nPut event into queue to be called *asynchronously* on the next timer tick.\nIf *callback* is supplied,\nthen Eventist will invoke it with the following argument values:\n\n* `null` or the exception value if one was caught;\n* *return value* return value from event processing;\n* array containing the `event` and all `arguments`.\n\nIf callback is not supplied, then exception from processing will not be caught.\n\n* ***callback*** is `function(*,*,Array\u003c*\u003e)` .\n* ***event*** string.\n* ***arguments*** are optional and can be of any type.\n* ***Returns:*** instance of itself for chaining.\n* ***Throws:*** any exception from event processing if `callback` is missing.\n\n### on ( event, handler[, instance] )\nRegister a handler (function or method) for event. No checks for duplicates.\n\n* ***event*** string.\n* ***handler*** function that will be called with arguments supplied\nto *emit()*. See return value handling above.\n* ***instance*** object instance.\n* ***Returns:*** instance of itself for chaining.\n* ***Throws:***  `TypeError` upon illegal argument type.\n\n### once ( event, handler[, instance] )\nSame as *on()*, but the handler will be removed right after it gets called.\n\n### off ( event, handler[, instance] )\nRemove previously registered handler for the `event`, if found.\nIf duplicate registrations were made, only the last one will be undone.\n`TypeError` exception will be thrown if *event* is not a string. \n\n* ***event*** string.\n* ***handler*** function previously registered via *on()* or *once()*.\n***or*** `true` to remove all handlers for given event.\n* ***instance*** object instance.\n* ***Returns:*** instance of itself for chaining.\n* ***Throws:***  `TypeError` if *event* is not a string.\n\n**NOTE:** The *instance* argument will be supplied to handler as\n[thisArg](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply),\nbut it can be any value.\n\n### unplug ( instance )\nRemove handlers, which are methods of given *instance*, if found.\n\n* ***instance*** object instance.\n* ***Returns:*** instance of itself for chaining.\n* ***Throws:***  `TypeError` exception will be thrown if *instance*\nis not an object or is `null`.\n\n## Debugging API\n\nThe following methods are intended for debugging and production code should not\nuse those. This API or parts of it may be deprecated soon.\n\n### hook ( [callback] )\nSet the hook callback function. Callback hook will be called by *emit()*\nbefore any of the handlers. Callback hook function may modify the arguments\narray in any way, before it will be applied to handlers. Calling the method\nwith falsey argument will remove the hook callback.\n\n* ***callback*** function receiving array of all arguments supplied to emit();\n* ***Returns:*** previous hook callback or null;\n* ***Throws:***  `TypeError` if `callback` is not falsey and is not a function.\n\n### reporter ( [callback] )\nSets a callback to be called in the end of every call of *emit()* or *send()*\nwith arguments `event`, `count` and `args`, \n*count* being a number of handlers actually\nexecuted on particular *event*. Otherwise, it is similar to *hook()* method.\n\n* ***callback*** function receiving *count* of handlers invoked and the *event*\nitself;\n* ***Returns:*** previous hook callback or null;\n* ***Throws:***  `TypeError` if `callback` is not falsey and is not a function.\n\n### depth ()\nReturn recursion depth of current event. Value 0 means that *emit()* is not\nactive.\n\n*Eventist* makes no checks or restrictions about event recursion. \n\n* ***Returns:*** number.\n\n### info ()\nReturn a dictionary object of handler counts for event types.\nMay be useful for debugging.\n\n* ***Returns:*** Object.\n\n### execute ( handler, arguments, event )\nActually call the handler function. You may want to override this method for\ndebugging/profiling purposes. This method should not be called directly.\n\n* ***handler*** function previously registered via *on()* or *once()*.\n* ***arguments*** array of those supplied to *emit()*.\n* ***event*** as supplied to *emit()*.\n* ***Returns:*** anything from *handler*.\n\n## Patterns\n\n### \u003ca name=prevent-leaks\u003e\u003c/a\u003ePreventing memory leaks\nIf code like `eventist.on(someEvent, objectsMethod, objectInstance)`\nis executed, then `eventist.unplug(objectInstance)` should be called before\ndiscarding the object instance. Failure to do so will result in memory leak.\n\nBefore v1.0 the only way to use instance method as event handler was to use\nclosures, which resulted in ugly code.\n\n### Reporting unhandled events\nThis feature comes handy for fishing out possible event name typo errors.\n\n```javascript\n  var eventist = new Eventist();\n  \n  eventist.reporter (function (count, event) {\n      count || console.log('UNHANDLED:', event);\n  });\n  ...\n```\n\n### Find out the handler giving specific response to certain event\n\n```javascript\n  var eventist = new Eventist();\n  var original = eventist.execute;\n  eventist.execute = function(cb, args, event) {\n    var response = original.call(eventist, ev, cb);\n    if(response === THIS \u0026\u0026 event === THAT){\n      cb = 0; // set a breakpoint here\n    }\n    return response;\n  }\n```\n\n### Profiling\nThe following example is probably not very useful by itself, but the code\nbefore and after `emit` invocation can do something more interesting,\nlike maintaining profiling dictionaries etc.\n\n```javascript\n  var eventist = new Eventist();\n  var count = 0, time = 0;\n  var emit = eventist.emit;\n  eventist.emit = function(ev) {\n    var t0 = getCurrentTime();\n    var res = emit.apply(eventist, arguments);\n    time += getCurrentTime() - t0;\n    count += 1;\n    return res;\n  }\n```\n\n### Sending events asynchronously\n*Emitting* a new event right from a handler may result in deeply nested events,\npuzzling execution track potentially hard-to-spot bugs. In such cases *send()*\nmay be better choice. In the following example, the return value will be\nforwarded with some other data via event2 and the callback will run after this\nhas been processed.\n\n```javascript\n  var handler1 = function() {\n    var res1 = doMyProcessing();\n    eventist.send(function(err, res2, args) {\n      // Process if necessary...\n      }, \n      'event2', res1, somethingMore...);\n    return res1; // If necessary...\n  };\n```\nIf we do not need result processing or we do not need to wait until our response\ngets processed, then the *\"If necessary...\"* lines can be omitted.\n\n## Tests\nThe same tests can be run for different environments using the following commands:\n```\n  \u003e gulp test              # --\u003e reports/coverage/node/\n  \u003e karma start            # --\u003e reports/coverage/browser-amd/\n  \u003e karma start --direct   # --\u003e reports/coverage/browser/\n```\n\n## Links\n\n* [Olical/EventEmitter](https://github.com/Olical/EventEmitter) - similar,\nbut slightly different project with beautiful code.\nI spotted this one when I was already on final.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalango%2Feventist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalango%2Feventist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalango%2Feventist/lists"}