File size: 8,568 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { afterEach, describe, expect, it } from 'vitest';

import { Disposable, DisposableStore, type IDisposable } from '#/_base/di/lifecycle';
import { Emitter, Event } from '#/_base/event';
import {
  resetUnexpectedErrorHandler,
  setUnexpectedErrorHandler,
} from '#/_base/errors/unexpectedError';

afterEach(() => {
  resetUnexpectedErrorHandler();
});

function captureThrown(fn: () => void): unknown {
  try {
    fn();
    return undefined;
  } catch (error) {
    return error;
  }
}

describe('Emitter / Event', () => {
  it('fire delivers to all listeners in subscribe order', () => {
    const emitter = new Emitter<number>();
    const seen: string[] = [];
    emitter.event((value) => seen.push(`a:${value}`));
    emitter.event((value) => seen.push(`b:${value}`));
    emitter.event((value) => seen.push(`c:${value}`));

    emitter.fire(1);
    emitter.fire(2);

    expect(seen).toEqual(['a:1', 'b:1', 'c:1', 'a:2', 'b:2', 'c:2']);
    emitter.dispose();
  });

  it('returned IDisposable removes the listener', () => {
    const emitter = new Emitter<number>();
    const seen: number[] = [];
    const subscription = emitter.event((value) => seen.push(value));

    emitter.fire(1);
    subscription.dispose();
    emitter.fire(2);

    expect(seen).toEqual([1]);
    emitter.dispose();
  });

  it('binds thisArg so the listener sees the supplied context', () => {
    const emitter = new Emitter<string>();
    const context = { tag: 'ctx', got: [] as string[] };

    emitter.event(
      function (this: typeof context, value: string) {
        this.got.push(value);
      },
      context,
    );
    emitter.fire('hello');

    expect(context.got).toEqual(['hello']);
    emitter.dispose();
  });

  it('listener exception routes to onUnexpectedError and does not skip siblings', () => {
    const captured: unknown[] = [];
    setUnexpectedErrorHandler((error) => captured.push(error));
    const emitter = new Emitter<number>();
    const seen: string[] = [];
    emitter.event(() => {
      seen.push('a');
    });
    emitter.event(() => {
      throw new Error('listener-boom');
    });
    emitter.event(() => {
      seen.push('c');
    });

    emitter.fire(1);

    expect(seen).toEqual(['a', 'c']);
    expect(captured).toHaveLength(1);
    expect((captured[0] as Error).message).toBe('listener-boom');
    emitter.dispose();
  });

  it('dispose makes fire a no-op and event subscribe returns Disposable.None', () => {
    const emitter = new Emitter<number>();
    const seen: number[] = [];
    emitter.event((value) => seen.push(value));

    emitter.dispose();
    emitter.fire(1);
    const subscription = emitter.event((value) => seen.push(value));
    emitter.fire(2);

    expect(seen).toEqual([]);
    expect(subscription).toBe(Disposable.None);
    expect(() => subscription.dispose()).not.toThrow();
  });

  it('disposables array overload collects the subscription disposable', () => {
    const emitter = new Emitter<number>();
    const bag: IDisposable[] = [];

    emitter.event(() => undefined, undefined, bag);

    expect(bag).toHaveLength(1);
    emitter.dispose();
  });

  it('disposables DisposableStore overload collects the subscription disposable', () => {
    const emitter = new Emitter<number>();
    const store = new DisposableStore();
    const seen: number[] = [];

    emitter.event((value) => seen.push(value), undefined, store);
    emitter.fire(1);
    store.dispose();
    emitter.fire(2);

    expect(seen).toEqual([1]);
    emitter.dispose();
  });

  it('listener added during fire does not receive the in-flight value', () => {
    const emitter = new Emitter<number>();
    const seen: string[] = [];
    emitter.event(() => {
      seen.push('a');
      emitter.event(() => seen.push('late'));
    });

    emitter.fire(1);
    expect(seen).toEqual(['a']);
    emitter.fire(2);
    expect(seen).toEqual(['a', 'a', 'late']);
    emitter.dispose();
  });

  it('listener removing itself during fire does not corrupt iteration', () => {
    const emitter = new Emitter<number>();
    const seen: string[] = [];
    const subA = emitter.event(() => {
      seen.push('a');
      subA.dispose();
    });
    emitter.event(() => seen.push('b'));

    emitter.fire(1);
    emitter.fire(2);

    expect(seen).toEqual(['a', 'b', 'b']);
    emitter.dispose();
  });
});

