Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
EppoPrecomputedJSClient,
getConfigUrl,
getInstance,
getPrecomputedInstance,
IAssignmentLogger,
init,
offlineInit,
Expand Down Expand Up @@ -1278,6 +1279,19 @@ describe('offlinePrecomputedInit', () => {
});
});

describe('getPrecomputedInstance', () => {
it('returns an instance that safely returns defaults without logging', () => {
const mockLogger = td.object<IAssignmentLogger>();
const instance = getPrecomputedInstance();
instance.setAssignmentLogger(mockLogger);

const result = instance.getStringAssignment('any-flag', 'default-value');

expect(result).toBe('default-value');
td.verify(mockLogger.logAssignment(td.matchers.anything()), { times: 0 });
});
});

it('initializes without an assignment logger', () => {
const client = offlinePrecomputedInit({ precomputedConfiguration });

Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Copy link
Collaborator

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?

Copy link
Contributor Author

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

precomputedFlagStore: memoryOnlyPrecomputedFlagsStore,
subject: {
subjectKey: '',
subjectAttributes: {},
},
});
public static initialized = false;

public getStringAssignment(flagKey: string, defaultValue: string): string {
Expand Down Expand Up @@ -609,7 +615,7 @@ export class EppoPrecomputedJSClient extends EppoPrecomputedClient {
export async function precomputedInit(
config: IPrecomputedClientConfig,
): Promise<EppoPrecomputedClient> {
if (EppoPrecomputedJSClient.instance) {
if (EppoPrecomputedJSClient.initialized) {
Copy link
Contributor Author

@sameerank sameerank Jan 16, 2025

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

return EppoPrecomputedJSClient.instance;
}

Expand Down Expand Up @@ -764,7 +770,7 @@ export function offlinePrecomputedInit(
}

function shutdownEppoPrecomputedClient() {
if (EppoPrecomputedJSClient.instance && EppoPrecomputedJSClient.initialized) {
if (EppoPrecomputedJSClient.initialized) {
EppoPrecomputedJSClient.instance.stopPolling();
EppoPrecomputedJSClient.initialized = false;
applicationLogger.warn('[Eppo SDK] Precomputed client is being re-initialized.');
Expand Down
Loading