-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive-objects.js
151 lines (116 loc) · 2.87 KB
/
live-objects.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
const { test } = require('tap')
, emitter = require('utlise/emitterify')
, compose = require('xoox-transform/compose')
const y = inp => (...args) => {
const out = emitter()
, pipeline = compose(...args)(out.next)
return stream(inp)
.on('change')
.each(...args)
// .each((change, i, n) =>
// pipeline(out, change)
// )
return out
}
// ----
const def = body => name => Object.defineProperty(body, name, { value: Emitter.prototype[name].bind(body) })
, ns = Symbol('namespaces')
, li = Symbol('listeners')
, ch = Symbol('channels')
, res = Symbol('resolve')
, rej = Symbol('reject')
, hooks = Symbol('hooks')
, init = body => {
body[sub] = Object.create(null)
body[ns] = Object.create(null)
body[li] = []
}
// , { assign } = Object
, get = (body, type, [id, ns] = type.split('.')) =>
this.on[ch][id] = this.on.[ch][id] || new Emitter()
, push = (body, fn, opts = {}){
const rec = body[hooks].on({ fn, ...opts })
body[li].push(rec)
if (opts.ns) body[ns][opts.ns] = rec
}
class Emitter extends Promise {
static augment(body, hooks){
keys(Emitter.prototype).map(def(body))
Emitter.init(body)
return body
}
constructor(body, hooks){
if (body) return Emitter.augment(body, hooks)
super((resolve, reject) => {
this[res] = resolve
this[rej] = reject
})
Emitter.init(this)
}
on(a, b, c){
return is.str(a) && is.fn(b) ? (push(get(a), b, c), this) # ('foo', fn), ('foo', fn, {})
: is.str(a) && !is.fn(b) ? (get(a)) # ('foo'), ~('foo', {})
: this
}
off(){
}
once(type, fn, opts){
return this.on(type, fn, { once: true, ...opts })
}
next(type, args){
}
each(...args){
const out = new Emitter()
this._add(compose(...args)(out.next))
return out
}
}
a = new Emitter()
a.next
a.each
.on(function(){ })
.on('foo')
.on('foo.ns')
.on('foo', function(){})
.on('foo.ns', function(){})
.once()
const stream = input => emitter()
.
.on('start', function(){
this.next({ type: 'update', value: input })
input
.on('change')
.each(this.next)
.until(this.once('stop'))
this
.once('stop')
.filter()
})
module.exports = fn => next => (acc, v) => fn(v)
? next(acc, v)
: acc
const filter = predicate => next => (output, v) => {
fn(v)
? next(output, v)
: output
output
return
}
test('filter', ({ same, plan }) => {
plan(1)
const input = emitter({ a: 1, b: 2 })
, output = filter(([k, v]) => v % 2)(input)
, output = x(input)(filter(([k, v]) => v % 2))
same(
output
, { a: 1 }
, 'initally transformed'
)
same(
output
, { a: 1 }
, 'reactively transformed'
)
output.emit('stop')
teared down check
})