-
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.
- Loading branch information
1 parent
d5b0728
commit 7088291
Showing
4 changed files
with
46 additions
and
1 deletion.
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,6 +1,6 @@ | ||
{ | ||
"name": "@holochain-open-dev/signals", | ||
"version": "0.300.0-dev.4", | ||
"version": "0.300.0-dev.5", | ||
"description": "Holochain async-signals to build reusable holochain-open-dev modules", | ||
"author": "[email protected]", | ||
"main": "dist/index.js", | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AsyncComputed, AsyncSignal } from "async-signals"; | ||
|
||
/** | ||
* Transforms the value for the given signal, when its status is 'completed'. | ||
* | ||
* When the status of the given `AsyncSignal` is other than 'completed', it returns that result. | ||
*/ | ||
export function mapCompleted<T, U>( | ||
asyncSignal: AsyncSignal<T>, | ||
mapFn: (value: T) => U | ||
): AsyncSignal<U> { | ||
return new AsyncComputed(() => { | ||
const result = asyncSignal.get(); | ||
if (result.status !== "completed") return result; | ||
const value = mapFn(result.value); | ||
return { | ||
status: "completed", | ||
value, | ||
}; | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expect, it } from "vitest"; | ||
import { fromPromise } from "async-signals"; | ||
|
||
import { mapCompleted, watch } from "../src"; | ||
|
||
const sleep = (ms) => new Promise((r) => setTimeout(() => r(), ms)); | ||
|
||
it("mapCompleted", async () => { | ||
const signal = fromPromise(async () => { | ||
await sleep(10); | ||
return 1; | ||
}); | ||
const signal2 = mapCompleted(signal, (n) => n + 1); | ||
|
||
watch(signal2, () => {}); | ||
|
||
expect(signal2.get().status).to.be.equal("pending"); | ||
|
||
await sleep(20); | ||
|
||
expect(signal2.get().status).to.be.equal("completed"); | ||
expect(signal2.get().value).to.be.equal(2); | ||
}); |