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

Update dependency memoize-one to v6 #6832

Merged
merged 1 commit into from
Oct 27, 2021
Merged

Conversation

renovate[bot]
Copy link
Contributor

@renovate renovate bot commented Oct 26, 2021

WhiteSource Renovate

This PR contains the following updates:

Package Change Age Adoption Passing Confidence
memoize-one ^5.2.1 -> ^6.0.0 age adoption passing confidence

Release Notes

alexreardon/memoize-one

v6.0.0

Compare Source

At a glance

🧹 New .clear() function to remove the current memoization cache
🏋️‍♀️ Stronger types
🥰 Improved documentation

This release is a major, but there are no behaviour or API changes. The major is to reflect that some of the TypeScript types have been tightened which might cause some peoples TypeScript builds to break.

PSA: memoize-one will soon™️ be dropping ie11 support
More details

New: Cache releasing with .clear() 🧹

A .clear() property is now added to memoized functions to allow you to clear it's memoization cache

This is helpful if you want to:

  • Release memory
  • Allow the underlying function to be called again without having to change arguments
import memoizeOne from 'memoize-one';

function add(a: number, b: number): number {
  return a + b;
}

const memoizedAdd = memoizeOne(add);

// first call - not memoized
const first = memoizedAdd(1, 2);

// second call - cache hit (underlying function not called)
const second = memoizedAdd(1, 2);

// 👋 clearing memoization cache
memoizedAdd.clear();

// third call - not memoized (cache was cleared)
const third = memoizedAdd(1, 2);

Improved: memoized function type

There are no API changes to memoize-one, this is merely a typing improvement

Our previous type for a memoized function was simple:

Previous

export declare type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;

declare function memoizeOne<ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>>(resultFn: ResultFn, isEqual?: EqualityFn): ResultFn;

A memoized function claimed to be the same type (ResultFn) as the original function. This was not true, as memoize-one does not copy of any existing object properties on the original function (TFunc)

Updated

export declare type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
  clear: () => void;
  (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
};

declare function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>(
  resultFn: TFunc,
  isEqual?: EqualityFn<TFunc>,
): MemoizedFn<TFunc>;

A memoized function returns the same callable signature as the original function (TFunc → was ResultFn), but it makes it clear that no function object properties on the original function (TFunc) are being carried forward. The memoized function also now includes a .clear() function object property

If you want to continue to use the old types where the memoized function is the same type as the function being memoized, you can achieve this by casting the type of your memoized function:

function add(first: number, second: number): number {
  return first + second;
}
// a type that matches our add function
type AddFn = (first: number, second: number) => number;

// type of memoized will be MemoizedFn<typeof add>
const memoized = memoize(add);

// option 1
const memoized: typeof add = memoize(add);

// option 2
const memoized: AddFn = memoize(add);

// option 3
const memoized = memoize(add) as typeof add;

// option 4
const memoized = memoize(add) as AddFn;

This type change has been labelled as a patch (fix) as the previous type was not correct. However, you could consider it a major given that the new type is narrower than before

Improved: equality function type

There are no API changes to equality functions, this is merely a typing improvement

Previous

export type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;

Current

export type EqualityFn<TFunc extends (...args: any[]) => any> = (
  newArgs: Parameters<TFunc>,
  lastArgs: Parameters<TFunc>,
) => boolean;

This looks a little scary, but it is pretty neat! It means that you can dramatically improve the type safety of your custom equality functions if you want to.

If you are not using a custom equality function

No changes for you!

If you are using a custom equality function

Most people will not be impacted!

This type tightening allows you to be a lot stricter with the shape of your functions passed in as equality functions. If you are using generic equality functions such as lodash.isequal their types are loose and there is nothing you will need to do. But if you want to write more efficient and typesafe equality functions, you are in for a treat.

An example of what things looked like in 5.x

import memoize, { EqualityFn } from "memoize-one";

type Person = {
  id: string;
  name: string;
};

function invite(person: Person) {
  // This could do something fancy, but just keeping things basic
  console.log("invited:", person.name);
}

// Yuck, we don't know anything about the args
// Note: don't really need the `EqualityFn` type as it is just:
// `type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;`
const isEqual: EqualityFn = (newArgs: any[], lastArgs: any[]): boolean => {
  // Yuck #&#8203;2: we have to cast here
  // We would also be creating a bug if isEqual is used on a function
  // that has no arguments
  const first = newArgs[0] as Person;
  const second = lastArgs[0] as Person;
  return first.id === second.id;
};

const memoized = memoize(invite, isEqual);

const alex: Person = {
  name: "Alex",
  id: "11111"
};

memoized(alex);
// Won't do anything as `alex` has the same id as the last `alex`
memoized(alex);

You can play with this example on codesandbox.io

The same example in 6.x

import memoize, { EqualityFn } from "memoize-one";

type Person = {
  id: string;
  name: string;
};

function invite(person: Person) {
  console.log("invited:", person.name);
}

// Yum: we know that newArgs + lastArgs are the tuple `[Person]`
const isEqual: EqualityFn<typeof invite> = ([first], [second]): boolean => {
  return first.id === second.id;
};

const memoized = memoize(invite, isEqual);

const alex: Person = {
  name: "Alex",
  id: "11111"
};

memoized(alex);
// Won't do anything as `alex` has the same id as the last `alex`
memoized(alex);

// When declared inline, our isEqual function has the correct types inferred
const inferred = memoize(invite, function isEqual([first], [second]): boolean {
  return first.id === second.id;
});

You can play with this example on codesandbox.io

