foundation.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { describe, it, expect, vi } from "vitest";
  2. import {
  3. toDisposable,
  4. DisposableStore,
  5. Emitter,
  6. Event,
  7. Relay,
  8. EventMultiplexer,
  9. CancellationTokenSource,
  10. } from "../src/foundation.js";
  11. describe("toDisposable", () => {
  12. it("should call the function on dispose", () => {
  13. const fn = vi.fn();
  14. const d = toDisposable(fn);
  15. d.dispose();
  16. expect(fn).toHaveBeenCalledOnce();
  17. });
  18. it("should only call the function once", () => {
  19. const fn = vi.fn();
  20. const d = toDisposable(fn);
  21. d.dispose();
  22. d.dispose();
  23. expect(fn).toHaveBeenCalledOnce();
  24. });
  25. });
  26. describe("DisposableStore", () => {
  27. it("should dispose all items", () => {
  28. const store = new DisposableStore();
  29. const fn1 = vi.fn();
  30. const fn2 = vi.fn();
  31. store.add(toDisposable(fn1));
  32. store.add(toDisposable(fn2));
  33. store.dispose();
  34. expect(fn1).toHaveBeenCalledOnce();
  35. expect(fn2).toHaveBeenCalledOnce();
  36. });
  37. it("should dispose items added after dispose immediately", () => {
  38. const store = new DisposableStore();
  39. store.dispose();
  40. const fn = vi.fn();
  41. store.add(toDisposable(fn));
  42. expect(fn).toHaveBeenCalledOnce();
  43. });
  44. it("should not double-dispose", () => {
  45. const store = new DisposableStore();
  46. const fn = vi.fn();
  47. store.add(toDisposable(fn));
  48. store.dispose();
  49. store.dispose();
  50. expect(fn).toHaveBeenCalledOnce();
  51. });
  52. });
  53. describe("Emitter", () => {
  54. it("should fire events to listeners", () => {
  55. const emitter = new Emitter<number>();
  56. const values: number[] = [];
  57. emitter.event((v) => values.push(v));
  58. emitter.fire(1);
  59. emitter.fire(2);
  60. expect(values).toEqual([1, 2]);
  61. });
  62. it("should support multiple listeners", () => {
  63. const emitter = new Emitter<string>();
  64. const a: string[] = [];
  65. const b: string[] = [];
  66. emitter.event((v) => a.push(v));
  67. emitter.event((v) => b.push(v));
  68. emitter.fire("hello");
  69. expect(a).toEqual(["hello"]);
  70. expect(b).toEqual(["hello"]);
  71. });
  72. it("should allow unsubscribing via dispose", () => {
  73. const emitter = new Emitter<number>();
  74. const values: number[] = [];
  75. const d = emitter.event((v) => values.push(v));
  76. emitter.fire(1);
  77. d.dispose();
  78. emitter.fire(2);
  79. expect(values).toEqual([1]);
  80. });
  81. it("should not fire after dispose", () => {
  82. const emitter = new Emitter<number>();
  83. const values: number[] = [];
  84. emitter.event((v) => values.push(v));
  85. emitter.dispose();
  86. emitter.fire(1);
  87. expect(values).toEqual([]);
  88. });
  89. it("should call onWillAddFirstListener and onDidRemoveLastListener", () => {
  90. const onFirst = vi.fn();
  91. const onLast = vi.fn();
  92. const emitter = new Emitter<void>({
  93. onWillAddFirstListener: onFirst,
  94. onDidRemoveLastListener: onLast,
  95. });
  96. const d1 = emitter.event(() => {});
  97. expect(onFirst).toHaveBeenCalledOnce();
  98. expect(onLast).not.toHaveBeenCalled();
  99. const d2 = emitter.event(() => {});
  100. expect(onFirst).toHaveBeenCalledOnce(); // not called again
  101. d1.dispose();
  102. expect(onLast).not.toHaveBeenCalled(); // still has d2
  103. d2.dispose();
  104. expect(onLast).toHaveBeenCalledOnce();
  105. });
  106. });
  107. describe("Event", () => {
  108. it("Event.once should only fire once", () => {
  109. const emitter = new Emitter<number>();
  110. const values: number[] = [];
  111. Event.once(emitter.event)((v) => values.push(v));
  112. emitter.fire(1);
  113. emitter.fire(2);
  114. expect(values).toEqual([1]);
  115. });
  116. it("Event.toPromise should resolve on first fire", async () => {
  117. const emitter = new Emitter<string>();
  118. const promise = Event.toPromise(emitter.event);
  119. emitter.fire("done");
  120. expect(await promise).toBe("done");
  121. });
  122. it("Event.filter should only pass matching events", () => {
  123. const emitter = new Emitter<number>();
  124. const values: number[] = [];
  125. Event.filter(emitter.event, (n) => n > 2)((v) => values.push(v));
  126. emitter.fire(1);
  127. emitter.fire(3);
  128. emitter.fire(2);
  129. emitter.fire(5);
  130. expect(values).toEqual([3, 5]);
  131. });
  132. it("Event.map should transform events", () => {
  133. const emitter = new Emitter<number>();
  134. const values: string[] = [];
  135. Event.map(emitter.event, (n) => `val:${n}`)((v) => values.push(v));
  136. emitter.fire(1);
  137. emitter.fire(2);
  138. expect(values).toEqual(["val:1", "val:2"]);
  139. });
  140. });
  141. describe("Relay", () => {
  142. it("should relay events from input source", () => {
  143. const source = new Emitter<number>();
  144. const relay = new Relay<number>();
  145. relay.input = source.event;
  146. const values: number[] = [];
  147. relay.event((v) => values.push(v));
  148. source.fire(1);
  149. source.fire(2);
  150. expect(values).toEqual([1, 2]);
  151. });
  152. it("should switch input source", () => {
  153. const source1 = new Emitter<number>();
  154. const source2 = new Emitter<number>();
  155. const relay = new Relay<number>();
  156. const values: number[] = [];
  157. relay.event((v) => values.push(v));
  158. relay.input = source1.event;
  159. source1.fire(1);
  160. relay.input = source2.event;
  161. source1.fire(2); // should be ignored
  162. source2.fire(3);
  163. expect(values).toEqual([1, 3]);
  164. });
  165. });
  166. describe("EventMultiplexer", () => {
  167. it("should aggregate multiple event sources", () => {
  168. const e1 = new Emitter<string>();
  169. const e2 = new Emitter<string>();
  170. const mux = new EventMultiplexer<string>();
  171. mux.add(e1.event);
  172. mux.add(e2.event);
  173. const values: string[] = [];
  174. mux.event((v) => values.push(v));
  175. e1.fire("a");
  176. e2.fire("b");
  177. expect(values).toEqual(["a", "b"]);
  178. });
  179. });
  180. describe("CancellationTokenSource", () => {
  181. it("should fire cancellation event on cancel", () => {
  182. const cts = new CancellationTokenSource();
  183. const fn = vi.fn();
  184. cts.token.onCancellationRequested(fn);
  185. expect(cts.token.isCancellationRequested).toBe(false);
  186. cts.cancel();
  187. expect(fn).toHaveBeenCalledOnce();
  188. });
  189. it("should only fire once", () => {
  190. const cts = new CancellationTokenSource();
  191. const fn = vi.fn();
  192. cts.token.onCancellationRequested(fn);
  193. cts.cancel();
  194. cts.cancel();
  195. expect(fn).toHaveBeenCalledOnce();
  196. });
  197. });