-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (131 loc) · 3.37 KB
/
index.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
import t, { is } from 'tst'
import sube from '../sube.js'
import { time, tick } from 'wait-please'
import v from 'value-ref'
import { effect, signal } from '@preact/signals-core'
t('Promise: next', async () => {
let arr = []
let p = new Promise(ok => setTimeout(() => ok(1)))
sube(p, v => arr.push(v), e => arr.push(e), () => arr.push('end'))
is(arr, [])
await time()
is(arr, [1, 'end'])
})
t('Promise: error', async () => {
let arr = []
let p = new Promise((ok, err) => setTimeout(() => { err(Error('xyz')) }))
sube(p, null, err => arr.push(err.message), done => arr.push(done))
is(arr, [])
await time()
is(arr, ['xyz'])
})
t('rxjs', async () => {
const arr = []
const { Subject } = await import('https://cdn.skypack.dev/rxjs')
const subject = new Subject();
const unsub = sube(subject, v => arr.push(v), err => arr.push(err), () => arr.push('end'))
is(arr, [])
subject.next(1);
is(arr, [1])
subject.next(2);
is(arr, [1, 2])
subject.complete()
is(arr, [1, 2, 'end'])
unsub()
})
t('observ', async () => {
const arr = []
const { default: Observable } = await import('https://cdn.skypack.dev/observ')
var v = Observable(1)
sube(v, v => arr.push(v))
is(arr, [])
v.set(2)
is(arr, [2])
})
t('observable', async () => {
const arr = []
const { default: Observable } = await import('https://cdn.skypack.dev/observable')
var v = Observable(1)
sube(v, v => arr.push(v))
is(arr, [1])
v(2)
is(arr, [1, 2])
})
t('asyncIterable', async () => {
const arr = []
const asyncIterable = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) return new Promise(ok => ok({ value: this.i++, done: false }));
return new Promise(ok => ok({ done: true }));
}
};
}
};
sube(asyncIterable, v => arr.push(v), err => err, v => arr.push('end'))
is(arr, [])
await tick()
is(arr, [0, 1])
await tick()
is(arr, [0, 1, 2, 'end'])
})
t('does not keep observer refs for mock', async () => {
let arr = []
let mock = { subscribe() { arr.push('sub'); return { unsubscribe: () => arr.push('unsub') } } }
let unsub = sube(mock, v => arr.push(v))
is(arr, ['sub'])
mock = null
await gc()
is(arr, ['sub', 'unsub'])
})
t('collecting callback doesnt invoke unsubscribe', async () => {
let arr = []
let mock = { subscribe() { arr.push('sub'); return { unsubscribe: () => arr.push('unsub') } } }
let cb = v => arr.push(v)
let unsub = sube(mock, cb)
is(arr, ['sub'])
cb = null
await gc()
is(arr, ['sub'])
})
t('does not keep observer refs for v', async () => {
let arr = []
let v0 = v(0),
// NOTE: the one below retains memory
v1 = v0 // v.from(v0, v => v),
sube(v1, v => arr.push(v), null, () => arr.push('end'))
is(arr, [0])
await gc()
is(arr, [0])
v1.value = 1
await tick()
is(arr, [0, 1])
v1 = v0 = null
await gc()
is(arr, [0, 1, 'end'])
})
t('does not keep observer refs for signal', async () => {
let arr = []
let s1 = signal(0)
const unsub = sube(s1, v => arr.push(v), null, () => arr.push('end'))
is(arr, [0])
await gc()
is(arr, [0])
s1.value = 1
await tick()
is(arr, [0, 1])
// NOTE: this is unwanted
unsub()
s1 = null
await gc()
is(arr, [0, 1, 'end'])
})
async function gc() {
// gc is async somehow
await time(50)
global.gc()
eval("%CollectGarbage('all')");
await time(50)
}