@fluentui/react-context-selector 9.0.0-nightly.d730088d7f.0 → 9.0.0-rc.10
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 +282 -8
- package/CHANGELOG.md +154 -48
- package/dist/{react-context-selector.d.ts → index.d.ts} +0 -0
- package/{lib → dist}/tsdoc-metadata.json +0 -0
- package/lib/createContext.js +53 -40
- package/lib/createContext.js.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/useContextSelector.js +66 -49
- package/lib/useContextSelector.js.map +1 -1
- package/lib/useHasParentContext.js +8 -5
- package/lib/useHasParentContext.js.map +1 -1
- package/lib-commonjs/createContext.js +64 -44
- package/lib-commonjs/createContext.js.map +1 -1
- package/lib-commonjs/index.js +31 -9
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/types.js +4 -1
- package/lib-commonjs/types.js.map +1 -1
- package/lib-commonjs/useContextSelector.js +76 -52
- package/lib-commonjs/useContextSelector.js.map +1 -1
- package/lib-commonjs/useHasParentContext.js +16 -7
- package/lib-commonjs/useHasParentContext.js.map +1 -1
- package/package.json +19 -20
- package/lib/createContext.d.ts +0 -2
- package/lib/index.d.ts +0 -4
- package/lib/types.d.ts +0 -19
- package/lib/useContextSelector.d.ts +0 -7
- package/lib/useHasParentContext.d.ts +0 -9
- package/lib-commonjs/createContext.d.ts +0 -2
- package/lib-commonjs/index.d.ts +0 -4
- package/lib-commonjs/types.d.ts +0 -19
- package/lib-commonjs/useContextSelector.d.ts +0 -7
- package/lib-commonjs/useHasParentContext.d.ts +0 -9
@@ -5,63 +5,80 @@ import * as React from 'react';
|
|
5
5
|
* It will only accept context created by `createContext`.
|
6
6
|
* It will trigger re-render if only the selected value is referencially changed.
|
7
7
|
*/
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
return prevState; // do not update
|
26
|
-
}
|
27
|
-
var nextSelected = selector(payload[1]);
|
28
|
-
if (objectIs(prevState[1], nextSelected)) {
|
29
|
-
return prevState; // do not update
|
30
|
-
}
|
31
|
-
return [payload[1], nextSelected];
|
32
|
-
}
|
33
|
-
catch (e) {
|
34
|
-
// ignored (stale props or some other reason)
|
35
|
-
}
|
36
|
-
// explicitly spread to enforce typing
|
37
|
-
return [prevState[0], prevState[1]]; // schedule update
|
38
|
-
}, [value, selected]), state = _a[0], dispatch = _a[1];
|
39
|
-
if (!objectIs(state[1], selected)) {
|
40
|
-
// schedule re-render
|
41
|
-
// this is safe because it's self contained
|
42
|
-
dispatch(undefined);
|
8
|
+
|
9
|
+
export const useContextSelector = (context, selector) => {
|
10
|
+
const contextValue = React.useContext(context);
|
11
|
+
const {
|
12
|
+
value: {
|
13
|
+
current: value
|
14
|
+
},
|
15
|
+
version: {
|
16
|
+
current: version
|
17
|
+
},
|
18
|
+
listeners
|
19
|
+
} = contextValue;
|
20
|
+
const selected = selector(value);
|
21
|
+
const [state, dispatch] = React.useReducer((prevState, payload) => {
|
22
|
+
if (!payload) {
|
23
|
+
// early bail out when is dispatched during render
|
24
|
+
return [value, selected];
|
43
25
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
26
|
+
|
27
|
+
if (payload[0] <= version) {
|
28
|
+
if (objectIs(prevState[1], selected)) {
|
29
|
+
return prevState; // bail out
|
30
|
+
}
|
31
|
+
|
32
|
+
return [value, selected];
|
33
|
+
}
|
34
|
+
|
35
|
+
try {
|
36
|
+
if (objectIs(prevState[0], payload[1])) {
|
37
|
+
return prevState; // do not update
|
38
|
+
}
|
39
|
+
|
40
|
+
const nextSelected = selector(payload[1]);
|
41
|
+
|
42
|
+
if (objectIs(prevState[1], nextSelected)) {
|
43
|
+
return prevState; // do not update
|
44
|
+
}
|
45
|
+
|
46
|
+
return [payload[1], nextSelected];
|
47
|
+
} catch (e) {// ignored (stale props or some other reason)
|
48
|
+
} // explicitly spread to enforce typing
|
49
|
+
|
50
|
+
|
51
|
+
return [prevState[0], prevState[1]]; // schedule update
|
52
|
+
}, [value, selected]);
|
53
|
+
|
54
|
+
if (!objectIs(state[1], selected)) {
|
55
|
+
// schedule re-render
|
56
|
+
// this is safe because it's self contained
|
57
|
+
dispatch(undefined);
|
58
|
+
}
|
59
|
+
|
60
|
+
useIsomorphicLayoutEffect(() => {
|
61
|
+
listeners.push(dispatch);
|
62
|
+
return () => {
|
63
|
+
const index = listeners.indexOf(dispatch);
|
64
|
+
listeners.splice(index, 1);
|
65
|
+
};
|
66
|
+
}, [listeners]);
|
67
|
+
return state[1];
|
52
68
|
};
|
53
69
|
/**
|
54
70
|
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
55
71
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
56
72
|
*/
|
57
73
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
74
|
+
|
58
75
|
function is(x, y) {
|
59
|
-
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
76
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
77
|
+
;
|
78
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
79
|
+
|
80
|
+
|
81
|
+
const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
65
82
|
// @ts-ignore fallback to native if it exists (not in IE11)
|
66
83
|
typeof Object.is === 'function' ? Object.is : is;
|
67
84
|
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useContextSelector.ts"],"names":[],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CAYE,OAAO,CAAP,EAAU,CACV;AACD,KA5BkC,CA8BnC;;;AACA,WAAO,CAAC,SAAS,CAAC,CAAD,CAAV,EAAe,SAAS,CAAC,CAAD,CAAxB,CAAP,CA/BmC,CA+BW;AAC/C,GAtCuB,EAuCxB,CAAC,KAAD,EAAQ,QAAR,CAvCwB,CAA1B;;AA0CA,MAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAD,CAAN,EAAW,QAAX,CAAb,EAAmC;AACjC;AACA;AACA,IAAA,QAAQ,CAAC,SAAD,CAAR;AACD;;AAED,EAAA,yBAAyB,CAAC,MAAK;AAC7B,IAAA,SAAS,CAAC,IAAV,CAAe,QAAf;AAEA,WAAO,MAAK;AACV,YAAM,KAAK,GAAG,SAAS,CAAC,OAAV,CAAkB,QAAlB,CAAd;AACA,MAAA,SAAS,CAAC,MAAV,CAAiB,KAAjB,EAAwB,CAAxB;AACD,KAHD;AAID,GAPwB,EAOtB,CAAC,SAAD,CAPsB,CAAzB;AASA,SAAO,KAAK,CAAC,CAAD,CAAZ;AACD,CAvEM;AAyEP;;;AAGG;AACH;;AACA,SAAS,EAAT,CAAY,CAAZ,EAAoB,CAApB,EAA0B;AACxB,SACG,CAAC,KAAK,CAAN,KAAY,CAAC,KAAK,CAAN,IAAW,IAAI,CAAJ,KAAU,IAAI,CAArC,CAAD,IAA8C,CAAC,KAAK,CAAN,IAAW,CAAC,KAAK,CADjE,CACoE;AADpE;AAGD,C,CAED;;;AACA,MAAM,QAAQ,GACZ;AACA;AACA,OAAO,MAAM,CAAC,EAAd,KAAqB,UAArB,GAAkC,MAAM,CAAC,EAAzC,GAA8C,EAHhD","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"],"sourceRoot":"../src/"}
|
@@ -6,11 +6,14 @@ import * as React from 'react';
|
|
6
6
|
* @param context - context created by react-context-selector
|
7
7
|
* @returns whether the hook is wrapped by a parent context
|
8
8
|
*/
|
9
|
+
|
9
10
|
export function useHasParentContext(context) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
const contextValue = React.useContext(context);
|
12
|
+
|
13
|
+
if (contextValue.version) {
|
14
|
+
return contextValue.version.current !== -1;
|
15
|
+
}
|
16
|
+
|
17
|
+
return false;
|
15
18
|
}
|
16
19
|
//# sourceMappingURL=useHasParentContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useHasParentContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAGA;;;;;;AAMG;;AACH,OAAM,SAAU,mBAAV,CAAqC,OAArC,EAA4D;AAChE,QAAM,YAAY,GAAG,KAAK,CAAC,UAAN,CAAkB,OAAlB,CAArB;;AAEA,MAAI,YAAY,CAAC,OAAjB,EAA0B;AACxB,WAAO,YAAY,CAAC,OAAb,CAAqB,OAArB,KAAiC,CAAC,CAAzC;AACD;;AAED,SAAO,KAAP;AACD","sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\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"],"sourceRoot":"../src/"}
|
@@ -1,51 +1,71 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
3
6
|
exports.createContext = void 0;
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
contextValue.current.listeners.forEach(function (listener) {
|
27
|
-
listener([versionRef.current, props.value]);
|
28
|
-
});
|
29
|
-
});
|
30
|
-
}, [props.value]);
|
31
|
-
return React.createElement(Original, { value: contextValue.current }, props.children);
|
32
|
-
};
|
33
|
-
/* istanbul ignore else */
|
34
|
-
if (process.env.NODE_ENV !== 'production') {
|
35
|
-
Provider.displayName = 'ContextSelector.Provider';
|
7
|
+
|
8
|
+
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
+
|
10
|
+
const React = /*#__PURE__*/require("react");
|
11
|
+
|
12
|
+
const scheduler_1 = /*#__PURE__*/require("scheduler");
|
13
|
+
|
14
|
+
const createProvider = Original => {
|
15
|
+
const Provider = props => {
|
16
|
+
// Holds an actual "props.value"
|
17
|
+
const valueRef = React.useRef(props.value); // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
|
18
|
+
|
19
|
+
const versionRef = React.useRef(0); // A stable object, is used to avoid context updates via mutation of its values.
|
20
|
+
|
21
|
+
const contextValue = React.useRef();
|
22
|
+
|
23
|
+
if (!contextValue.current) {
|
24
|
+
contextValue.current = {
|
25
|
+
value: valueRef,
|
26
|
+
version: versionRef,
|
27
|
+
listeners: []
|
28
|
+
};
|
36
29
|
}
|
37
|
-
|
30
|
+
|
31
|
+
react_utilities_1.useIsomorphicLayoutEffect(() => {
|
32
|
+
valueRef.current = props.value;
|
33
|
+
versionRef.current += 1;
|
34
|
+
scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, () => {
|
35
|
+
contextValue.current.listeners.forEach(listener => {
|
36
|
+
listener([versionRef.current, props.value]);
|
37
|
+
});
|
38
|
+
});
|
39
|
+
}, [props.value]);
|
40
|
+
return React.createElement(Original, {
|
41
|
+
value: contextValue.current
|
42
|
+
}, props.children);
|
43
|
+
};
|
44
|
+
/* istanbul ignore else */
|
45
|
+
|
46
|
+
|
47
|
+
if (process.env.NODE_ENV !== 'production') {
|
48
|
+
Provider.displayName = 'ContextSelector.Provider';
|
49
|
+
}
|
50
|
+
|
51
|
+
return Provider;
|
38
52
|
};
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
}
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
53
|
+
|
54
|
+
const createContext = defaultValue => {
|
55
|
+
const context = React.createContext({
|
56
|
+
value: {
|
57
|
+
current: defaultValue
|
58
|
+
},
|
59
|
+
version: {
|
60
|
+
current: -1
|
61
|
+
},
|
62
|
+
listeners: []
|
63
|
+
});
|
64
|
+
context.Provider = createProvider(context.Provider); // We don't support Consumer API
|
65
|
+
|
66
|
+
delete context.Consumer;
|
67
|
+
return context;
|
49
68
|
};
|
69
|
+
|
50
70
|
exports.createContext = createContext;
|
51
71
|
//# sourceMappingURL=createContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["createContext.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,iBAAA,gBAAA,OAAA,CAAA,2BAAA,CAAA;;AACA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,WAAA,gBAAA,OAAA,CAAA,WAAA,CAAA;;AAIA,MAAM,cAAc,GAAW,QAAR,IAAyD;AAC9E,QAAM,QAAQ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QAAM,OAAO,GAAG,KAAK,CAAC,aAAN,CAAyC;AACvD,IAAA,KAAK,EAAE;AAAE,MAAA,OAAO,EAAE;AAAX,KADgD;AAEvD,IAAA,OAAO,EAAE;AAAE,MAAA,OAAO,EAAE,CAAC;AAAZ,KAF8C;AAGvD,IAAA,SAAS,EAAE;AAH4C,GAAzC,CAAhB;AAMA,EAAA,OAAO,CAAC,QAAR,GAAmB,cAAc,CAAQ,OAAO,CAAC,QAAhB,CAAjC,CAP0E,CAS1E;;AACA,SAAS,OAAsC,CAAC,QAAhD;AAEA,SAAQ,OAAR;AACD,CAbM;;AAAM,OAAA,CAAA,aAAA,GAAa,aAAb","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"],"sourceRoot":"../src/"}
|
package/lib-commonjs/index.js
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
3
6
|
exports.useHasParentContext = exports.useContextSelector = exports.createContext = void 0;
|
4
|
-
|
5
|
-
var createContext_1 = require("./createContext");
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
|
8
|
+
var createContext_1 = /*#__PURE__*/require("./createContext");
|
9
|
+
|
10
|
+
Object.defineProperty(exports, "createContext", {
|
11
|
+
enumerable: true,
|
12
|
+
get: function () {
|
13
|
+
return createContext_1.createContext;
|
14
|
+
}
|
15
|
+
});
|
16
|
+
|
17
|
+
var useContextSelector_1 = /*#__PURE__*/require("./useContextSelector");
|
18
|
+
|
19
|
+
Object.defineProperty(exports, "useContextSelector", {
|
20
|
+
enumerable: true,
|
21
|
+
get: function () {
|
22
|
+
return useContextSelector_1.useContextSelector;
|
23
|
+
}
|
24
|
+
});
|
25
|
+
|
26
|
+
var useHasParentContext_1 = /*#__PURE__*/require("./useHasParentContext");
|
27
|
+
|
28
|
+
Object.defineProperty(exports, "useHasParentContext", {
|
29
|
+
enumerable: true,
|
30
|
+
get: function () {
|
31
|
+
return useHasParentContext_1.useHasParentContext;
|
32
|
+
}
|
33
|
+
});
|
12
34
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;;AAAA,IAAA,eAAA,gBAAA,OAAA,CAAA,iBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,eAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,eAAA,CAAA,aAAA;AAAa;AAAb,CAAA;;AACT,IAAA,oBAAA,gBAAA,OAAA,CAAA,sBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,oBAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,oBAAA,CAAA,kBAAA;AAAkB;AAAlB,CAAA;;AACT,IAAA,qBAAA,gBAAA,OAAA,CAAA,uBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,qBAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,qBAAA,CAAA,mBAAA;AAAmB;AAAnB,CAAA","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"],"sourceRoot":"../src/"}
|
package/lib-commonjs/types.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourceRoot":"../src/"}
|
@@ -1,71 +1,95 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
3
6
|
exports.useContextSelector = void 0;
|
4
|
-
|
5
|
-
|
7
|
+
|
8
|
+
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
|
9
|
+
|
10
|
+
const React = /*#__PURE__*/require("react");
|
6
11
|
/**
|
7
12
|
* This hook returns context selected value by selector.
|
8
13
|
* It will only accept context created by `createContext`.
|
9
14
|
* It will trigger re-render if only the selected value is referencially changed.
|
10
15
|
*/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
}
|
30
|
-
var nextSelected = selector(payload[1]);
|
31
|
-
if (objectIs(prevState[1], nextSelected)) {
|
32
|
-
return prevState; // do not update
|
33
|
-
}
|
34
|
-
return [payload[1], nextSelected];
|
35
|
-
}
|
36
|
-
catch (e) {
|
37
|
-
// ignored (stale props or some other reason)
|
38
|
-
}
|
39
|
-
// explicitly spread to enforce typing
|
40
|
-
return [prevState[0], prevState[1]]; // schedule update
|
41
|
-
}, [value, selected]), state = _a[0], dispatch = _a[1];
|
42
|
-
if (!objectIs(state[1], selected)) {
|
43
|
-
// schedule re-render
|
44
|
-
// this is safe because it's self contained
|
45
|
-
dispatch(undefined);
|
16
|
+
|
17
|
+
|
18
|
+
const useContextSelector = (context, selector) => {
|
19
|
+
const contextValue = React.useContext(context);
|
20
|
+
const {
|
21
|
+
value: {
|
22
|
+
current: value
|
23
|
+
},
|
24
|
+
version: {
|
25
|
+
current: version
|
26
|
+
},
|
27
|
+
listeners
|
28
|
+
} = contextValue;
|
29
|
+
const selected = selector(value);
|
30
|
+
const [state, dispatch] = React.useReducer((prevState, payload) => {
|
31
|
+
if (!payload) {
|
32
|
+
// early bail out when is dispatched during render
|
33
|
+
return [value, selected];
|
46
34
|
}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
35
|
+
|
36
|
+
if (payload[0] <= version) {
|
37
|
+
if (objectIs(prevState[1], selected)) {
|
38
|
+
return prevState; // bail out
|
39
|
+
}
|
40
|
+
|
41
|
+
return [value, selected];
|
42
|
+
}
|
43
|
+
|
44
|
+
try {
|
45
|
+
if (objectIs(prevState[0], payload[1])) {
|
46
|
+
return prevState; // do not update
|
47
|
+
}
|
48
|
+
|
49
|
+
const nextSelected = selector(payload[1]);
|
50
|
+
|
51
|
+
if (objectIs(prevState[1], nextSelected)) {
|
52
|
+
return prevState; // do not update
|
53
|
+
}
|
54
|
+
|
55
|
+
return [payload[1], nextSelected];
|
56
|
+
} catch (e) {// ignored (stale props or some other reason)
|
57
|
+
} // explicitly spread to enforce typing
|
58
|
+
|
59
|
+
|
60
|
+
return [prevState[0], prevState[1]]; // schedule update
|
61
|
+
}, [value, selected]);
|
62
|
+
|
63
|
+
if (!objectIs(state[1], selected)) {
|
64
|
+
// schedule re-render
|
65
|
+
// this is safe because it's self contained
|
66
|
+
dispatch(undefined);
|
67
|
+
}
|
68
|
+
|
69
|
+
react_utilities_1.useIsomorphicLayoutEffect(() => {
|
70
|
+
listeners.push(dispatch);
|
71
|
+
return () => {
|
72
|
+
const index = listeners.indexOf(dispatch);
|
73
|
+
listeners.splice(index, 1);
|
74
|
+
};
|
75
|
+
}, [listeners]);
|
76
|
+
return state[1];
|
55
77
|
};
|
78
|
+
|
56
79
|
exports.useContextSelector = useContextSelector;
|
57
80
|
/**
|
58
81
|
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
59
82
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
60
83
|
*/
|
61
84
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
85
|
+
|
62
86
|
function is(x, y) {
|
63
|
-
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
87
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
88
|
+
;
|
89
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
90
|
+
|
91
|
+
|
92
|
+
const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
69
93
|
// @ts-ignore fallback to native if it exists (not in IE11)
|
70
94
|
typeof Object.is === 'function' ? Object.is : is;
|
71
95
|
//# sourceMappingURL=useContextSelector.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useContextSelector.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,iBAAA,gBAAA,OAAA,CAAA,2BAAA,CAAA;;AACA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;AAaA;;;;AAIG;;;AACI,MAAM,kBAAkB,GAAG,CAChC,OADgC,EAEhC,QAFgC,KAGf;AACjB,QAAM,YAAY,GAAG,KAAK,CAAC,UAAN,CAAkB,OAAlB,CAArB;AAEA,QAAM;AACJ,IAAA,KAAK,EAAE;AAAE,MAAA,OAAO,EAAE;AAAX,KADH;AAEJ,IAAA,OAAO,EAAE;AAAE,MAAA,OAAO,EAAE;AAAX,KAFL;AAGJ,IAAA;AAHI,MAIF,YAJJ;AAKA,QAAM,QAAQ,GAAG,QAAQ,CAAC,KAAD,CAAzB;AAEA,QAAM,CAAC,KAAD,EAAQ,QAAR,IAAoB,KAAK,CAAC,UAAN,CACxB,CACE,SADF,EAEE,OAFF,KAKqC;AACnC,QAAI,CAAC,OAAL,EAAc;AACZ;AACA,aAAO,CAAC,KAAD,EAAQ,QAAR,CAAP;AACD;;AAED,QAAI,OAAO,CAAC,CAAD,CAAP,IAAc,OAAlB,EAA2B;AACzB,UAAI,QAAQ,CAAC,SAAS,CAAC,CAAD,CAAV,EAAe,QAAf,CAAZ,EAAsC;AACpC,eAAO,SAAP,CADoC,CAClB;AACnB;;AAED,aAAO,CAAC,KAAD,EAAQ,QAAR,CAAP;AACD;;AAED,QAAI;AACF,UAAI,QAAQ,CAAC,SAAS,CAAC,CAAD,CAAV,EAAe,OAAO,CAAC,CAAD,CAAtB,CAAZ,EAAwC;AACtC,eAAO,SAAP,CADsC,CACpB;AACnB;;AAED,YAAM,YAAY,GAAG,QAAQ,CAAC,OAAO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ArB,GAAkC,MAAM,CAAC,EAAzC,GAA8C,EAHhD","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"],"sourceRoot":"../src/"}
|
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
3
6
|
exports.useHasParentContext = void 0;
|
4
|
-
|
7
|
+
|
8
|
+
const React = /*#__PURE__*/require("react");
|
5
9
|
/**
|
6
10
|
* Utility hook for contexts created by react-context-selector to determine if a parent context exists
|
7
11
|
* WARNING: This hook will not work for native React contexts
|
@@ -9,12 +13,17 @@ var React = require("react");
|
|
9
13
|
* @param context - context created by react-context-selector
|
10
14
|
* @returns whether the hook is wrapped by a parent context
|
11
15
|
*/
|
16
|
+
|
17
|
+
|
12
18
|
function useHasParentContext(context) {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
const contextValue = React.useContext(context);
|
20
|
+
|
21
|
+
if (contextValue.version) {
|
22
|
+
return contextValue.version.current !== -1;
|
23
|
+
}
|
24
|
+
|
25
|
+
return false;
|
18
26
|
}
|
27
|
+
|
19
28
|
exports.useHasParentContext = useHasParentContext;
|
20
29
|
//# sourceMappingURL=useHasParentContext.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useHasParentContext.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;AAGA;;;;;;AAMG;;;AACH,SAAgB,mBAAhB,CAA2C,OAA3C,EAAkE;AAChE,QAAM,YAAY,GAAG,KAAK,CAAC,UAAN,CAAkB,OAAlB,CAArB;;AAEA,MAAI,YAAY,CAAC,OAAjB,EAA0B;AACxB,WAAO,YAAY,CAAC,OAAb,CAAqB,OAArB,KAAiC,CAAC,CAAzC;AACD;;AAED,SAAO,KAAP;AACD;;AARD,OAAA,CAAA,mBAAA,GAAA,mBAAA","sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\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"],"sourceRoot":"../src/"}
|