From e9f665db478d2c5dd0c4f4644d690ce5810e535a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillem=20C=C3=B3rdoba?= Date: Mon, 18 Dec 2023 18:03:19 +0100 Subject: [PATCH] try catch on pipe --- packages/stores/package.json | 2 +- packages/stores/src/pipe.ts | 64 ++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/packages/stores/package.json b/packages/stores/package.json index d11fa58..5f69052 100644 --- a/packages/stores/package.json +++ b/packages/stores/package.json @@ -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": "guillem.cordoba@gmail.com", "main": "dist/index.js", diff --git a/packages/stores/src/pipe.ts b/packages/stores/src/pipe.ts index e51e651..85b0d59 100644 --- a/packages/stores/src/pipe.ts +++ b/packages/stores/src/pipe.ts @@ -16,37 +16,43 @@ function pipeStep( 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).subscribe) { - return (v as Readable).subscribe((value) => { - if ((value as AsyncStatus).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).subscribe) { + return (v as Readable).subscribe((value) => { + if ((value as AsyncStatus).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 = { status: "error", error: e }; + set(status); return () => {}; } }