@fluentui/react-context-selector 9.1.20 → 9.1.22
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 +46 -1
- package/CHANGELOG.md +20 -2
- package/lib/createContext.js +51 -49
- package/lib/createContext.js.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/types.js +0 -1
- package/lib/types.js.map +1 -1
- package/lib/useContextSelector.js +62 -59
- package/lib/useContextSelector.js.map +1 -1
- package/lib/useHasParentContext.js +6 -8
- package/lib/useHasParentContext.js.map +1 -1
- package/lib-commonjs/createContext.js +1 -3
- package/lib-commonjs/createContext.js.map +1 -1
- package/lib-commonjs/index.js +0 -3
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/types.js +0 -3
- package/lib-commonjs/types.js.map +1 -1
- package/lib-commonjs/useContextSelector.js +2 -5
- package/lib-commonjs/useContextSelector.js.map +1 -1
- package/lib-commonjs/useHasParentContext.js +1 -3
- package/lib-commonjs/useHasParentContext.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.json
CHANGED
@@ -2,7 +2,52 @@
|
|
2
2
|
"name": "@fluentui/react-context-selector",
|
3
3
|
"entries": [
|
4
4
|
{
|
5
|
-
"date": "
|
5
|
+
"date": "Wed, 31 May 2023 06:43:06 GMT",
|
6
|
+
"tag": "@fluentui/react-context-selector_v9.1.22",
|
7
|
+
"version": "9.1.22",
|
8
|
+
"comments": {
|
9
|
+
"patch": [
|
10
|
+
{
|
11
|
+
"author": "beachball",
|
12
|
+
"package": "@fluentui/react-context-selector",
|
13
|
+
"comment": "Bump @fluentui/react-utilities to v9.9.2",
|
14
|
+
"commit": "17ca96c6b7f684f36a5c63e40e78b4986a86c713"
|
15
|
+
}
|
16
|
+
]
|
17
|
+
}
|
18
|
+
},
|
19
|
+
{
|
20
|
+
"date": "Thu, 25 May 2023 10:00:48 GMT",
|
21
|
+
"tag": "@fluentui/react-context-selector_v9.1.21",
|
22
|
+
"version": "9.1.21",
|
23
|
+
"comments": {
|
24
|
+
"patch": [
|
25
|
+
{
|
26
|
+
"author": "beachball",
|
27
|
+
"package": "@fluentui/react-context-selector",
|
28
|
+
"comment": "Bump @fluentui/react-utilities to v9.9.1",
|
29
|
+
"commit": "8f7c1b7591b3e665ae39e1b22ab0f0b28fb795c4"
|
30
|
+
}
|
31
|
+
]
|
32
|
+
}
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"date": "Wed, 24 May 2023 20:45:25 GMT",
|
36
|
+
"tag": "@fluentui/react-context-selector_v9.1.20",
|
37
|
+
"version": "9.1.20",
|
38
|
+
"comments": {
|
39
|
+
"none": [
|
40
|
+
{
|
41
|
+
"author": "olfedias@microsoft.com",
|
42
|
+
"package": "@fluentui/react-context-selector",
|
43
|
+
"commit": "69e0617a93cb44ef42f3bd19284b7dc5fe27fed3",
|
44
|
+
"comment": "chore: update test-ssr script"
|
45
|
+
}
|
46
|
+
]
|
47
|
+
}
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"date": "Thu, 18 May 2023 00:39:05 GMT",
|
6
51
|
"tag": "@fluentui/react-context-selector_v9.1.20",
|
7
52
|
"version": "9.1.20",
|
8
53
|
"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, 31 May 2023 06:43:06 GMT and should not be manually modified.
|
4
4
|
|
5
5
|
<!-- Start content -->
|
6
6
|
|
7
|
+
## [9.1.22](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.22)
|
8
|
+
|
9
|
+
Wed, 31 May 2023 06:43:06 GMT
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.21..@fluentui/react-context-selector_v9.1.22)
|
11
|
+
|
12
|
+
### Patches
|
13
|
+
|
14
|
+
- Bump @fluentui/react-utilities to v9.9.2 ([PR #28054](https://github.com/microsoft/fluentui/pull/28054) by beachball)
|
15
|
+
|
16
|
+
## [9.1.21](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.21)
|
17
|
+
|
18
|
+
Thu, 25 May 2023 10:00:48 GMT
|
19
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.20..@fluentui/react-context-selector_v9.1.21)
|
20
|
+
|
21
|
+
### Patches
|
22
|
+
|
23
|
+
- Bump @fluentui/react-utilities to v9.9.1 ([PR #27988](https://github.com/microsoft/fluentui/pull/27988) by beachball)
|
24
|
+
|
7
25
|
## [9.1.20](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.20)
|
8
26
|
|
9
|
-
Thu, 18 May 2023 00:
|
27
|
+
Thu, 18 May 2023 00:39:05 GMT
|
10
28
|
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.19..@fluentui/react-context-selector_v9.1.20)
|
11
29
|
|
12
30
|
### Patches
|
package/lib/createContext.js
CHANGED
@@ -1,57 +1,59 @@
|
|
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
|
-
const createProvider = Original
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
4
|
+
const createProvider = (Original)=>{
|
5
|
+
const Provider = (props)=>{
|
6
|
+
// Holds an actual "props.value"
|
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.
|
11
|
+
const contextValue = React.useRef();
|
12
|
+
if (!contextValue.current) {
|
13
|
+
contextValue.current = {
|
14
|
+
value: valueRef,
|
15
|
+
version: versionRef,
|
16
|
+
listeners: []
|
17
|
+
};
|
18
|
+
}
|
19
|
+
useIsomorphicLayoutEffect(()=>{
|
20
|
+
valueRef.current = props.value;
|
21
|
+
versionRef.current += 1;
|
22
|
+
runWithPriority(NormalPriority, ()=>{
|
23
|
+
contextValue.current.listeners.forEach((listener)=>{
|
24
|
+
listener([
|
25
|
+
versionRef.current,
|
26
|
+
props.value
|
27
|
+
]);
|
28
|
+
});
|
29
|
+
});
|
30
|
+
}, [
|
31
|
+
props.value
|
32
|
+
]);
|
33
|
+
return React.createElement(Original, {
|
34
|
+
value: contextValue.current
|
35
|
+
}, props.children);
|
36
|
+
};
|
37
|
+
/* istanbul ignore else */ if (process.env.NODE_ENV !== 'production') {
|
38
|
+
Provider.displayName = 'ContextSelector.Provider';
|
18
39
|
}
|
19
|
-
|
20
|
-
valueRef.current = props.value;
|
21
|
-
versionRef.current += 1;
|
22
|
-
runWithPriority(NormalPriority, () => {
|
23
|
-
contextValue.current.listeners.forEach(listener => {
|
24
|
-
listener([versionRef.current, props.value]);
|
25
|
-
});
|
26
|
-
});
|
27
|
-
}, [props.value]);
|
28
|
-
return /*#__PURE__*/React.createElement(Original, {
|
29
|
-
value: contextValue.current
|
30
|
-
}, props.children);
|
31
|
-
};
|
32
|
-
/* istanbul ignore else */
|
33
|
-
if (process.env.NODE_ENV !== 'production') {
|
34
|
-
Provider.displayName = 'ContextSelector.Provider';
|
35
|
-
}
|
36
|
-
return Provider;
|
40
|
+
return Provider;
|
37
41
|
};
|
38
42
|
/**
|
39
43
|
* @internal
|
40
|
-
*/
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
return context;
|
44
|
+
*/ export const createContext = (defaultValue)=>{
|
45
|
+
// eslint-disable-next-line @fluentui/no-context-default-value
|
46
|
+
const context = React.createContext({
|
47
|
+
value: {
|
48
|
+
current: defaultValue
|
49
|
+
},
|
50
|
+
version: {
|
51
|
+
current: -1
|
52
|
+
},
|
53
|
+
listeners: []
|
54
|
+
});
|
55
|
+
context.Provider = createProvider(context.Provider);
|
56
|
+
// We don't support Consumer API
|
57
|
+
delete context.Consumer;
|
58
|
+
return context;
|
56
59
|
};
|
57
|
-
//# sourceMappingURL=createContext.js.map
|
package/lib/createContext.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["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"],"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"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ,4BAA4B;AACtE,YAAYC,WAAW,QAAQ;AAC/B,SAASC,2BAA2BC,cAAc,EAAEC,4BAA4BC,eAAe,QAAQ,YAAY;AAInH,MAAMC,iBAAiB,CAAQC,WAAkD;IAC/E,MAAMC,WAAiDC,CAAAA,QAAS;QAC9D,gCAAgC;QAChC,MAAMC,WAAWT,MAAMU,MAAM,CAACF,MAAMG,KAAK;QACzC,+GAA+G;QAC/G,MAAMC,aAAaZ,MAAMU,MAAM,CAAC;QAEhC,gFAAgF;QAChF,MAAMG,eAAeb,MAAMU,MAAM;QAEjC,IAAI,CAACG,aAAaC,OAAO,EAAE;YACzBD,aAAaC,OAAO,GAAG;gBACrBH,OAAOF;gBACPM,SAASH;gBACTI,WAAW,EAAE;YACf;QACF,CAAC;QAEDjB,0BAA0B,IAAM;YAC9BU,SAASK,OAAO,GAAGN,MAAMG,KAAK;YAC9BC,WAAWE,OAAO,IAAI;YAEtBV,gBAAgBF,gBAAgB,IAAM;gBACnCW,aAAaC,OAAO,CAAyBE,SAAS,CAACC,OAAO,CAACC,CAAAA,WAAY;oBAC1EA,SAAS;wBAACN,WAAWE,OAAO;wBAAEN,MAAMG,KAAK;qBAAC;gBAC5C;YACF;QACF,GAAG;YAACH,MAAMG,KAAK;SAAC;QAEhB,OAAOX,MAAMmB,aAAa,CAACb,UAAU;YAAEK,OAAOE,aAAaC,OAAO;QAAC,GAAGN,MAAMY,QAAQ;IACtF;IAEA,wBAAwB,GACxB,IAAIC,QAAQC,GAAG,CAACC,QAAQ,KAAK,cAAc;QACzChB,SAASiB,WAAW,GAAG;IACzB,CAAC;IAED,OAAOjB;AACT;AAEA;;CAEC,GACD,OAAO,MAAMkB,gBAAgB,CAAQC,eAAwC;IAC3E,8DAA8D;IAC9D,MAAMC,UAAU3B,MAAMyB,aAAa,CAAsB;QACvDd,OAAO;YAAEG,SAASY;QAAa;QAC/BX,SAAS;YAAED,SAAS,CAAC;QAAE;QACvBE,WAAW,EAAE;IACf;IAEAW,QAAQpB,QAAQ,GAAGF,eAAsBsB,QAAQpB,QAAQ;IAEzD,gCAAgC;IAChC,OAAO,AAACoB,QAAsCC,QAAQ;IAEtD,OAAOD;AACT,EAAE"}
|
package/lib/index.js
CHANGED
package/lib/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["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"],"names":["createContext","useContextSelector","useHasParentContext"],"mappings":"AAAA,SAASA,aAAa,QAAQ,kBAAkB;AAChD,SAASC,kBAAkB,QAAQ,uBAAuB;AAC1D,SAASC,mBAAmB,QAAQ,wBAAwB"}
|
package/lib/types.js
CHANGED
package/lib/types.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["types.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * @internal\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\n/**\n * @internal\n */\nexport type ContextVersion = number;\n\n/**\n * @internal\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\n/**\n * @internal\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"],"names":["React"],"mappings":"AAAA,YAAYA,WAAW,QAAQ"}
|
@@ -5,73 +5,76 @@ import * as React from 'react';
|
|
5
5
|
* This hook returns context selected value by selector.
|
6
6
|
* It will only accept context created by `createContext`.
|
7
7
|
* It will trigger re-render if only the selected value is referentially changed.
|
8
|
-
*/
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
8
|
+
*/ export const useContextSelector = (context, selector)=>{
|
9
|
+
const contextValue = React.useContext(context);
|
10
|
+
const { value: { current: value } , version: { current: version } , listeners } = contextValue;
|
11
|
+
const selected = selector(value);
|
12
|
+
const [state, dispatch] = React.useReducer((prevState, payload)=>{
|
13
|
+
if (!payload) {
|
14
|
+
// early bail out when is dispatched during render
|
15
|
+
return [
|
16
|
+
value,
|
17
|
+
selected
|
18
|
+
];
|
19
|
+
}
|
20
|
+
if (payload[0] <= version) {
|
21
|
+
if (objectIs(prevState[1], selected)) {
|
22
|
+
return prevState; // bail out
|
23
|
+
}
|
24
|
+
return [
|
25
|
+
value,
|
26
|
+
selected
|
27
|
+
];
|
28
|
+
}
|
29
|
+
try {
|
30
|
+
if (objectIs(prevState[0], payload[1])) {
|
31
|
+
return prevState; // do not update
|
32
|
+
}
|
33
|
+
const nextSelected = selector(payload[1]);
|
34
|
+
if (objectIs(prevState[1], nextSelected)) {
|
35
|
+
return prevState; // do not update
|
36
|
+
}
|
37
|
+
return [
|
38
|
+
payload[1],
|
39
|
+
nextSelected
|
40
|
+
];
|
41
|
+
} catch (e) {
|
42
|
+
// ignored (stale props or some other reason)
|
43
|
+
}
|
44
|
+
// explicitly spread to enforce typing
|
45
|
+
return [
|
46
|
+
prevState[0],
|
47
|
+
prevState[1]
|
48
|
+
]; // schedule update
|
49
|
+
}, [
|
50
|
+
value,
|
51
|
+
selected
|
52
|
+
]);
|
53
|
+
if (!objectIs(state[1], selected)) {
|
54
|
+
// schedule re-render
|
55
|
+
// this is safe because it's self contained
|
56
|
+
dispatch(undefined);
|
25
57
|
}
|
26
|
-
|
27
|
-
|
28
|
-
return
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
}
|
37
|
-
|
38
|
-
const nextSelected = selector(payload[1]);
|
39
|
-
if (objectIs(prevState[1], nextSelected)) {
|
40
|
-
return prevState; // do not update
|
41
|
-
}
|
42
|
-
|
43
|
-
return [payload[1], nextSelected];
|
44
|
-
} catch (e) {
|
45
|
-
// ignored (stale props or some other reason)
|
46
|
-
}
|
47
|
-
// explicitly spread to enforce typing
|
48
|
-
return [prevState[0], prevState[1]]; // schedule update
|
49
|
-
}, [value, selected]);
|
50
|
-
if (!objectIs(state[1], selected)) {
|
51
|
-
// schedule re-render
|
52
|
-
// this is safe because it's self contained
|
53
|
-
dispatch(undefined);
|
54
|
-
}
|
55
|
-
useIsomorphicLayoutEffect(() => {
|
56
|
-
listeners.push(dispatch);
|
57
|
-
return () => {
|
58
|
-
const index = listeners.indexOf(dispatch);
|
59
|
-
listeners.splice(index, 1);
|
60
|
-
};
|
61
|
-
}, [listeners]);
|
62
|
-
return state[1];
|
58
|
+
useIsomorphicLayoutEffect(()=>{
|
59
|
+
listeners.push(dispatch);
|
60
|
+
return ()=>{
|
61
|
+
const index = listeners.indexOf(dispatch);
|
62
|
+
listeners.splice(index, 1);
|
63
|
+
};
|
64
|
+
}, [
|
65
|
+
listeners
|
66
|
+
]);
|
67
|
+
return state[1];
|
63
68
|
};
|
64
69
|
/**
|
65
70
|
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
66
71
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
67
72
|
*/ // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
68
73
|
function is(x, y) {
|
69
|
-
|
70
|
-
|
74
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
75
|
+
;
|
71
76
|
}
|
72
77
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
73
|
-
const objectIs =
|
74
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
78
|
+
const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
75
79
|
// @ts-ignore fallback to native if it exists (not in IE11)
|
76
80
|
typeof Object.is === 'function' ? Object.is : is;
|
77
|
-
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["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 referentially 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"],"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"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ,4BAA4B;AACtE,YAAYC,WAAW,QAAQ;AAa/B;;;;;CAKC,GACD,OAAO,MAAMC,qBAAqB,CAChCC,SACAC,WACkB;IAClB,MAAMC,eAAeJ,MAAMK,UAAU,CAACH;IAEtC,MAAM,EACJI,OAAO,EAAEC,SAASD,MAAK,EAAE,CAAA,EACzBE,SAAS,EAAED,SAASC,QAAO,EAAE,CAAA,EAC7BC,UAAS,EACV,GAAGL;IACJ,MAAMM,WAAWP,SAASG;IAE1B,MAAM,CAACK,OAAOC,SAAS,GAAGZ,MAAMa,UAAU,CACxC,CACEC,WACAC,UAGoC;QACpC,IAAI,CAACA,SAAS;YACZ,kDAAkD;YAClD,OAAO;gBAACT;gBAAOI;aAAS;QAC1B,CAAC;QAED,IAAIK,OAAO,CAAC,EAAE,IAAIP,SAAS;YACzB,IAAIQ,SAASF,SAAS,CAAC,EAAE,EAAEJ,WAAW;gBACpC,OAAOI,WAAW,WAAW;YAC/B,CAAC;YAED,OAAO;gBAACR;gBAAOI;aAAS;QAC1B,CAAC;QAED,IAAI;YACF,IAAIM,SAASF,SAAS,CAAC,EAAE,EAAEC,OAAO,CAAC,EAAE,GAAG;gBACtC,OAAOD,WAAW,gBAAgB;YACpC,CAAC;YAED,MAAMG,eAAed,SAASY,OAAO,CAAC,EAAE;YAExC,IAAIC,SAASF,SAAS,CAAC,EAAE,EAAEG,eAAe;gBACxC,OAAOH,WAAW,gBAAgB;YACpC,CAAC;YAED,OAAO;gBAACC,OAAO,CAAC,EAAE;gBAAEE;aAAa;QACnC,EAAE,OAAOC,GAAG;QACV,6CAA6C;QAC/C;QAEA,sCAAsC;QACtC,OAAO;YAACJ,SAAS,CAAC,EAAE;YAAEA,SAAS,CAAC,EAAE;SAAC,EAAW,kBAAkB;IAClE,GACA;QAACR;QAAOI;KAAS;IAGnB,IAAI,CAACM,SAASL,KAAK,CAAC,EAAE,EAAED,WAAW;QACjC,qBAAqB;QACrB,2CAA2C;QAC3CE,SAASO;IACX,CAAC;IAEDpB,0BAA0B,IAAM;QAC9BU,UAAUW,IAAI,CAACR;QAEf,OAAO,IAAM;YACX,MAAMS,QAAQZ,UAAUa,OAAO,CAACV;YAChCH,UAAUc,MAAM,CAACF,OAAO;QAC1B;IACF,GAAG;QAACZ;KAAU;IAEd,OAAOE,KAAK,CAAC,EAAE;AACjB,EAAE;AAEF;;;CAGC,GACD,8DAA8D;AAC9D,SAASa,GAAGC,CAAM,EAAEC,CAAM,EAAE;IAC1B,OACE,AAACD,MAAMC,KAAMD,CAAAA,MAAM,KAAK,IAAIA,MAAM,IAAIC,CAAAA,KAAQD,MAAMA,KAAKC,MAAMA,EAAG,sCAAsC;;AAE5G;AAEA,8DAA8D;AAC9D,MAAMV,WACJ,6DAA6D;AAC7D,2DAA2D;AAC3D,OAAOW,OAAOH,EAAE,KAAK,aAAaG,OAAOH,EAAE,GAAGA,EAAE"}
|
@@ -6,12 +6,10 @@ import * as React from 'react';
|
|
6
6
|
*
|
7
7
|
* @param context - context created by react-context-selector
|
8
8
|
* @returns whether the hook is wrapped by a parent context
|
9
|
-
*/
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
return false;
|
9
|
+
*/ export function useHasParentContext(context) {
|
10
|
+
const contextValue = React.useContext(context);
|
11
|
+
if (contextValue.version) {
|
12
|
+
return contextValue.version.current !== -1;
|
13
|
+
}
|
14
|
+
return false;
|
16
15
|
}
|
17
|
-
//# sourceMappingURL=useHasParentContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["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"],"names":["React","useHasParentContext","context","contextValue","useContext","version","current"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAG/B;;;;;;;CAOC,GACD,OAAO,SAASC,oBAA2BC,OAAuB,EAAE;IAClE,MAAMC,eAAeH,MAAMI,UAAU,CAACF;IAEtC,IAAIC,aAAaE,OAAO,EAAE;QACxB,OAAOF,aAAaE,OAAO,CAACC,OAAO,KAAK,CAAC;IAC3C,CAAC;IAED,OAAO,KAAK;AACd,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["createContext.js"],"sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';\nconst createProvider = (Original)=>{\n const Provider = (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 // A stable object, is used to avoid context updates via mutation of its values.\n const contextValue = React.useRef();\n if (!contextValue.current) {\n contextValue.current = {\n value: valueRef,\n version: versionRef,\n listeners: []\n };\n }\n useIsomorphicLayoutEffect(()=>{\n valueRef.current = props.value;\n versionRef.current += 1;\n runWithPriority(NormalPriority, ()=>{\n contextValue.current.listeners.forEach((listener)=>{\n listener([\n versionRef.current,\n props.value\n ]);\n });\n });\n }, [\n props.value\n ]);\n return React.createElement(Original, {\n value: contextValue.current\n }, props.children);\n };\n /* istanbul ignore else */ if (process.env.NODE_ENV !== 'production') {\n Provider.displayName = 'ContextSelector.Provider';\n }\n return Provider;\n};\n/**\n * @internal\n */ export const createContext = (defaultValue)=>{\n // eslint-disable-next-line @fluentui/no-context-default-value\n const context = React.createContext({\n value: {\n current: defaultValue\n },\n version: {\n current: -1\n },\n listeners: []\n });\n context.Provider = createProvider(context.Provider);\n // We don't support Consumer API\n delete context.Consumer;\n return context;\n};\n"],"names":["createContext","createProvider","Original","Provider","props","valueRef","React","useRef","value","versionRef","contextValue","current","version","listeners","useIsomorphicLayoutEffect","runWithPriority","NormalPriority","forEach","listener","createElement","children","process","env","NODE_ENV","displayName","defaultValue","context","Consumer"],"mappings":";;;;+BA2CiBA;;aAAAA;;;gCA3CyB;6DACnB;2BACgF;AACvG,MAAMC,iBAAiB,CAACC,WAAW;IAC/B,MAAMC,WAAW,CAACC,QAAQ;QACtB,gCAAgC;QAChC,MAAMC,WAAWC,OAAMC,MAAM,CAACH,MAAMI,KAAK;QACzC,+GAA+G;QAC/G,MAAMC,aAAaH,OAAMC,MAAM,CAAC;QAChC,gFAAgF;QAChF,MAAMG,eAAeJ,OAAMC,MAAM;QACjC,IAAI,CAACG,aAAaC,OAAO,EAAE;YACvBD,aAAaC,OAAO,GAAG;gBACnBH,OAAOH;gBACPO,SAASH;gBACTI,WAAW,EAAE;YACjB;QACJ,CAAC;QACDC,IAAAA,yCAAyB,EAAC,IAAI;YAC1BT,SAASM,OAAO,GAAGP,MAAMI,KAAK;YAC9BC,WAAWE,OAAO,IAAI;YACtBI,IAAAA,mCAAe,EAACC,kCAAc,EAAE,IAAI;gBAChCN,aAAaC,OAAO,CAACE,SAAS,CAACI,OAAO,CAAC,CAACC,WAAW;oBAC/CA,SAAS;wBACLT,WAAWE,OAAO;wBAClBP,MAAMI,KAAK;qBACd;gBACL;YACJ;QACJ,GAAG;YACCJ,MAAMI,KAAK;SACd;QACD,qBAAOF,OAAMa,aAAa,CAACjB,UAAU;YACjCM,OAAOE,aAAaC,OAAO;QAC/B,GAAGP,MAAMgB,QAAQ;IACrB;IACA,wBAAwB,GAAG,IAAIC,QAAQC,GAAG,CAACC,QAAQ,KAAK,cAAc;QAClEpB,SAASqB,WAAW,GAAG;IAC3B,CAAC;IACD,OAAOrB;AACX;AAGW,MAAMH,gBAAgB,CAACyB,eAAe;IAC7C,8DAA8D;IAC9D,MAAMC,wBAAUpB,OAAMN,aAAa,CAAC;QAChCQ,OAAO;YACHG,SAASc;QACb;QACAb,SAAS;YACLD,SAAS,CAAC;QACd;QACAE,WAAW,EAAE;IACjB;IACAa,QAAQvB,QAAQ,GAAGF,eAAeyB,QAAQvB,QAAQ;IAClD,gCAAgC;IAChC,OAAOuB,QAAQC,QAAQ;IACvB,OAAOD;AACX"}
|
package/lib-commonjs/index.js
CHANGED
@@ -16,6 +16,3 @@ _export(exports, {
|
|
16
16
|
const _createContext = require("./createContext");
|
17
17
|
const _useContextSelector = require("./useContextSelector");
|
18
18
|
const _useHasParentContext = require("./useHasParentContext");
|
19
|
-
//# sourceMappingURL=index.js.map
|
20
|
-
|
21
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["index.js"],"sourcesContent":["export { createContext } from './createContext';\nexport { useContextSelector } from './useContextSelector';\nexport { useHasParentContext } from './useHasParentContext';\n"],"names":["createContext","useContextSelector","useHasParentContext"],"mappings":";;;;;;;;;;;IAASA,aAAa,MAAbA,4BAAa;IACbC,kBAAkB,MAAlBA,sCAAkB;IAClBC,mBAAmB,MAAnBA,wCAAmB;;+BAFE;oCACK;qCACC"}
|
package/lib-commonjs/types.js
CHANGED
@@ -4,6 +4,3 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
});
|
5
5
|
const _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
|
6
6
|
const _react = /*#__PURE__*/ _interopRequireWildcard(require("react"));
|
7
|
-
//# sourceMappingURL=types.js.map
|
8
|
-
|
9
|
-
//# sourceMappingURL=types.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["types.js"],"sourcesContent":["import * as React from 'react';\n"],"names":[],"mappings":";;;;;6DAAuB"}
|
@@ -79,8 +79,5 @@ function is(x, y) {
|
|
79
79
|
;
|
80
80
|
}
|
81
81
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
82
|
-
const objectIs = //
|
83
|
-
|
84
|
-
typeof Object.is === 'function' ? Object.is : is; //# sourceMappingURL=useContextSelector.js.map
|
85
|
-
|
86
|
-
//# sourceMappingURL=useContextSelector.js.map
|
82
|
+
const objectIs = // @ts-ignore fallback to native if it exists (not in IE11)
|
83
|
+
typeof Object.is === 'function' ? Object.is : is;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["useContextSelector.js"],"sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\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 referentially changed.\n */ export const useContextSelector = (context, selector)=>{\n const contextValue = React.useContext(context);\n const { value: { current: value } , version: { current: version } , listeners } = contextValue;\n const selected = selector(value);\n const [state, dispatch] = React.useReducer((prevState, payload)=>{\n if (!payload) {\n // early bail out when is dispatched during render\n return [\n value,\n selected\n ];\n }\n if (payload[0] <= version) {\n if (objectIs(prevState[1], selected)) {\n return prevState; // bail out\n }\n return [\n value,\n selected\n ];\n }\n try {\n if (objectIs(prevState[0], payload[1])) {\n return prevState; // do not update\n }\n const nextSelected = selector(payload[1]);\n if (objectIs(prevState[1], nextSelected)) {\n return prevState; // do not update\n }\n return [\n payload[1],\n nextSelected\n ];\n } catch (e) {\n // ignored (stale props or some other reason)\n }\n // explicitly spread to enforce typing\n return [\n prevState[0],\n prevState[1]\n ]; // schedule update\n }, [\n value,\n selected\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 useIsomorphicLayoutEffect(()=>{\n listeners.push(dispatch);\n return ()=>{\n const index = listeners.indexOf(dispatch);\n listeners.splice(index, 1);\n };\n }, [\n listeners\n ]);\n return state[1];\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 */ // eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction is(x, y) {\n return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare\n ;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore fallback to native if it exists (not in IE11)\ntypeof Object.is === 'function' ? Object.is : is;\n"],"names":["useContextSelector","context","selector","contextValue","React","useContext","value","current","version","listeners","selected","state","dispatch","useReducer","prevState","payload","objectIs","nextSelected","e","undefined","useIsomorphicLayoutEffect","push","index","indexOf","splice","is","x","y","Object"],"mappings":";;;;+BAOiBA;;aAAAA;;;gCAPyB;6DACnB;AAMZ,MAAMA,qBAAqB,CAACC,SAASC,WAAW;IACvD,MAAMC,eAAeC,OAAMC,UAAU,CAACJ;IACtC,MAAM,EAAEK,OAAO,EAAEC,SAASD,MAAK,EAAG,CAAA,EAAGE,SAAS,EAAED,SAASC,QAAO,EAAG,CAAA,EAAGC,UAAS,EAAG,GAAGN;IACrF,MAAMO,WAAWR,SAASI;IAC1B,MAAM,CAACK,OAAOC,SAAS,GAAGR,OAAMS,UAAU,CAAC,CAACC,WAAWC,UAAU;QAC7D,IAAI,CAACA,SAAS;YACV,kDAAkD;YAClD,OAAO;gBACHT;gBACAI;aACH;QACL,CAAC;QACD,IAAIK,OAAO,CAAC,EAAE,IAAIP,SAAS;YACvB,IAAIQ,SAASF,SAAS,CAAC,EAAE,EAAEJ,WAAW;gBAClC,OAAOI,WAAW,WAAW;YACjC,CAAC;YACD,OAAO;gBACHR;gBACAI;aACH;QACL,CAAC;QACD,IAAI;YACA,IAAIM,SAASF,SAAS,CAAC,EAAE,EAAEC,OAAO,CAAC,EAAE,GAAG;gBACpC,OAAOD,WAAW,gBAAgB;YACtC,CAAC;YACD,MAAMG,eAAef,SAASa,OAAO,CAAC,EAAE;YACxC,IAAIC,SAASF,SAAS,CAAC,EAAE,EAAEG,eAAe;gBACtC,OAAOH,WAAW,gBAAgB;YACtC,CAAC;YACD,OAAO;gBACHC,OAAO,CAAC,EAAE;gBACVE;aACH;QACL,EAAE,OAAOC,GAAG;QACZ,6CAA6C;QAC7C;QACA,sCAAsC;QACtC,OAAO;YACHJ,SAAS,CAAC,EAAE;YACZA,SAAS,CAAC,EAAE;SACf,EAAE,kBAAkB;IACzB,GAAG;QACCR;QACAI;KACH;IACD,IAAI,CAACM,SAASL,KAAK,CAAC,EAAE,EAAED,WAAW;QAC/B,qBAAqB;QACrB,2CAA2C;QAC3CE,SAASO;IACb,CAAC;IACDC,IAAAA,yCAAyB,EAAC,IAAI;QAC1BX,UAAUY,IAAI,CAACT;QACf,OAAO,IAAI;YACP,MAAMU,QAAQb,UAAUc,OAAO,CAACX;YAChCH,UAAUe,MAAM,CAACF,OAAO;QAC5B;IACJ,GAAG;QACCb;KACH;IACD,OAAOE,KAAK,CAAC,EAAE;AACnB;AACA;;;CAGC,GAAG,8DAA8D;AAClE,SAASc,GAAGC,CAAC,EAAEC,CAAC,EAAE;IACd,OAAOD,MAAMC,KAAMD,CAAAA,MAAM,KAAK,IAAIA,MAAM,IAAIC,CAAAA,KAAMD,MAAMA,KAAKC,MAAMA,EAAE,sCAAsC;;AAE/G;AACA,8DAA8D;AAC9D,MAAMX,WACN,2DAA2D;AAC3D,OAAOY,OAAOH,EAAE,KAAK,aAAaG,OAAOH,EAAE,GAAGA,EAAE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["useHasParentContext.js"],"sourcesContent":["import * as React from 'react';\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 */ export function useHasParentContext(context) {\n const contextValue = React.useContext(context);\n if (contextValue.version) {\n return contextValue.version.current !== -1;\n }\n return false;\n}\n"],"names":["useHasParentContext","context","contextValue","React","useContext","version","current"],"mappings":";;;;+BAQoBA;;aAAAA;;;6DARG;AAQZ,SAASA,oBAAoBC,OAAO,EAAE;IAC7C,MAAMC,eAAeC,OAAMC,UAAU,CAACH;IACtC,IAAIC,aAAaG,OAAO,EAAE;QACtB,OAAOH,aAAaG,OAAO,CAACC,OAAO,KAAK,CAAC;IAC7C,CAAC;IACD,OAAO,KAAK;AAChB"}
|
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.22",
|
4
4
|
"description": "React useContextSelector hook in userland",
|
5
5
|
"main": "lib-commonjs/index.js",
|
6
6
|
"module": "lib/index.js",
|
@@ -20,7 +20,7 @@
|
|
20
20
|
"test": "jest --passWithNoTests",
|
21
21
|
"type-check": "tsc -b tsconfig.json",
|
22
22
|
"generate-api": "just-scripts generate-api",
|
23
|
-
"test-ssr": "test-ssr ./stories/**/*.stories.tsx"
|
23
|
+
"test-ssr": "test-ssr \"./stories/**/*.stories.tsx\""
|
24
24
|
},
|
25
25
|
"devDependencies": {
|
26
26
|
"@fluentui/eslint-plugin": "*",
|
@@ -28,7 +28,7 @@
|
|
28
28
|
"@fluentui/scripts-tasks": "*"
|
29
29
|
},
|
30
30
|
"dependencies": {
|
31
|
-
"@fluentui/react-utilities": "^9.9.
|
31
|
+
"@fluentui/react-utilities": "^9.9.2",
|
32
32
|
"@swc/helpers": "^0.4.14"
|
33
33
|
},
|
34
34
|
"peerDependencies": {
|