diff --git a/dist/effect.d.ts b/dist/effect.d.ts new file mode 100644 index 0000000..3dbaedf --- /dev/null +++ b/dist/effect.d.ts @@ -0,0 +1,3 @@ +import * as Eq from 'fp-ts/Eq'; +import { EffectCallback, useEffect } from 'react'; +export declare const useStableEffect: (callback: EffectCallback, dependencies: A, eq: Eq.Eq) => ReturnType; diff --git a/dist/effect.js b/dist/effect.js new file mode 100644 index 0000000..47f3f36 --- /dev/null +++ b/dist/effect.js @@ -0,0 +1,18 @@ +import { useEffect, useRef } from 'react'; +// Use effect prior art comes from https://github.com/kentcdodds/use-deep-compare-effect/blob/master/src/index.ts +var useEqMemoize = function (value, eq) { + var ref = useRef(); + var signalRef = useRef(0); + if (ref.current == null) { + ref.current = value; + } + if (!eq.equals(value, ref.current)) { + ref.current = value; + signalRef.current += 1; + } + return [signalRef.current]; +}; +export var useStableEffect = function (callback, dependencies, eq) { + return useEffect(callback, useEqMemoize(dependencies, eq)); +}; +//# sourceMappingURL=effect.js.map \ No newline at end of file diff --git a/dist/effect.js.map b/dist/effect.js.map new file mode 100644 index 0000000..495d64a --- /dev/null +++ b/dist/effect.js.map @@ -0,0 +1 @@ +{"version":3,"file":"effect.js","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1D,iHAAiH;AACjH,IAAM,YAAY,GAAG,UAAmC,KAAQ,EAAE,EAAY;IAC5E,IAAM,GAAG,GAAG,MAAM,EAAK,CAAC;IACxB,IAAM,SAAS,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAEpC,IAAI,GAAG,CAAC,OAAO,IAAI,IAAI,EAAE;QACvB,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC;KACrB;IAED,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,EAAE;QAClC,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC;QACpB,SAAS,CAAC,OAAO,IAAI,CAAC,CAAC;KACxB;IAED,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAC7B,CAAC,CAAC;AAEF,MAAM,CAAC,IAAM,eAAe,GAAG,UAC7B,QAAwB,EACxB,YAAe,EACf,EAAY;IAEZ,OAAO,SAAS,CAAC,QAAQ,EAAE,YAAY,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC;AAC7D,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..67468f6 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,2 @@ +export { useStable, useStableE, useStableO } from './state'; +export { useStableEffect } from './effect'; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..ecab7bb --- /dev/null +++ b/dist/index.js @@ -0,0 +1,3 @@ +export { useStable, useStableE, useStableO } from './state'; +export { useStableEffect } from './effect'; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..3e6e67d --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC"} \ No newline at end of file diff --git a/dist/state.d.ts b/dist/state.d.ts new file mode 100644 index 0000000..733f1d4 --- /dev/null +++ b/dist/state.d.ts @@ -0,0 +1,7 @@ +import * as E from 'fp-ts/Either'; +import * as Eq from 'fp-ts/Eq'; +import * as O from 'fp-ts/Option'; +import { Dispatch } from 'react'; +export declare const useStable: (initState: A, eq: Eq.Eq) => [A, Dispatch]; +export declare const useStableO: (initState: O.Option) => [O.Option, Dispatch>]; +export declare const useStableE: (initState: E.Either) => [E.Either, Dispatch>]; diff --git a/dist/state.js b/dist/state.js new file mode 100644 index 0000000..f1d4cad --- /dev/null +++ b/dist/state.js @@ -0,0 +1,14 @@ +import * as E from 'fp-ts/Either'; +import * as Eq from 'fp-ts/Eq'; +import * as O from 'fp-ts/Option'; +import { useReducer } from 'react'; +export var useStable = function (initState, eq) { + return useReducer(function (s1, s2) { return eq.equals(s1, s2) ? s1 : s2; }, initState); +}; +export var useStableO = function (initState) { + return useStable(initState, O.getEq(Eq.eqStrict)); +}; +export var useStableE = function (initState) { + return useStable(initState, E.getEq(Eq.eqStrict, Eq.eqStrict)); +}; +//# sourceMappingURL=state.js.map \ No newline at end of file diff --git a/dist/state.js.map b/dist/state.js.map new file mode 100644 index 0000000..749f42b --- /dev/null +++ b/dist/state.js.map @@ -0,0 +1 @@ +{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAY,UAAU,EAAE,MAAM,OAAO,CAAC;AAE7C,MAAM,CAAC,IAAM,SAAS,GAAG,UAAI,SAAY,EAAE,EAAY;IACrD,OAAA,UAAU,CACR,UAAC,EAAK,EAAE,EAAK,IAAK,OAAA,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAA3B,CAA2B,EAC7C,SAAS,CACV;AAHD,CAGC,CAAC;AAEJ,MAAM,CAAC,IAAM,UAAU,GAAG,UAAI,SAAsB;IAClD,OAAA,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AAA1C,CAA0C,CAAC;AAE7C,MAAM,CAAC,IAAM,UAAU,GAAG,UAAO,SAAyB;IACxD,OAAA,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC;AAAvD,CAAuD,CAAC"} \ No newline at end of file