Skip to content

Commit

Permalink
Merge pull request #1235 from interval/page-loading
Browse files Browse the repository at this point in the history
Add `ctx.loading` to PageCtx and support to page handlers
  • Loading branch information
jacobmischka authored Apr 27, 2023
2 parents 1b4f9e3 + 74f49b5 commit 4972019
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/classes/IntervalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,26 @@ export default class IntervalClient {
page: inputs.page,
redirect: (props: LegacyLinkProps) =>
intervalClient.#sendRedirect(pageKey, props),
loading: new TransactionLoadingState({
logger: intervalClient.#logger,
send: async loadingState => {
intervalClient.#transactionLoadingStates.set(
pageKey,
loadingState
)
if (this.#config.getClientHandlers) {
await this.#config.getClientHandlers()?.LOADING_STATE({
transactionId: pageKey,
...loadingState,
})
} else {
await intervalClient.#send('SEND_LOADING_CALL', {
transactionId: pageKey,
...loadingState,
})
}
},
}),
}

let page: Layout | undefined = undefined
Expand Down
17 changes: 17 additions & 0 deletions src/examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,24 @@ const prod = new Interval({
async_page_test: new Page({
name: 'Async page test',
handler: async () => {
await sleep(2_000)

await ctx.loading.start('Generating page...')

await sleep(2_000)

await ctx.loading.start({
label: 'Generating rows...',
itemsInQueue: 100,
})

for (let i = 0; i < 100; i++) {
await ctx.loading.completeOne()
await sleep(100)
}

const allData = generateRows(100)

return new Layout({
children: [
io.display.table<ReturnType<typeof generateRows>[0]>(
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const ctx: ActionCtx & PageCtx = {
get user() { return getSomeStore().ctx.user },
get params() { return getSomeStore().ctx.params },
get environment() { return getSomeStore().ctx.environment },
get loading() { return getActionStore().ctx.loading },
get loading() { return getSomeStore().ctx.loading },
get log() { return getActionStore().ctx.log },
get organization() { return getSomeStore().ctx.organization },
get action() { return getActionStore().ctx.action },
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export type ActionCtx = {

export type PageCtx = Pick<
ActionCtx,
'user' | 'params' | 'environment' | 'organization' | 'redirect'
'user' | 'params' | 'environment' | 'organization' | 'redirect' | 'loading'
> & {
/**
* Information about the currently open page.
Expand Down

0 comments on commit 4972019

Please sign in to comment.