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-cli-dispatch

v1.3.1

Published

An ember-cli addon to extend computed properties with asynchronous values

Downloads

7

Readme

ember-cli-dispatch Build Status Maintainability

An Ember CLI addon to extend computed properties with asynchronous values.

Computed properties are really handy for taking one or more static properties, or in fact other computed properties, and transforming their values. However it is often the case that a value is not immediately present and may need to be fetch asynchronously. This prevents us from using computed properties because we cannot return a future value. To accomplish this currently we could return a promise from a computed property and create, potentially, a long chain of then's and catch's to then finally compute the value. This is very cumbersome and will not lend itself well if required within template logic.

This addon implements a computed property macro that works internally with promises. Each computed property can be assigned a default value that will be used until the promise resolves and the computed property's value is updated.

Compatibility

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

Installation

From within your Ember CLI project directory run:

ember install ember-cli-dispatch

Usage

This addon implements a utility function wrapping a promise object, which once resolved will set the value of a computed property to that returned by the promise.

Computed Promise

In order to create an asynchronous computed property you must import the computedPromise macro included in this addon. The macro accepts a property function, that may return a promise, and optionally a default value that will be used until the internal promise resolves.

Computed Promise Example
// app/controllers/application.js
import Controller from '@ember/controller';
import $ from 'jquery';

import computedPromise from 'ember-cli-dispatch/utils/computed-promise';

export default Controller.extend({
  serverResponse: computedPromise(function() {
    return $.getJSON('https://www.example.com/response.json')
  }, 'fetching...')
});

This will create a computed property named serverResponse that will take on the value fetching... until the result of the promise returned from jQuery's getJson method is resolved. Once resolved the computed property will equal the value returned by the promise.

Bundled Macros

In addition to the computedPromise macro this addon includes a couple of utility macros that make use of wrapping computationally expensive operations within promises. The first of these utility macros is map which transforms every value within an array; and the second is reduce which applies a function against an accumulator for each value of an array (from left-to-right) to reduce it to a single value.

Computed Map Example
// app/controllers/application.js
import Controller from '@ember/controller';
import computedMap from 'ember-cli-dispatch/utils/computed-map';

export default Controller.extend({
  scores: [1, 2, 3, 4, 5],

  doubleScores: computedMap('scores', function(score) {
    return score * 2;
  }, 'computing...')
});

This is a contrived example to create a computed property that doubles each of the values within the scores array.

Computed Reduce Example
// app/controllers/application.js
import Controller from '@ember/controller';
import computedReduce from 'ember-cli-dispatch/utils/computed-reduce';

export default Controller.extend({
  scores: [1, 2, 3, 4, 5],

  totalScores: computedReduce('scores', function(cumulativeTotal, score) {
    return cumulativeTotal + score;
  }, 'computing...')
});

This is a contrived example to create a computed property that totals up the values within the scores array.

Custom Macro

It is also possible to create your own asynchronous computed property macros. To do this you need to import the computedPromise macro.

Custom Macro Example
// app/utils/computed-macro.js
import computedPromise from 'ember-cli-dispatch/utils/computed-promise';
import { get } from '@ember/object';

export default function computedMacro(propertyName, defaultValue) {
  return computedPromise(`${propertyName}.[]`, function() {
    return get(this, propertyName);
  }, defaultValue);
};

This shows a very basic implementation of a computed property macro that passes back the value of the specified property. This of course can be extended to perform whatever operation is required by your applications.

License

This project is licensed under the MIT License.