describe('Event.None', () => {
  it('returns Disposable.None and never fires', () => {
    const seen: number[] = [];
    const subscription = Event.None(() => seen.push(1));

    expect(subscription).toBe(Disposable.None);
    expect(seen).toHaveLength(0);
  });
});

describe('Emitter debug name / EventSubscription ledger labels', () => {
  it('named emitter subscriptions land on the store ledger as on:<name>', () => {
    const emitter = new Emitter<number>('test.event');
    const store = new DisposableStore();

    emitter.event(() => undefined, undefined, store);

    expect(store.ledger.entries().map((entry) => entry.label)).toContain('on:test.event');
    store.dispose();
    emitter.dispose();
  });

  it('unnamed emitter subscriptions fall back to disposable:EventSubscription', () => {
    const emitter = new Emitter<number>();
    const store = new DisposableStore();

    emitter.event(() => undefined, undefined, store);

    expect(store.ledger.entries().map((entry) => entry.label)).toContain(
      'disposable:EventSubscription',
    );
    store.dispose();
    emitter.dispose();
  });

  it('listenerCount tracks subscribe and dispose', () => {
    const emitter = new Emitter<number>();
    expect(emitter.listenerCount).toBe(0);

    const subscription = emitter.event(() => undefined);
    expect(emitter.listenerCount).toBe(1);

    subscription.dispose();
    expect(emitter.listenerCount).toBe(0);
    emitter.dispose();
  });
});

describe('Event.once', () => {
  it('delivers exactly once then auto-disposes', () => {
    const emitter = new Emitter<number>();
    const seen: number[] = [];
    Event.once(emitter.event)((value) => seen.push(value));

    emitter.fire(1);
    emitter.fire(2);

    expect(seen).toEqual([1]);
    emitter.dispose();
  });
});

describe('Event.map', () => {
  it('projects values', () => {
    const emitter = new Emitter<number>();
    const doubled = Event.map(emitter.event, (value) => value * 2);
    const seen: number[] = [];

    doubled((value) => seen.push(value));
    emitter.fire(3);
    emitter.fire(5);

    expect(seen).toEqual([6, 10]);
    emitter.dispose();
  });
});

describe('Event.filter', () => {
  it('drops values that fail the predicate', () => {
    const emitter = new Emitter<number>();
    const evens = Event.filter(emitter.event, (value) => value % 2 === 0);
    const seen: number[] = [];

    evens((value) => seen.push(value));
    emitter.fire(1);
    emitter.fire(2);
    emitter.fire(3);
    emitter.fire(4);

    expect(seen).toEqual([2, 4]);
    emitter.dispose();
  });
});

describe('Event.any', () => {
  it('forwards any source fire to the subscriber', () => {
    const a = new Emitter<string>();
    const b = new Emitter<string>();
    const seen: string[] = [];
    Event.any(a.event, b.event)((value) => seen.push(value));

    a.fire('A');
    b.fire('B');
    a.fire('A2');

    expect(seen).toEqual(['A', 'B', 'A2']);
    a.dispose();
    b.dispose();
  });

  it('disposing the combined subscription detaches from all sources', () => {
    const a = new Emitter<string>();
    const b = new Emitter<string>();
    const seen: string[] = [];
    const subscription = Event.any(a.event, b.event)((value) => seen.push(value));

    a.fire('A');
    subscription.dispose();
    a.fire('A2');
    b.fire('B');

    expect(seen).toEqual(['A']);
    a.dispose();
    b.dispose();
  });

  it('disposing the combined subscription disposes all source subscriptions before throwing AggregateError', () => {
    const order: string[] = [];
    const first: Event<string> = () => ({
      dispose: () => {
        order.push('first');
        throw new Error('first-dispose');
      },
    });
    const second: Event<string> = () => ({
      dispose: () => {
        order.push('second');
        throw new Error('second-dispose');
      },
    });

    const error = captureThrown(() => {
      Event.any(first, second)(() => undefined).dispose();
    });

    expect(order).toEqual(['first', 'second']);
    expect(error).toBeInstanceOf(AggregateError);
    expect((error as AggregateError).errors.map((err) => (err as Error).message)).toEqual([
      'first-dispose',
      'second-dispose',
    ]);
  });
});