@contember/react-utils 1.2.0-alpha.1 → 1.2.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/debug-context/Context.js +6 -2
- package/dist/development/debug-context/Context.js.map +1 -1
- package/dist/development/hooks/useArrayMapMemo.js +2 -0
- package/dist/development/hooks/useArrayMapMemo.js.map +1 -1
- package/dist/development/hooks/useConstantLengthInvariant.js +9 -0
- package/dist/development/hooks/useConstantLengthInvariant.js.map +1 -1
- package/dist/development/hooks/useConstantValueInvariant.js +9 -0
- package/dist/development/hooks/useConstantValueInvariant.js.map +1 -1
- package/dist/development/hooks/useExpectSameValueReference.js +6 -0
- package/dist/development/hooks/useExpectSameValueReference.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
2
2
|
import { assert, isNonEmptyString } from "@contember/utilities";
|
|
3
3
|
import { createContext, useContext, useMemo, useRef, memo } from "react";
|
|
4
4
|
import { noopScopedConsole, isNoopScopedConsole } from "./Constants.js";
|
|
@@ -56,7 +56,11 @@ const useScopedConsoleRef = (prefix) => {
|
|
|
56
56
|
const DebugChildren = memo(({ active = true, children, id }) => {
|
|
57
57
|
const parentScopedConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext));
|
|
58
58
|
const console2 = active && id ? createPrefixedConsole(id, parentScopedConsole) : noopScopedConsole;
|
|
59
|
-
return /* @__PURE__ */
|
|
59
|
+
return /* @__PURE__ */ jsxDEV(ScopedConsoleContext.Provider, { value: active ? console2 : noopScopedConsole, children }, void 0, false, {
|
|
60
|
+
fileName: "/__w/interface/interface/packages/react-utils/src/debug-context/Context.tsx",
|
|
61
|
+
lineNumber: 77,
|
|
62
|
+
columnNumber: 3
|
|
63
|
+
}, globalThis);
|
|
60
64
|
});
|
|
61
65
|
DebugChildren.displayName = "DebugChildren";
|
|
62
66
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Context.js","sources":["../../../src/debug-context/Context.tsx"],"sourcesContent":["import { assert, isNonEmptyString } from '@contember/utilities'\nimport { ReactNode, createContext, memo, useContext, useMemo, useRef } from 'react'\nimport { isNoopScopedConsole, noopScopedConsole } from './Constants'\nimport { ScopedConsoleContextType } from './Types'\n\nfunction createPrefixedConsole(message: string, prefixedConsole?: ScopedConsoleContextType): ScopedConsoleContextType {\n\treturn {\n\t\terror: function error(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).error(message, ...parameters)\n\t\t},\n\t\terrored: function errored<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).error(message, value)\n\t\t\treturn value\n\t\t},\n\t\tlog: function log(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).log(message, ...parameters)\n\t\t},\n\t\tlogged: function logged<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).log(message, value)\n\t\t\treturn value\n\t\t},\n\t\ttrace: function trace(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).trace(message, ...parameters)\n\t\t},\n\t\ttraced: function traced<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).trace(message, value)\n\t\t\treturn value\n\t\t},\n\t\twarn: function warn(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).warn(message, ...parameters)\n\t\t},\n\t\twarned: function warned<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).warn(message, value)\n\t\t\treturn value\n\t\t},\n\t}\n}\n\nexport const ScopedConsoleContext = createContext<ScopedConsoleContextType>(null!)\nScopedConsoleContext.displayName = 'ScopedConsoleContext'\n\nfunction parentScopedConsoleOrNone(parentConsole: ScopedConsoleContextType): ScopedConsoleContextType | undefined {\n\treturn isNoopScopedConsole(parentConsole) ? undefined : parentConsole\n}\n\nexport const useScopedConsoleRef = (prefix: string) => {\n\tassert('prefix is non-empty string', prefix, isNonEmptyString)\n\n\tconst parentConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst scopedConsole = useMemo(() => {\n\t\tif (parentConsole) {\n\t\t\treturn createPrefixedConsole(prefix, parentConsole)\n\t\t} else {\n\t\t\treturn noopScopedConsole\n\t\t}\n\t}, [parentConsole, prefix])\n\n\tconst ref = useRef(scopedConsole)\n\tref.current = scopedConsole\n\n\treturn ref\n}\n\nexport type DebugChildrenProps =\n\t| { active: true; children: ReactNode; id: string; }\n\t| { active?: false; children: ReactNode; id?: string; }\n\nexport const DebugChildren = memo<DebugChildrenProps>(({ active = true, children, id }) => {\n\tconst parentScopedConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst console = active && id\n\t\t? createPrefixedConsole(id, parentScopedConsole)\n\t\t: noopScopedConsole\n\n\treturn (\n\t\t<ScopedConsoleContext.Provider value={active ? console : noopScopedConsole}>\n\t\t\t{children}\n\t\t</ScopedConsoleContext.Provider>\n\t)\n})\nDebugChildren.displayName = 'DebugChildren'\n"],"names":["message","console"],"mappings":";;;;AAKA,SAAS,sBAAsB,SAAiB,iBAAsE;AAC9G,SAAA;AAAA,IACN,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,SAAS,SAAS,QAAWA,UAAiB,OAAa;AAC1D,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,KAAK,SAAS,OAAO,YAAmB;AACvC,OAAC,mBAAmB,SAAS,IAAI,SAAS,GAAG,UAAU;AAAA,IACxD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,IAAIA,UAAS,KAAK;AACxC,aAAA;AAAA,IACR;AAAA,IACA,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,MAAM,SAAS,QAAQ,YAAmB;AACzC,OAAC,mBAAmB,SAAS,KAAK,SAAS,GAAG,UAAU;AAAA,IACzD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,KAAKA,UAAS,KAAK;AACzC,aAAA;AAAA,IACR;AAAA,EAAA;AAEF;AAEa,MAAA,uBAAuB,cAAwC,IAAK;AACjF,qBAAqB,cAAc;AAEnC,SAAS,0BAA0B,eAA+E;AAC1G,SAAA,oBAAoB,aAAa,IAAI,SAAY;AACzD;AAEa,MAAA,sBAAsB,CAAC,WAAmB;AAC/C,SAAA,8BAA8B,QAAQ,gBAAgB;AAE7D,QAAM,gBAAgB,0BAA0B,WAAW,oBAAoB,CAAC;AAE1E,QAAA,gBAAgB,QAAQ,MAAM;AACnC,QAAI,eAAe;AACX,aAAA,sBAAsB,QAAQ,aAAa;AAAA,IAAA,OAC5C;AACC,aAAA;AAAA,IACR;AAAA,EAAA,GACE,CAAC,eAAe,MAAM,CAAC;AAEpB,QAAA,MAAM,OAAO,aAAa;AAChC,MAAI,UAAU;AAEP,SAAA;AACR;AAMa,MAAA,gBAAgB,KAAyB,CAAC,EAAE,SAAS,MAAM,UAAU,SAAS;AAC1F,QAAM,sBAAsB,0BAA0B,WAAW,oBAAoB,CAAC;AAEtF,QAAMC,WAAU,UAAU,KACvB,sBAAsB,IAAI,mBAAmB,IAC7C;AAGF,SAAA,
|
|
1
|
+
{"version":3,"file":"Context.js","sources":["../../../src/debug-context/Context.tsx"],"sourcesContent":["import { assert, isNonEmptyString } from '@contember/utilities'\nimport { ReactNode, createContext, memo, useContext, useMemo, useRef } from 'react'\nimport { isNoopScopedConsole, noopScopedConsole } from './Constants'\nimport { ScopedConsoleContextType } from './Types'\n\nfunction createPrefixedConsole(message: string, prefixedConsole?: ScopedConsoleContextType): ScopedConsoleContextType {\n\treturn {\n\t\terror: function error(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).error(message, ...parameters)\n\t\t},\n\t\terrored: function errored<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).error(message, value)\n\t\t\treturn value\n\t\t},\n\t\tlog: function log(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).log(message, ...parameters)\n\t\t},\n\t\tlogged: function logged<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).log(message, value)\n\t\t\treturn value\n\t\t},\n\t\ttrace: function trace(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).trace(message, ...parameters)\n\t\t},\n\t\ttraced: function traced<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).trace(message, value)\n\t\t\treturn value\n\t\t},\n\t\twarn: function warn(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).warn(message, ...parameters)\n\t\t},\n\t\twarned: function warned<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).warn(message, value)\n\t\t\treturn value\n\t\t},\n\t}\n}\n\nexport const ScopedConsoleContext = createContext<ScopedConsoleContextType>(null!)\nScopedConsoleContext.displayName = 'ScopedConsoleContext'\n\nfunction parentScopedConsoleOrNone(parentConsole: ScopedConsoleContextType): ScopedConsoleContextType | undefined {\n\treturn isNoopScopedConsole(parentConsole) ? undefined : parentConsole\n}\n\nexport const useScopedConsoleRef = (prefix: string) => {\n\tassert('prefix is non-empty string', prefix, isNonEmptyString)\n\n\tconst parentConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst scopedConsole = useMemo(() => {\n\t\tif (parentConsole) {\n\t\t\treturn createPrefixedConsole(prefix, parentConsole)\n\t\t} else {\n\t\t\treturn noopScopedConsole\n\t\t}\n\t}, [parentConsole, prefix])\n\n\tconst ref = useRef(scopedConsole)\n\tref.current = scopedConsole\n\n\treturn ref\n}\n\nexport type DebugChildrenProps =\n\t| { active: true; children: ReactNode; id: string; }\n\t| { active?: false; children: ReactNode; id?: string; }\n\nexport const DebugChildren = memo<DebugChildrenProps>(({ active = true, children, id }) => {\n\tconst parentScopedConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst console = active && id\n\t\t? createPrefixedConsole(id, parentScopedConsole)\n\t\t: noopScopedConsole\n\n\treturn (\n\t\t<ScopedConsoleContext.Provider value={active ? console : noopScopedConsole}>\n\t\t\t{children}\n\t\t</ScopedConsoleContext.Provider>\n\t)\n})\nDebugChildren.displayName = 'DebugChildren'\n"],"names":["message","console","this"],"mappings":";;;;AAKA,SAAS,sBAAsB,SAAiB,iBAAsE;AAC9G,SAAA;AAAA,IACN,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,SAAS,SAAS,QAAWA,UAAiB,OAAa;AAC1D,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,KAAK,SAAS,OAAO,YAAmB;AACvC,OAAC,mBAAmB,SAAS,IAAI,SAAS,GAAG,UAAU;AAAA,IACxD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,IAAIA,UAAS,KAAK;AACxC,aAAA;AAAA,IACR;AAAA,IACA,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,MAAM,SAAS,QAAQ,YAAmB;AACzC,OAAC,mBAAmB,SAAS,KAAK,SAAS,GAAG,UAAU;AAAA,IACzD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,KAAKA,UAAS,KAAK;AACzC,aAAA;AAAA,IACR;AAAA,EAAA;AAEF;AAEa,MAAA,uBAAuB,cAAwC,IAAK;AACjF,qBAAqB,cAAc;AAEnC,SAAS,0BAA0B,eAA+E;AAC1G,SAAA,oBAAoB,aAAa,IAAI,SAAY;AACzD;AAEa,MAAA,sBAAsB,CAAC,WAAmB;AAC/C,SAAA,8BAA8B,QAAQ,gBAAgB;AAE7D,QAAM,gBAAgB,0BAA0B,WAAW,oBAAoB,CAAC;AAE1E,QAAA,gBAAgB,QAAQ,MAAM;AACnC,QAAI,eAAe;AACX,aAAA,sBAAsB,QAAQ,aAAa;AAAA,IAAA,OAC5C;AACC,aAAA;AAAA,IACR;AAAA,EAAA,GACE,CAAC,eAAe,MAAM,CAAC;AAEpB,QAAA,MAAM,OAAO,aAAa;AAChC,MAAI,UAAU;AAEP,SAAA;AACR;AAMa,MAAA,gBAAgB,KAAyB,CAAC,EAAE,SAAS,MAAM,UAAU,SAAS;AAC1F,QAAM,sBAAsB,0BAA0B,WAAW,oBAAoB,CAAC;AAEtF,QAAMC,WAAU,UAAU,KACvB,sBAAsB,IAAI,mBAAmB,IAC7C;AAGF,SAAA,uBAAC,qBAAqB,UAArB,EAA8B,OAAO,SAASA,WAAU,mBACvD,SADF,GAAA,QAAA,OAAA;AAAA,IAAA,UAAA;AAAA,IAAA,YAAA;AAAA,IAAA,cAAA;AAAA,EAEA,GAAAC,UAAA;AAEF,CAAC;AACD,cAAc,cAAc;"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
+
import { useConstantLengthInvariant } from "./useConstantLengthInvariant.js";
|
|
2
3
|
const useArrayMapMemo = (items, map) => {
|
|
4
|
+
useConstantLengthInvariant(items);
|
|
3
5
|
const mapped = useMemo(() => items.map(map), [map, ...items]);
|
|
4
6
|
return useMemo(() => mapped, mapped);
|
|
5
7
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useArrayMapMemo.js","sources":["../../../src/hooks/useArrayMapMemo.ts"],"sourcesContent":["import { useMemo } from 'react'\nimport { useConstantLengthInvariant } from './useConstantLengthInvariant'\n\n/**\n * ⚠️ ONLY USE THIS IF YOU *REALLY* KNOW WHAT YOU'RE DOING! ⚠️\n * ⚠ HERE BE DRAGONS THAT KNOW WHERE YOU LIVE! ⚠\n *\n * This is a bit of React naughtiness that allows us to memoize outputs of Array.prototype.map() based on the contents\n * of the original array, and not its referential equality. That way, if we create a new array with identical items,\n * we get an array that is referentially equal to the previous one.\n */\nexport const useArrayMapMemo = <Item, OutputItem>(\n\titems: Item[],\n\tmap: (value: Item, index: number, array: Item[]) => OutputItem,\n): OutputItem[] => {\n\tuseConstantLengthInvariant(items)\n\n\t// This is tricky. We don't want to exclude the map function from the dependency list because if it changes but the\n\t// items array remains the same, we still want to re-compute the new array. However, if that yields the same array,\n\t// in the sense that the items are identical, albeit computed via a different map function, we still want to\n\t// preserve the referential equality of the original. Hence the second memo.\n\n\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\tconst mapped = useMemo(() => items.map(map), [map, ...items])\n\n\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\treturn useMemo(() => mapped, mapped) // This is deliberately not `[mapped]`\n}\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useArrayMapMemo.js","sources":["../../../src/hooks/useArrayMapMemo.ts"],"sourcesContent":["import { useMemo } from 'react'\nimport { useConstantLengthInvariant } from './useConstantLengthInvariant'\n\n/**\n * ⚠️ ONLY USE THIS IF YOU *REALLY* KNOW WHAT YOU'RE DOING! ⚠️\n * ⚠ HERE BE DRAGONS THAT KNOW WHERE YOU LIVE! ⚠\n *\n * This is a bit of React naughtiness that allows us to memoize outputs of Array.prototype.map() based on the contents\n * of the original array, and not its referential equality. That way, if we create a new array with identical items,\n * we get an array that is referentially equal to the previous one.\n */\nexport const useArrayMapMemo = <Item, OutputItem>(\n\titems: Item[],\n\tmap: (value: Item, index: number, array: Item[]) => OutputItem,\n): OutputItem[] => {\n\tuseConstantLengthInvariant(items)\n\n\t// This is tricky. We don't want to exclude the map function from the dependency list because if it changes but the\n\t// items array remains the same, we still want to re-compute the new array. However, if that yields the same array,\n\t// in the sense that the items are identical, albeit computed via a different map function, we still want to\n\t// preserve the referential equality of the original. Hence the second memo.\n\n\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\tconst mapped = useMemo(() => items.map(map), [map, ...items])\n\n\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\treturn useMemo(() => mapped, mapped) // This is deliberately not `[mapped]`\n}\n"],"names":[],"mappings":";;AAWa,MAAA,kBAAkB,CAC9B,OACA,QACkB;AAClB,6BAA2B,KAAK;AAQ1B,QAAA,SAAS,QAAQ,MAAM,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC;AAGrD,SAAA,QAAQ,MAAM,QAAQ,MAAM;AACpC;"}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
import { usePreviousValue } from "./usePreviousValue.js";
|
|
1
2
|
const useConstantLengthInvariant = (items, message) => {
|
|
3
|
+
{
|
|
4
|
+
const previous = usePreviousValue(items);
|
|
5
|
+
if (previous.length !== items.length) {
|
|
6
|
+
throw new Error(
|
|
7
|
+
`Invariant violation: ${message || "Changing the length of an array whose length must remain constant between renders."}`
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
2
11
|
};
|
|
3
12
|
export {
|
|
4
13
|
useConstantLengthInvariant
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useConstantLengthInvariant.js","sources":["../../../src/hooks/useConstantLengthInvariant.ts"],"sourcesContent":["import { usePreviousValue } from './usePreviousValue'\n\n// This entire hook should get optimized away for prod\nexport const useConstantLengthInvariant = <Item>(items: Item[], message?: string) => {\n\tif (import.meta.env.DEV) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst previous = usePreviousValue(items)\n\t\tif (previous.length !== items.length) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invariant violation: ${message || 'Changing the length of an array whose length must remain constant between renders.'\n\t\t\t\t}`,\n\t\t\t)\n\t\t}\n\t}\n}\n"],"names":[],"mappings":"AAGa,MAAA,6BAA6B,CAAO,OAAe,YAAqB;
|
|
1
|
+
{"version":3,"file":"useConstantLengthInvariant.js","sources":["../../../src/hooks/useConstantLengthInvariant.ts"],"sourcesContent":["import { usePreviousValue } from './usePreviousValue'\n\n// This entire hook should get optimized away for prod\nexport const useConstantLengthInvariant = <Item>(items: Item[], message?: string) => {\n\tif (import.meta.env.DEV) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst previous = usePreviousValue(items)\n\t\tif (previous.length !== items.length) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invariant violation: ${message || 'Changing the length of an array whose length must remain constant between renders.'\n\t\t\t\t}`,\n\t\t\t)\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";AAGa,MAAA,6BAA6B,CAAO,OAAe,YAAqB;AAC3D;AAElB,UAAA,WAAW,iBAAiB,KAAK;AACnC,QAAA,SAAS,WAAW,MAAM,QAAQ;AACrC,YAAM,IAAI;AAAA,QACT,wBAAwB,WAAW;AAAA,MAAA;AAAA,IAGrC;AAAA,EACD;AACD;"}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
import { usePreviousValue } from "./usePreviousValue.js";
|
|
1
2
|
const useConstantValueInvariant = (value, message) => {
|
|
3
|
+
{
|
|
4
|
+
const previous = usePreviousValue(value);
|
|
5
|
+
if (previous !== value) {
|
|
6
|
+
throw new Error(
|
|
7
|
+
`Invariant violation: ${message || "Changing a value which must remain constant between renders."}`
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
2
11
|
};
|
|
3
12
|
export {
|
|
4
13
|
useConstantValueInvariant
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useConstantValueInvariant.js","sources":["../../../src/hooks/useConstantValueInvariant.ts"],"sourcesContent":["import { usePreviousValue } from './usePreviousValue'\n\n// This entire hook should get optimized away for prod\nexport const useConstantValueInvariant = <Value>(value: Value, message?: string) => {\n\tif (import.meta.env.DEV) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst previous = usePreviousValue(value)\n\t\tif (previous !== value) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invariant violation: ${message || 'Changing a value which must remain constant between renders.'}`,\n\t\t\t)\n\t\t}\n\t}\n}\n"],"names":[],"mappings":"AAGa,MAAA,4BAA4B,CAAQ,OAAc,YAAqB;
|
|
1
|
+
{"version":3,"file":"useConstantValueInvariant.js","sources":["../../../src/hooks/useConstantValueInvariant.ts"],"sourcesContent":["import { usePreviousValue } from './usePreviousValue'\n\n// This entire hook should get optimized away for prod\nexport const useConstantValueInvariant = <Value>(value: Value, message?: string) => {\n\tif (import.meta.env.DEV) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst previous = usePreviousValue(value)\n\t\tif (previous !== value) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invariant violation: ${message || 'Changing a value which must remain constant between renders.'}`,\n\t\t\t)\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";AAGa,MAAA,4BAA4B,CAAQ,OAAc,YAAqB;AAC1D;AAElB,UAAA,WAAW,iBAAiB,KAAK;AACvC,QAAI,aAAa,OAAO;AACvB,YAAM,IAAI;AAAA,QACT,wBAAwB,WAAW;AAAA,MAAA;AAAA,IAErC;AAAA,EACD;AACD;"}
|
|
@@ -3,6 +3,12 @@ import { useRef } from "react";
|
|
|
3
3
|
function useExpectSameValueReference(next, shouldThrow = false) {
|
|
4
4
|
const previous = useRef(next);
|
|
5
5
|
if (next !== previous.current && typeof next === "object" && typeof previous.current === "object" && deepEqual(next, previous.current)) {
|
|
6
|
+
{
|
|
7
|
+
console.error("Previous and next next do not match in reference although match in value", {
|
|
8
|
+
previous: previous.current,
|
|
9
|
+
next
|
|
10
|
+
});
|
|
11
|
+
}
|
|
6
12
|
if (shouldThrow) {
|
|
7
13
|
throw new Error("Expecting same reference for same values");
|
|
8
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useExpectSameValueReference.js","sources":["../../../src/hooks/useExpectSameValueReference.ts"],"sourcesContent":["import deepEqual from 'deep-equal'\nimport { useRef } from 'react'\n\n// TODO: Unify error behavior with useConstantValueInvariant() and maybe merge into one hook.\nexport function useExpectSameValueReference<T>(next: T, shouldThrow: boolean = false) {\n\tconst previous = useRef(next)\n\n\tif (next !== previous.current\n\t\t&& typeof next === 'object'\n\t\t&& typeof previous.current === 'object'\n\t\t&& deepEqual(next, previous.current)\n\t) {\n\t\tif (import.meta.env.DEV) {\n\t\t\tconsole.error('Previous and next next do not match in reference although match in value', {\n\t\t\t\tprevious: previous.current,\n\t\t\t\tnext,\n\t\t\t})\n\t\t}\n\n\t\tif (shouldThrow) {\n\t\t\tthrow new Error('Expecting same reference for same values')\n\t\t}\n\t}\n\n\tprevious.current = next\n}\n"],"names":[],"mappings":";;AAIgB,SAAA,4BAA+B,MAAS,cAAuB,OAAO;AAC/E,QAAA,WAAW,OAAO,IAAI;AAE5B,MAAI,SAAS,SAAS,WAClB,OAAO,SAAS,YAChB,OAAO,SAAS,YAAY,YAC5B,UAAU,MAAM,SAAS,OAAO,GAClC;
|
|
1
|
+
{"version":3,"file":"useExpectSameValueReference.js","sources":["../../../src/hooks/useExpectSameValueReference.ts"],"sourcesContent":["import deepEqual from 'deep-equal'\nimport { useRef } from 'react'\n\n// TODO: Unify error behavior with useConstantValueInvariant() and maybe merge into one hook.\nexport function useExpectSameValueReference<T>(next: T, shouldThrow: boolean = false) {\n\tconst previous = useRef(next)\n\n\tif (next !== previous.current\n\t\t&& typeof next === 'object'\n\t\t&& typeof previous.current === 'object'\n\t\t&& deepEqual(next, previous.current)\n\t) {\n\t\tif (import.meta.env.DEV) {\n\t\t\tconsole.error('Previous and next next do not match in reference although match in value', {\n\t\t\t\tprevious: previous.current,\n\t\t\t\tnext,\n\t\t\t})\n\t\t}\n\n\t\tif (shouldThrow) {\n\t\t\tthrow new Error('Expecting same reference for same values')\n\t\t}\n\t}\n\n\tprevious.current = next\n}\n"],"names":[],"mappings":";;AAIgB,SAAA,4BAA+B,MAAS,cAAuB,OAAO;AAC/E,QAAA,WAAW,OAAO,IAAI;AAE5B,MAAI,SAAS,SAAS,WAClB,OAAO,SAAS,YAChB,OAAO,SAAS,YAAY,YAC5B,UAAU,MAAM,SAAS,OAAO,GAClC;AACwB;AACxB,cAAQ,MAAM,4EAA4E;AAAA,QACzF,UAAU,SAAS;AAAA,QACnB;AAAA,MAAA,CACA;AAAA,IACF;AAEA,QAAI,aAAa;AACV,YAAA,IAAI,MAAM,0CAA0C;AAAA,IAC3D;AAAA,EACD;AAEA,WAAS,UAAU;AACpB;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/react-utils",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "1.2.0-alpha.
|
|
4
|
+
"version": "1.2.0-alpha.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/production/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"typings": "./dist/types/index.d.ts",
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "yarn build:js:dev && yarn build:js:prod",
|
|
24
|
-
"build:js:dev": "vite build --mode development",
|
|
24
|
+
"build:js:dev": "NODE_ENV=development vite build --mode development",
|
|
25
25
|
"build:js:prod": "vite build --mode production",
|
|
26
26
|
"ae:build": "api-extractor run --local",
|
|
27
27
|
"ae:test": "api-extractor run"
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"react": "^17 || ^18"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@contember/utilities": "1.2.0-alpha.
|
|
33
|
+
"@contember/utilities": "1.2.0-alpha.3",
|
|
34
34
|
"deep-equal": "^2.2.0"
|
|
35
35
|
},
|
|
36
36
|
"stableVersion": "0.0.0"
|