-
-
Notifications
You must be signed in to change notification settings - Fork 97
-
This is for long-running tasks that don't resolve from a simple fetch. Endpoint is subscribed (meaning they can use polling or other methods like websockets); then on updates - they check a conditional (function set in endpoint) and if does not pass, throws a promise for next polling frequency.
This will be used for workflow result stream. By using suspense in react 18 the entire flow is very simple - useTransition can simply be used on the workflow button to grab next results.
const WFR = endpoint.extend({ condition: (response) => response.status === 'done' }) const workflowResults = useSuspenseCondition(WFR, { id }) const [isPending, startTransition] = useTransition(); const handleRun = useCallback(
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Instead, we can introduce useSuspenseQuery()
, which is like useQuery() but suspends until the query is not undefined
or INVALID
.
The workflow above could be implemented by creating a query that returns 'undefined' unless the status === 'done'.
const workflowDone = new schema.Query(Workflow, (workflow) => workflow.status === 'done' ? undefined : true);
This also enables 'fallback queries' to eliminate the nasty undefined
conditional:
const queryPrice = new schema.Query( { ticker: Ticker, stats: Stats }, ({ ticker, stats }) => ticker?.price ?? stats?.last, ); function AssetPrice({ product_id }) { const price = useQuery(queryPrice, { product_id }); if (!price) return <span></span>; }
const queryPrice = new schema.Query( { ticker: Ticker, stats: Stats }, ({ ticker, stats }) => ticker?.price ?? stats?.last, ); function AssetPrice({ product_id }) { const price = useSuspenseQuery(queryPrice, { product_id }); }
Beta Was this translation helpful? Give feedback.