Giter Site home page Giter Site logo

solve's Introduction

solve

Recursively converts asynchronous data into static data.

NPM version Downloads Bower version

Build Status Coverage Status Dependency status Dev Dependency status

Usage

var solve = require('solve');

var stream = solve(data, function (result) {
	// called immediately, and whenever promises resolve or callbacks are called
});

// can add other listeners later
stream(function (result) {
	// do something
});

// can chain additional streams
var downstream = stream(function (result) {
	return result + 1;
});

downstream(function (newResult) {
	// do something
});

// later, when we don't want downstream to call callbacks anymore
downstream.destroy();

Example

var solve = require('solve');

var data = {
	foo: function(callback) {
		setTimeout(function () {
			callback('dynamic');
		}, 1);

		return 'static'
	},
	nested: function () {
		return function () {
			return function () {
				return 'deep';
			}
		}
	},
	merge: solve({
		promise: new Promise(function(resolve){
			resolve('done');
		})
	})
};

solve(data, function(data) {
	console.log(data);
});

This will output:

{ foo: 'static', nested: 'deep', merge: { promise: undefined } }
{ foo: 'static', nested: 'deep', merge: { promise: 'done' } }
{ foo: 'dynamic', nested: 'deep', merge: { promise: 'done' } }

Documentation

Read the tests for more details on what solve can do.

License

MIT

solve's People

Contributors

jesseskinner avatar

Stargazers

tg-z avatar Eric Clemmons avatar

Watchers

 avatar James Cloos avatar  avatar

solve's Issues

Delay solving until a listener has been added

It makes sense to use solve for certain models that load data from an api or server, but only used for a subset of the app.

It'd be helpful to be able to define a solve but not execute the server calls until or unless someone is listening. For example:

var solver = solve(function (callback) {
  // define how we get data, but don't call it right away
  api.getData({ id: 123 }, function (error, data) {
    callback(data);
  });
});

var renderList;

$('button').click(function () {
  if (!renderList) {
    // only at this point is the API method first called
    renderList = solver(function(data) {
      renderTemplate('#list', data);
    });
  }
});

API - allow ways to mute and umute callbacks

Perhaps there should be some new API to enable muting and unmuting streams.

We can add these methods as properties of the function that gets returned from solve.

var randomNumberStream = solve(function (callback) {
        setInterval(function () {
            callback(Math.random());
        }, 500);
});

var logRandomNumbers = randomNumberStream(function (number) {
    console.log(number);
});

// stop logging
logRandomNumbers.mute();

// continue logging
logRandomNumbers.unmute();

// stop any listeners on any random number stream
randomNumberStream.mute();

API - Allow a way to unsubscribe or destroy a solve stream

When using solve with single-page apps and data streams, it seems necessary to be able to remove a listener and/or destroy a stream as the user navigates to different parts of the app. Here's a contrived example:

// make a stream that sends the current time once a second
var timeStream = solve({
  time: function (callback) {
    function update() {
      var time = (new Date).toLocaleTimeString()
      callback(time)
    }
    update()
    setInterval(update, 1000)
  }
})

// later, when the user goes to the clock page
timeStream(function (data) {
  document.body.innerHTML = 'The time is currently ' + data.time
})

Once that final function is attached, there is no way to stop the body innerHTML from being overwritten every second. We could do a workaround by only rendering when the user is on the clock page (as demonstrated in #1), but for performance reasons, and to have a better separation of concerns, perhaps there needs to be a way to remove a listener once attached.

I suggest the following new API:

var timeRender = timeStream(function (data) {
  document.body.innerHTML = 'The time is currently ' + data.time
})

// timeRender and any downstream solves will be destroyed
timeRender.destroy()

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.