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

enhance: Simplify action shapes #3143

Merged
merged 1 commit into from
Jul 12, 2024
Merged

enhance: Simplify action shapes #3143

merged 1 commit into from
Jul 12, 2024

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jul 11, 2024

Motivation

  • Simplification
  • Performance: reduce distinct ICs

Solution

actions flattening

  • meta for most actions (all but fetch) are just date information, to ensure common format
  • hoisted args and key to top level (action.meta.key -> action.key)
export interface ActionMeta {
  readonly fetchedAt: number;
  readonly date: number;
  readonly expiresAt: number;
}
export interface SetAction<S extends Queryable = any> {
  type: typeof SET_TYPE;
  schema: S;
  args: readonly any[];
  meta: ActionMeta;
  value: {} | ((previousValue: Denormalize<S>) => {});
}
export interface SetResponseActionBase<
  E extends EndpointAndUpdate<E> = EndpointDefault,
> {
  type: typeof SET_RESPONSE_TYPE;
  endpoint: E;
  args: readonly any[];
  key: string;
  meta: ActionMeta;
}

normalize() interface

function normalize(
  schema,
  input,
  args = [],
  { entities, indexes, entityMeta } = {
    entities: {},
    indexes: {},
    entityMeta: {},
  },
  meta = { fetchedAt: 0, date: Date.now(), expiresAt: Infinity },
);

actions namespace

Add actions export

actions is a namespace for all action creators. It is typically
preferred to use Controller's type-safe dispatch methods, as
members of this namespace could have breaking changes in a minor release.

import { actions, type Manager, type Middleware } from '@data-client/core';

export default class MyManager implements Manager {
  getMiddleware = (): Middleware => controller => next => {
    const todo = { id: '5', title: 'my first todo' };

    // These do the same thing
    controller.dispatch(
      actions.createSet(Todo, { args: [{ id: todo.id }], value: todo }),
    );
    // This is simpler; type-enforced; and will only change in major versions
    controller.set(Todo, { id: todo.id }, todo);

    return async action => next(action);
  };

  cleanup() {}
}

BREAKING CHANGE: Removed createFetch, createSet, createSetResponse from export. Use action.createFetch instead.

Copy link

changeset-bot bot commented Jul 11, 2024

🦋 Changeset detected

Latest commit: 221fed5

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

This PR includes changesets to release 7 packages
Name Type
@data-client/core Minor
@data-client/react Minor
@data-client/normalizr Minor
example-benchmark Patch
normalizr-github-example Patch
normalizr-relationships Patch
coinbase-lite 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

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: 221fed5 Previous: d225595 Ratio
normalizeLong 516 ops/sec (±0.23%) 508 ops/sec (±0.62%) 0.98
infer All 9532 ops/sec (±0.37%) 9576 ops/sec (±1.56%) 1.00
denormalizeLong 334 ops/sec (±2.56%) 328 ops/sec (±1.70%) 0.98
denormalizeLong donotcache 886 ops/sec (±0.31%) 878 ops/sec (±2.04%) 0.99
denormalizeShort donotcache 500x 1326 ops/sec (±0.06%) 1368 ops/sec (±0.16%) 1.03
denormalizeShort 500x 959 ops/sec (±0.15%) 976 ops/sec (±0.10%) 1.02
denormalizeShort 500x withCache 4898 ops/sec (±0.48%) 4846 ops/sec (±0.19%) 0.99
denormalizeLong with mixin Entity 288 ops/sec (±0.34%) 303 ops/sec (±0.25%) 1.05
denormalizeLong withCache 6588 ops/sec (±0.21%) 6434 ops/sec (±0.47%) 0.98
denormalizeLong All withCache 6580 ops/sec (±0.20%) 6788 ops/sec (±0.19%) 1.03
denormalizeLong Query-sorted withCache 6370 ops/sec (±0.24%) 6691 ops/sec (±0.52%) 1.05
denormalizeLongAndShort withEntityCacheOnly 1459 ops/sec (±0.45%) 1473 ops/sec (±0.46%) 1.01
getResponse 6354 ops/sec (±1.06%) 6013 ops/sec (±0.57%) 0.95
getResponse (null) 5512467 ops/sec (±0.93%) 5589476 ops/sec (±1.02%) 1.01
getResponse (clear cache) 293 ops/sec (±0.34%) 293 ops/sec (±0.40%) 1
getSmallResponse 2580 ops/sec (±0.26%) 2736 ops/sec (±0.20%) 1.06
getSmallInferredResponse 1987 ops/sec (±0.20%) 2024 ops/sec (±0.20%) 1.02
getResponse Query-sorted 5664 ops/sec (±0.49%) 6641 ops/sec (±1.01%) 1.17
getResponse Collection 5825 ops/sec (±0.60%) 6249 ops/sec (±1.05%) 1.07
get Collection 5267 ops/sec (±1.73%) 5379 ops/sec (±1.32%) 1.02
setLong 516 ops/sec (±0.18%) 511 ops/sec (±1.39%) 0.99
setLongWithMerge 200 ops/sec (±0.29%) 202 ops/sec (±0.30%) 1.01
setLongWithSimpleMerge 210 ops/sec (±0.44%) 207 ops/sec (±0.60%) 0.99
setSmallResponse 500x 896 ops/sec (±0.18%) 894 ops/sec (±0.35%) 1.00

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

@ntucker ntucker force-pushed the action-breaking branch 6 times, most recently from f36b186 to 636b8c0 Compare July 11, 2024 18:28
Copy link

codecov bot commented Jul 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.79%. Comparing base (01b5908) to head (221fed5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3143      +/-   ##
==========================================
- Coverage   98.79%   98.79%   -0.01%     
==========================================
  Files         126      128       +2     
  Lines        2248     2242       -6     
  Branches      460      455       -5     
==========================================
- Hits         2221     2215       -6     
  Misses         16       16              
  Partials       11       11              

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

@ntucker ntucker force-pushed the action-breaking branch 4 times, most recently from c488ccc to 8a9e2f7 Compare July 12, 2024 13:00
@ntucker ntucker merged commit f4cf8a4 into master Jul 12, 2024
26 checks passed
@ntucker ntucker deleted the action-breaking branch July 12, 2024 13:19
@github-actions github-actions bot mentioned this pull request Jul 12, 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.

None yet

1 participant