Skip to content

Commit

Permalink
refactor(deps): drop memoize-one
Browse files Browse the repository at this point in the history
  • Loading branch information
100terres committed Jan 4, 2025
1 parent 786ee75 commit 82e1d0a
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 19 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"dependencies": {
"@babel/runtime": "^7.25.6",
"css-box-model": "^1.2.1",
"memoize-one": "^6.0.0",
"raf-schd": "^4.0.3",
"react-redux": "^9.1.2",
"redux": "^5.0.1",
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/are-inputs-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Original author: Alex Reardon
* License: MIT License
* Repo: https://github.com/alexreardon/memoize-one
*/

function isEqual(first: unknown, second: unknown): boolean {
if (first === second) {
return true;
}

// Special case for NaN (NaN !== NaN)
if (Number.isNaN(first) && Number.isNaN(second)) {
return true;
}

return false;
}

export default function areInputsEqual(
newInputs: readonly unknown[],
lastInputs: readonly unknown[],
): boolean {
// no checks needed if the inputs length has changed
if (newInputs.length !== lastInputs.length) {
return false;
}
// Using for loop for speed. It generally performs better than array.every
// https://github.com/alexreardon/memoize-one/pull/59
for (let i = 0; i < newInputs.length; i++) {
if (!isEqual(newInputs[i], lastInputs[i])) {
return false;
}
}
return true;
}
66 changes: 66 additions & 0 deletions src/memoize-one.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Original author: Alex Reardon
* License: MIT License
* Repo: https://github.com/alexreardon/memoize-one
* Description: A memoization library which only remembers the latest invocation.
*/

import areInputsEqual from './are-inputs-equal';

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

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

// internal type
interface Cache<TFunc extends (this: any, ...args: any[]) => any> {
lastThis: ThisParameterType<TFunc>;
lastArgs: Parameters<TFunc>;
lastResult: ReturnType<TFunc>;
}

function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>(
resultFn: TFunc,
isEqual: EqualityFn<TFunc> = areInputsEqual,
): MemoizedFn<TFunc> {
let cache: Cache<TFunc> | null = null;

// breaking cache when context (this) or arguments change
function memoized(
this: ThisParameterType<TFunc>,
...newArgs: Parameters<TFunc>
): ReturnType<TFunc> {
if (cache && cache.lastThis === this && isEqual(newArgs, cache.lastArgs)) {
return cache.lastResult;
}

// Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz
// Doing the lastResult assignment first so that if it throws
// the cache will not be overwritten
const lastResult = resultFn.apply(this, newArgs);
cache = {
lastResult,
lastArgs: newArgs,
lastThis: this,
};

return lastResult;
}

// Adding the ability to clear the cache of a memoized function
memoized.clear = function clear() {
cache = null;
};

return memoized;
}

export default memoizeOne;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import memoizeOne from 'memoize-one';
import type { Position } from 'css-box-model';
import memoizeOne from '../../../memoize-one';
import type {
DroppableDimension,
DroppableDimensionMap,
Expand Down
2 changes: 1 addition & 1 deletion src/state/dimension-structures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memoizeOne from 'memoize-one';
import memoizeOne from '../memoize-one';
import type {
DroppableDimension,
DroppableDimensionMap,
Expand Down
2 changes: 1 addition & 1 deletion src/state/get-displaced-by.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import memoizeOne from 'memoize-one';
import type { Position } from 'css-box-model';
import memoizeOne from '../memoize-one';
import type { Axis, DisplacedBy } from '../types';
import { patch } from './position';

Expand Down
2 changes: 1 addition & 1 deletion src/state/get-draggables-inside-droppable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memoizeOne from 'memoize-one';
import memoizeOne from '../memoize-one';
import { toDraggableList } from './dimension-structures';
import type {
DraggableDimension,
Expand Down
2 changes: 1 addition & 1 deletion src/state/remove-draggable-from-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memoizeOne from 'memoize-one';
import memoizeOne from '../memoize-one';
import type { DraggableDimension } from '../types';

export default memoizeOne(
Expand Down
2 changes: 1 addition & 1 deletion src/view/draggable/connected-draggable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Position } from 'css-box-model';
import memoizeOne from 'memoize-one';
import type { FunctionComponent } from 'react';
import { connect } from 'react-redux';
import memoizeOne from '../../memoize-one';
import Draggable from './draggable';
import isDragging from '../../state/is-dragging';
import { origin, negate } from '../../state/position';
Expand Down
2 changes: 1 addition & 1 deletion src/view/droppable/connected-droppable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { connect } from 'react-redux';
import memoizeOne from 'memoize-one';
import { FunctionComponent } from 'react';
import memoizeOne from '../../memoize-one';
import { invariant } from '../../invariant';
import type {
State,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef } from 'react';
import type { Position } from 'css-box-model';
import rafSchedule from 'raf-schd';
import { useMemo, useCallback } from 'use-memo-one';
import memoizeOne from 'memoize-one';
import memoizeOne from '../../memoize-one';
import { invariant } from '../../invariant';
import checkForNestedScrollContainers from './check-for-nested-scroll-container';
import * as dataAttr from '../data-attributes';
Expand Down
2 changes: 1 addition & 1 deletion src/view/use-style-marshal/use-style-marshal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef, MutableRefObject } from 'react';
import memoizeOne from 'memoize-one';
import { useMemo, useCallback } from 'use-memo-one';
import memoizeOne from '../../memoize-one';
import { invariant } from '../../invariant';
import type { StyleMarshal } from './style-marshal-types';
import type { ContextId, DropReason } from '../../types';
Expand Down
2 changes: 1 addition & 1 deletion stories/src/accessible/task.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { Component, ReactElement } from 'react';
import ReactDOM from 'react-dom';
import memoizeOne from 'memoize-one';
import styled from '@emotion/styled';
import { colors } from '@atlaskit/theme';
import { Draggable } from '@hello-pangea/dnd';
import type {
DraggableProvided,
DraggableStateSnapshot,
} from '@hello-pangea/dnd';
import memoizeOne from '../../../src/memoize-one';
import { invariant } from '../../../src/invariant';
import type { Task as TaskType } from '../types';
import { grid, borderRadius } from '../constants';
Expand Down

0 comments on commit 82e1d0a

Please sign in to comment.