svelte.ts
1 import { getContext } from 'svelte'; 2 import type { Opt } from '@jet/environment'; 3 import type { ActionOutcome } from '@jet/engine'; 4 5 import type { ActionModel } from '~/jet/models'; 6 import type { Jet } from '~/jet/jet'; 7 8 export const CONTEXT_NAME = 'jet'; 9 10 /** 11 * Gets the current Jet instance from the Svelte context. 12 * 13 * @return jet The current instance of Jet 14 */ 15 export function getJet(): Jet { 16 const jet = getContext<Opt<Jet>>(CONTEXT_NAME); 17 18 if (!jet) { 19 throw new Error('getJet called before Jet.load'); 20 } 21 22 return jet; 23 } 24 25 /** 26 * Jet helper to expose jet.perform in single location 27 * 28 * @return Promise<ActionOutcome> 29 */ 30 type ActionUndefined = 'noActionProvided'; 31 32 export function getJetPerform(): ( 33 action: ActionModel, 34 ) => Promise<ActionOutcome | ActionUndefined> { 35 const jet = getJet(); 36 37 return (action: ActionModel) => { 38 if (!action) { 39 //TODO: rdar://73165545 (Error Handling Across App) 40 return Promise.resolve('noActionProvided'); 41 } 42 43 return jet.perform(action); 44 }; 45 }