Skip to content

Commit

Permalink
feat: implement onLaikaReady callback for createGlobalLaikaLink (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: Donovan Yohan <[email protected]>
  • Loading branch information
donovan-yohan and Donovan Yohan authored Aug 26, 2022
1 parent f7a41bb commit bf91ff3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/createGlobalLaikaLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const getLaikaSingleton = memoize(
(
globalPropertyName: string = DEFAULT_GLOBAL_PROPERTY_NAME,
startLoggingImmediately: boolean = false,
onLaikaReady?: (laika: Laika) => void,
) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access,no-multi-assign
const singleton = ((globalThis as any)[globalPropertyName] = new Laika({
Expand All @@ -16,6 +17,9 @@ export const getLaikaSingleton = memoize(
if (startLoggingImmediately) {
singleton.log.startLogging()
}

onLaikaReady?.(singleton)

return singleton
},
)
Expand All @@ -28,11 +32,16 @@ export function createGlobalLaikaLink({
clientName = '__unknown__',
globalPropertyName,
startLoggingImmediately = false,
onLaikaReady,
}: CreateLaikaLinkOptions) {
if (clientName === '__unknown__') {
throw new Error('LaikaLink: clientName is required')
}
const laika = getLaikaSingleton(globalPropertyName, startLoggingImmediately)
const laika = getLaikaSingleton(
globalPropertyName,
startLoggingImmediately,
onLaikaReady,
)
return laika.createLink((operation) => {
operation.setContext({ clientName })
})
Expand Down
2 changes: 2 additions & 0 deletions src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Observable,
Operation,
} from '@apollo/client/core'
import type { Laika } from './laika'

/** @ignore */
export type { FetchResult, NextLink, Operation } from '@apollo/client/core'
Expand Down Expand Up @@ -123,6 +124,7 @@ export interface CreateLaikaLinkOptions {
clientName: string
globalPropertyName?: string
startLoggingImmediately?: boolean
onLaikaReady?: (laika: Laika) => void
}

// helpers
Expand Down

0 comments on commit bf91ff3

Please sign in to comment.