-
Notifications
You must be signed in to change notification settings - Fork 1
/
join.js
103 lines (80 loc) · 1.39 KB
/
join.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
/* eslint complexity: [ 2, 9 ] */
import Nothing from './Nothing'
import End from './End'
import domain from './_/domain'
var affected_now = domain.affected.is_now
export default function join (...args)
{
if (typeof args[0] === 'function')
{
var buds = args.slice(1)
var fn = args[0]
}
else
{
var buds = args.slice(0, -1)
var fn = args[args.length - 1]
}
var value = collect(buds, fn, true)
var next = buds[0].constructor(value)
if (value !== End)
{
/* compute function: */
var compute = Collect(buds, fn)
/* collect direct & reverse deps: */
domain.deps.register(next, buds, compute)
}
return next
}
function Collect (buds, fn)
{
if (buds.length === 1)
{
var bud = buds[0]
return () =>
{
return collect_only(bud, fn)
}
}
else
{
return () =>
{
return collect(buds, fn)
}
}
}
function collect_only (bud, fn, any_touched = false)
{
var value = bud.value
if (value === Nothing)
{
return Nothing
}
if (! (any_touched || affected_now(bud)))
{
return Nothing
}
return fn(value)
}
function collect (buds, fn, any_touched = false)
{
var L = buds.length
var args = []
for (var n = 0; (n < L); n++)
{
var bud = buds[n]
var value = bud.value
if (value === Nothing)
{
return Nothing
}
any_touched || (any_touched = affected_now(bud))
args.push(value)
}
if (! any_touched)
{
return Nothing
}
return fn(...args)
}