1 | import { Observable } from '../Observable';
|
---|
2 | import { identity } from '../util/identity';
|
---|
3 | import { isScheduler } from '../util/isScheduler';
|
---|
4 | export function generate(initialStateOrOptions, condition, iterate, resultSelectorOrObservable, scheduler) {
|
---|
5 | let resultSelector;
|
---|
6 | let initialState;
|
---|
7 | if (arguments.length == 1) {
|
---|
8 | const options = initialStateOrOptions;
|
---|
9 | initialState = options.initialState;
|
---|
10 | condition = options.condition;
|
---|
11 | iterate = options.iterate;
|
---|
12 | resultSelector = options.resultSelector || identity;
|
---|
13 | scheduler = options.scheduler;
|
---|
14 | }
|
---|
15 | else if (resultSelectorOrObservable === undefined || isScheduler(resultSelectorOrObservable)) {
|
---|
16 | initialState = initialStateOrOptions;
|
---|
17 | resultSelector = identity;
|
---|
18 | scheduler = resultSelectorOrObservable;
|
---|
19 | }
|
---|
20 | else {
|
---|
21 | initialState = initialStateOrOptions;
|
---|
22 | resultSelector = resultSelectorOrObservable;
|
---|
23 | }
|
---|
24 | return new Observable(subscriber => {
|
---|
25 | let state = initialState;
|
---|
26 | if (scheduler) {
|
---|
27 | return scheduler.schedule(dispatch, 0, {
|
---|
28 | subscriber,
|
---|
29 | iterate,
|
---|
30 | condition,
|
---|
31 | resultSelector,
|
---|
32 | state
|
---|
33 | });
|
---|
34 | }
|
---|
35 | do {
|
---|
36 | if (condition) {
|
---|
37 | let conditionResult;
|
---|
38 | try {
|
---|
39 | conditionResult = condition(state);
|
---|
40 | }
|
---|
41 | catch (err) {
|
---|
42 | subscriber.error(err);
|
---|
43 | return undefined;
|
---|
44 | }
|
---|
45 | if (!conditionResult) {
|
---|
46 | subscriber.complete();
|
---|
47 | break;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | let value;
|
---|
51 | try {
|
---|
52 | value = resultSelector(state);
|
---|
53 | }
|
---|
54 | catch (err) {
|
---|
55 | subscriber.error(err);
|
---|
56 | return undefined;
|
---|
57 | }
|
---|
58 | subscriber.next(value);
|
---|
59 | if (subscriber.closed) {
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | try {
|
---|
63 | state = iterate(state);
|
---|
64 | }
|
---|
65 | catch (err) {
|
---|
66 | subscriber.error(err);
|
---|
67 | return undefined;
|
---|
68 | }
|
---|
69 | } while (true);
|
---|
70 | return undefined;
|
---|
71 | });
|
---|
72 | }
|
---|
73 | function dispatch(state) {
|
---|
74 | const { subscriber, condition } = state;
|
---|
75 | if (subscriber.closed) {
|
---|
76 | return undefined;
|
---|
77 | }
|
---|
78 | if (state.needIterate) {
|
---|
79 | try {
|
---|
80 | state.state = state.iterate(state.state);
|
---|
81 | }
|
---|
82 | catch (err) {
|
---|
83 | subscriber.error(err);
|
---|
84 | return undefined;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | else {
|
---|
88 | state.needIterate = true;
|
---|
89 | }
|
---|
90 | if (condition) {
|
---|
91 | let conditionResult;
|
---|
92 | try {
|
---|
93 | conditionResult = condition(state.state);
|
---|
94 | }
|
---|
95 | catch (err) {
|
---|
96 | subscriber.error(err);
|
---|
97 | return undefined;
|
---|
98 | }
|
---|
99 | if (!conditionResult) {
|
---|
100 | subscriber.complete();
|
---|
101 | return undefined;
|
---|
102 | }
|
---|
103 | if (subscriber.closed) {
|
---|
104 | return undefined;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | let value;
|
---|
108 | try {
|
---|
109 | value = state.resultSelector(state.state);
|
---|
110 | }
|
---|
111 | catch (err) {
|
---|
112 | subscriber.error(err);
|
---|
113 | return undefined;
|
---|
114 | }
|
---|
115 | if (subscriber.closed) {
|
---|
116 | return undefined;
|
---|
117 | }
|
---|
118 | subscriber.next(value);
|
---|
119 | if (subscriber.closed) {
|
---|
120 | return undefined;
|
---|
121 | }
|
---|
122 | return this.schedule(state);
|
---|
123 | }
|
---|
124 | //# sourceMappingURL=generate.js.map |
---|