Skip to content

Commit

Permalink
try catch on pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Dec 18, 2023
1 parent dd273a3 commit e9f665d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/stores/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@holochain-open-dev/stores",
"version": "0.8.3",
"version": "0.8.4",
"description": "Re-export of svelte/store, with additional utilities to build reusable holochain-open-dev modules",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down
64 changes: 35 additions & 29 deletions packages/stores/src/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,43 @@ function pipeStep<T, U>(
if (value.status === "error") set(value);
else if (value.status === "pending") set(value);
else {
const v = stepFn(
value.value,
...values
.slice(1)
.map((v) => (v as any).value)
.reverse()
);
try {
const v = stepFn(
value.value,
...values
.slice(1)
.map((v) => (v as any).value)
.reverse()
);

if ((v as Readable<any>).subscribe) {
return (v as Readable<any>).subscribe((value) => {
if ((value as AsyncStatus<U>).status) {
set(value);
} else {
set({ status: "complete", value });
}
});
} else if (is_promise(v)) {
set({ status: "pending" });
Promise.resolve(v)
.then((v) => {
set({ status: "complete", value: v });
})
.catch((error) => {
set({
status: "error",
error,
});
if ((v as Readable<any>).subscribe) {
return (v as Readable<any>).subscribe((value) => {
if ((value as AsyncStatus<U>).status) {
set(value);
} else {
set({ status: "complete", value });
}
});
return () => {};
} else {
set({ status: "complete", value: v as U });
} else if (is_promise(v)) {
set({ status: "pending" });
Promise.resolve(v)
.then((v) => {
set({ status: "complete", value: v });
})
.catch((error) => {
set({
status: "error",
error,
});
});
return () => {};
} else {
set({ status: "complete", value: v as U });
return () => {};
}
} catch (e) {
const status: AsyncStatus<U> = { status: "error", error: e };
set(status);
return () => {};
}
}
Expand Down

0 comments on commit e9f665d

Please sign in to comment.