silentium 0.0.183 → 0.0.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,4 +39,14 @@ describe("Applied.test", () => {
39
39
 
40
40
  expect(g).not.toHaveBeenCalled();
41
41
  });
42
+
43
+ test("applier returns a message", () => {
44
+ const base = Of(2);
45
+ const applied = Applied(base, (x) => Of(x * 3));
46
+
47
+ const g = vi.fn();
48
+ applied.then(g);
49
+
50
+ expect(g).toBeCalledWith(6);
51
+ });
42
52
  });
@@ -1,5 +1,7 @@
1
1
  import { ActualMessage } from "base/ActualMessage";
2
+ import { DestroyContainer } from "base/DestroyContainer";
2
3
  import { Message } from "base/Message";
4
+ import { isMessage } from "helpers/guards";
3
5
  import { ConstructorType } from "types/ConstructorType";
4
6
  import { MaybeMessage } from "types/MessageType";
5
7
 
@@ -9,13 +11,20 @@ import { MaybeMessage } from "types/MessageType";
9
11
  */
10
12
  export function Applied<const T, R>(
11
13
  base: MaybeMessage<T>,
12
- applier: ConstructorType<[T], R>,
14
+ applier: ConstructorType<[T], MaybeMessage<R>>,
13
15
  ) {
14
16
  const $base = ActualMessage(base);
15
17
  return Message<R>(function AppliedImpl(resolve, reject) {
18
+ const dc = DestroyContainer();
16
19
  $base.catch(reject);
17
20
  $base.then((v) => {
18
- resolve(applier(v));
21
+ const result = applier(v);
22
+ if (isMessage(result)) {
23
+ dc.destroy();
24
+ result.catch(reject).then(resolve);
25
+ } else {
26
+ resolve(result);
27
+ }
19
28
  });
20
29
  });
21
30
  }