summaryrefslogtreecommitdiff
path: root/src/jet/svelte.ts
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /src/jet/svelte.ts
init commit
Diffstat (limited to 'src/jet/svelte.ts')
-rw-r--r--src/jet/svelte.ts45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/jet/svelte.ts b/src/jet/svelte.ts
new file mode 100644
index 0000000..f1870ca
--- /dev/null
+++ b/src/jet/svelte.ts
@@ -0,0 +1,45 @@
+import { getContext } from 'svelte';
+import type { Opt } from '@jet/environment';
+import type { ActionOutcome } from '@jet/engine';
+
+import type { ActionModel } from '~/jet/models';
+import type { Jet } from '~/jet/jet';
+
+export const CONTEXT_NAME = 'jet';
+
+/**
+ * Gets the current Jet instance from the Svelte context.
+ *
+ * @return jet The current instance of Jet
+ */
+export function getJet(): Jet {
+ const jet = getContext<Opt<Jet>>(CONTEXT_NAME);
+
+ if (!jet) {
+ throw new Error('getJet called before Jet.load');
+ }
+
+ return jet;
+}
+
+/**
+ * Jet helper to expose jet.perform in single location
+ *
+ * @return Promise<ActionOutcome>
+ */
+type ActionUndefined = 'noActionProvided';
+
+export function getJetPerform(): (
+ action: ActionModel,
+) => Promise<ActionOutcome | ActionUndefined> {
+ const jet = getJet();
+
+ return (action: ActionModel) => {
+ if (!action) {
+ //TODO: rdar://73165545 (Error Handling Across App)
+ return Promise.resolve('noActionProvided');
+ }
+
+ return jet.perform(action);
+ };
+}