Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit d63c286

Browse files
committed
Don't rely on module.exports
1 parent 7ead6f0 commit d63c286

File tree

3 files changed

+103
-268
lines changed

3 files changed

+103
-268
lines changed

src/FRP/Behavior.js

Lines changed: 44 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,67 @@
11
"use strict";
22

3-
var Event = require('FRP/Event').Event;
4-
5-
/**
6-
* Live :: forall a. (-> a) -> Live a
7-
*/
8-
var Live = function(get) {
9-
10-
this.get = get;
11-
};
12-
13-
module.exports.Live = Live;
14-
15-
/**
16-
* Behavior :: forall a. (-> Live a) -> Behavior a
17-
*/
18-
var Behavior = function(subscribe) {
19-
20-
this.subscribe = subscribe;
21-
};
22-
23-
/**
24-
* Behavior.pure :: forall a. a -> Behavior a
25-
*/
26-
Behavior.pure = function(a) {
27-
28-
return new Behavior(function() {
29-
30-
return new Live(function() {
31-
3+
exports.pureImpl = function (a) {
4+
return function() {
5+
return function() {
326
return a;
33-
});
34-
});
35-
};
36-
37-
/**
38-
* Behavior.map :: forall a b. (Behavior a, a -> b) -> Behavior b
39-
*/
40-
Behavior.map = function(b1, f) {
41-
42-
return new Behavior(function() {
43-
44-
var live = b1.subscribe();
45-
46-
return new Live(function() {
47-
48-
return f(live.get());
49-
});
50-
});
51-
};
52-
53-
/**
54-
* Behavior.zip :: forall a b c. (Behavior a, Behavior b, (a, b) -> c) -> Behavior c
55-
*/
56-
Behavior.zip = function (b1, b2, f) {
57-
58-
return new Behavior(function() {
59-
60-
var l1 = b1.subscribe();
61-
var l2 = b2.subscribe();
62-
63-
return new Live(function() {
64-
65-
return f(l1.get(), l2.get());
66-
});
67-
});
68-
};
69-
70-
/**
71-
* Behavior.step :: forall a. (a, Event a) -> Behavior a
72-
*/
73-
Behavior.step = function(a, e) {
74-
75-
return new Behavior(function() {
76-
77-
var latest = a;
78-
79-
e.subscribe(function(value) {
80-
81-
latest = value;
82-
});
83-
84-
return new Live(function() {
85-
86-
return latest;
87-
});
88-
});
89-
};
90-
91-
/**
92-
* Behavior.sample :: forall a b c. (Behavior a, Event b, (a, b) -> c) -> Event c
93-
*/
94-
Behavior.sample = function(b1, e, f) {
95-
96-
return new Event(function(sub) {
97-
98-
var live = b1.subscribe();
99-
100-
e.subscribe(function(value) {
101-
102-
sub(f(live.get(), value));
103-
});
104-
});
7+
};
8+
};
1059
};
10610

107-
module.exports.Behavior = Behavior;
108-
109-
exports.pureImpl = function (a) {
110-
return Behavior.pure(a);
111-
}
112-
11311
exports.mapImpl = function (f) {
114-
return function(e) {
115-
return Behavior.map(e, f);
12+
return function(b) {
13+
return function() {
14+
var live = b();
15+
16+
return function() {
17+
return f(live());
18+
};
19+
};
11620
};
117-
}
21+
};
11822

11923
exports.zip = function (f) {
12024
return function (b1) {
12125
return function (b2) {
122-
return Behavior.zip(b1, b2, function(a, b) {
123-
return f(a)(b);
124-
});
26+
return function() {
27+
var l1 = b1();
28+
var l2 = b2();
29+
30+
return function() {
31+
return f(l1())(l2());
32+
};
33+
};
12534
};
12635
};
127-
}
36+
};
12837

12938
exports.step = function (a) {
13039
return function (e) {
131-
return Behavior.step(a, e);
40+
return function() {
41+
var latest = a;
42+
43+
e(function(value) {
44+
latest = value;
45+
});
46+
47+
return function() {
48+
return latest;
49+
};
50+
};
13251
};
133-
}
52+
};
13453

13554
exports.sample = function (f) {
13655
return function(b) {
13756
return function(e) {
138-
return Behavior.sample(b, e, function(a, b) {
139-
return f(a)(b);
140-
});
57+
return function(sub) {
58+
var live = b();
59+
60+
e(function(value) {
61+
62+
sub(f(live())(value));
63+
});
64+
};
14165
};
14266
};
143-
}
67+
};

0 commit comments

Comments
 (0)