npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-lifeline-decorators

v2.0.0

Published

Decorators for easily using ember-lifeline

Downloads

135

Readme

ember-lifeline-decorators

CI npm version Download Total Ember Observer Score Ember Versions code style: prettier
Dependabot enabled dependencies devDependencies

This Ember addon gives you decorators for elegantly using ember-lifeline with ES6 classes.

Installation

Install as any other addon. You will also need ember-lifeline itself:

ember install ember-lifeline-decorators ember-lifeline

Usage

Available decorators

| Decorator | ember-lifeline | Description | |----------------------------------------|--------------------------------------------|--------------------------------------------------------| | @later | runTask | Delay the execution of this method | | @schedule | scheduleTask | Schedule this method on a run loop queue | | @debounce | debounceTask | Debounce this method | | @throttle | throttleTask | Throttle this method | | @destructor | registerDisposable | Automatically execute this method during willDestroy | | @eventListener | addEventListener | Execute this method when a DOM event is fired |

@later

  • timeout: number — delay in milliseconds

runTask / import { later } from '@ember/runloop';

Delays the execution of the decorator by timeout milliseconds.

import Component from '@ember/component';
import { later } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  @later(500)
  function callMeMaybe() {
    // ...
  }

  // and then elsewhere
  hereIsMyNumber() {
    this.callMeMaybe();
  }
}

@schedule

  • queue: RunLoopQueue — the queue to put the method in
    • sync
    • actions
    • routerTransitions
    • render
    • destroy

scheduleTask / import { schedule } from '@ember/runloop';

When the method is called, it is scheduled to be run in the specified run loop queue.

import Component from '@ember/component';
import { later } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  @schedule('render')
  function callMeMaybe() {
    // ...
  }

  // and then elsewhere
  hereIsMyNumber() {
    this.callMeMaybe();
  }
}

@debounce

  • wait: number — delay in milliseconds
  • immediate = false: boolean — trigger the function on the leading instead of the trailing edge of the wait interval

debounceTask / import { debounce } from '@ember/runloop';

Delay calling the target method until the debounce period has elapsed with no additional debounce calls. If the method is called again before the specified time has elapsed, the timer is reset and the entire period must pass again before the target method is called.

import Component from '@ember/component';
import { debounce } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  @debounce(500)
  function callMeMaybe() {
    // ...
  }

  // and then elsewhere
  hereIsMyNumber() {
    this.callMeMaybe();
  }
}

@throttle

  • spacing: number — Number of milliseconds to space out executions
  • immediate = true: boolean — trigger the function on the leading instead of the trailing edge of the wait interval

throttleTask / import { throttle } from '@ember/runloop';

Ensure that the target method is never called more frequently than the specified spacing period.

import Component from '@ember/component';
import { throttle } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  @throttle(500)
  function callMeMaybe() {
    // ...
  }

  // and then elsewhere
  hereIsMyNumber() {
    this.callMeMaybe();
  }
}

@destructor

registerDisposable / import { registerDestructor } from '@ember/destroyable';

Calls this method during destruction of the object.

import { action } from '@ember/object';
import Component from '@glimemr/component';
import { destructor } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  constructor(owner, args) {
    super(owner, args);

    window.addEventListener('resize', this.onResize);
  }

  @destructor
  unregister() {
    window.removeEventListener('resize', this.onResize);
  }

  @action
  onResize(event) {
    // ...
  }
}

@eventListener

  • target:
    • EventTarget — target, such as window or HTMLElement, to register the listener on
    • (this: EmberObject, ctx: EmberObject) => EventTarget — a callback that is called with the context bound to and the first parameter as the object the decorator is used on
  • eventName: string — the event to listen for
  • options?: object — optional options to pass to addEventListener

addEventListener / EventTarget.addEventListener

⚠️👉 In almost all scenarios it is much more sensible to use ember-on-modifier or ember-on-helper.

Automatically calls this method whenever the given event is fired on target.

import Service from '@ember/service';
import { eventListener } from 'ember-lifeline-decorators';

export default class ScrollService extends Service {
  @eventListener(window, 'scroll', { passive: true })
  function onScroll(event) {
    // ...
  }
}

By passing a callback function as the first parameter, you can query the class instance the decorator is used on for information. For instance, you can access the element property of a Component:

import Component from '@ember/component';
import { eventListener } from 'ember-lifeline-decorators';

export default class ExampleComponent extends Component {
  @eventListener(t => t.element, 'scroll', { passive: true })
  function onScroll(event) {
    // ...
  }
}

If the @eventListener is used on a class that implements the didInsertElement hook, like the good ol' Ember Component or sparkles-component, the callback will be executed during the didInsertElement hook. For all other subclasses of EmberObject the callback will be executed during init.