Skip to content

Commit

Permalink
fix: getprecomputedinstance should always return an instance (#151)
Browse files Browse the repository at this point in the history
* Initialize with an empty client

* Add a test

* Adjust check for singleton was initialized

* Check for instance is no longer necessary because it is always defined
  • Loading branch information
sameerank authored Jan 17, 2025
1 parent b8f579e commit ec71cac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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({
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) {
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

0 comments on commit ec71cac

Please sign in to comment.