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

feat: [do not re-init and perform a network request if the singleton … #95

Merged
merged 3 commits into from
Aug 5, 2024

Conversation

leoromanovsky
Copy link
Member

@leoromanovsky leoromanovsky commented Aug 1, 2024

…has already been init] (FF-2921)


labels: mergeable

Fixes: #issue

Motivation and Context

If a user calls init repeatedly, the client will issue multiple network requests. This is not desirable behavior as the configuration and assignment cache are also reset.

To support users explicitly reloading configuration we should offer a dedicated refetch method.

Description

  • Default behavior change: if the singleton already has been initialized, defined by the init method completing (not taking into account the AsyncStore or others), return it immediately without re-instantiating.
  • Offers a new flag called forceReinitialize - setting to true re-instantiates the client. I'm not convinced this is needed for users but is useful for tests.
  • Bumped to 3.5.0 since this is new functionality - you can argue that functionality is "breaking" but I view this as a correction. We could even argue to publish it as a bug fix.

How has this been tested?

  • New unit tests.

Question for reviewers

  1. Should we publish as a patch release?

@leoromanovsky leoromanovsky marked this pull request as ready for review August 1, 2024 01:50
@@ -191,6 +191,7 @@ describe('EppoJSClient E2E test', () => {
apiKey,
baseUrl,
assignmentLogger: mockLogger,
forceReinitialize: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this is pretty messy - ideally we would not need to do this in tests but the singletons persist across test cases. Maybe this trick is what's needed for an isolated test:

      jest.isolateModules(() => {
        // Isolate and re-require so that the static instance is reset to it's default state
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const reloadedModule = require('./index');
        init = reloadedModule.init;
        getInstance = reloadedModule.getInstance;
      });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's nice to be able to force re-initialize, for tests and possibly other applications

@@ -871,6 +932,7 @@ describe('initialization options', () => {
assignmentLogger: mockLogger,
updateOnFetch,
useExpiredCache: true,
forceReinitialize: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of the test cases here could be incorrect. this one:

// Init again with an expired cache so a fetch will kicked off too
// We allow the expired cache, so it will succeed and fetch will be delayed

might be contrary to the singleton approach - or perhaps we need to check more conditions than just initialized

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were using multiple initializations as a hack to check populating and then using the cache. If we want, we could rework tests to check cache contents and set up an already-populated cache another way but I think this is probably fine for now.

assignmentLogger: mockLogger,
});

expect(callCount).toBe(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test

src/index.ts Outdated
if (EppoJSClient.initialized) {
if (forceReinitialize) {
applicationLogger.warn(
'Eppo SDK is already initialized. Since forceReinitialize is true, reinitializing.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd change this to an info instead of a warn. also minor rewording: Eppo SDK is already initialized. Reinitializing since forceReinitialize is true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH none of these logs are getting emitted anyways since this is the browser 😄

src/index.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@aarsilv aarsilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this!

@@ -191,6 +191,7 @@ describe('EppoJSClient E2E test', () => {
apiKey,
baseUrl,
assignmentLogger: mockLogger,
forceReinitialize: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's nice to be able to force re-initialize, for tests and possibly other applications

forceReinitialize: true,
});

expect(callCount).toBe(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -871,6 +932,7 @@ describe('initialization options', () => {
assignmentLogger: mockLogger,
updateOnFetch,
useExpiredCache: true,
forceReinitialize: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were using multiple initializations as a hack to check populating and then using the cache. If we want, we could rework tests to check cache contents and set up an already-populated cache another way but I think this is probably fine for now.

src/index.ts Outdated Show resolved Hide resolved
@leoromanovsky leoromanovsky merged commit c34f847 into main Aug 5, 2024
2 checks passed
@leoromanovsky leoromanovsky deleted the lr/ff-2921 branch August 5, 2024 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants