Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for asynchronous computed Observable #9

Closed
pago opened this issue Jan 25, 2014 · 1 comment
Closed

Add support for asynchronous computed Observable #9

pago opened this issue Jan 25, 2014 · 1 comment

Comments

@pago
Copy link
Collaborator

pago commented Jan 25, 2014

If the value generated by an Observable.computed (on read) is a Promise, we should notify its listeners only after the Promise has been fulfilled. This will open various possibilities when used in combination with Observable#map, i.e.

var url = Observable('/foo.json');
var json = url.map(function(url) {
  return xhrPromise(url);
}).map(function(content) {
  return JSON.parse(content);
});
@ghost ghost assigned pago Jan 25, 2014
@pago
Copy link
Collaborator Author

pago commented Feb 11, 2014

Actually, that won't work and would require a huge amount of hacking. There's a better way to do it, though:

Add AsyncObservable, which is similar to a calculated observable expect that it notifies its listeners only after its value has been resolved. There should not be automatic dependency management for this type of observable (unlike ComputedObservable).

Usage:

var url = Observable('/foo.json');
url.async(function(urlObservable) {
  // we pass in the observable, not its value, to allow lazy evaluation
  // in case its a computed observable
  return xhrPromise(urlObservable.get());
}).map(function(content) {
  return JSON.parse(content);
});

Such an AsyncObservable starts with a value of null and is expected to return a Promise. Once that Promise is resolved, it will store the value and notify the listeners. If its dependency changes while a computation promise is running, it will ignore those changes. Maybe add an option to reevaluate the value once the running promise is resolved.

Primary use case: Make it much easier to delay the computation of an observable (i.e. to do so only on an animation frame).

pago added a commit that referenced this issue Feb 14, 2014
@pago pago closed this as completed Feb 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant