-
Notifications
You must be signed in to change notification settings - Fork 13
/
Observable.hx
225 lines (167 loc) · 5.34 KB
/
Observable.hx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package tink.state;
import tink.state.Promised;
using tink.CoreApi;
@:forward
abstract Observable<T>(ObservableObject<T>) from ObservableObject<T> to ObservableObject<T> {
public var value(get, never):T;
@:to inline function get_value()
return this.value;
public inline function new(get, changed)
this = new BasicObservable<T>(get, changed);
public function combine<A, R>(that:Observable<A>, f:T->A->R)
return new Observable<R>(
function () return f(this.value, that.value),
this.changed.join(that.changed)
);
public function join(that:Observable<T>) {
var lastA = null;
return combine(that, function (a, b) {
var ret =
if (lastA == a) b;
else a;
lastA = a;
return ret;
});
}
public function map<R>(f:Transform<T, R>)
return new Observable<R>(
function () return f(this.value),
this.changed
);
public function combineAsync<A, R>(that:Observable<A>, f:T->A->Promise<R>):Observable<Promised<R>>
return combine(that, f).mapAsync(function (x) return x);
public function mapAsync<R>(f:T->Promise<R>):Observable<Promised<R>> {
var ret = new State(Loading),
link:CallbackLink = null;
bind(function (data) {
link.dissolve();
ret.set(Loading);
link = f(data).handle(function (r) ret.set(switch r {
case Success(v): Done(v);
case Failure(v): Failed(v);
}));
});
return ret;
}
public function switchSync<R>(cases:Array<{ when: T->Bool, then: Lazy<Observable<R>> }>, dfault:Lazy<Observable<R>>):Observable<R> {
var trigger = Signal.trigger();
function fire(_)
trigger.trigger(Noise);
this.changed.handle(fire);
return new Observable(function () {
var matched = dfault,
value = value;
for (c in cases)
if (c.when(value)) {
matched = c.then;
break;
}
var ret = matched.get();
ret.changed.next().handle(fire);
return ret.value;
}, trigger);
}
public function bind(?options:{ ?direct: Bool }, cb:Callback<T>):CallbackLink
return
switch options {
case null | { direct: null | false }:
cb.invoke(this.value);
this.changed.handle(function () cb.invoke(this.value));
default:
var scheduled = false,
active = true,
update = function () if (active) {
cb.invoke(this.value);
scheduled = false;
}
function doSchedule() {
if (scheduled) return;
scheduled = true;
schedule(update);
}
doSchedule();
var link = this.changed.handle(doSchedule);
return function ()
if (active) {
active = false;
link.dissolve();
}
}
static var scheduled:Array<Void->Void> =
#if (js || tink_runloop)
[];
#else
null;
#end
static function schedule(f:Void->Void)
switch scheduled {
case null:
f();
case []:
scheduled.push(f);
#if tink_runloop
tink.RunLoop.current.atNextStep(updateAll);
#elseif js
js.Browser.window.requestAnimationFrame(function (_) updateAll());
#else
throw 'this should be unreachable';
#end
case v:
v.push(f);
}
static public function updateAll() {
var old = scheduled;
scheduled = null;
for (o in old) o();
scheduled = [];
}
@:noUsing @:from static public function const<T>(value:T):Observable<T>
return new ConstObservable(value);
}
//typedef Transform<T, R> = T->R;
@:callable
abstract Transform<T, R>(T->R) {
@:from static function ofNaive<T, R>(f:T->R):Transform<Promised<T>, Promised<R>>
return function (p) return switch p {
case Failed(e): Failed(e);
case Loading: Loading;
case Done(v): Done(f(v));
}
@:from static function ofExact<T, R>(f:T->R):Transform<T, R>
return cast f;
}
interface ObservableObject<T> {
public var changed(get, never):Signal<Noise>;
public var value(get, never):T;
}
private class ConstObservable<T> implements ObservableObject<T> {
static var NEVER = new Signal<Noise>(function (_) return null);
public var value(get, null):T;
inline function get_value()
return value;
public var changed(get, null):Signal<Noise>;
inline function get_changed()
return NEVER;
public function new(value)
this.value = value;
}
private class BasicObservable<T> implements ObservableObject<T> {
var getValue:Void->T;
var valid:Bool;
var cache:T;
public var changed(get, null):Signal<Noise>;
inline function get_changed()
return changed;
public var value(get, never):T;
function get_value() {
if (!valid) {
cache = getValue();
valid = true;
}
return cache;
}
public function new(getValue, changed:Signal<Noise>) {
this.getValue = getValue;
this.changed = changed.filter(function (_) return valid && !(valid = false));//the things you do for neat output ...
}
}