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-stream-helper

v1.0.1

Published

Base class for creating helpers that emit values over time

Downloads

14

Readme

ember-stream-helper

Build Status NPM Version

Base class for creating helpers that emit values over time

Most of the time, Ember Helpers are thought of as pure functions that take some input, transform it, and return a new output. Helpers can actually do a lot more than that, though -- because they can recompute themselves, they can react to events and emit a new value even when their inputs have no changed. After using this pattern a few times, I thought it might be useful to extract out a base class that can be used to simplify this pattern.

Usage

Let's talk through an example of how you might use this class.

Imagine we have a service that connects to a WebSocket and emits an event when the number of "likes" on a blog post changes. Our helper will want to do four things:

  1. Take a post as an input
  2. Tell the service to subscribe to those events
  3. Produce a new value each time the event happens
  4. Unsubscribe from the events when it's done.

An implementation might look something like this:

// app/helpers/post-likes.js
import StreamHelper from "ember-stream-helper";
import { inject as service } from "@ember-decorators/service";

export default class LikesOnPost extends StreamHelper {
  /** This is our theoretical services that emits events about posts */
  @service blogPostEvents;

  /**
   * (1) The `subscribe` method is where we should set up a subscription to our
   *     source of events
   *
   *     It is passed the same arguments as the `compute` hook that a Helper normally
   *     has
   */
  subscribe([post]) {
    const callback = numberOfLikes => {
      // (3) The `emit` method is used to produce a new value. Each time `emit` is
      //     called, the argument passed in will be returned from the helper into the
      //     template
      this.emit(numberOfLikes);
    };

    // (2) We'll invoke the callback when a "likes" event takes place. This part will
    //     differ based on how the events are communicated in your specific API
    this.blogPostEvents.on(post, "likes", callback);

    // (4) We return a function from `subscribe` that performs whatever cleanup logic
    //     is required to clean up the subscription
    return () => {
      this.blogPostEvents.off(post, "likes", callback);
    };
  }
}

Now, you can use the helper in a template like this:

There are {{post-likes post}} likes!

As your blogPostEvents service emits new numbers of "likes", your template will stay updated in real-time!

Parameter Changes

The StreamHelper class will handle running your unsubscribe callback when the parameters to the helper change and calling subscribe againw with the new values. This means that subscribe can be called multiple times during the "lifespan" of the helper.

The unsubscribe callback

Make sure that you return any required cleanup logic in from subscribe -- do not perform it yourself in willDestroy. Any "cleanup" logic should be run both when the helper is destroyed and when the parameters change and we're preparing to subscribew again with the new values. StreamHelper takes care of this for you, if you return the unsubscribe logic from subscribe!

Installation

ember install ember-stream-helper

Compatibility

  • Ember.js v2.18 or above
  • Ember CLI v2.13 or above

Prior Art and Influences

ember-drafts

This is an addon that I created recently at work, where I first played around with the idea of a helper that takes one input and emits many outputs over time. Specifically, the original object was passed in, and a new draft state was emitted each time the draft was changed.

React's useEffect hook

I designed the unsubscribe callback API based on the useEffect hook in React, which follows the same pattern of returning a callback to perform cleanup logic.

License

This project is licensed under the MIT License.