Skip to content

Commit 7e79ba4

Browse files
committed
spaces -> tabs. Bye history
1 parent f29d3ae commit 7e79ba4

22 files changed

Lines changed: 1029 additions & 3080 deletions

dist/mobservable.js

Lines changed: 0 additions & 2044 deletions
This file was deleted.

dist/mobservable.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/mobservable.min.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

dist/mobservable.min.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/api/autorun.ts

Lines changed: 128 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,166 @@
11
import {Lambda, once} from "../utils/utils";
2-
import {ValueMode, getValueModeFromValue} from "../types/modifiers";
2+
import {assertUnwrapped} from "../types/modifiers";
33
import Reaction from "../core/reaction";
44
import globalState, {isComputingDerivation} from "../core/globalstate";
55
import {observable} from "../api/observable";
66
import {IObservable, reportObserved} from "../core/observable";
77

88
/**
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+
*/
1515
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");
2221
if (scope)
23-
unwrappedView = unwrappedView.bind(scope);
24-
22+
view = view.bind(scope);
23+
2524
const reaction = new Reaction(view.name, function () {
26-
this.track(unwrappedView);
25+
this.track(view);
2726
});
2827
if (isComputingDerivation() || globalState.inTransaction > 0)
2928
globalState.pendingReactions.push(reaction);
3029
else
31-
reaction.runReaction();
30+
reaction.runReaction();
3231
/*
33-
let disposedPrematurely = false;
34-
let started = false;
32+
let disposedPrematurely = false;
33+
let started = false;
3534
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+
});
4241
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;
5150
*/
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;
5554
}
5655

5756
/**
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+
*/
6564
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;
8380
}
8481

8582
/**
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+
*/
9693
// TODO: remove this one
9794
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;
10097

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+
});
110107

111-
return once(() => {
112-
disposer();
113-
if (timeoutHandle)
114-
clearTimeout(timeoutHandle);
115-
});
108+
return once(() => {
109+
disposer();
110+
if (timeoutHandle)
111+
clearTimeout(timeoutHandle);
112+
});
116113
}
117114

118115
// Deprecate:
119116
export function autorunAsync<T>(view: () => T, effect: (latestValue : T ) => void, delay?:number, scope?: any): Lambda;
120117
export function autorunAsync(func: Lambda, delay?:number, scope?: any): Lambda;
121118
// Deprecate weird overload:
122119
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;
163130

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+
});
169166
}

src/api/expr.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import {isComputingDerivation} from "../core/globalstate";
22
import {observable} from "../api/observable";
33

44
/**
5-
* expr can be used to create temporarily views inside views.
6-
* This can be improved to improve performance if a value changes often, but usually doesn't affect the outcome of an expression.
7-
*
8-
* In the following example the expression prevents that a component is rerender _each time_ the selection changes;
9-
* instead it will only rerenders when the current todo is (de)selected.
10-
*
11-
* reactiveComponent((props) => {
12-
* const todo = props.todo;
13-
* const isSelected = mobservable.expr(() => props.viewState.selection === todo);
14-
* return <div className={isSelected ? "todo todo-selected" : "todo"}>{todo.title}</div>
15-
* });
16-
*
17-
*/
5+
* expr can be used to create temporarily views inside views.
6+
* This can be improved to improve performance if a value changes often, but usually doesn't affect the outcome of an expression.
7+
*
8+
* In the following example the expression prevents that a component is rerender _each time_ the selection changes;
9+
* instead it will only rerenders when the current todo is (de)selected.
10+
*
11+
* reactiveComponent((props) => {
12+
* const todo = props.todo;
13+
* const isSelected = mobservable.expr(() => props.viewState.selection === todo);
14+
* return <div className={isSelected ? "todo todo-selected" : "todo"}>{todo.title}</div>
15+
* });
16+
*
17+
*/
1818
export function expr<T>(expr: () => T, scope?):T {
19-
if (!isComputingDerivation())
20-
console.warn("[mobservable.expr] 'expr' should only be used inside other reactive functions.");
21-
// optimization: would be more efficient if the expr itself wouldn't be evaluated first on the next change, but just a 'changed' signal would be fired
22-
return observable(expr, scope) ();
19+
if (!isComputingDerivation())
20+
console.warn("[mobservable.expr] 'expr' should only be used inside other reactive functions.");
21+
// optimization: would be more efficient if the expr itself wouldn't be evaluated first on the next change, but just a 'changed' signal would be fired
22+
return observable(expr, scope) ();
2323
}

src/api/extras.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@ import {isObservable} from '../api/observable';
1515
import globalState from "../core/globalstate";
1616

1717
export interface IDependencyTree {
18-
id: number;
19-
name: string;
20-
dependencies?: IDependencyTree[];
18+
id: number;
19+
name: string;
20+
dependencies?: IDependencyTree[];
2121
}
2222

2323
export interface IObserverTree {
24-
id: number;
25-
name: string;
26-
observers?: IObserverTree[];
27-
listeners?: number; // amount of functions manually attached using an .observe method
24+
id: number;
25+
name: string;
26+
observers?: IObserverTree[];
27+
listeners?: number; // amount of functions manually attached using an .observe method
2828
}
2929

3030
export interface ITransitionEvent {
31-
id: number;
32-
name: string;
33-
state: string;
34-
changed: boolean;
35-
node: any; // TODO: IAtom;
31+
id: number;
32+
name: string;
33+
state: string;
34+
changed: boolean;
35+
node: any; // TODO: IAtom;
3636
}
3737

3838
/**
39-
* If strict is enabled, views are not allowed to modify the state.
40-
* This is a recommended practice, as it makes reasoning about your application simpler.
41-
*/
39+
* If strict is enabled, views are not allowed to modify the state.
40+
* This is a recommended practice, as it makes reasoning about your application simpler.
41+
*/
4242
export function allowStateChanges<T>(allowStateChanges: boolean, func:() => T):T {
43-
const prev = globalState.allowStateChanges;
44-
globalState.allowStateChanges = allowStateChanges;
45-
const res = func();
46-
globalState.allowStateChanges = prev;
47-
return res;
43+
const prev = globalState.allowStateChanges;
44+
globalState.allowStateChanges = allowStateChanges;
45+
const res = func();
46+
globalState.allowStateChanges = prev;
47+
return res;
4848
}
4949

5050

0 commit comments

Comments
 (0)