@fluentui/react-context-selector 9.0.0-alpha.7 → 9.0.0-nightly.f81b28ceb3.1
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/CHANGELOG.json +722 -1
- package/CHANGELOG.md +323 -2
- package/README.md +20 -0
- package/dist/react-context-selector.d.ts +9 -0
- package/lib/createContext.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/types.js.map +1 -1
- package/lib/useContextSelector.js.map +1 -1
- package/lib/useHasParentContext.d.ts +9 -0
- package/lib/useHasParentContext.js +16 -0
- package/lib/useHasParentContext.js.map +1 -0
- package/lib-commonjs/createContext.js.map +1 -1
- package/lib-commonjs/index.d.ts +1 -0
- package/lib-commonjs/index.js +3 -1
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/types.js.map +1 -1
- package/lib-commonjs/useContextSelector.js.map +1 -1
- package/lib-commonjs/useHasParentContext.d.ts +9 -0
- package/lib-commonjs/useHasParentContext.js +20 -0
- package/lib-commonjs/useHasParentContext.js.map +1 -0
- package/package.json +7 -7
- package/config/api-extractor.json +0 -3
- package/config/tests.js +0 -1
- package/etc/react-context-selector.api.md +0 -42
- package/just.config.ts +0 -3
- package/lib-amd/createContext.d.ts +0 -2
- package/lib-amd/createContext.js +0 -48
- package/lib-amd/createContext.js.map +0 -1
- package/lib-amd/index.d.ts +0 -3
- package/lib-amd/index.js +0 -9
- package/lib-amd/index.js.map +0 -1
- package/lib-amd/types.d.ts +0 -19
- package/lib-amd/types.js +0 -5
- package/lib-amd/types.js.map +0 -1
- package/lib-amd/useContextSelector.d.ts +0 -7
- package/lib-amd/useContextSelector.js +0 -71
- package/lib-amd/useContextSelector.js.map +0 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@fluentui/react-context-selector",
|
3
|
-
"version": "9.0.0-
|
3
|
+
"version": "9.0.0-nightly.f81b28ceb3.1",
|
4
4
|
"description": "React useContextSelector hook in userland",
|
5
5
|
"main": "lib-commonjs/index.js",
|
6
6
|
"module": "lib/index.js",
|
@@ -17,12 +17,12 @@
|
|
17
17
|
"code-style": "just-scripts code-style",
|
18
18
|
"just": "just-scripts",
|
19
19
|
"lint": "just-scripts lint",
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
20
|
+
"test": "jest",
|
21
|
+
"docs": "api-extractor run --config=config/api-extractor.local.json --local",
|
22
|
+
"build:local": "tsc -p . --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output dist/react-context-selector/src && yarn docs"
|
23
23
|
},
|
24
24
|
"devDependencies": {
|
25
|
-
"@fluentui/eslint-plugin": "
|
25
|
+
"@fluentui/eslint-plugin": "*",
|
26
26
|
"@fluentui/scripts": "^1.0.0",
|
27
27
|
"@types/scheduler": "^0.16.1",
|
28
28
|
"@types/react": "16.9.42",
|
@@ -34,7 +34,7 @@
|
|
34
34
|
"react-test-renderer": "^16.3.0"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
|
-
"@fluentui/react-utilities": "
|
37
|
+
"@fluentui/react-utilities": "9.0.0-nightly.f81b28ceb3.1",
|
38
38
|
"scheduler": "^0.20.1",
|
39
39
|
"tslib": "^2.1.0"
|
40
40
|
},
|
@@ -45,7 +45,7 @@
|
|
45
45
|
"react-dom": ">=16.8.0 <17.0.0"
|
46
46
|
},
|
47
47
|
"beachball": {
|
48
|
-
"tag": "
|
48
|
+
"tag": "beta",
|
49
49
|
"disallowedChangeTypes": [
|
50
50
|
"major",
|
51
51
|
"minor",
|
package/config/tests.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
/** Jest test setup file. This file is required for tests to run */
|
@@ -1,42 +0,0 @@
|
|
1
|
-
## API Report File for "@fluentui/react-context-selector"
|
2
|
-
|
3
|
-
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
4
|
-
|
5
|
-
```ts
|
6
|
-
|
7
|
-
import * as React_2 from 'react';
|
8
|
-
|
9
|
-
// @public (undocumented)
|
10
|
-
export type Context<Value> = React_2.Context<Value> & {
|
11
|
-
Provider: React_2.FC<React_2.ProviderProps<Value>>;
|
12
|
-
Consumer: never;
|
13
|
-
};
|
14
|
-
|
15
|
-
// @public (undocumented)
|
16
|
-
export type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
|
17
|
-
|
18
|
-
// @public (undocumented)
|
19
|
-
export type ContextValue<Value> = {
|
20
|
-
listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
|
21
|
-
value: React_2.MutableRefObject<Value>;
|
22
|
-
version: React_2.MutableRefObject<ContextVersion>;
|
23
|
-
};
|
24
|
-
|
25
|
-
// @public (undocumented)
|
26
|
-
export type ContextValues<Value> = ContextValue<Value> & {
|
27
|
-
listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];
|
28
|
-
};
|
29
|
-
|
30
|
-
// @public (undocumented)
|
31
|
-
export type ContextVersion = number;
|
32
|
-
|
33
|
-
// @public (undocumented)
|
34
|
-
export const createContext: <Value>(defaultValue: Value) => Context<Value>;
|
35
|
-
|
36
|
-
// @public
|
37
|
-
export const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
|
38
|
-
|
39
|
-
|
40
|
-
// (No @packageDocumentation comment for this package)
|
41
|
-
|
42
|
-
```
|
package/just.config.ts
DELETED
package/lib-amd/createContext.js
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
define(["require", "exports", "@fluentui/react-utilities", "react", "scheduler"], function (require, exports, react_utilities_1, React, scheduler_1) {
|
2
|
-
"use strict";
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
exports.createContext = void 0;
|
5
|
-
var createProvider = function (Original) {
|
6
|
-
var Provider = function (props) {
|
7
|
-
// Holds an actual "props.value"
|
8
|
-
var valueRef = React.useRef(props.value);
|
9
|
-
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
|
10
|
-
var versionRef = React.useRef(0);
|
11
|
-
// A stable object, is used to avoid context updates via mutation of its values.
|
12
|
-
var contextValue = React.useRef();
|
13
|
-
if (!contextValue.current) {
|
14
|
-
contextValue.current = {
|
15
|
-
value: valueRef,
|
16
|
-
version: versionRef,
|
17
|
-
listeners: [],
|
18
|
-
};
|
19
|
-
}
|
20
|
-
react_utilities_1.useIsomorphicLayoutEffect(function () {
|
21
|
-
valueRef.current = props.value;
|
22
|
-
versionRef.current += 1;
|
23
|
-
scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, function () {
|
24
|
-
contextValue.current.listeners.forEach(function (listener) {
|
25
|
-
listener([versionRef.current, props.value]);
|
26
|
-
});
|
27
|
-
});
|
28
|
-
}, [props.value]);
|
29
|
-
return React.createElement(Original, { value: contextValue.current }, props.children);
|
30
|
-
};
|
31
|
-
/* istanbul ignore else */
|
32
|
-
|
33
|
-
return Provider;
|
34
|
-
};
|
35
|
-
var createContext = function (defaultValue) {
|
36
|
-
var context = React.createContext({
|
37
|
-
value: { current: defaultValue },
|
38
|
-
version: { current: -1 },
|
39
|
-
listeners: [],
|
40
|
-
});
|
41
|
-
context.Provider = createProvider(context.Provider);
|
42
|
-
// We don't support Consumer API
|
43
|
-
delete context.Consumer;
|
44
|
-
return context;
|
45
|
-
};
|
46
|
-
exports.createContext = createContext;
|
47
|
-
});
|
48
|
-
//# sourceMappingURL=createContext.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"createContext.js","sourceRoot":"../src/","sources":["createContext.ts"],"names":[],"mappings":";;;;IAMA,IAAM,cAAc,GAAG,UAAQ,QAA6C;QAC1E,IAAM,QAAQ,GAAyC,UAAA,KAAK;YAC1D,gCAAgC;YAChC,IAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC3C,+GAA+G;YAC/G,IA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SAAS,EAAE,EAAE;SACd,CAAC,CAAC;QAEH,OAAO,CAAC,QAAQ,GAAG,cAAc,CAAQ,OAAO,CAAC,QAAQ,CAAC,CAAC;QAE3D,gCAAgC;QAChC,OAAS,OAAsC,CAAC,QAAQ,CAAC;QAEzD,OAAQ,OAAqC,CAAC;IAChD,CAAC,CAAC;IAbW,QAAA,aAAa,iBAaxB","sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';\n\nimport { Context, ContextValue } from './types';\n\nconst createProvider = <Value>(Original: React.Provider<ContextValue<Value>>) => {\n const Provider: React.FC<React.ProviderProps<Value>> = props => {\n // Holds an actual \"props.value\"\n const valueRef = React.useRef(props.value);\n // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.\n const versionRef = React.useRef(0);\n\n // A stable object, is used to avoid context updates via mutation of its values.\n const contextValue = React.useRef<ContextValue<Value>>();\n\n if (!contextValue.current) {\n contextValue.current = {\n value: valueRef,\n version: versionRef,\n listeners: [],\n };\n }\n\n useIsomorphicLayoutEffect(() => {\n valueRef.current = props.value;\n versionRef.current += 1;\n\n runWithPriority(NormalPriority, () => {\n (contextValue.current as ContextValue<Value>).listeners.forEach(listener => {\n listener([versionRef.current, props.value]);\n });\n });\n }, [props.value]);\n\n return React.createElement(Original, { value: contextValue.current }, props.children);\n };\n\n /* istanbul ignore else */\n if (process.env.NODE_ENV !== 'production') {\n Provider.displayName = 'ContextSelector.Provider';\n }\n\n return (Provider as unknown) as React.Provider<ContextValue<Value>>;\n};\n\nexport const createContext = <Value>(defaultValue: Value): Context<Value> => {\n const context = React.createContext<ContextValue<Value>>({\n value: { current: defaultValue },\n version: { current: -1 },\n listeners: [],\n });\n\n context.Provider = createProvider<Value>(context.Provider);\n\n // We don't support Consumer API\n delete ((context as unknown) as Context<Value>).Consumer;\n\n return (context as unknown) as Context<Value>;\n};\n"]}
|
package/lib-amd/index.d.ts
DELETED
package/lib-amd/index.js
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
define(["require", "exports", "tslib", "./createContext", "./useContextSelector", "./types"], function (require, exports, tslib_1, createContext_1, useContextSelector_1, types_1) {
|
2
|
-
"use strict";
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
exports.useContextSelector = exports.createContext = void 0;
|
5
|
-
Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return createContext_1.createContext; } });
|
6
|
-
Object.defineProperty(exports, "useContextSelector", { enumerable: true, get: function () { return useContextSelector_1.useContextSelector; } });
|
7
|
-
tslib_1.__exportStar(types_1, exports);
|
8
|
-
});
|
9
|
-
//# sourceMappingURL=index.js.map
|
package/lib-amd/index.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"../src/","sources":["index.ts"],"names":[],"mappings":";;;;IAAS,8GAAA,aAAa,OAAA;IACb,wHAAA,kBAAkB,OAAA;IAC3B,uCAAwB","sourcesContent":["export { createContext } from './createContext';\nexport { useContextSelector } from './useContextSelector';\nexport * from './types';\n"]}
|
package/lib-amd/types.d.ts
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
import * as React from 'react';
|
2
|
-
export declare type Context<Value> = React.Context<Value> & {
|
3
|
-
Provider: React.FC<React.ProviderProps<Value>>;
|
4
|
-
Consumer: never;
|
5
|
-
};
|
6
|
-
export declare type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
|
7
|
-
export declare type ContextVersion = number;
|
8
|
-
export declare type ContextValue<Value> = {
|
9
|
-
/** Holds a set of subscribers from components. */
|
10
|
-
listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
|
11
|
-
/** Holds an actual value of React's context that will be propagated down for computations. */
|
12
|
-
value: React.MutableRefObject<Value>;
|
13
|
-
/** A version field is used to sync a context value and consumers. */
|
14
|
-
version: React.MutableRefObject<ContextVersion>;
|
15
|
-
};
|
16
|
-
export declare type ContextValues<Value> = ContextValue<Value> & {
|
17
|
-
/** List of listners to publish changes */
|
18
|
-
listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];
|
19
|
-
};
|
package/lib-amd/types.js
DELETED
package/lib-amd/types.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"../src/","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["import * as React from 'react';\n\nexport type Context<Value> = React.Context<Value> & {\n Provider: React.FC<React.ProviderProps<Value>>;\n Consumer: never;\n};\n\nexport type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;\n\nexport type ContextVersion = number;\n\nexport type ContextValue<Value> = {\n /** Holds a set of subscribers from components. */\n listeners: ((payload: readonly [ContextVersion, Value]) => void)[];\n\n /** Holds an actual value of React's context that will be propagated down for computations. */\n value: React.MutableRefObject<Value>;\n\n /** A version field is used to sync a context value and consumers. */\n version: React.MutableRefObject<ContextVersion>;\n};\n\nexport type ContextValues<Value> = ContextValue<Value> & {\n /** List of listners to publish changes */\n listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];\n};\n"]}
|
@@ -1,7 +0,0 @@
|
|
1
|
-
import { Context, ContextSelector } from './types';
|
2
|
-
/**
|
3
|
-
* This hook returns context selected value by selector.
|
4
|
-
* It will only accept context created by `createContext`.
|
5
|
-
* It will trigger re-render if only the selected value is referencially changed.
|
6
|
-
*/
|
7
|
-
export declare const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
|
@@ -1,71 +0,0 @@
|
|
1
|
-
define(["require", "exports", "@fluentui/react-utilities", "react"], function (require, exports, react_utilities_1, React) {
|
2
|
-
"use strict";
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
exports.useContextSelector = void 0;
|
5
|
-
/**
|
6
|
-
* This hook returns context selected value by selector.
|
7
|
-
* It will only accept context created by `createContext`.
|
8
|
-
* It will trigger re-render if only the selected value is referencially changed.
|
9
|
-
*/
|
10
|
-
var useContextSelector = function (context, selector) {
|
11
|
-
var contextValue = React.useContext(context);
|
12
|
-
var value = contextValue.value.current, version = contextValue.version.current, listeners = contextValue.listeners;
|
13
|
-
var selected = selector(value);
|
14
|
-
var _a = React.useReducer(function (prevState, payload) {
|
15
|
-
if (!payload) {
|
16
|
-
// early bail out when is dispatched during render
|
17
|
-
return [value, selected];
|
18
|
-
}
|
19
|
-
if (payload[0] <= version) {
|
20
|
-
if (objectIs(prevState[1], selected)) {
|
21
|
-
return prevState; // bail out
|
22
|
-
}
|
23
|
-
return [value, selected];
|
24
|
-
}
|
25
|
-
try {
|
26
|
-
if (objectIs(prevState[0], payload[1])) {
|
27
|
-
return prevState; // do not update
|
28
|
-
}
|
29
|
-
var nextSelected = selector(payload[1]);
|
30
|
-
if (objectIs(prevState[1], nextSelected)) {
|
31
|
-
return prevState; // do not update
|
32
|
-
}
|
33
|
-
return [payload[1], nextSelected];
|
34
|
-
}
|
35
|
-
catch (e) {
|
36
|
-
// ignored (stale props or some other reason)
|
37
|
-
}
|
38
|
-
// explicitly spread to enforce typing
|
39
|
-
return [prevState[0], prevState[1]]; // schedule update
|
40
|
-
}, [value, selected]), state = _a[0], dispatch = _a[1];
|
41
|
-
if (!objectIs(state[1], selected)) {
|
42
|
-
// schedule re-render
|
43
|
-
// this is safe because it's self contained
|
44
|
-
dispatch(undefined);
|
45
|
-
}
|
46
|
-
react_utilities_1.useIsomorphicLayoutEffect(function () {
|
47
|
-
listeners.push(dispatch);
|
48
|
-
return function () {
|
49
|
-
var index = listeners.indexOf(dispatch);
|
50
|
-
listeners.splice(index, 1);
|
51
|
-
};
|
52
|
-
}, [listeners]);
|
53
|
-
return state[1];
|
54
|
-
};
|
55
|
-
exports.useContextSelector = useContextSelector;
|
56
|
-
/**
|
57
|
-
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
58
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
59
|
-
*/
|
60
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
61
|
-
function is(x, y) {
|
62
|
-
return ((x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
|
63
|
-
);
|
64
|
-
}
|
65
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
66
|
-
var objectIs =
|
67
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
68
|
-
// @ts-ignore fallback to native if it exists (not in IE11)
|
69
|
-
typeof Object.is === 'function' ? Object.is : is;
|
70
|
-
});
|
71
|
-
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"useContextSelector.js","sourceRoot":"../src/","sources":["useContextSelector.ts"],"names":[],"mappings":";;;;IAcA;;;;OAIG;IACI,IAAM,kBAAkB,GAAG,UAChC,OAAuB,EACvB,QAA+C;QAE/C,IAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAE,OAAmD,CAAC,CAAC;QAGjF,IAAS,KAAK,GAGrB,YAAY,cAHS,EACH,OAAO,GAEzB,YAAY,gBAFa,EAC3B,SAAS,GACP,YAAY,UADL,CACM;QACjB,IAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAE3B,IAAA,KAAoB,KAAK,CAAC,UAAU,CACxC,UACE,SAAmF,EACnF,OAEoC;YAEpC,IAAI,CAAC,OAAO,EAAE;gBACZ,kDAAkD;gBAClD,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAU,CAAC;aACnC;YAED,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE;gBACzB,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE;oBACpC,OAAO,SAAS,CAAC,CAAC,WAAW;iBAC9B;gBAED,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAU,CAAC;aACnC;YAED,IAA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SAAS,EAAE,CAAC,CAAM,EAAE,CAAM;QACxB,OAAO,CACL,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,sCAAsC;SACzG,CAAC;IACJ,CAAC;IAED,8DAA8D;IAC9D,IAAM,QAAQ;IACZ,6DAA6D;IAC7D,2DAA2D;IAC3D,OAAO,MAAM,CAAC,EAAE,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC","sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\n\nimport { Context, ContextSelector, ContextValue, ContextVersion } from './types';\n\n/**\n * Narrowing React.Reducer type to be more easily usable below.\n * No need to export this as it's for internal reducer usage.\n */\ntype ContextReducer<Value, SelectedValue> = React.Reducer<\n readonly [Value, SelectedValue],\n undefined | readonly [ContextVersion, Value]\n>;\n\n/**\n * This hook returns context selected value by selector.\n * It will only accept context created by `createContext`.\n * It will trigger re-render if only the selected value is referencially changed.\n */\nexport const useContextSelector = <Value, SelectedValue>(\n context: Context<Value>,\n selector: ContextSelector<Value, SelectedValue>,\n): SelectedValue => {\n const contextValue = React.useContext((context as unknown) as Context<ContextValue<Value>>);\n\n const {\n value: { current: value },\n version: { current: version },\n listeners,\n } = contextValue;\n const selected = selector(value);\n\n const [state, dispatch] = React.useReducer<ContextReducer<Value, SelectedValue>>(\n (\n prevState: readonly [Value /* contextValue */, SelectedValue /* selector(value) */],\n payload:\n | undefined // undefined from render below\n | readonly [ContextVersion, Value], // from provider effect\n ): readonly [Value, SelectedValue] => {\n if (!payload) {\n // early bail out when is dispatched during render\n return [value, selected] as const;\n }\n\n if (payload[0] <= version) {\n if (objectIs(prevState[1], selected)) {\n return prevState; // bail out\n }\n\n return [value, selected] as const;\n }\n\n try {\n if (objectIs(prevState[0], payload[1])) {\n return prevState; // do not update\n }\n\n const nextSelected = selector(payload[1]);\n\n if (objectIs(prevState[1], nextSelected)) {\n return prevState; // do not update\n }\n\n return [payload[1], nextSelected] as const;\n } catch (e) {\n // ignored (stale props or some other reason)\n }\n\n // explicitly spread to enforce typing\n return [prevState[0], prevState[1]] as const; // schedule update\n },\n [value, selected] as const,\n );\n\n if (!objectIs(state[1], selected)) {\n // schedule re-render\n // this is safe because it's self contained\n dispatch(undefined);\n }\n\n useIsomorphicLayoutEffect(() => {\n listeners.push(dispatch);\n\n return () => {\n const index = listeners.indexOf(dispatch);\n listeners.splice(index, 1);\n };\n }, [listeners]);\n\n return state[1] as SelectedValue;\n};\n\n/**\n * inlined Object.is polyfill to avoid requiring consumers ship their own\n * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction is(x: any, y: any) {\n return (\n (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare\n );\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst objectIs: (x: any, y: any) => boolean =\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore fallback to native if it exists (not in IE11)\n typeof Object.is === 'function' ? Object.is : is;\n"]}
|