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: Add configuration to getDefaultManagers() #3161

Merged
merged 1 commit into from
Jul 23, 2024

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jul 22, 2024

Motivation

Currently, customizing managers found in the default managers required awkward filtering them out, and then replacing them in the correct position.

const getManagers =
  process.env.NODE_ENV === 'development' ?
    () => [
      ...getDefaultManagers().filter(
        mgr => mgr.constructor.name !== 'DevToolsManager',
      ),
    ]
  : getDefaultManagers;
function getManagers() {
  const managers = getDefaultManagers().filter(
    manager => manager.constructor.name !== 'DevToolsManager',
  );
  if (process.env.NODE_ENV !== 'production') {
    const networkManager: NetworkManager | undefined = managers.find(
      manager => manager instanceof NetworkManager,
    ) as any;
    managers.unshift(
      new DevToolsManager(
        {
          // double latency to help with high frequency updates
          latency: 1000,
          // skip websocket updates as these are too spammy
          predicate: (state, action) =>
            action.type !== actionTypes.SET_TYPE || action.schema !== Ticker,
        },
        networkManager && (action => networkManager.skipLogging(action)),
      ),
    );
  }
  return managers;
}

This is very cumbersome to write, and makes simple customizations non-delightful

Solution

Use getDefaultManagers' arguments to configure the provided managers. The argument will be a key of manager name to possible values:

  • An instance of the manager itself - enabling complete control
  • Arguments to be sent to constructor
  • null meaning disable. This will not work on NetworkManager.
// completely remove DevToolsManager
const managers = getDefaultManagers({ devToolsManager: null });
// easier configuration
const managers = getDefaultManagers({
  devToolsManager: {
    // double latency to help with high frequency updates
    latency: 1000,
    // skip websocket updates as these are too spammy
    predicate: (state, action) =>
      action.type !== actionTypes.SET_TYPE || action.schema !== Ticker,
  }
});
// passing instance allows us to use custom classes as well
const managers = getDefaultManagers({
	networkManager: new CustomNetworkManager(),
});

Copy link

changeset-bot bot commented Jul 22, 2024

🦋 Changeset detected

Latest commit: 8eaac3b

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

This PR includes changesets to release 3 packages
Name Type
@data-client/react Patch
@data-client/core Patch
example-benchmark Patch

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

@ntucker ntucker force-pushed the defaultmanagers-options branch from 2c5fde3 to 35e7efa Compare July 22, 2024 12:51
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 8eaac3b Previous: e4751d9 Ratio
normalizeLong 491 ops/sec (±0.51%) 469 ops/sec (±0.32%) 0.96
infer All 9691 ops/sec (±0.46%) 9422 ops/sec (±0.72%) 0.97
denormalizeLong 339 ops/sec (±0.65%) 315 ops/sec (±2.46%) 0.93
denormalizeLong donotcache 906 ops/sec (±0.37%) 897 ops/sec (±0.19%) 0.99
denormalizeShort donotcache 500x 1349 ops/sec (±0.45%) 1363 ops/sec (±0.14%) 1.01
denormalizeShort 500x 976 ops/sec (±0.27%) 986 ops/sec (±0.23%) 1.01
denormalizeShort 500x withCache 4868 ops/sec (±0.15%) 5030 ops/sec (±0.21%) 1.03
denormalizeLong with mixin Entity 285 ops/sec (±0.51%) 300 ops/sec (±0.47%) 1.05
denormalizeLong withCache 7006 ops/sec (±0.26%) 6917 ops/sec (±0.19%) 0.99
denormalizeLong All withCache 6427 ops/sec (±0.24%) 6686 ops/sec (±0.19%) 1.04
denormalizeLong Query-sorted withCache 6337 ops/sec (±0.40%) 6448 ops/sec (±0.40%) 1.02
denormalizeLongAndShort withEntityCacheOnly 1534 ops/sec (±0.46%) 1515 ops/sec (±0.56%) 0.99
getResponse 6500 ops/sec (±1.28%) 5988 ops/sec (±1.36%) 0.92
getResponse (null) 5662920 ops/sec (±1.18%) 5240260 ops/sec (±0.64%) 0.93
getResponse (clear cache) 289 ops/sec (±0.86%) 289 ops/sec (±0.68%) 1
getSmallResponse 2673 ops/sec (±0.45%) 2644 ops/sec (±0.33%) 0.99
getSmallInferredResponse 2050 ops/sec (±0.28%) 2084 ops/sec (±0.21%) 1.02
getResponse Query-sorted 6791 ops/sec (±0.38%) 6797 ops/sec (±0.68%) 1.00
getResponse Collection 6560 ops/sec (±0.72%) 5911 ops/sec (±0.83%) 0.90
get Collection 5652 ops/sec (±1.17%) 4915 ops/sec (±1.48%) 0.87
setLong 500 ops/sec (±0.46%) 485 ops/sec (±0.58%) 0.97
setLongWithMerge 199 ops/sec (±0.30%) 194 ops/sec (±0.43%) 0.97
setLongWithSimpleMerge 210 ops/sec (±0.44%) 208 ops/sec (±0.46%) 0.99
setSmallResponse 500x 911 ops/sec (±0.23%) 896 ops/sec (±0.09%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Copy link

codecov bot commented Jul 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.76%. Comparing base (e4751d9) to head (1cd1abc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3161      +/-   ##
==========================================
+ Coverage   98.58%   98.76%   +0.18%     
==========================================
  Files         131      132       +1     
  Lines        2260     2275      +15     
  Branches      459      466       +7     
==========================================
+ Hits         2228     2247      +19     
+ Misses         21       17       -4     
  Partials       11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ntucker ntucker force-pushed the defaultmanagers-options branch 2 times, most recently from 3a3a867 to 1cd1abc Compare July 23, 2024 09:26
@ntucker ntucker force-pushed the defaultmanagers-options branch from 1cd1abc to 8eaac3b Compare July 23, 2024 09:37
@ntucker ntucker merged commit b932dca into master Jul 23, 2024
24 checks passed
@ntucker ntucker deleted the defaultmanagers-options branch July 23, 2024 09:44
@github-actions github-actions bot mentioned this pull request Jul 23, 2024
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