Skip to content

Commit

Permalink
Merge branch 'main' into table-cell-bgcolor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmischka committed Apr 14, 2023
2 parents 6216451 + ee5991c commit 4cc98e2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
70 changes: 41 additions & 29 deletions src/classes/IntervalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,16 +1366,17 @@ export default class IntervalClient {
page: inputs.page,
}

let page: Layout
let page: Layout | undefined = undefined
let menuItems: InternalButtonItem[] | undefined = undefined
let renderInstruction: T_IO_RENDER_INPUT | undefined = undefined
let errors: PageError[] = []

const MAX_PAGE_RETRIES = 5

const sendPage = async () => {
let pageLayout: LayoutSchemaInput | undefined
if (page instanceof BasicLayout) {
const pageLayout: LayoutSchemaInput = {
pageLayout = {
kind: 'BASIC',
title:
page.title === undefined
Expand All @@ -1399,33 +1400,35 @@ export default class IntervalClient {
'The `metadata` property on `Layout` is deprecated. Please use `io.display.metadata` in the `children` array instead.'
)
}
}

if (this.#config.getClientHandlers) {
await this.#config.getClientHandlers()?.RENDER_PAGE({
pageKey,
page: JSON.stringify(pageLayout),
hostInstanceId: 'demo',
})
} else {
for (let i = 0; i < MAX_PAGE_RETRIES; i++) {
try {
const page = JSON.stringify(pageLayout)
if (this.#config.getClientHandlers) {
await this.#config.getClientHandlers()?.RENDER_PAGE({
pageKey,
page: pageLayout ? JSON.stringify(pageLayout) : undefined,
hostInstanceId: 'demo',
})
} else {
for (let i = 0; i < MAX_PAGE_RETRIES; i++) {
try {
const page = pageLayout ? JSON.stringify(pageLayout) : undefined
if (page) {
this.#pendingPageLayouts.set(pageKey, page)
await this.#send('SEND_PAGE', {
pageKey,
page,
})
return
} catch (err) {
this.#logger.debug('Failed sending page', err)
this.#logger.debug('Retrying in', this.#retryIntervalMs)
await sleep(this.#retryIntervalMs)
}
await this.#send('SEND_PAGE', {
pageKey,
page,
})
return
} catch (err) {
this.#logger.debug('Failed sending page', err)
this.#logger.debug('Retrying in', this.#retryIntervalMs)
await sleep(this.#retryIntervalMs)
}
throw new IntervalError(
'Unsuccessful sending page, max retries exceeded.'
)
}
throw new IntervalError(
'Unsuccessful sending page, max retries exceeded.'
)
}
}

Expand Down Expand Up @@ -1519,6 +1522,11 @@ export default class IntervalClient {
.then(res => {
page = res

if (!page) {
scheduleSendPage()
return
}

if (typeof page.title === 'function') {
try {
page.title = page.title()
Expand All @@ -1531,8 +1539,10 @@ export default class IntervalClient {
if (page.title instanceof Promise) {
page.title
.then(title => {
page.title = title
scheduleSendPage()
if (page) {
page.title = title
scheduleSendPage()
}
})
.catch(err => {
this.#logger.error(err)
Expand All @@ -1554,8 +1564,10 @@ export default class IntervalClient {
if (page.description instanceof Promise) {
page.description
.then(description => {
page.description = description
scheduleSendPage()
if (page) {
page.description = description
scheduleSendPage()
}
})
.catch(err => {
this.#logger.error(err)
Expand Down Expand Up @@ -1589,7 +1601,7 @@ export default class IntervalClient {
)
}

if (page.children) {
if (page.children?.length) {
group(page.children).then(
() => {
this.#logger.debug(
Expand Down
28 changes: 28 additions & 0 deletions src/examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ const sidebar_depth = new Page({
},
})

const empty_page = new Page({
name: 'Empty page',
handler: async () => {
if (ctx.params.show_layout) {
return new Layout({
title: 'Contents!',
children: [io.display.markdown('Children!')],
menuItems: [
{
label: 'Hide layout',
route: 'empty_page',
},
],
})
}
},
routes: {
child_action: new Action(async () => {
return 'Hello!'
}),
show_layout: new Action(async () => {
ctx.redirect({ route: 'empty_page', params: { show_layout: 1 } })
}),
},
})

const confirmIdentity = new Action({
name: 'Confirm identity',
handler: async () => {
Expand Down Expand Up @@ -335,6 +361,7 @@ const prod = new Interval({
},
})
},
empty_page,
grids: gridsPage,
tables: new Page({
name: 'Tables',
Expand Down Expand Up @@ -421,6 +448,7 @@ const interval = new Interval({
routes: {
sidebar_depth,
echoContext,
empty_page,
inputRightAfterDisplay: async () => {
await io.display.link('Display', {
url: '',
Expand Down
8 changes: 4 additions & 4 deletions src/internalRpcSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ export const wsServerSchema = {
SEND_PAGE: {
inputs: z.object({
pageKey: z.string(),
// stringified PAGE_SCHEMA
page: z.string(),
// stringified LAYOUT_SCHEMA
page: z.string().nullish(),
}),
returns: z.boolean(),
},
Expand Down Expand Up @@ -479,8 +479,8 @@ export const clientSchema = {
RENDER_PAGE: {
inputs: z.object({
pageKey: z.string(),
// stringified PAGE_SCHEMA
page: z.string(),
// stringified LAYOUT_SCHEMA
page: z.string().nullish(),
hostInstanceId: z.string(),
}),
returns: z.boolean(),
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export type IntervalRouteDefinitions = Record<
export type IntervalPageHandler = (
display: IO['display'],
ctx: PageCtx
) => Promise<Layout>
) => Promise<Layout | undefined>

export type RequiredPropsIOComponentFunction<
MethodName extends T_IO_METHOD_NAMES,
Expand Down

0 comments on commit 4cc98e2

Please sign in to comment.