There are a few cases where this could cause your TypeScript types to start failing, so this change has been listed as a major

Improved: Documentation

  • Cleanup and audit of examples
  • Added documentation for .clear()
  • Added documentation about function properties and the .length property
  • Updated performance benchmarks

Internal code health

  • Now on TypeScript@4.4.3
  • Moved from TravisCI to Github workflows
  • Upgraded devDependencies

Thanks ❤️

Thanks so much to the following people who helped make this release possible:

Catch you next time,


Configuration

📅 Schedule: "before 7am on Tuesday,before 7am on Wednesday" in timezone Australia/Sydney.

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this PR and you won't be reminded about these updates again.


  • If you want to rebase/retry this PR, click this checkbox.

This PR has been generated by WhiteSource Renovate. View repository job log here.

@vercel
Copy link

vercel bot commented Oct 26, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/keystonejs/keystone-next-docs/GCkPrekZDXW9pErcvw5zFBYSmKZE
✅ Preview: https://keystone-next-docs-git-renovate-memoize-one-6x-keystonejs.vercel.app

@changeset-bot
Copy link

changeset-bot bot commented Oct 26, 2021

⚠️ No Changeset found

Latest commit: 0312861

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@vercel vercel bot temporarily deployed to Preview October 26, 2021 16:31 Inactive
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 26, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0312861:

Sandbox Source
memoize-one-old PR
memoize-one-new PR

@vercel vercel bot temporarily deployed to Preview October 26, 2021 22:49 Inactive
@dcousens dcousens changed the base branch from main to renovating October 27, 2021 03:11
@renovate renovate bot force-pushed the renovate/memoize-one-6.x branch from d367eb0 to 0312861 Compare October 27, 2021 03:20
@vercel vercel bot temporarily deployed to Preview October 27, 2021 03:20 Inactive
Copy link
Member

@dcousens dcousens left a comment

Choose a reason for hiding this comment

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

https://github.com/alexreardon/memoize-one/releases/tag/v6.0.0

This release is a major, but there are no behaviour or API changes. The major is to reflect that some of the TypeScript types have been tightened which might cause some peoples TypeScript builds to break.

@dcousens dcousens merged commit 09d1fa0 into renovating Oct 27, 2021
@dcousens dcousens deleted the renovate/memoize-one-6.x branch October 27, 2021 05:09
dcousens pushed a commit that referenced this pull request Nov 1, 2021
Co-authored-by: Renovate Bot <bot@renovateapp.com>
dcousens pushed a commit that referenced this pull request Nov 1, 2021
Co-authored-by: Renovate Bot <bot@renovateapp.com>
gwyneplaine pushed a commit that referenced this pull request Nov 2, 2021
Co-authored-by: Renovate Bot <bot@renovateapp.com>
dcousens pushed a commit that referenced this pull request Nov 8, 2021
Co-authored-by: Renovate Bot <bot@renovateapp.com>
gwyneplaine added a commit that referenced this pull request Nov 9, 2021
* Update dependency @testing-library/dom to ^8.10.1 (#6817)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Lock file maintenance (#6810)

* Lock file maintenance

* resolve type errors

* revert mdx bump to resolve vercel build errors

* conflict resolution

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: gwyneplaine <cc.lee@live.com.au>

* Update dependency @graphql-tools/schema to ^8.3.0 (#6815)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update nextjs monorepo to v12 (major) (#6835)

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
Co-authored-by: mitchellhamilton <mitchell@hamil.town>

* Update dependency @manypkg/cli to ^0.19.0 (#6859)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency @testing-library/dom to ^8.10.1 (#6817)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update typescript-eslint monorepo to ^5.2.0 (#6829)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency memoize-one to v6 (#6832)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* add CHANGESET

* Lock file maintenance (#6810)

* Lock file maintenance

* resolve type errors

* revert mdx bump to resolve vercel build errors

* conflict resolution

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: gwyneplaine <cc.lee@live.com.au>

* Update dependency @graphql-tools/schema to ^8.3.0 (#6815)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency @emotion/cache to v11.5.0 (#6827)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update Node.js to v16 (#6830)

* Update Node.js to v16

* bump workflows to Node 16

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>

* Update nextjs monorepo to v12 (major) (#6835)

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
Co-authored-by: mitchellhamilton <mitchell@hamil.town>

* Update dependency @manypkg/cli to ^0.19.0 (#6859)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency mime to ^2.6.0 (#6879)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency @testing-library/jest-dom to ^5.15.0 (#6873)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update patch dependencies (#6857)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency stripe to ^8.186.0 (#6821)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency graphql to ^15.7.2 (#6860)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency react-select to ^5.2.0 (#6874)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update typescript-eslint monorepo to ^5.3.0 (#6861)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Lock file maintenance (#6852)

* Lock file maintenance

* resolve type errors

* resolve build issues

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: gwyneplaine <cc.lee@live.com.au>

* Update prisma monorepo to v3.4.0 (minor) (#6876)

* Update prisma monorepo to v3.4.0

* update prisma version in tests

* changeset

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: gwyneplaine <cc.lee@live.com.au>

* Update dependency @changesets/cli to ^2.18.0 (#6900)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update dependency mime to v3 (#6903)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update Apollo GraphQL packages (#6899)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
Co-authored-by: mitchellhamilton <mitchell@hamil.town>
dcousens pushed a commit that referenced this pull request Dec 15, 2021
Co-authored-by: Renovate Bot <bot@renovateapp.com>
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.

2 participants