Skip to content

Commit

Permalink
Updated eslint, changed joinMap to handle results
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Apr 30, 2024
1 parent 6c0acb8 commit 0331149
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 83 deletions.
76 changes: 38 additions & 38 deletions packages/signals/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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"
}
63 changes: 23 additions & 40 deletions packages/signals/src/join-map.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends AsyncSignal<infer U>
? U
: T extends AsyncSignal<infer U>
? U
: never;
type AsyncResultValue<T> = T extends AsyncResult<infer U> ? U : never;

/**
* Joins all the results in a HoloHashMap of `AsyncSignals`
*/
export function joinAsyncMap<K extends HoloHash, V extends AsyncSignal<any>>(
map: ReadonlyMap<K, V>,
joinOptions?: JoinAsyncOptions
): AsyncSignal<ReadonlyMap<K, SignalValue<V>>> {
const signalArray = Array.from(map.entries()).map(
([key, signal]) =>
new AsyncComputed<[K, SignalValue<V>]>(() => {
const result = signal.get();
if (result.status !== "completed") return result;
const value = [key, result.value] as [K, SignalValue<V>];
return {
status: "completed",
value,
};
})
);
const arraySignal = joinAsync(signalArray, joinOptions);
export function joinAsyncMap<K extends HoloHash, V extends AsyncResult<any>>(
map: ReadonlyMap<K, V>,
joinOptions?: JoinAsyncOptions,
): AsyncResult<ReadonlyMap<K, AsyncResultValue<V>>> {
const resultsArray = Array.from(map.entries()).map(([key, result]) => {
if (result.status !== 'completed') return result;
const value = [key, result.value] as [K, AsyncResultValue<V>];
return {
status: 'completed',
value,
} as AsyncResult<[K, AsyncResultValue<V>]>;
});
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<K, SignalValue<V>>(result.value);
return {
status: "completed",
value,
} as AsyncResult<ReadonlyMap<K, SignalValue<V>>>;
});
const value = new HoloHashMap<K, AsyncResultValue<V>>(arrayResult.value);
return {
status: 'completed',
value,
} as AsyncResult<ReadonlyMap<K, AsyncResultValue<V>>>;
}
18 changes: 13 additions & 5 deletions packages/signals/tests/join-map.test.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0331149

Please sign in to comment.