Skip to content

Commit

Permalink
fix: implement onAbort() and soft-deprecate `onTelefunctionRemoteCa…
Browse files Browse the repository at this point in the history
…llError()`
  • Loading branch information
brillout committed Nov 4, 2022
1 parent 5457a30 commit ff15341
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 52 deletions.
8 changes: 3 additions & 5 deletions docs/pages/permissions.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ function getUser() {
```js
// Environment: Browser

import { onTelefunctionRemoteCallError } from 'telefunc/client'
import { onAbort } from 'telefunc/client'

// onTelefunctionRemoteCallError() is called whenever the browser-side fails to make
// a telefunction call, allowing us to implement global behavior like below
onTelefunctionRemoteCallError(err => {
if (err.isAbort && err.abortValue.notLoggedIn) {
onAbort(err => {
if (err.abortValue.notLoggedIn) {
// Redirect user to login page
window.location.href = '/login'
}
Expand Down
6 changes: 3 additions & 3 deletions examples/authentication/renderer/_default.page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ReactDOM from 'react-dom/client'
import { PageShell } from './PageShell'
import type { PageContextClient } from './types'

import { onTelefunctionRemoteCallError } from 'telefunc/client'
import { onAbort } from 'telefunc/client'

let root: ReactDOM.Root
async function render(pageContext: PageContextClient) {
Expand All @@ -27,8 +27,8 @@ async function render(pageContext: PageContextClient) {
}
}

onTelefunctionRemoteCallError((err) => {
if (err.isAbort && err.abortValue === 'LOGGED_OUT') {
onAbort((err) => {
if (err.abortValue === 'LOGGED_OUT') {
window.location.reload()
}
})
32 changes: 16 additions & 16 deletions telefunc/client/TelefunctionError.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export type { TelefunctionError }
export type { TelefunctionCallAbort }

type TelefunctionError = Error &
(
| {
isConnectionError?: undefined
isAbort?: undefined
}
| {
isConnectionError: true
isAbort?: undefined
}
| {
isConnectionError?: undefined
isAbort: true
abortValue: unknown
}
)
type TelefunctionError = TelefunctionCallErrorSsr | TelefunctionCallErrorConnection | TelefunctionCallAbort

type TelefunctionCallErrorSsr = Error & {
isConnectionError?: undefined
isAbort?: undefined
}
type TelefunctionCallErrorConnection = Error & {
isConnectionError: true
isAbort?: undefined
}
type TelefunctionCallAbort = Error & {
isConnectionError?: undefined
isAbort: true
abortValue: unknown
}
2 changes: 1 addition & 1 deletion telefunc/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { telefuncConfig } from './clientConfig'
export { onTelefunctionRemoteCallError } from './remoteTelefunctionCall/onTelefunctionRemoteCallError'
export { onAbort, onTelefunctionRemoteCallError } from './remoteTelefunctionCall/onAbort'
export type { TelefunctionError } from './TelefunctionError'

export { remoteTelefunctionCall as __remoteTelefunctionCall } from './remoteTelefunctionCall'
5 changes: 2 additions & 3 deletions telefunc/client/remoteTelefunctionCall/makeHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { makeHttpRequest }

import { parse } from '@brillout/json-serializer/parse'
import { assert, assertUsage, isObject, objectAssign } from '../utils'
import { executeCallErrorListeners } from './onTelefunctionRemoteCallError'
import { callOnAbortListeners } from './onAbort'

const method = 'POST'
const STATUS_CODE_SUCCESS = 200
Expand Down Expand Up @@ -31,7 +31,6 @@ async function makeHttpRequest(callContext: {
} catch (_) {
const telefunctionCallError = new Error('No Server Connection')
objectAssign(telefunctionCallError, { isConnectionError: true as const })
executeCallErrorListeners(telefunctionCallError)
throw telefunctionCallError
}

Expand All @@ -48,7 +47,7 @@ async function makeHttpRequest(callContext: {
`Aborted telefunction call ${callContext.telefunctionName}() (${callContext.telefuncFilePath}).`
)
objectAssign(telefunctionCallError, { isAbort: true as const, abortValue })
executeCallErrorListeners(telefunctionCallError)
callOnAbortListeners(telefunctionCallError)
throw telefunctionCallError
} else if (statusCode === STATUS_CODE_BUG) {
const responseBody = await response.text()
Expand Down
34 changes: 34 additions & 0 deletions telefunc/client/remoteTelefunctionCall/onAbort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export { onAbort }
export { callOnAbortListeners }
export { onTelefunctionRemoteCallError }

import type { TelefunctionError, TelefunctionCallAbort } from '../TelefunctionError'
import { assertWarning } from '../utils'

type Listener = (err: TelefunctionCallAbort) => void

/** Outdated: use onAbort() instead */
function onTelefunctionRemoteCallError(listener: (err: TelefunctionError) => void) {
assertWarning(false, 'onTelefunctionRemoteCallError() deprecated in favor of onAbort()', {
onlyOnce: true,
showStackTrace: true
})
onAbort(listener)
}

function onAbort(listener: Listener) {
window.__telefunc_errorListeners = window.__telefunc_errorListeners || []
window.__telefunc_errorListeners.push(listener)
}

function callOnAbortListeners(err: TelefunctionCallAbort) {
;(window.__telefunc_errorListeners || []).forEach((listener) => {
listener(err)
})
}

declare global {
interface Window {
__telefunc_errorListeners: Listener[]
}
}

This file was deleted.

4 changes: 3 additions & 1 deletion telefunc/node/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// no-op
export const telefuncConfig = {}
export const onTelefunctionRemoteCallError = () => {};
export const onAbort = () => {}
/** @deprecated */
export const onTelefunctionRemoteCallError = () => {}

0 comments on commit ff15341

Please sign in to comment.