SIGN IN SIGN UP
Netflix / falcor UNCLAIMED

A JavaScript library for efficient data fetching

0 0 12 JavaScript
2015-08-25 08:58:37 -07:00
var Rx = require("rx");
var TriggerDataSource = function TriggerDataSource(response) {
this._triggers = [];
2015-08-25 08:58:37 -07:00
this._idx = -1;
if (Array.isArray(response)) {
this._response = response;
} else {
this._response = [response];
}
this._length = this._response.length;
};
TriggerDataSource.prototype = {
get: function(paths) {
var self = this;
return Rx.Observable.create(function(observer) {
self._triggers.push(function() {
var out = self._response[++self._idx % self._length];
if (typeof out === 'function') {
out = out();
}
observer.onNext(out);
observer.onCompleted();
});
});
},
trigger: function() {
this._triggers.forEach(function(t) {
t();
});
this._triggers = [];
}
};
module.exports = TriggerDataSource;