@weborigami/async-tree 0.7.0-beta.2 → 0.7.0-beta.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.7.0-beta.2",
3
+ "version": "0.7.0-beta.3",
4
4
  "description": "Asynchronous tree drivers based on standard JavaScript classes",
5
5
  "type": "module",
6
6
  "main": "./main.js",
@@ -96,7 +96,7 @@ export default async function traverseOrThrow(maplike, ...keys) {
96
96
  : "A path tried to unpack data that's already unpacked.";
97
97
  throw new TraverseError(message, {
98
98
  head: maplike,
99
- lastValue,
99
+ lastValue: value ?? lastValue,
100
100
  keys,
101
101
  position,
102
102
  });
@@ -16,19 +16,22 @@ export default function toFunction(obj) {
16
16
  return obj;
17
17
  } else if (isUnpackable(obj)) {
18
18
  // Extract the contents of the object and convert that to a function.
19
- let fnPromise;
20
- return async function (...args) {
21
- if (!fnPromise) {
22
- // unpack() may return a function or a promise for a function; normalize
23
- // to a promise for a function
24
- const unpackPromise = Promise.resolve(
25
- /** @type {any} */ (obj).unpack()
26
- );
27
- fnPromise = unpackPromise.then((content) => toFunction(content));
19
+ const unpacked = /** @type {any} */ (obj).unpack();
20
+ if (unpacked instanceof Promise) {
21
+ return async function (...args) {
22
+ const fn = toFunction(await unpacked);
23
+ if (fn === null) {
24
+ throw new TypeError("unpack() did not return a function");
25
+ }
26
+ return fn(...args);
27
+ };
28
+ } else {
29
+ const fn = toFunction(unpacked);
30
+ if (fn === null) {
31
+ throw new TypeError("unpack() did not return a function");
28
32
  }
29
- const fn = await fnPromise;
30
- return fn(...args);
31
- };
33
+ return fn;
34
+ }
32
35
  } else if (isMaplike(obj)) {
33
36
  // Return a function that invokes the tree's getter.
34
37
  const tree = from(obj);
@@ -65,7 +65,7 @@ describe("castArraylike", () => {
65
65
  });
66
66
  });
67
67
 
68
- test.only("preserves trailing slashes if map has both forms of the key", () => {
68
+ test("preserves trailing slashes if map has both forms of the key", () => {
69
69
  const map = new /** @type {any} */ (Map)([
70
70
  ["a/", 1],
71
71
  ["a", 2],
@@ -26,6 +26,14 @@ describe("toFunction", () => {
26
26
  assert.equal(await fn(), "result");
27
27
  });
28
28
 
29
+ test("can use a packed object's async `unpack` as a function", async () => {
30
+ const obj = new String();
31
+ /** @type {any} */ (obj).unpack = async () => () => "result";
32
+ const fn = toFunction(obj);
33
+ // @ts-ignore
34
+ assert.equal(await fn(), "result");
35
+ });
36
+
29
37
  test("returns null for something that's not a function", () => {
30
38
  // @ts-ignore
31
39
  const result = toFunction("this is not a function");