mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 19:45:05 +01:00
001dbefbcf
because it becomes hard to remember where you used them and where not
55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
export class BaseObservable {
|
|
constructor() {
|
|
this._handlers = new Set();
|
|
}
|
|
|
|
onSubscribeFirst() {
|
|
|
|
}
|
|
|
|
onUnsubscribeLast() {
|
|
|
|
}
|
|
|
|
subscribe(handler) {
|
|
this._handlers.add(handler);
|
|
if (this._handlers.size === 1) {
|
|
this.onSubscribeFirst();
|
|
}
|
|
return () => {
|
|
if (handler) {
|
|
this._handlers.delete(handler);
|
|
if (this._handlers.size === 0) {
|
|
this.onUnsubscribeLast();
|
|
}
|
|
handler = null;
|
|
}
|
|
return null;
|
|
};
|
|
}
|
|
|
|
// Add iterator over handlers here
|
|
}
|
|
|
|
export function tests() {
|
|
class Collection extends BaseObservable {
|
|
constructor() {
|
|
super();
|
|
this.firstSubscribeCalls = 0;
|
|
this.firstUnsubscribeCalls = 0;
|
|
}
|
|
onSubscribeFirst() { this.firstSubscribeCalls += 1; }
|
|
onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; }
|
|
}
|
|
|
|
return {
|
|
test_unsubscribe(assert) {
|
|
const c = new Collection();
|
|
const unsubscribe = c.subscribe({});
|
|
unsubscribe();
|
|
assert.equal(c.firstSubscribeCalls, 1);
|
|
assert.equal(c.firstUnsubscribeCalls, 1);
|
|
}
|
|
}
|
|
}
|