-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: getprecomputedinstance should always return an instance #151
fix: getprecomputedinstance should always return an instance #151
Conversation
@@ -608,7 +614,7 @@ export class EppoPrecomputedJSClient extends EppoPrecomputedClient { | |||
export async function precomputedInit( | |||
config: IPrecomputedClientConfig, | |||
): Promise<EppoPrecomputedClient> { | |||
if (EppoPrecomputedJSClient.instance) { | |||
if (EppoPrecomputedJSClient.initialized) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another bit of feedback was about "shutting down" the client #117 (comment)
Since the client instance now will always be defined, I am switching to use the initialized
check. This makes it so you can't replace the singleton by re-running precomputedInit
: it'll just return the previously initialized instance. There is a private method visible below (shutdownEppoPrecomputedClient
) that would need to be called first before re-initializing, if we want to use it here to allow re-initialization/overwriting the singleton
…ld-always-return-an-instance
@@ -557,7 +557,13 @@ export function getConfigUrl(apiKey: string, baseUrl?: string): URL { | |||
* @public | |||
*/ | |||
export class EppoPrecomputedJSClient extends EppoPrecomputedClient { | |||
public static instance: EppoPrecomputedJSClient; | |||
public static instance = new EppoPrecomputedJSClient({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to confirm, this instantiation doesn't kick off any network requests or polling, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, you have to go through the precomputedInit
method to start the poller
labels: mergeable
Fixes: #issue
Motivation and Context
Some feedback in a previous PR that we should avoid overwriting the Eppo client instance.
This is challenging because it breaks the expected behavior that
getPrecomputedInstance
always returns an instance ofEppoPrecomputedClient
.js-client-sdk/src/index.ts
Lines 784 to 786 in f5d4eb4
It would be
undefined
before any of the initialization functions (precomputedInit
orofflinePrecomputedInit
) are called. This is likely to happen in complex applications with race conditions between various rendering components. This means the code would need an extra protection that the singleton client is defined before callingclient.getStringAssignment(..., defaultValue)
, and this is poor DX because they can't simply rely on the function falling back on returning thedefaultValue
if called before initialization and need to write in their own extra protection.Description
This initializes the client with an instance with an empty precomputed config store.
So instead of doing
you can just do
How has this been tested?