From ff153416f5eac4a8ce295219c50762f4d37ebc0b Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 4 Nov 2022 16:42:09 +0100 Subject: [PATCH] fix: implement `onAbort()` and soft-deprecate `onTelefunctionRemoteCallError()` --- docs/pages/permissions.page.server.mdx | 8 ++--- .../renderer/_default.page.client.tsx | 6 ++-- telefunc/client/TelefunctionError.ts | 32 ++++++++--------- telefunc/client/index.ts | 2 +- .../remoteTelefunctionCall/makeHttpRequest.ts | 5 ++- .../client/remoteTelefunctionCall/onAbort.ts | 34 +++++++++++++++++++ .../onTelefunctionRemoteCallError.ts | 23 ------------- telefunc/node/client.ts | 4 ++- 8 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 telefunc/client/remoteTelefunctionCall/onAbort.ts delete mode 100644 telefunc/client/remoteTelefunctionCall/onTelefunctionRemoteCallError.ts diff --git a/docs/pages/permissions.page.server.mdx b/docs/pages/permissions.page.server.mdx index 1b2ea697..352ff140 100644 --- a/docs/pages/permissions.page.server.mdx +++ b/docs/pages/permissions.page.server.mdx @@ -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' } diff --git a/examples/authentication/renderer/_default.page.client.tsx b/examples/authentication/renderer/_default.page.client.tsx index 7a4cb5d0..bd803974 100644 --- a/examples/authentication/renderer/_default.page.client.tsx +++ b/examples/authentication/renderer/_default.page.client.tsx @@ -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) { @@ -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() } }) diff --git a/telefunc/client/TelefunctionError.ts b/telefunc/client/TelefunctionError.ts index 783c377c..67528897 100644 --- a/telefunc/client/TelefunctionError.ts +++ b/telefunc/client/TelefunctionError.ts @@ -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 +} diff --git a/telefunc/client/index.ts b/telefunc/client/index.ts index 7704ecd2..3dd82e85 100644 --- a/telefunc/client/index.ts +++ b/telefunc/client/index.ts @@ -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' diff --git a/telefunc/client/remoteTelefunctionCall/makeHttpRequest.ts b/telefunc/client/remoteTelefunctionCall/makeHttpRequest.ts index 43f1ca56..b6cdd4a8 100644 --- a/telefunc/client/remoteTelefunctionCall/makeHttpRequest.ts +++ b/telefunc/client/remoteTelefunctionCall/makeHttpRequest.ts @@ -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 @@ -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 } @@ -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() diff --git a/telefunc/client/remoteTelefunctionCall/onAbort.ts b/telefunc/client/remoteTelefunctionCall/onAbort.ts new file mode 100644 index 00000000..10cbd148 --- /dev/null +++ b/telefunc/client/remoteTelefunctionCall/onAbort.ts @@ -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[] + } +} diff --git a/telefunc/client/remoteTelefunctionCall/onTelefunctionRemoteCallError.ts b/telefunc/client/remoteTelefunctionCall/onTelefunctionRemoteCallError.ts deleted file mode 100644 index 46c0fe35..00000000 --- a/telefunc/client/remoteTelefunctionCall/onTelefunctionRemoteCallError.ts +++ /dev/null @@ -1,23 +0,0 @@ -export { onTelefunctionRemoteCallError } -export { executeCallErrorListeners } - -import type { TelefunctionError } from '../TelefunctionError' - -type Listener = (err: TelefunctionError) => void - -function onTelefunctionRemoteCallError(listener: Listener) { - window.__telefunc_errorListeners = window.__telefunc_errorListeners || [] - window.__telefunc_errorListeners.push(listener) -} - -function executeCallErrorListeners(err: TelefunctionError) { - ;(window.__telefunc_errorListeners || []).forEach((listener) => { - listener(err) - }) -} - -declare global { - interface Window { - __telefunc_errorListeners: Listener[] - } -} diff --git a/telefunc/node/client.ts b/telefunc/node/client.ts index 654de760..29d14781 100644 --- a/telefunc/node/client.ts +++ b/telefunc/node/client.ts @@ -1,3 +1,5 @@ // no-op export const telefuncConfig = {} -export const onTelefunctionRemoteCallError = () => {}; +export const onAbort = () => {} +/** @deprecated */ +export const onTelefunctionRemoteCallError = () => {}