Giter VIP home page Giter VIP logo

asyncdata.js's Introduction

asyncdata.js

Signal notifications for asynchronous data loading and post-processing

How to use

// you have a function that requests data and returns a promise
function loadData(startDate){
  return $http.get('api/data?start-date=' + startDate);
}
// create an instance by passing this function
var data = asyncData(loadData);

// attach post-processing callbacks
data
  .resolved(function(data){
    return transform1(data);
  })
  .resolved(function(transformed){
    // this receives the result of the previous 'resolved'
    return transform2(transformed);
  });

data.load('2015-01-01');
// triggers all resolved callbacks
data.load('2015-02-15');
// triggers all callbacks again with the new data

Notifications can be added on each request's start

data
  .requested(function(){
      console.log('loading data...')
    })
  .resolved(function(data){
    return transform(data);
  })
  .requested(function(){
    console.log('transforming data...')
  })
  .resolved(function(data){
    console.log('got the transformed data', data);
  });
data.load();
// we will see:
// 'loading data...'
// 'transforming data...'
// 'got the transformed data'

Late resolveds will be called immediately with the previous step's cached results

var transformed = data
  .resolved(function(data){
    return transform1(data);
  });
  
data.load();
// time goes by..

transformed.resolved(function(data){
  //this is triggered instantly as long as the transform1 has finished
  return transform2(data);
});

How is this different than promises? It

  1. triggers the callbacks each time it is load()ed, instead of only once
  2. it provides a notification on request start
  3. it is slightly uglier as an API

Install

bower install dtheodor/asyncdata.js --production

Run tests

bower install dtheodor/asyncdata.js

Then simply open SpecRunner.html with your browser.

Rationale:

Problem

  • There's a module that knows how to load (and re-load) data through an API, and stores it locally.
  • Multiple modules access the loaded data, and transform them into a different representation (each module does a different transform), also storing them locally. Each time the data they depend on is reloaded, they need to re-run the transformation to update.
  • The transformed data is exposed to the view, with a 'loading' indication when the original data is being (re)loaded and/or the transformation is in progress.

Solution

Can promises do this? Promises support:

  • on resolution callbacks, where the transform function can be attached.
  • a isResolved attribute, which is false when the promise has been started but not yet resolved.
  • propagation, so that transformations can be chained

However this support is fire-once. Once a promise is resolved, it is resolved forever. The reloading use-case is not supported.

Would be great to have a promise-like object that:

  1. its completion can be reset or re-triggered, which will invoke all the .then methods again
  2. its start can also be a notification that is also possible to re-trigger.

A promise-based implementation of this is possible, with a wrapper that on re-load discards the old promise and creates a new one and re-attaches all the .then() callbacks. This can also be implemented with signals:

  • a .resolved(successCallback, failureCallback, finalyCallback) signal that allows to register callbacks on load finish. This returns a new object with a .resolved method, so it can be chained as promises.
  • a .requested(callback) signal that allows to register a single method on request start

This implementation is a signal-based implementation.

asyncdata.js's People

Contributors

dtheodor avatar

Watchers

James Cloos avatar Mark Marijnissen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.