2015-08-25 08:58:37 -07:00
|
|
|
var Rx = require("rx");
|
2015-08-24 20:12:00 -07:00
|
|
|
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;
|
2015-08-24 20:12:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TriggerDataSource.prototype = {
|
|
|
|
|
get: function(paths) {
|
|
|
|
|
var self = this;
|
|
|
|
|
return Rx.Observable.create(function(observer) {
|
|
|
|
|
self._triggers.push(function() {
|
2015-08-31 09:01:09 -07:00
|
|
|
var out = self._response[++self._idx % self._length];
|
|
|
|
|
if (typeof out === 'function') {
|
|
|
|
|
out = out();
|
|
|
|
|
}
|
|
|
|
|
observer.onNext(out);
|
2015-08-24 20:12:00 -07:00
|
|
|
observer.onCompleted();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
trigger: function() {
|
|
|
|
|
this._triggers.forEach(function(t) {
|
|
|
|
|
t();
|
|
|
|
|
});
|
|
|
|
|
this._triggers = [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = TriggerDataSource;
|