foundation.test.ts 4.8 KB

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