diff --git a/packages/signals/package.json b/packages/signals/package.json index 9da9613..1377306 100644 --- a/packages/signals/package.json +++ b/packages/signals/package.json @@ -1,40 +1,40 @@ { - "name": "@holochain-open-dev/signals", - "version": "0.300.0-dev.5", - "description": "Holochain async-signals to build reusable holochain-open-dev modules", - "author": "guillem.cordoba@gmail.com", - "main": "dist/index.js", - "module": "dist/index.js", - "types": "dist/index.d.ts", - "files": ["dist", "src"], - "exports": { - ".": "./dist/index.js", - "./dist/*": "./dist/*" - }, - "scripts": { - "start": "vite --open", - "build": "npm run lint && tsc", - "lint": "eslint --ext .ts . --ignore-path .gitignore", - "test": "vitest run", - "prepublish": "npm run build" - }, - "dependencies": { - "@holochain-open-dev/utils": "^0.300.0-dev.2", - "@holochain/client": "^0.17.0-dev.12", - "@shoelace-style/shoelace": "^2.11.2", - "async-signals": "^0.1.1", - "lit-signal-watcher": "^0.1.0", - "signal-polyfill": "^0.1.0" - }, - "devDependencies": { - "@types/lodash-es": "^4.17.6", - "js-base64": "^3.7.7", - "typescript": "^5.4.0", - "vite": "^4.1.1", - "vitest": "^1.5.0" - }, - "publishConfig": { - "access": "public" - }, - "type": "module" + "name": "@holochain-open-dev/signals", + "version": "0.300.0-dev.6", + "description": "Holochain async-signals to build reusable holochain-open-dev modules", + "author": "guillem.cordoba@gmail.com", + "main": "dist/index.js", + "module": "dist/index.js", + "types": "dist/index.d.ts", + "files": ["dist", "src"], + "exports": { + ".": "./dist/index.js", + "./dist/*": "./dist/*" + }, + "scripts": { + "start": "vite --open", + "build": "npm run lint && tsc", + "lint": "eslint src", + "test": "vitest run", + "prepublish": "npm run build" + }, + "dependencies": { + "@holochain-open-dev/utils": "^0.300.0-dev.2", + "@holochain/client": "^0.17.0-dev.12", + "@shoelace-style/shoelace": "^2.11.2", + "async-signals": "^0.1.7", + "lit-signal-watcher": "^0.1.0", + "signal-polyfill": "^0.1.0" + }, + "devDependencies": { + "@types/lodash-es": "^4.17.6", + "js-base64": "^3.7.7", + "typescript": "^5.4.0", + "vite": "^4.1.1", + "vitest": "^1.5.0" + }, + "publishConfig": { + "access": "public" + }, + "type": "module" } diff --git a/packages/signals/src/join-map.ts b/packages/signals/src/join-map.ts index a1e3f7a..ed614c6 100644 --- a/packages/signals/src/join-map.ts +++ b/packages/signals/src/join-map.ts @@ -1,48 +1,31 @@ -import { HoloHashMap } from "@holochain-open-dev/utils"; -import { HoloHash } from "@holochain/client"; -import { - AsyncComputed, - AsyncResult, - AsyncSignal, - joinAsync, - JoinAsyncOptions, -} from "async-signals"; +import { HoloHashMap } from '@holochain-open-dev/utils'; +import { HoloHash } from '@holochain/client'; +import { AsyncResult, JoinAsyncOptions, joinAsync } from 'async-signals'; -type SignalValue = T extends AsyncSignal - ? U - : T extends AsyncSignal - ? U - : never; +type AsyncResultValue = T extends AsyncResult ? U : never; /** * Joins all the results in a HoloHashMap of `AsyncSignals` */ -export function joinAsyncMap>( - map: ReadonlyMap, - joinOptions?: JoinAsyncOptions -): AsyncSignal>> { - const signalArray = Array.from(map.entries()).map( - ([key, signal]) => - new AsyncComputed<[K, SignalValue]>(() => { - const result = signal.get(); - if (result.status !== "completed") return result; - const value = [key, result.value] as [K, SignalValue]; - return { - status: "completed", - value, - }; - }) - ); - const arraySignal = joinAsync(signalArray, joinOptions); +export function joinAsyncMap>( + map: ReadonlyMap, + joinOptions?: JoinAsyncOptions, +): AsyncResult>> { + const resultsArray = Array.from(map.entries()).map(([key, result]) => { + if (result.status !== 'completed') return result; + const value = [key, result.value] as [K, AsyncResultValue]; + return { + status: 'completed', + value, + } as AsyncResult<[K, AsyncResultValue]>; + }); + const arrayResult = joinAsync(resultsArray, joinOptions); - return new AsyncComputed(() => { - const result = arraySignal.get(); - if (result.status !== "completed") return result; + if (arrayResult.status !== 'completed') return arrayResult; - const value = new HoloHashMap>(result.value); - return { - status: "completed", - value, - } as AsyncResult>>; - }); + const value = new HoloHashMap>(arrayResult.value); + return { + status: 'completed', + value, + } as AsyncResult>>; } diff --git a/packages/signals/tests/join-map.test.js b/packages/signals/tests/join-map.test.js index 52718cb..0f56ee2 100644 --- a/packages/signals/tests/join-map.test.js +++ b/packages/signals/tests/join-map.test.js @@ -1,7 +1,8 @@ import { expect, it } from "vitest"; -import { fromPromise } from "async-signals"; +import { AsyncComputed, fromPromise } from "async-signals"; import { fromUint8Array, toUint8Array } from "js-base64"; import { Signal } from "signal-polyfill"; +import { mapValues } from "@holochain-open-dev/utils"; import { joinAsyncMap } from "../src"; @@ -43,7 +44,9 @@ it("joinAsyncMap", async () => { lazyMap.get(h); } - const j = joinAsyncMap(lazyMap); + const j = new AsyncComputed(() => + joinAsyncMap(mapValues(lazyMap, (s) => s.get())) + ); const w = new Signal.subtle.Watcher(() => {}); w.watch(j); @@ -72,9 +75,14 @@ it("joinAsyncMap with error filtering", async () => { lazyMap.get(h); } - const j = joinAsyncMap(lazyMap, { - errors: "filter_out", - }); + const j = new AsyncComputed(() => + joinAsyncMap( + mapValues(lazyMap, (s) => s.get()), + { + errors: "filter_out", + } + ) + ); const w = new Signal.subtle.Watcher(() => {}); w.watch(j);