@fluentui/react-context-selector 9.1.2 → 9.1.4
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 +45 -1
- package/CHANGELOG.md +20 -2
- package/lib/createContext.js +6 -14
- package/lib/createContext.js.map +1 -1
- package/lib/useContextSelector.js +8 -15
- package/lib/useContextSelector.js.map +1 -1
- package/lib/useHasParentContext.js +0 -3
- package/lib/useHasParentContext.js.map +1 -1
- package/lib-commonjs/createContext.js +6 -18
- package/lib-commonjs/createContext.js.map +1 -1
- package/lib-commonjs/index.js +0 -6
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/types.js.map +1 -1
- package/lib-commonjs/useContextSelector.js +8 -19
- package/lib-commonjs/useContextSelector.js.map +1 -1
- package/lib-commonjs/useHasParentContext.js +0 -6
- package/lib-commonjs/useHasParentContext.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.json
CHANGED
@@ -2,7 +2,51 @@
|
|
2
2
|
"name": "@fluentui/react-context-selector",
|
3
3
|
"entries": [
|
4
4
|
{
|
5
|
-
"date": "
|
5
|
+
"date": "Wed, 04 Jan 2023 01:36:22 GMT",
|
6
|
+
"tag": "@fluentui/react-context-selector_v9.1.4",
|
7
|
+
"version": "9.1.4",
|
8
|
+
"comments": {
|
9
|
+
"none": [
|
10
|
+
{
|
11
|
+
"author": "martinhochel@microsoft.com",
|
12
|
+
"package": "@fluentui/react-context-selector",
|
13
|
+
"commit": "4ec2b998b294d6d9c3196d3d82893bdd97d0c105",
|
14
|
+
"comment": "chore(scripts): move index.ts to to follow sub-folder domain packaging"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"author": "martinhochel@microsoft.com",
|
18
|
+
"package": "@fluentui/react-context-selector",
|
19
|
+
"commit": "194b0cf0cc27c1c1233aa945f09b3ad29778d8ca",
|
20
|
+
"comment": "chore(scripts): use for @fluentui/scripts version within all package.json"
|
21
|
+
}
|
22
|
+
],
|
23
|
+
"patch": [
|
24
|
+
{
|
25
|
+
"author": "beachball",
|
26
|
+
"package": "@fluentui/react-context-selector",
|
27
|
+
"comment": "Bump @fluentui/react-utilities to v9.3.1",
|
28
|
+
"commit": "3e322d15529451be153e97298873253e21af4082"
|
29
|
+
}
|
30
|
+
]
|
31
|
+
}
|
32
|
+
},
|
33
|
+
{
|
34
|
+
"date": "Tue, 20 Dec 2022 14:59:34 GMT",
|
35
|
+
"tag": "@fluentui/react-context-selector_v9.1.3",
|
36
|
+
"version": "9.1.3",
|
37
|
+
"comments": {
|
38
|
+
"patch": [
|
39
|
+
{
|
40
|
+
"author": "beachball",
|
41
|
+
"package": "@fluentui/react-context-selector",
|
42
|
+
"comment": "Bump @fluentui/react-utilities to v9.3.0",
|
43
|
+
"commit": "e7530bc179fd0e303448083c76a4af41a0e15322"
|
44
|
+
}
|
45
|
+
]
|
46
|
+
}
|
47
|
+
},
|
48
|
+
{
|
49
|
+
"date": "Thu, 17 Nov 2022 23:05:38 GMT",
|
6
50
|
"tag": "@fluentui/react-context-selector_v9.1.2",
|
7
51
|
"version": "9.1.2",
|
8
52
|
"comments": {
|
package/CHANGELOG.md
CHANGED
@@ -1,12 +1,30 @@
|
|
1
1
|
# Change Log - @fluentui/react-context-selector
|
2
2
|
|
3
|
-
This log was last generated on
|
3
|
+
This log was last generated on Wed, 04 Jan 2023 01:36:22 GMT and should not be manually modified.
|
4
4
|
|
5
5
|
<!-- Start content -->
|
6
6
|
|
7
|
+
## [9.1.4](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.4)
|
8
|
+
|
9
|
+
Wed, 04 Jan 2023 01:36:22 GMT
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.3..@fluentui/react-context-selector_v9.1.4)
|
11
|
+
|
12
|
+
### Patches
|
13
|
+
|
14
|
+
- Bump @fluentui/react-utilities to v9.3.1 ([PR #26114](https://github.com/microsoft/fluentui/pull/26114) by beachball)
|
15
|
+
|
16
|
+
## [9.1.3](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.3)
|
17
|
+
|
18
|
+
Tue, 20 Dec 2022 14:59:34 GMT
|
19
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.2..@fluentui/react-context-selector_v9.1.3)
|
20
|
+
|
21
|
+
### Patches
|
22
|
+
|
23
|
+
- Bump @fluentui/react-utilities to v9.3.0 ([PR #26047](https://github.com/microsoft/fluentui/pull/26047) by beachball)
|
24
|
+
|
7
25
|
## [9.1.2](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.2)
|
8
26
|
|
9
|
-
Thu, 17 Nov 2022 23:
|
27
|
+
Thu, 17 Nov 2022 23:05:38 GMT
|
10
28
|
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.1..@fluentui/react-context-selector_v9.1.2)
|
11
29
|
|
12
30
|
### Patches
|
package/lib/createContext.js
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
|
2
2
|
import * as React from 'react';
|
3
3
|
import { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';
|
4
|
-
|
5
4
|
const createProvider = Original => {
|
6
5
|
const Provider = props => {
|
7
6
|
// Holds an actual "props.value"
|
8
|
-
const valueRef = React.useRef(props.value);
|
9
|
-
|
10
|
-
const versionRef = React.useRef(0);
|
11
|
-
|
7
|
+
const valueRef = React.useRef(props.value);
|
8
|
+
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
|
9
|
+
const versionRef = React.useRef(0);
|
10
|
+
// A stable object, is used to avoid context updates via mutation of its values.
|
12
11
|
const contextValue = React.useRef();
|
13
|
-
|
14
12
|
if (!contextValue.current) {
|
15
13
|
contextValue.current = {
|
16
14
|
value: valueRef,
|
@@ -18,7 +16,6 @@ const createProvider = Original => {
|
|
18
16
|
listeners: []
|
19
17
|
};
|
20
18
|
}
|
21
|
-
|
22
19
|
useIsomorphicLayoutEffect(() => {
|
23
20
|
valueRef.current = props.value;
|
24
21
|
versionRef.current += 1;
|
@@ -33,19 +30,14 @@ const createProvider = Original => {
|
|
33
30
|
}, props.children);
|
34
31
|
};
|
35
32
|
/* istanbul ignore else */
|
36
|
-
|
37
|
-
|
38
33
|
if (process.env.NODE_ENV !== 'production') {
|
39
34
|
Provider.displayName = 'ContextSelector.Provider';
|
40
35
|
}
|
41
|
-
|
42
36
|
return Provider;
|
43
37
|
};
|
44
38
|
/**
|
45
39
|
* @internal
|
46
40
|
*/
|
47
|
-
|
48
|
-
|
49
41
|
export const createContext = defaultValue => {
|
50
42
|
// eslint-disable-next-line @fluentui/no-context-default-value
|
51
43
|
const context = /*#__PURE__*/React.createContext({
|
@@ -57,8 +49,8 @@ export const createContext = defaultValue => {
|
|
57
49
|
},
|
58
50
|
listeners: []
|
59
51
|
});
|
60
|
-
context.Provider = createProvider(context.Provider);
|
61
|
-
|
52
|
+
context.Provider = createProvider(context.Provider);
|
53
|
+
// We don't support Consumer API
|
62
54
|
delete context.Consumer;
|
63
55
|
return context;
|
64
56
|
};
|
package/lib/createContext.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":"AAAA,SAASA,yBAAyB,QAAQ,2BAA2B;AACrE,OAAO,KAAKC,KAAK,MAAM,OAAO;AAC9B,SAASC,uBAAuB,IAAIC,cAAc,EAAEC,wBAAwB,IAAIC,eAAe,QAAQ,WAAW;AAIlH,MAAMC,cAAc,GAAWC,QAA6C,IAAI;EAC9E,MAAMC,QAAQ,GAAyCC,KAAK,IAAG;IAC7D;IACA,MAAMC,QAAQ,GAAGT,KAAK,CAACU,MAAM,CAACF,KAAK,CAACG,KAAK,CAAC;IAC1C;IACA,MAAMC,UAAU,GAAGZ,KAAK,CAACU,MAAM,CAAC,CAAC,CAAC;IAElC;IACA,MAAMG,YAAY,GAAGb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QAAQ;EAExD,OAAQD,OAAqC;AAC/C,CAAC","names":["useIsomorphicLayoutEffect","React","unstable_NormalPriority","NormalPriority","unstable_runWithPriority","runWithPriority","createProvider","Original","Provider","props","valueRef","useRef","value","versionRef","contextValue","current","version","listeners","forEach","listener","createElement","children","process","env","NODE_ENV","displayName","createContext","defaultValue","context","Consumer"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/createContext.ts"],"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\n/**\n * @internal\n */\nexport const createContext = <Value>(defaultValue: Value): Context<Value> => {\n // eslint-disable-next-line @fluentui/no-context-default-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"]}
|
@@ -6,7 +6,6 @@ import * as React from 'react';
|
|
6
6
|
* It will only accept context created by `createContext`.
|
7
7
|
* It will trigger re-render if only the selected value is referencially changed.
|
8
8
|
*/
|
9
|
-
|
10
9
|
export const useContextSelector = (context, selector) => {
|
11
10
|
const contextValue = React.useContext(context);
|
12
11
|
const {
|
@@ -24,7 +23,6 @@ export const useContextSelector = (context, selector) => {
|
|
24
23
|
// early bail out when is dispatched during render
|
25
24
|
return [value, selected];
|
26
25
|
}
|
27
|
-
|
28
26
|
if (payload[0] <= version) {
|
29
27
|
if (objectIs(prevState[1], selected)) {
|
30
28
|
return prevState; // bail out
|
@@ -32,32 +30,28 @@ export const useContextSelector = (context, selector) => {
|
|
32
30
|
|
33
31
|
return [value, selected];
|
34
32
|
}
|
35
|
-
|
36
33
|
try {
|
37
34
|
if (objectIs(prevState[0], payload[1])) {
|
38
35
|
return prevState; // do not update
|
39
36
|
}
|
40
37
|
|
41
38
|
const nextSelected = selector(payload[1]);
|
42
|
-
|
43
39
|
if (objectIs(prevState[1], nextSelected)) {
|
44
40
|
return prevState; // do not update
|
45
41
|
}
|
46
42
|
|
47
43
|
return [payload[1], nextSelected];
|
48
|
-
} catch (e) {
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
} catch (e) {
|
45
|
+
// ignored (stale props or some other reason)
|
46
|
+
}
|
47
|
+
// explicitly spread to enforce typing
|
52
48
|
return [prevState[0], prevState[1]]; // schedule update
|
53
49
|
}, [value, selected]);
|
54
|
-
|
55
50
|
if (!objectIs(state[1], selected)) {
|
56
51
|
// schedule re-render
|
57
52
|
// this is safe because it's self contained
|
58
53
|
dispatch(undefined);
|
59
54
|
}
|
60
|
-
|
61
55
|
useIsomorphicLayoutEffect(() => {
|
62
56
|
listeners.push(dispatch);
|
63
57
|
return () => {
|
@@ -72,14 +66,13 @@ export const useContextSelector = (context, selector) => {
|
|
72
66
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
73
67
|
*/
|
74
68
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
75
|
-
|
76
69
|
function is(x, y) {
|
77
70
|
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
78
71
|
;
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
|
72
|
+
}
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
74
|
+
const objectIs =
|
75
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
83
76
|
// @ts-ignore fallback to native if it exists (not in IE11)
|
84
77
|
typeof Object.is === 'function' ? Object.is : is;
|
85
78
|
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":"AAAA,SAASA,yBAAyB,QAAQ,2BAA2B;AACrE,OAAO,KAAKC,KAAK,MAAM,OAAO;AAa9B;;;;;;AAMA,OAAO,MAAMC,kBAAkB,GAAG,CAChCC,OAAuB,EACvBC,QAA+C,KAC9B;EACjB,MAAMC,YAAY,GAAGJ,KAAK,CAACK,UAAU,CAAEH,OAAmD,CAAC;EAE3F,MAAM;IACJI,KAAK,EAAE;MAAEC,OAAO,EAAED;IAAK,CAAE;IACzBE,OAAO,EAAE;MAAED,OAAO,EAAEC;IAAO,CAAE;IAC7BC;EAAS,CACV,GAAGL,YAAY;EAChB,MAAMM,QAAQ,GAAGP,QAAQ,CAACG,KAAK,CAAC;EAEhC,MAAM,CAACK,KAAK,EAAEC,QAAQ,CAAC,GAAGZ,KAAK,CAACa,UAAU,CACxC,CACEC,SAAmF,EACnFC,OAEoC,KACD;IACnC,IAAI,CAACA,OAAO,EAAE;MACZ;MACA,OAAO,CAACT,KAAK,EAAEI,QAAQ,CAAU;;IAGnC,IAAIK,OAAO,CAAC,CAAC,CAAC,IAAIP,OA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AM,EAAEC,CAAM;EACxB,OACGD,CAAC,KAAKC,CAAC,KAAKD,CAAC,KAAK,CAAC,IAAI,CAAC,GAAGA,CAAC,KAAK,CAAC,GAAGC,CAAC,CAAC,IAAMD,CAAC,KAAKA,CAAC,IAAIC,CAAC,KAAKA,CAAE,CAAC;EAAA;AAEtE;AAEA;AACA,MAAMV,QAAQ;AACZ;AACA;AACA,OAAOW,MAAM,CAACH,EAAE,KAAK,UAAU,GAAGG,MAAM,CAACH,EAAE,GAAGA,EAAE","names":["useIsomorphicLayoutEffect","React","useContextSelector","context","selector","contextValue","useContext","value","current","version","listeners","selected","state","dispatch","useReducer","prevState","payload","objectIs","nextSelected","e","undefined","push","index","indexOf","splice","is","x","y","Object"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/useContextSelector.ts"],"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 * @internal\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"]}
|
@@ -7,14 +7,11 @@ import * as React from 'react';
|
|
7
7
|
* @param context - context created by react-context-selector
|
8
8
|
* @returns whether the hook is wrapped by a parent context
|
9
9
|
*/
|
10
|
-
|
11
10
|
export function useHasParentContext(context) {
|
12
11
|
const contextValue = React.useContext(context);
|
13
|
-
|
14
12
|
if (contextValue.version) {
|
15
13
|
return contextValue.version.current !== -1;
|
16
14
|
}
|
17
|
-
|
18
15
|
return false;
|
19
16
|
}
|
20
17
|
//# sourceMappingURL=useHasParentContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":"AAAA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAG9B;;;;;;;;AAQA,OAAM,SAAUC,mBAAmB,CAAQC,OAAuB;EAChE,MAAMC,YAAY,GAAGH,KAAK,CAACI,UAAU,CAAEF,OAAmD,CAAC;EAE3F,IAAIC,YAAY,CAACE,OAAO,EAAE;IACxB,OAAOF,YAAY,CAACE,OAAO,CAACC,OAAO,KAAK,CAAC,CAAC;;EAG5C,OAAO,KAAK;AACd","names":["React","useHasParentContext","context","contextValue","useContext","version","current"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/useHasParentContext.ts"],"sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\n * @internal\n * Utility hook for contexts created by react-context-selector to determine if a parent context exists\n * WARNING: This hook will not work for native React contexts\n *\n * @param context - context created by react-context-selector\n * @returns whether the hook is wrapped by a parent context\n */\nexport function useHasParentContext<Value>(context: Context<Value>) {\n const contextValue = React.useContext((context as unknown) as Context<ContextValue<Value>>);\n\n if (contextValue.version) {\n return contextValue.version.current !== -1;\n }\n\n return false;\n}\n"]}
|
@@ -4,22 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.createContext = void 0;
|
7
|
-
|
8
7
|
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
-
|
10
8
|
const React = /*#__PURE__*/require("react");
|
11
|
-
|
12
9
|
const scheduler_1 = /*#__PURE__*/require("scheduler");
|
13
|
-
|
14
10
|
const createProvider = Original => {
|
15
11
|
const Provider = props => {
|
16
12
|
// Holds an actual "props.value"
|
17
|
-
const valueRef = React.useRef(props.value);
|
18
|
-
|
19
|
-
const versionRef = React.useRef(0);
|
20
|
-
|
13
|
+
const valueRef = React.useRef(props.value);
|
14
|
+
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
|
15
|
+
const versionRef = React.useRef(0);
|
16
|
+
// A stable object, is used to avoid context updates via mutation of its values.
|
21
17
|
const contextValue = React.useRef();
|
22
|
-
|
23
18
|
if (!contextValue.current) {
|
24
19
|
contextValue.current = {
|
25
20
|
value: valueRef,
|
@@ -27,7 +22,6 @@ const createProvider = Original => {
|
|
27
22
|
listeners: []
|
28
23
|
};
|
29
24
|
}
|
30
|
-
|
31
25
|
react_utilities_1.useIsomorphicLayoutEffect(() => {
|
32
26
|
valueRef.current = props.value;
|
33
27
|
versionRef.current += 1;
|
@@ -42,19 +36,14 @@ const createProvider = Original => {
|
|
42
36
|
}, props.children);
|
43
37
|
};
|
44
38
|
/* istanbul ignore else */
|
45
|
-
|
46
|
-
|
47
39
|
if (process.env.NODE_ENV !== 'production') {
|
48
40
|
Provider.displayName = 'ContextSelector.Provider';
|
49
41
|
}
|
50
|
-
|
51
42
|
return Provider;
|
52
43
|
};
|
53
44
|
/**
|
54
45
|
* @internal
|
55
46
|
*/
|
56
|
-
|
57
|
-
|
58
47
|
const createContext = defaultValue => {
|
59
48
|
// eslint-disable-next-line @fluentui/no-context-default-value
|
60
49
|
const context = React.createContext({
|
@@ -66,11 +55,10 @@ const createContext = defaultValue => {
|
|
66
55
|
},
|
67
56
|
listeners: []
|
68
57
|
});
|
69
|
-
context.Provider = createProvider(context.Provider);
|
70
|
-
|
58
|
+
context.Provider = createProvider(context.Provider);
|
59
|
+
// We don't support Consumer API
|
71
60
|
delete context.Consumer;
|
72
61
|
return context;
|
73
62
|
};
|
74
|
-
|
75
63
|
exports.createContext = createContext;
|
76
64
|
//# sourceMappingURL=createContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":";;;;;;AAAA;AACA;AACA;AAIA,MAAMA,cAAc,GAAWC,QAA6C,IAAI;EAC9E,MAAMC,QAAQ,GAAyCC,KAAK,IAAG;IAC7D;IACA,MAAMC,QAAQ,GA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AC,CAAE;IACxBE,SAAS,EAAE;GACZ,CAAC;EAEFa,OAAO,CAACvB,QAAQ,GAAGF,cAAc,CAAQyB,OAAO,CAACvB,QAAQ,CAAC;EAE1D;EACA,OAASuB,OAAsC,CAACC,QAAQ;EAExD,OAAQD,OAAqC;AAC/C,CAAC;AAdYE,qBAAa","names":["createProvider","Original","Provider","props","valueRef","React","useRef","value","versionRef","contextValue","current","version","listeners","react_utilities_1","scheduler_1","forEach","listener","createElement","children","process","env","NODE_ENV","displayName","createContext","defaultValue","context","Consumer","exports"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/createContext.ts"],"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\n/**\n * @internal\n */\nexport const createContext = <Value>(defaultValue: Value): Context<Value> => {\n // eslint-disable-next-line @fluentui/no-context-default-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-commonjs/index.js
CHANGED
@@ -4,27 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.useHasParentContext = exports.useContextSelector = exports.createContext = void 0;
|
7
|
-
|
8
7
|
var createContext_1 = /*#__PURE__*/require("./createContext");
|
9
|
-
|
10
8
|
Object.defineProperty(exports, "createContext", {
|
11
9
|
enumerable: true,
|
12
10
|
get: function () {
|
13
11
|
return createContext_1.createContext;
|
14
12
|
}
|
15
13
|
});
|
16
|
-
|
17
14
|
var useContextSelector_1 = /*#__PURE__*/require("./useContextSelector");
|
18
|
-
|
19
15
|
Object.defineProperty(exports, "useContextSelector", {
|
20
16
|
enumerable: true,
|
21
17
|
get: function () {
|
22
18
|
return useContextSelector_1.useContextSelector;
|
23
19
|
}
|
24
20
|
});
|
25
|
-
|
26
21
|
var useHasParentContext_1 = /*#__PURE__*/require("./useHasParentContext");
|
27
|
-
|
28
22
|
Object.defineProperty(exports, "useHasParentContext", {
|
29
23
|
enumerable: true,
|
30
24
|
get: function () {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":";;;;;;AAAA;AAASA;EAAAC;EAAAC;IAAA,oCAAa;EAAA;AAAA;AACtB;AAASF;EAAAC;EAAAC;IAAA,8CAAkB;EAAA;AAAA;AAC3B;AAASF;EAAAC;EAAAC;IAAA,gDAAmB;EAAA;AAAA","names":["Object","enumerable","get"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/index.ts"],"sourcesContent":["export { createContext } from './createContext';\nexport { useContextSelector } from './useContextSelector';\nexport { useHasParentContext } from './useHasParentContext';\n// eslint-disable-next-line @fluentui/ban-context-export\nexport type { Context, ContextSelector, ContextValue, ContextValues, ContextVersion } from './types';\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":"","names":[],"sourceRoot":"../src/","sources":[],"sourcesContent":[]}
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.useContextSelector = void 0;
|
7
|
-
|
8
7
|
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
-
|
10
8
|
const React = /*#__PURE__*/require("react");
|
11
9
|
/**
|
12
10
|
* @internal
|
@@ -14,8 +12,6 @@ const React = /*#__PURE__*/require("react");
|
|
14
12
|
* It will only accept context created by `createContext`.
|
15
13
|
* It will trigger re-render if only the selected value is referencially changed.
|
16
14
|
*/
|
17
|
-
|
18
|
-
|
19
15
|
const useContextSelector = (context, selector) => {
|
20
16
|
const contextValue = React.useContext(context);
|
21
17
|
const {
|
@@ -33,7 +29,6 @@ const useContextSelector = (context, selector) => {
|
|
33
29
|
// early bail out when is dispatched during render
|
34
30
|
return [value, selected];
|
35
31
|
}
|
36
|
-
|
37
32
|
if (payload[0] <= version) {
|
38
33
|
if (objectIs(prevState[1], selected)) {
|
39
34
|
return prevState; // bail out
|
@@ -41,32 +36,28 @@ const useContextSelector = (context, selector) => {
|
|
41
36
|
|
42
37
|
return [value, selected];
|
43
38
|
}
|
44
|
-
|
45
39
|
try {
|
46
40
|
if (objectIs(prevState[0], payload[1])) {
|
47
41
|
return prevState; // do not update
|
48
42
|
}
|
49
43
|
|
50
44
|
const nextSelected = selector(payload[1]);
|
51
|
-
|
52
45
|
if (objectIs(prevState[1], nextSelected)) {
|
53
46
|
return prevState; // do not update
|
54
47
|
}
|
55
48
|
|
56
49
|
return [payload[1], nextSelected];
|
57
|
-
} catch (e) {
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
} catch (e) {
|
51
|
+
// ignored (stale props or some other reason)
|
52
|
+
}
|
53
|
+
// explicitly spread to enforce typing
|
61
54
|
return [prevState[0], prevState[1]]; // schedule update
|
62
55
|
}, [value, selected]);
|
63
|
-
|
64
56
|
if (!objectIs(state[1], selected)) {
|
65
57
|
// schedule re-render
|
66
58
|
// this is safe because it's self contained
|
67
59
|
dispatch(undefined);
|
68
60
|
}
|
69
|
-
|
70
61
|
react_utilities_1.useIsomorphicLayoutEffect(() => {
|
71
62
|
listeners.push(dispatch);
|
72
63
|
return () => {
|
@@ -76,21 +67,19 @@ const useContextSelector = (context, selector) => {
|
|
76
67
|
}, [listeners]);
|
77
68
|
return state[1];
|
78
69
|
};
|
79
|
-
|
80
70
|
exports.useContextSelector = useContextSelector;
|
81
71
|
/**
|
82
72
|
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
83
73
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
84
74
|
*/
|
85
75
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
86
|
-
|
87
76
|
function is(x, y) {
|
88
77
|
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
89
78
|
;
|
90
|
-
}
|
91
|
-
|
92
|
-
|
93
|
-
|
79
|
+
}
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
81
|
+
const objectIs =
|
82
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
94
83
|
// @ts-ignore fallback to native if it exists (not in IE11)
|
95
84
|
typeof Object.is === 'function' ? Object.is : is;
|
96
85
|
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":";;;;;;AAAA;AACA;AAaA;;;;;;AAMO,MAAMA,kBAAkB,GAAG,CAChCC,OAAuB,EACvBC,QAA+C,KAC9B;EACjB,MAAMC,YAAY,GAAGC,KAAK,CAACC,UAAU,CAAEJ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KAAK,CAAC,CAAC,CAAC,EAAED,QAAQ,CAAC,EAAE;IACjC;IACA;IACAE,QAAQ,CAACO,SAAS,CAAC;;EAGrBC,2CAAyB,CAAC,MAAK;IAC7BX,SAAS,CAACY,IAAI,CAACT,QAAQ,CAAC;IAExB,OAAO,MAAK;MACV,MAAMU,KAAK,GAAGb,SAAS,CAACc,OAAO,CAACX,QAAQ,CAAC;MACzCH,SAAS,CAACe,MAAM,CAACF,KAAK,EAAE,CAAC,CAAC;IAC5B,CAAC;EACH,CAAC,EAAE,CAACb,SAAS,CAAC,CAAC;EAEf,OAAOE,KAAK,CAAC,CAAC,CAAkB;AAClC,CAAC;AAvEYc,0BAAkB;AAyE/B;;;;AAIA;AACA,SAASC,EAAE,CAACC,CAAM,EAAEC,CAAM;EACxB,OACGD,CAAC,KAAKC,CAAC,KAAKD,CAAC,KAAK,CAAC,IAAI,CAAC,GAAGA,CAAC,KAAK,CAAC,GAAGC,CAAC,CAAC,IAAMD,CAAC,KAAKA,CAAC,IAAIC,CAAC,KAAKA,CAAE,CAAC;EAAA;AAEtE;AAEA;AACA,MAAMZ,QAAQ;AACZ;AACA;AACA,OAAOa,MAAM,CAACH,EAAE,KAAK,UAAU,GAAGG,MAAM,CAACH,EAAE,GAAGA,EAAE","names":["useContextSelector","context","selector","contextValue","React","useContext","value","current","version","listeners","selected","state","dispatch","useReducer","prevState","payload","objectIs","nextSelected","e","undefined","react_utilities_1","push","index","indexOf","splice","exports","is","x","y","Object"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/useContextSelector.ts"],"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 * @internal\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"]}
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.useHasParentContext = void 0;
|
7
|
-
|
8
7
|
const React = /*#__PURE__*/require("react");
|
9
8
|
/**
|
10
9
|
* @internal
|
@@ -14,17 +13,12 @@ const React = /*#__PURE__*/require("react");
|
|
14
13
|
* @param context - context created by react-context-selector
|
15
14
|
* @returns whether the hook is wrapped by a parent context
|
16
15
|
*/
|
17
|
-
|
18
|
-
|
19
16
|
function useHasParentContext(context) {
|
20
17
|
const contextValue = React.useContext(context);
|
21
|
-
|
22
18
|
if (contextValue.version) {
|
23
19
|
return contextValue.version.current !== -1;
|
24
20
|
}
|
25
|
-
|
26
21
|
return false;
|
27
22
|
}
|
28
|
-
|
29
23
|
exports.useHasParentContext = useHasParentContext;
|
30
24
|
//# sourceMappingURL=useHasParentContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"mappings":";;;;;;AAAA;AAGA;;;;;;;;AAQA,SAAgBA,mBAAmB,CAAQC,OAAuB;EAChE,MAAMC,YAAY,GAAGC,KAAK,CAACC,UAAU,CAAEH,OAAmD,CAAC;EAE3F,IAAIC,YAAY,CAACG,OAAO,EAAE;IACxB,OAAOH,YAAY,CAACG,OAAO,CAACC,OAAO,KAAK,CAAC,CAAC;;EAG5C,OAAO,KAAK;AACd;AARAC","names":["useHasParentContext","context","contextValue","React","useContext","version","current","exports"],"sourceRoot":"../src/","sources":["packages/react-components/react-context-selector/src/useHasParentContext.ts"],"sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\n * @internal\n * Utility hook for contexts created by react-context-selector to determine if a parent context exists\n * WARNING: This hook will not work for native React contexts\n *\n * @param context - context created by react-context-selector\n * @returns whether the hook is wrapped by a parent context\n */\nexport function useHasParentContext<Value>(context: Context<Value>) {\n const contextValue = React.useContext((context as unknown) as Context<ContextValue<Value>>);\n\n if (contextValue.version) {\n return contextValue.version.current !== -1;\n }\n\n return false;\n}\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@fluentui/react-context-selector",
|
3
|
-
"version": "9.1.
|
3
|
+
"version": "9.1.4",
|
4
4
|
"description": "React useContextSelector hook in userland",
|
5
5
|
"main": "lib-commonjs/index.js",
|
6
6
|
"module": "lib/index.js",
|
@@ -23,10 +23,10 @@
|
|
23
23
|
},
|
24
24
|
"devDependencies": {
|
25
25
|
"@fluentui/eslint-plugin": "*",
|
26
|
-
"@fluentui/scripts": "
|
26
|
+
"@fluentui/scripts": "*"
|
27
27
|
},
|
28
28
|
"dependencies": {
|
29
|
-
"@fluentui/react-utilities": "^9.
|
29
|
+
"@fluentui/react-utilities": "^9.3.1",
|
30
30
|
"tslib": "^2.1.0"
|
31
31
|
},
|
32
32
|
"peerDependencies": {
|