blob: 4aa193e6ba91e3f3d4ef1381f9beceb2a4bda973 [file] [log] [blame]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Observable_1 = require("../Observable");
var identity_1 = require("../util/identity");
var isScheduler_1 = require("../util/isScheduler");
function generate(initialStateOrOptions, condition, iterate, resultSelectorOrObservable, scheduler) {
var resultSelector;
var initialState;
if (arguments.length == 1) {
var options = initialStateOrOptions;
initialState = options.initialState;
condition = options.condition;
iterate = options.iterate;
resultSelector = options.resultSelector || identity_1.identity;
scheduler = options.scheduler;
}
else if (resultSelectorOrObservable === undefined || isScheduler_1.isScheduler(resultSelectorOrObservable)) {
initialState = initialStateOrOptions;
resultSelector = identity_1.identity;
scheduler = resultSelectorOrObservable;
}
else {
initialState = initialStateOrOptions;
resultSelector = resultSelectorOrObservable;
}
return new Observable_1.Observable(function (subscriber) {
var state = initialState;
if (scheduler) {
return scheduler.schedule(dispatch, 0, {
subscriber: subscriber,
iterate: iterate,
condition: condition,
resultSelector: resultSelector,
state: state
});
}
do {
if (condition) {
var conditionResult = void 0;
try {
conditionResult = condition(state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
if (!conditionResult) {
subscriber.complete();
break;
}
}
var value = void 0;
try {
value = resultSelector(state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
subscriber.next(value);
if (subscriber.closed) {
break;
}
try {
state = iterate(state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
} while (true);
return undefined;
});
}
exports.generate = generate;
function dispatch(state) {
var subscriber = state.subscriber, condition = state.condition;
if (subscriber.closed) {
return undefined;
}
if (state.needIterate) {
try {
state.state = state.iterate(state.state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
}
else {
state.needIterate = true;
}
if (condition) {
var conditionResult = void 0;
try {
conditionResult = condition(state.state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
if (!conditionResult) {
subscriber.complete();
return undefined;
}
if (subscriber.closed) {
return undefined;
}
}
var value;
try {
value = state.resultSelector(state.state);
}
catch (err) {
subscriber.error(err);
return undefined;
}
if (subscriber.closed) {
return undefined;
}
subscriber.next(value);
if (subscriber.closed) {
return undefined;
}
return this.schedule(state);
}
//# sourceMappingURL=generate.js.map