Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

useSuspenseCondition() #2400

ntucker started this conversation in Ideas
Discussion options

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(
You must be logged in to vote

Replies: 1 comment

Comment options

ntucker
Jul 29, 2024
Maintainer Author

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 });
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /