|
1 | 1 | import {Lambda, once} from "../utils/utils"; |
2 | | -import {ValueMode, getValueModeFromValue} from "../types/modifiers"; |
| 2 | +import {assertUnwrapped} from "../types/modifiers"; |
3 | 3 | import Reaction from "../core/reaction"; |
4 | 4 | import globalState, {isComputingDerivation} from "../core/globalstate"; |
5 | 5 | import {observable} from "../api/observable"; |
6 | 6 | import {IObservable, reportObserved} from "../core/observable"; |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Creates a reactive view and keeps it alive, so that the view is always |
10 | | - * updated if one of the dependencies changes, even when the view is not further used by something else. |
11 | | - * @param view The reactive view |
12 | | - * @param scope (optional) |
13 | | - * @returns disposer function, which can be used to stop the view from being updated in the future. |
14 | | - */ |
| 9 | + * Creates a reactive view and keeps it alive, so that the view is always |
| 10 | + * updated if one of the dependencies changes, even when the view is not further used by something else. |
| 11 | + * @param view The reactive view |
| 12 | + * @param scope (optional) |
| 13 | + * @returns disposer function, which can be used to stop the view from being updated in the future. |
| 14 | + */ |
15 | 15 | export function autorun(view:Lambda, scope?:any):Lambda { |
16 | | - // TODO: don't unwrap and such |
17 | | - var [mode, unwrappedView] = getValueModeFromValue(view,ValueMode.Recursive); |
18 | | - if (typeof unwrappedView !== "function") |
19 | | - throw new Error("[mobservable.autorun] expects a function"); |
20 | | - if (unwrappedView.length !== 0) |
21 | | - throw new Error("[mobservable.autorun] expects a function without arguments"); |
| 16 | + assertUnwrapped(view, "autorun methods cannot have modifiers"); |
| 17 | + if (typeof view !== "function") |
| 18 | + throw new Error("[mobservable.autorun] expects a function"); |
| 19 | + if (view.length !== 0) |
| 20 | + throw new Error("[mobservable.autorun] expects a function without arguments"); |
22 | 21 | if (scope) |
23 | | - unwrappedView = unwrappedView.bind(scope); |
24 | | - |
| 22 | + view = view.bind(scope); |
| 23 | + |
25 | 24 | const reaction = new Reaction(view.name, function () { |
26 | | - this.track(unwrappedView); |
| 25 | + this.track(view); |
27 | 26 | }); |
28 | 27 | if (isComputingDerivation() || globalState.inTransaction > 0) |
29 | 28 | globalState.pendingReactions.push(reaction); |
30 | 29 | else |
31 | | - reaction.runReaction(); |
| 30 | + reaction.runReaction(); |
32 | 31 | /* |
33 | | - let disposedPrematurely = false; |
34 | | - let started = false; |
| 32 | + let disposedPrematurely = false; |
| 33 | + let started = false; |
35 | 34 |
|
36 | | - runAfterTransaction(() => { |
37 | | - if (!disposedPrematurely) { |
38 | | - // TODO: restore observable.setRefCount(+1); |
39 | | - started = true; |
40 | | - } |
41 | | - }); |
| 35 | + runAfterTransaction(() => { |
| 36 | + if (!disposedPrematurely) { |
| 37 | + // TODO: restore observable.setRefCount(+1); |
| 38 | + started = true; |
| 39 | + } |
| 40 | + }); |
42 | 41 |
|
43 | | - const disposer = once(() => { |
44 | | - if (started) { |
45 | | - // TODO: restore observable.setRefCount(-1); |
46 | | - }else |
47 | | - disposedPrematurely = true; |
48 | | - }); |
49 | | - (<any>disposer).$mobservable = observable; |
50 | | - return disposer; |
| 42 | + const disposer = once(() => { |
| 43 | + if (started) { |
| 44 | + // TODO: restore observable.setRefCount(-1); |
| 45 | + }else |
| 46 | + disposedPrematurely = true; |
| 47 | + }); |
| 48 | + (<any>disposer).$mobservable = observable; |
| 49 | + return disposer; |
51 | 50 | */ |
52 | | - const disposer = () => reaction.dispose(); |
53 | | - (<any>disposer).$mobservable = reaction; |
54 | | - return disposer; |
| 51 | + const disposer = () => reaction.dispose(); |
| 52 | + (<any>disposer).$mobservable = reaction; |
| 53 | + return disposer; |
55 | 54 | } |
56 | 55 |
|
57 | 56 | /** |
58 | | - * Similar to 'observer', observes the given predicate until it returns true. |
59 | | - * Once it returns true, the 'effect' function is invoked an the observation is cancelled. |
60 | | - * @param predicate |
61 | | - * @param effect |
62 | | - * @param scope (optional) |
63 | | - * @returns disposer function to prematurely end the observer. |
64 | | - */ |
| 57 | + * Similar to 'observer', observes the given predicate until it returns true. |
| 58 | + * Once it returns true, the 'effect' function is invoked an the observation is cancelled. |
| 59 | + * @param predicate |
| 60 | + * @param effect |
| 61 | + * @param scope (optional) |
| 62 | + * @returns disposer function to prematurely end the observer. |
| 63 | + */ |
65 | 64 | export function autorunUntil(predicate: ()=>boolean, effect: Lambda, scope?: any): Lambda { |
66 | | - // TODO: rename to when |
67 | | - // TODO: use Reaction class |
68 | | - let disposeImmediately = false; |
69 | | - const disposer = autorun(() => { |
70 | | - if (predicate.call(scope)) { |
71 | | - if (disposer) |
72 | | - disposer(); |
73 | | - else |
74 | | - disposeImmediately = true; |
75 | | - // TODO:untracked(() => |
76 | | - effect.call(scope) |
77 | | - //); |
78 | | - } |
79 | | - }); |
80 | | - if (disposeImmediately) |
81 | | - disposer(); |
82 | | - return disposer; |
| 65 | + // TODO: rename to when |
| 66 | + // TODO: use Reaction class |
| 67 | + let disposeImmediately = false; |
| 68 | + const disposer = autorun(() => { |
| 69 | + if (predicate.call(scope)) { |
| 70 | + if (disposer) |
| 71 | + disposer(); |
| 72 | + else |
| 73 | + disposeImmediately = true; |
| 74 | + effect.call(scope) |
| 75 | + } |
| 76 | + }); |
| 77 | + if (disposeImmediately) |
| 78 | + disposer(); |
| 79 | + return disposer; |
83 | 80 | } |
84 | 81 |
|
85 | 82 | /** |
86 | | - * Once the view triggers, effect will be scheduled in the background. |
87 | | - * If observer triggers multiple times, effect will still be triggered only once, so it achieves a similar effect as transaction. |
88 | | - * This might be useful for stuff that is expensive and doesn't need to happen synchronously; such as server communication. |
89 | | - * Afther the effect has been fired, it can be scheduled again if the view is triggered in the future. |
90 | | - * |
91 | | - * @param view to observe. If it returns a value, the latest returned value will be passed into the scheduled effect. |
92 | | - * @param the effect that will be executed, a fixed amount of time after the first trigger of 'view'. |
93 | | - * @param delay, optional. After how many milleseconds the effect should fire. |
94 | | - * @param scope, optional, the 'this' value of 'view' and 'effect'. |
95 | | - */ |
| 83 | + * Once the view triggers, effect will be scheduled in the background. |
| 84 | + * If observer triggers multiple times, effect will still be triggered only once, so it achieves a similar effect as transaction. |
| 85 | + * This might be useful for stuff that is expensive and doesn't need to happen synchronously; such as server communication. |
| 86 | + * Afther the effect has been fired, it can be scheduled again if the view is triggered in the future. |
| 87 | + * |
| 88 | + * @param view to observe. If it returns a value, the latest returned value will be passed into the scheduled effect. |
| 89 | + * @param the effect that will be executed, a fixed amount of time after the first trigger of 'view'. |
| 90 | + * @param delay, optional. After how many milleseconds the effect should fire. |
| 91 | + * @param scope, optional, the 'this' value of 'view' and 'effect'. |
| 92 | + */ |
96 | 93 | // TODO: remove this one |
97 | 94 | function autorunAsyncDeprecated<T>(view: () => T, effect: (latestValue : T ) => void, delay:number = 1, scope?: any): Lambda { |
98 | | - var latestValue: T = undefined; |
99 | | - var timeoutHandle; |
| 95 | + let latestValue: T = undefined; |
| 96 | + let timeoutHandle; |
100 | 97 |
|
101 | | - const disposer = autorun(() => { |
102 | | - latestValue = view.call(scope); |
103 | | - if (!timeoutHandle) { |
104 | | - timeoutHandle = setTimeout(() => { |
105 | | - effect.call(scope, latestValue); |
106 | | - timeoutHandle = null; |
107 | | - }, delay); |
108 | | - } |
109 | | - }); |
| 98 | + const disposer = autorun(() => { |
| 99 | + latestValue = view.call(scope); |
| 100 | + if (!timeoutHandle) { |
| 101 | + timeoutHandle = setTimeout(() => { |
| 102 | + effect.call(scope, latestValue); |
| 103 | + timeoutHandle = null; |
| 104 | + }, delay); |
| 105 | + } |
| 106 | + }); |
110 | 107 |
|
111 | | - return once(() => { |
112 | | - disposer(); |
113 | | - if (timeoutHandle) |
114 | | - clearTimeout(timeoutHandle); |
115 | | - }); |
| 108 | + return once(() => { |
| 109 | + disposer(); |
| 110 | + if (timeoutHandle) |
| 111 | + clearTimeout(timeoutHandle); |
| 112 | + }); |
116 | 113 | } |
117 | 114 |
|
118 | 115 | // Deprecate: |
119 | 116 | export function autorunAsync<T>(view: () => T, effect: (latestValue : T ) => void, delay?:number, scope?: any): Lambda; |
120 | 117 | export function autorunAsync(func: Lambda, delay?:number, scope?: any): Lambda; |
121 | 118 | // Deprecate weird overload: |
122 | 119 | export function autorunAsync<T>(func: Lambda | {():T}, delay:number | {(x:T):void} = 1, scope?: any): Lambda { |
123 | | - if (typeof delay === "function") { |
124 | | - console.warn("[mobservable] autorun(func, func) is deprecated and will removed in 2.0"); |
125 | | - return autorunAsyncDeprecated.apply(null, arguments); |
126 | | - } |
127 | | - let shouldRun = false; |
128 | | - let tickScheduled = false; |
129 | | - let tick = observable(0); |
130 | | - let observedValues: IObservable[] = []; |
131 | | - let disposer: Lambda; |
132 | | - let isDisposed = false; |
133 | | - |
134 | | - function schedule(f: Lambda) { |
135 | | - setTimeout(f, delay); |
136 | | - } |
137 | | - |
138 | | - function doTick() { |
139 | | - tickScheduled = false; |
140 | | - shouldRun = true; |
141 | | - tick(tick() + 1); |
142 | | - } |
143 | | - |
144 | | - disposer = autorun(() => { |
145 | | - if (isDisposed) |
146 | | - return; |
147 | | - tick(); // observe so that autorun fires on next tick |
148 | | - if (shouldRun) { |
149 | | - func.call(scope); |
150 | | - observedValues = (<any>disposer).$mobservable.observing; |
151 | | - shouldRun = false; |
152 | | - } else { |
153 | | - // keep observed values eager, probably cheaper then forgetting |
154 | | - // about the value and later re-evaluating lazily, |
155 | | - // probably cheaper when computations are expensive |
156 | | - observedValues.forEach(o => reportObserved(o)); |
157 | | - if (!tickScheduled) { |
158 | | - tickScheduled = true; |
159 | | - schedule(doTick); |
160 | | - } |
161 | | - } |
162 | | - }); |
| 120 | + if (typeof delay === "function") { |
| 121 | + console.warn("[mobservable] autorun(func, func) is deprecated and will removed in 2.0"); |
| 122 | + return autorunAsyncDeprecated.apply(null, arguments); |
| 123 | + } |
| 124 | + let shouldRun = false; |
| 125 | + let tickScheduled = false; |
| 126 | + let tick = observable(0); |
| 127 | + let observedValues: IObservable[] = []; |
| 128 | + let disposer: Lambda; |
| 129 | + let isDisposed = false; |
163 | 130 |
|
164 | | - return once(() => { |
165 | | - isDisposed = true; // short-circuit any pending calculation |
166 | | - if (disposer) |
167 | | - disposer(); |
168 | | - }); |
| 131 | + function schedule(f: Lambda) { |
| 132 | + setTimeout(f, delay); |
| 133 | + } |
| 134 | + |
| 135 | + function doTick() { |
| 136 | + tickScheduled = false; |
| 137 | + shouldRun = true; |
| 138 | + tick(tick() + 1); |
| 139 | + } |
| 140 | + |
| 141 | + disposer = autorun(() => { |
| 142 | + if (isDisposed) |
| 143 | + return; |
| 144 | + tick(); // observe so that autorun fires on next tick |
| 145 | + if (shouldRun) { |
| 146 | + func.call(scope); |
| 147 | + observedValues = (<any>disposer).$mobservable.observing; |
| 148 | + shouldRun = false; |
| 149 | + } else { |
| 150 | + // keep observed values eager, probably cheaper then forgetting |
| 151 | + // about the value and later re-evaluating lazily, |
| 152 | + // probably cheaper when computations are expensive |
| 153 | + observedValues.forEach(o => reportObserved(o)); |
| 154 | + if (!tickScheduled) { |
| 155 | + tickScheduled = true; |
| 156 | + schedule(doTick); |
| 157 | + } |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + return once(() => { |
| 162 | + isDisposed = true; // short-circuit any pending calculation |
| 163 | + if (disposer) |
| 164 | + disposer(); |
| 165 | + }); |
169 | 166 | } |
0 commit comments