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

[WIP] feat: improve typings #303

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.20.4
29 changes: 29 additions & 0 deletions src/calculateDeepEqualDiffs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @typedef {import('../types').ObjectDifference} ObjectDifference
*/
import {
isArray,
isPlainObject,
Expand All @@ -20,6 +23,15 @@ const REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
const isReactElement = object => object.$$typeof === REACT_ELEMENT_TYPE;
// end

/**
*
* @param {object} a
* @param {object} b
* @param {ObjectDifference[]} diffsAccumulator
* @param {string} pathString
* @param {ObjectDifference[]} diffType
* @returns {boolean}
*/
function trackDiff(a, b, diffsAccumulator, pathString, diffType) {
diffsAccumulator.push({
diffType,
Expand All @@ -36,6 +48,15 @@ function isGetter(obj, prop) {

export const dependenciesMap = new WeakMap();

/**
*
* @param {object} a
* @param {object} b
* @param {ObjectDifference[]} diffsAccumulator
* @param {string} pathString
* @param {{ detailed?: boolean }} options
* @returns {boolean}
*/
function accumulateDeepEqualDiffs(a, b, diffsAccumulator, pathString = '', { detailed }) {
if (a === b) {
if (detailed) {
Expand Down Expand Up @@ -197,6 +218,14 @@ function accumulateDeepEqualDiffs(a, b, diffsAccumulator, pathString = '', { det
return trackDiff(a, b, diffsAccumulator, pathString, diffTypes.different);
}

/**
*
* @param {object} a
* @param {object} b
* @param {string} initialPathString
* @param {{ detailed?: boolean }} options
* @returns {ObjectDifference[]}
*/
export default function calculateDeepEqualDiffs(a, b, initialPathString, { detailed = false } = {}) {
try {
const diffs = [];
Expand Down
24 changes: 14 additions & 10 deletions src/findObjectsDifferences.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { reduce } from 'lodash';
/**
* @typedef {import('../types').ObjectDifference} ObjectDifference
*/
import calculateDeepEqualDiffs from './calculateDeepEqualDiffs';

const emptyObject = {};

/**
* @param {object} userPrevObj
* @param {object} userNextObj
* @param {{ shallow?: boolean }} options
* @returns {ObjectDifference[] | false}
*/
export default function findObjectsDifferences(userPrevObj, userNextObj, { shallow = true } = {}) {
if (userPrevObj === userNextObj) {
return false;
Expand All @@ -17,14 +25,10 @@ export default function findObjectsDifferences(userPrevObj, userNextObj, { shall

const keysOfBothObjects = Object.keys({ ...prevObj, ...nextObj });

return reduce(keysOfBothObjects, (result, key) => {
const result = [];
for (const key of keysOfBothObjects) {
const deepEqualDiffs = calculateDeepEqualDiffs(prevObj[key], nextObj[key], key);
if (deepEqualDiffs) {
result = [
...result,
...deepEqualDiffs,
];
}
return result;
}, []);
deepEqualDiffs && result.push(...deepEqualDiffs);
}
return result;
}
3 changes: 3 additions & 0 deletions src/getDisplayName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { isString } from 'lodash';

/**
* @returns {string}
*/
export default function getDisplayName(type) {
return (
type.displayName ||
Expand Down
10 changes: 10 additions & 0 deletions src/getUpdateInfo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/**
* @typedef {import('../types').ObjectDifference} ObjectDifference
* @typedef {import('../types').OwnerData} OwnerData
* @typedef {import('../types').OwnerDifferences} OwnerDifferences
*/
import findObjectsDifferences from './findObjectsDifferences';
import wdyrStore from './wdyrStore';

/**
*
* @param {{ prevOwnerData: OwnerData; nextOwnerData: OwnerData }} param
* @returns {OwnerDifferences}
*/
function getOwnerDifferences({ prevOwnerData, nextOwnerData }) {
if (!prevOwnerData || !nextOwnerData) {
return false;
Expand Down
9 changes: 8 additions & 1 deletion src/wdyrStore.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('../types').OwnerData} OwnerData
*/

const wdyrStore = {
/* The React object we patch */
React: undefined,
Expand All @@ -17,7 +21,10 @@ const wdyrStore = {
/* A weak map of all React elements to their WDYR patched react elements */
componentsMap: new WeakMap(),

/* A weak map of props to the owner element that passed them */
/**
* A weak map of props to the owner element that passed them.
* @type {WeakMap<object, OwnerData>}
*/
ownerDataMap: new WeakMap(),

/* An array of hooks tracked during one render */
Expand Down
68 changes: 61 additions & 7 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import * as React from 'react';

export type UpdateDiffType =
| 'different'
| 'deepEquals'
| 'date'
| 'regex'
| 'reactElement'
| 'function'
| 'same';

export interface ObjectDifference {
pathString: string;
diffType: UpdateDiffType;
prevValue: any;
nextValue: any;
}

export interface HookDifference {
pathString: string;
diffType: string;
diffType: UpdateDiffType;
prevValue: any;
nextValue: any;
}

export interface OwnerHookDifference {
hookName: string;
differences: ObjectDifference[] | false;
}

export interface OwnerDifferences {
propsDifferences: ObjectDifference[] | false;
stateDifferences: ObjectDifference[] | false;
hookDifferences: OwnerHookDifference;
}

export interface ReasonForUpdate {
hookDifferences: HookDifference[];
propsDifferences: boolean;
stateDifferences: boolean;
hookDifferences: ObjectDifference[] | false;
propsDifferences: ObjectDifference[] | false;
stateDifferences: ObjectDifference[] | false;
ownerDifferences: OwnerDifferences | false;
}

export interface UpdateInfo {
Expand Down Expand Up @@ -47,14 +75,40 @@ export interface WhyDidYouRenderOptions {
customName?: string;
}

export type WhyDidYouRenderComponentMember = WhyDidYouRenderOptions|boolean
export type WhyDidYouRenderComponentMember = WhyDidYouRenderOptions | boolean;

export type Notifier = (options: UpdateInfo) => void;

export type Notifier = (options: UpdateInfo) => void
export interface OwnerData {
Component: React.ComponentType;
displayName: string;
props: object;
state: object | null;
hooks: unknown[];
additionalOwnerData?: unknown;
}

export interface WdyrStore {
React: typeof import('react');
componentsMap: WeakMap<React.ComponentType, unknown>;
hooksPerRender: unknown[];
options: WhyDidYouRenderOptions;
origCloneElement: typeof React.cloneElement;
origCreateElement: typeof React.createElement;
origCreateFactory: typeof React.createFactory;
ownerDataMap: WeakMap<object, OwnerData>;
}

declare function whyDidYouRender(react: typeof React, options?: WhyDidYouRenderOptions): typeof React;
declare function whyDidYouRender(
react: typeof React,
options?: WhyDidYouRenderOptions
): typeof React;

declare namespace whyDidYouRender {
export const defaultNotifier: Notifier;
export const wdyrStore: WdyrStore;
export const storeOwnerData: unknown;
export const getWDYRType: unknown;
}

export default whyDidYouRender;
Expand Down