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: to add cache-mode to adapter #1888

Merged
merged 17 commits into from
Apr 11, 2024
Merged

feat: to add cache-mode to adapter #1888

merged 17 commits into from
Apr 11, 2024

Conversation

tdeekens
Copy link
Owner

@tdeekens tdeekens commented Apr 5, 2024

Summary

Relates to #1879

This introduces the behaviour of eager and lazy cache modes. At the same time it removes the subscribeToFlagChanges as we always subscribe. Lastly, the unsubscribeFromCachedFlags is almost like the lazy cache.

Copy link

changeset-bot bot commented Apr 5, 2024

🦋 Changeset detected

Latest commit: a27dd86

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@flopflip/cache Major
@flopflip/graphql-adapter Major
@flopflip/http-adapter Major
@flopflip/launchdarkly-adapter Major
@flopflip/react Major
@flopflip/types Major
@flopflip/adapter-utilities Major
@flopflip/combine-adapters Major
@flopflip/cypress-plugin Major
@flopflip/localstorage-adapter Major
@flopflip/localstorage-cache Major
@flopflip/memory-adapter Major
@flopflip/react-broadcast Major
@flopflip/react-redux Major
@flopflip/sessionstorage-cache Major
@flopflip/splitio-adapter Major
@flopflip/test-utils Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codecov bot commented Apr 7, 2024

Codecov Report

Attention: Patch coverage is 84.00000% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 93.84%. Comparing base (df89f1e) to head (a27dd86).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1888      +/-   ##
==========================================
+ Coverage   93.26%   93.84%   +0.57%     
==========================================
  Files         109      109              
  Lines        1752     1754       +2     
  Branches      256      266      +10     
==========================================
+ Hits         1634     1646      +12     
+ Misses        118      108      -10     
Files Coverage Δ
...components/configure-adapter/configure-adapter.tsx 92.80% <100.00%> (+0.05%) ⬆️
...ckages/launchdarkly-adapter/src/adapter/adapter.ts 95.42% <90.90%> (+0.07%) ⬆️
packages/graphql-adapter/src/adapter/adapter.ts 86.36% <81.81%> (-0.31%) ⬇️
packages/http-adapter/src/adapter/adapter.ts 85.82% <69.23%> (-1.04%) ⬇️
packages/cache/src/cache/cache.ts 86.79% <86.79%> (ø)

Copy link
Owner Author

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Next up I'll have to plug the loading of flags from the cache into react-redux and react-broadcast before the initial "setState" so that there is no "flash of changing flags".

@@ -0,0 +1,36 @@
{
Copy link
Owner Author

Choose a reason for hiding this comment

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

This makes a lot of things easier. No adapter knows about a direct type (local or session storage) of cache. A facade takes care of delegating and lazily importing concrete caches.

const FLAGS_CACHE_KEY = 'flags';
const FLAGS_REFERENCE_CACHE_KEY = 'flags-reference';

function getCachePrefix(adapterIdentifiers: TAdapterIdentifiers) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

This allows for making it generic.


if (haveFlagsBeenWritten) {
referenceCache.set(
FLAGS_REFERENCE_KEY,
[flagsCachePrefix, FLAGS_KEY].join('/')
FLAGS_REFERENCE_CACHE_KEY,
Copy link
Owner Author

Choose a reason for hiding this comment

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

Just renaming it.

},
};
}

function getCachedFlags(cacheIdentifier: TCacheIdentifiers): TFlags {
function getCachedFlags(
Copy link
Owner Author

Choose a reason for hiding this comment

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

This will later be used in react-redux and react-broadcast.

@@ -75,38 +73,6 @@ class GraphQlAdapter implements TGraphQlAdapterInterface {
readonly #getIsFlagLocked = (flagName: TFlagName) =>
this.#adapterState.lockedFlags.has(flagName);

readonly #getCache = async (cacheIdentifier: TCacheIdentifiers) => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

This for each adapter moved to the cache package.


cache.set(nextFlags);
}

this.#adapterState.flags = nextFlags;

if (adapterArgs.cacheMode === 'lazy') {
Copy link
Owner Author

Choose a reason for hiding this comment

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

In lazy mode we just write to the cache. No flushing of updates. The next configuration or reconfiguration (can be a reload) will flush this state "into React".

return {};
}

if (adapter.effectIds) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

This allows us to load default flags from various adapters when combined.

@tdeekens tdeekens marked this pull request as ready for review April 11, 2024 07:40
@tdeekens tdeekens merged commit 72f308b into main Apr 11, 2024
8 checks passed
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.

1 participant