-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated eslint, changed joinMap to handle results
- Loading branch information
1 parent
6c0acb8
commit 0331149
Showing
3 changed files
with
74 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters