-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.js
76 lines (57 loc) · 1.38 KB
/
updater.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Delphi like updater
var update = new Updater(null, () => {
console.log('update');
});
function sub() {
update.do( () => console.log('sub'));
}
update.begin();
try {
sub();
}
finally {
update.end();
}
expected output:
> sub
> update
- up = new Updater(this, update_function)
- up.begin()
- up.end()
- up.do( function ) // call function in a block: `try{ up.begin(); func() } finally { up.end(); }`
- up.do() // triggers an update notification
*/
export default
// modules.export =
function Updater ( that, update ) {
var counter = 0;
update = update.bind(that);
return {
begin () {
return counter += 1;
},
end () {
counter -= 1;
if (counter <= 0) {
update();
return true;
}
return false;
},
do (func) {
counter += 1; // begin
try {
if (func) {
func = func.bind(that);
func();
}
}
finally {
counter -= 1; // end
if (counter <= 0) update();
}
return counter === 0;
}
}
}