From bf91ff3bc2f3643d1bef9133d9b990d9416aebec Mon Sep 17 00:00:00 2001 From: Donovan Yohan <34756395+donovan-yohan@users.noreply.github.com> Date: Fri, 26 Aug 2022 16:09:11 -0400 Subject: [PATCH] feat: implement onLaikaReady callback for createGlobalLaikaLink (#33) Co-authored-by: Donovan Yohan --- src/createGlobalLaikaLink.ts | 11 ++++++++++- src/typedefs.ts | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/createGlobalLaikaLink.ts b/src/createGlobalLaikaLink.ts index 55bb1f6..bdd8146 100644 --- a/src/createGlobalLaikaLink.ts +++ b/src/createGlobalLaikaLink.ts @@ -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({ @@ -16,6 +17,9 @@ export const getLaikaSingleton = memoize( if (startLoggingImmediately) { singleton.log.startLogging() } + + onLaikaReady?.(singleton) + return singleton }, ) @@ -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 }) }) diff --git a/src/typedefs.ts b/src/typedefs.ts index 7860cab..1f6ea18 100644 --- a/src/typedefs.ts +++ b/src/typedefs.ts @@ -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' @@ -123,6 +124,7 @@ export interface CreateLaikaLinkOptions { clientName: string globalPropertyName?: string startLoggingImmediately?: boolean + onLaikaReady?: (laika: Laika) => void } // helpers