-
Notifications
You must be signed in to change notification settings - Fork 0
/
on.js
136 lines (109 loc) · 3.88 KB
/
on.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
import { forEach, fromObs, pipe, take } from 'callbag-basics';
import { eachValueFrom } from 'rxjs-for-await';
import { first } from 'rxjs/operators';
import { from } from 'rxjs';
import { fromESObservable as fromESObservableBaconJs } from 'baconjs';
import { fromESObservable as fromESObservableKefirJs } from 'kefir';
import h from 'hyperf';
import { map } from '../helpers/map';
import { on } from '../../src/module';
import xs from 'xstream';
describe('on', () => {
let htmlElement;
afterEach(() => htmlElement.remove());
beforeEach(() => {
htmlElement = document.createElement('a');
setTimeout(() => htmlElement.dispatchEvent(new MouseEvent('click')));
});
it('should work with RxJS', (done) => {
from(on(htmlElement, 'click'))
.pipe(first())
.subscribe((event) => {
expect(event).to.be.an.instanceof(MouseEvent);
done();
});
});
it('should work with XStream', (done) => {
xs.fromObservable(on(htmlElement, 'click'))
.take(1)
.subscribe({
next(event) {
expect(event).to.be.an.instanceof(MouseEvent);
done();
}
});
});
it('should work with callbags', (done) => {
pipe(
fromObs(on(htmlElement, 'click')),
take(1),
forEach((event) => {
expect(event).to.be.an.instanceof(MouseEvent);
done();
})
);
});
it('should work with Bacon.js', (done) => {
fromESObservableBaconJs(on(htmlElement, 'click'))
.first()
.onValue((event) => {
expect(event).to.be.an.instanceof(MouseEvent);
done();
});
});
it('should work with Kefir.js', (done) => {
fromESObservableKefirJs(on(htmlElement, 'click'))
.take(1)
.onValue((event) => {
expect(event).to.be.an.instanceof(MouseEvent);
done();
});
});
it('should work with rxjs-for-await', async () => {
const source$ = from(on(htmlElement, 'click'));
// eslint-disable-next-line no-unreachable-loop
for await (const event of eachValueFrom(source$)) {
expect(event).to.be.an.instanceof(MouseEvent);
break;
}
});
describe('with a finalization registry', () => {
let finalizationRegistry;
let whenCollected;
afterEach(function (done) {
this.timeout(0);
const arrayBuffers = [];
const interval = setInterval(() => {
try {
arrayBuffers.push(
new ArrayBuffer(arrayBuffers.length === 0 ? 100 : arrayBuffers[arrayBuffers.length - 1].byteLength * 10)
);
} catch {
arrayBuffers.pop();
}
}, 100);
whenCollected = () => {
clearInterval(interval);
done();
};
});
// eslint-disable-next-line no-undef
beforeEach(() => (finalizationRegistry = new FinalizationRegistry(() => whenCollected())));
it('should work with hyperf', async () => {
const test = h`<div id="test">${map(on(htmlElement, 'click'), ({ target }) => target.nodeName)}</div>`;
document.body.appendChild(test);
finalizationRegistry.register(test);
while (true) {
try {
expect(document.getElementById('test').textContent).to.equal('A');
break;
} catch {
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
}
}
document.body.removeChild(test);
});
});
});