-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrowd.js
177 lines (152 loc) · 4.22 KB
/
arrowd.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function CpsA(cps) {
this.cps = cps; /* cps :: (x, k) → () */
}
CpsA.prototype.CpsA = function() {
return this;
}
CpsA.prototype.next = function(g) {
var f = this; g = g.CpsA();
/* CPS function composition */
return new CpsA(function(x, k) {
f.cps(x, function(y) {
g.cps(y, k);
});
});
}
CpsA.prototype.run = function(x) {
this.cps(x, function() { });
}
Function.prototype.CpsA = function() {
var f = this;
/* wrap f in CPS function */
return new CpsA(function(x, k) {
k(f(x));
});
}
function SimpleEventA(eventname) {
if (!( this instanceof SimpleEventA))
return new SimpleEventA(eventname);
this.eventname = eventname;
}
SimpleEventA.prototype = new CpsA(function(target, k) {
var f = this;
function handler(e) {
target.removeEventListener(
f.eventname, handler, false );
k(event );
}
target.bind(f.eventname, handler(e) );
});
function AsyncA(cps) { this.cps = cps; }
function ProgressA(){
if (!( this instanceof ProgressA)){
return new ProgressA();
}
this.cancellers = [];
this.observers = [];
};
AsyncA.prototype.AsyncA = function() { return this; };
AsyncA.prototype.next = function(g) {
var f = this; g = g.AsyncA();
return new AsyncA(function(x, p, k){
f.cps(x, p, function(y, q) {
g.cps(y, q, k );
});
});
};
AsyncA.prototype.run = function(x, p) {
p = p || new ProgressA();
this .cps(x, p, function (){});
return p;
};
Function.prototype.AsyncA = function() {
var f = this;
return new AsyncA(function(x,p,k){k(f(x),p);});
};
AsyncA.prototype.repeat = function(interval){
var f = this;
interval = interval || 0;
return new AsyncA(function rep(x,p,k){
return f.cps(x,p,function(y,q){
if(y.Repeat){
function cancel(){ clearTimeout(tid); }
q.addCanceller( cancel );
var tid = setTimeout(function(){
q.advance(cancel);
rep(y.value,q,k);
}, interval);
} else if(y.Done){
k(y.value, q);
} else {
throw new TypeError("Repeat or Done?");
}
});
});
};
function Repeat(x){ return { Repeat:true, value:x }; }
function Done(x){ return { Done:true, value:x }; }
ProgressA.prototype = new AsyncA(function(x, p, k) {
this.observers.push(function(y) { k(y, p); });
});
ProgressA.prototype.addCanceller = function( canceller ) {
this.cancellers .push( canceller );
};
ProgressA.prototype.advance = function( canceller ) {
var index = this.cancellers.indexOf( canceller );
if (index >= 0) {
this.cancellers.splice(index , 1);
}
while ( this.observers.length > 0){
this.observers.op()();
}
};
ProgressA.prototype.cancel = function() {
while ( this.cancellers.length > 0){
this.cancellers.pop()();
}
};
Function prototype.repeat = function(interval){
return this.AsyncA().repeat(interval);
}
AsyncA.prototype.or = function(g){
var f = this;
g = g.AsyncA();
return new AsyncA(function(x,p,k){
var p1 = new ProgressA();
var p2 = new ProgressA();
/* if one advances, cancel the other */
p1.next(function(){ p2.cancel();
p2 = null; }).run();
p2.next(function(){p1.cancel();
p1 = null; }).run();
function cancel(){
if(p1) p1.cancel();
if(p2) p2.cancel();
}
function join(y, q){
p.advance(cancel);
k(y,q);
}
p.addCanceller(cancel);
f.cps(x, p1, join);
g.cps(x, p2, join);
});
};
function EventA(eventname) {
if (!( this instanceof EventA))
return new EventA(eventname);
this.eventname = eventname;
};
EventA.prototype = new AsyncA(function(target, p, k) {
var f = this ;
function cancel() {
target.removeEventListener(f .eventname, handler, false );
}
function handler(event) {
p.advance(cancel );
cancel();
k(event, p);
}
p. addCanceller( cancel );
target.addEventListener( f.eventname, handler, false );
});