{"id":15536823,"url":"https://github.com/trentmwillis/ember-singularity","last_synced_at":"2025-04-23T13:44:41.537Z","repository":{"id":36081743,"uuid":"40382034","full_name":"trentmwillis/ember-singularity","owner":"trentmwillis","description":"Unified event handling in Ember","archived":false,"fork":false,"pushed_at":"2022-12-13T20:36:31.000Z","size":4836,"stargazers_count":35,"open_issues_count":22,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T00:11:53.912Z","etag":null,"topics":["dom-events","dom-listeners","ember","ember-addon"],"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/trentmwillis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-07T21:42:25.000Z","updated_at":"2024-05-30T02:37:01.000Z","dependencies_parsed_at":"2023-01-16T13:00:54.541Z","dependency_job_id":null,"html_url":"https://github.com/trentmwillis/ember-singularity","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trentmwillis%2Fember-singularity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trentmwillis%2Fember-singularity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trentmwillis%2Fember-singularity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trentmwillis%2Fember-singularity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trentmwillis","download_url":"https://codeload.github.com/trentmwillis/ember-singularity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250441813,"owners_count":21431221,"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":["dom-events","dom-listeners","ember","ember-addon"],"created_at":"2024-10-02T11:53:46.922Z","updated_at":"2025-04-23T13:44:41.519Z","avatar_url":"https://github.com/trentmwillis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ember Singularity\n\n[![npm version](https://badge.fury.io/js/ember-singularity.svg)](https://badge.fury.io/js/ember-singularity)\n[![Build Status](https://github.com/trentmwillis/ember-singularity/workflows/CI/badge.svg)](https://github.com/trentmwillis/ember-singularity/actions/)\n\nEmber Singularity integrates a [Unified Event Handler](https://github.com/trentmwillis/ember-singularity/blob/master/app/services/unified-event-handler.js)\nservice to help control DOM event listeners by taking normal DOM events and\nbinding them to Ember Events that are triggered by a singular DOM listener.\n\nIn other words, this means that instead of having multiple listeners for a\nsingle DOM event, you have one listener for a single DOM event that then\ntriggers multiple callbacks via Ember events.\n\nWhy do this? There are two primary motivations:\n\n1. **Centralize control of DOM listeners**: this has numerous benefits. Primary\n   would be that it reduces the risk of memory leaks and allows optimization of\n   the number of handlers being used. It allows an easy choke point for\n   throttling \"spammy\" events. Essentially greater and easier control over\n   DOM events not already handled by Ember.\n2. **Leverage Ember's event system**: this helps ensure events that cause\n   modifications to application state or the DOM get batched into the Ember\n   run-loop. Helping reduce churn (especially in cases such as scrolling) is a\n   huge win when trying to make performant applications.\n\n## Usage\n\nThe available interface for the `UnifiedEventHandler` is pretty simple and only\ncontains 3 available methods. That said, it is recommended that you abstract\naway any usage of the service via mixins or base-level components; this helps\nensure the benefits described in the above motivations.\n\n### `register(target, eventName, callback)`\n\nThis registers a callback to be tied to a specific target and event type. The\n`target` and `eventName` are expected to be of type `string` and `callback` is a\nfunction. The `callback` will receive the original event. Here's an example:\n\n```js\nlet ScrollMixin = Ember.Mixin.extend({\n  unifiedEventHandler: Ember.inject.service(),\n\n  _registerScrollCallback: Ember.on('init', function() {\n    this.get('unifiedEventHandler').register('window', 'scroll', (event) =\u003e {\n      console.log('scrolled!');\n      console.log(event);\n    });\n  })\n});\n```\n\n### `unregister(target, eventName, callback)`\n\nThis is the exact opposite of `register()` and expects the arguments to be the\nsame as were used to register the handler. Here's an example:\n\n```js\nlet ScrollMixin = Ember.Mixin.extend({\n  unifiedEventHandler: Ember.inject.service(),\n\n  scroll() { console.log('scrolled!'); },\n\n  _registerScrollCallback: Ember.on('init', function() {\n    this.get('unifiedEventHandler').register('window', 'scroll', this.scroll);\n  }),\n\n  _unregisterScrollCallback: Ember.on('willDestroy', function() {\n    this.get('unifiedEventHandler').unregister('window', 'scroll', this.scroll);\n  })\n});\n```\n\n### `triggerEvent(eventName)`\n\nThis allows you to trigger the Ember event that your callback got bound to. As\nof version 1.1.0, after you register a handler it's Ember event will be of the\nform `\u003cevent-name\u003e.\u003ctarget\u003e`. This probably won't be used often, but is\navailable for flexibility in testing, debugging, and extending functionality.\nHere's a final example of the total API:\n\n```js\nlet ScrollMixin = Ember.Mixin.extend({\n  unifiedEventHandler: Ember.inject.service(),\n\n  scroll() { console.log('scrolled!'); },\n\n  triggerScroll() {\n    this.get('unifiedEventHandler').triggerEvent('window.scroll');\n  },\n\n  _registerScrollCallback: Ember.on('init', function() {\n    this.get('unifiedEventHandler').register('window', 'scroll', this.scroll);\n  }),\n\n  _unregisterScrollCallback: Ember.on('willDestroy', function() {\n    this.get('unifiedEventHandler').unregister('window', 'scroll', this.scroll);\n  })\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrentmwillis%2Fember-singularity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrentmwillis%2Fember-singularity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrentmwillis%2Fember-singularity/lists"}