Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@fluentui/react-context-selector

Package Overview
Dependencies
Maintainers
13
Versions
870
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fluentui/react-context-selector - npm Package Compare versions

Comparing version 0.0.0-nightly57a9843f2620211119.1 to 0.0.0-nightly58c310891320211216.1

35

CHANGELOG.json

@@ -5,5 +5,5 @@ {

{
"date": "Fri, 19 Nov 2021 04:18:54 GMT",
"tag": "@fluentui/react-context-selector_v0.0.0-nightly57a9843f2620211119.1",
"version": "0.0.0-nightly57a9843f2620211119.1",
"date": "Thu, 16 Dec 2021 04:14:54 GMT",
"tag": "@fluentui/react-context-selector_v0.0.0-nightly58c310891320211216.1",
"version": "0.0.0-nightly58c310891320211216.1",
"comments": {

@@ -14,3 +14,3 @@ "prerelease": [

"package": "@fluentui/react-context-selector",
"commit": "e6017db01058700b10f0f2b6b36ef51efb28b9b9",
"commit": "919f08a1b25cd5045dc729d44a65f64f7838f4dc",
"comment": "Release nightly v9"

@@ -21,4 +21,4 @@ },

"package": "@fluentui/react-context-selector",
"comment": "Bump @fluentui/react-utilities to v0.0.0-nightly57a9843f2620211119.1",
"commit": "e6017db01058700b10f0f2b6b36ef51efb28b9b9"
"comment": "Bump @fluentui/react-utilities to v0.0.0-nightly58c310891320211216.1",
"commit": "919f08a1b25cd5045dc729d44a65f64f7838f4dc"
}

@@ -29,2 +29,25 @@ ]

{
"date": "Thu, 25 Nov 2021 08:34:13 GMT",
"tag": "@fluentui/react-context-selector_v9.0.0-beta.4",
"version": "9.0.0-beta.4",
"comments": {
"none": [
{
"author": "olfedias@microsoft.com",
"package": "@fluentui/react-context-selector",
"commit": "59b3bebb1944aa35f8e8754beec16dcc95f1d5fb",
"comment": "update tooling configs"
}
],
"prerelease": [
{
"author": "beachball",
"package": "@fluentui/react-context-selector",
"comment": "Bump @fluentui/react-utilities to v9.0.0-beta.4",
"commit": "48d236ac53a4950fabc3ddd52f91dac93ca0195b"
}
]
}
},
{
"date": "Fri, 12 Nov 2021 13:25:22 GMT",

@@ -31,0 +54,0 @@ "tag": "@fluentui/react-context-selector_v9.0.0-beta.3",

21

CHANGELOG.md
# Change Log - @fluentui/react-context-selector
This log was last generated on Fri, 19 Nov 2021 04:18:54 GMT and should not be manually modified.
This log was last generated on Thu, 16 Dec 2021 04:14:54 GMT and should not be manually modified.
<!-- Start content -->
## [0.0.0-nightly57a9843f2620211119.1](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v0.0.0-nightly57a9843f2620211119.1)
## [0.0.0-nightly58c310891320211216.1](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v0.0.0-nightly58c310891320211216.1)
Fri, 19 Nov 2021 04:18:54 GMT
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.0.0-beta.3..@fluentui/react-context-selector_v0.0.0-nightly57a9843f2620211119.1)
Thu, 16 Dec 2021 04:14:54 GMT
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.0.0-beta.4..@fluentui/react-context-selector_v0.0.0-nightly58c310891320211216.1)
### Changes
- Release nightly v9 ([commit](https://github.com/microsoft/fluentui/commit/e6017db01058700b10f0f2b6b36ef51efb28b9b9) by email not defined)
- Bump @fluentui/react-utilities to v0.0.0-nightly57a9843f2620211119.1 ([commit](https://github.com/microsoft/fluentui/commit/e6017db01058700b10f0f2b6b36ef51efb28b9b9) by beachball)
- Release nightly v9 ([commit](https://github.com/microsoft/fluentui/commit/919f08a1b25cd5045dc729d44a65f64f7838f4dc) by email not defined)
- Bump @fluentui/react-utilities to v0.0.0-nightly58c310891320211216.1 ([commit](https://github.com/microsoft/fluentui/commit/919f08a1b25cd5045dc729d44a65f64f7838f4dc) by beachball)
## [9.0.0-beta.4](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.0.0-beta.4)
Thu, 25 Nov 2021 08:34:13 GMT
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.0.0-beta.3..@fluentui/react-context-selector_v9.0.0-beta.4)
### Changes
- Bump @fluentui/react-utilities to v9.0.0-beta.4 ([PR #20762](https://github.com/microsoft/fluentui/pull/20762) by beachball)
## [9.0.0-beta.3](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.0.0-beta.3)

@@ -18,0 +27,0 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createContext = void 0;
const react_utilities_1 = require("@fluentui/react-utilities");
const React = require("react");
const scheduler_1 = require("scheduler");
const createProvider = (Original) => {
const Provider = props => {
// Holds an actual "props.value"
const valueRef = React.useRef(props.value);
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
const versionRef = React.useRef(0);
// A stable object, is used to avoid context updates via mutation of its values.
const contextValue = React.useRef();
if (!contextValue.current) {
contextValue.current = {
value: valueRef,
version: versionRef,
listeners: [],
};
}
react_utilities_1.useIsomorphicLayoutEffect(() => {
valueRef.current = props.value;
versionRef.current += 1;
scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, () => {
contextValue.current.listeners.forEach(listener => {
listener([versionRef.current, props.value]);
});
});
}, [props.value]);
return React.createElement(Original, { value: contextValue.current }, props.children);
};
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Provider.displayName = 'ContextSelector.Provider';
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
const React = /*#__PURE__*/require("react");
const scheduler_1 = /*#__PURE__*/require("scheduler");
const createProvider = Original => {
const Provider = props => {
// Holds an actual "props.value"
const valueRef = React.useRef(props.value); // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
const versionRef = React.useRef(0); // A stable object, is used to avoid context updates via mutation of its values.
const contextValue = React.useRef();
if (!contextValue.current) {
contextValue.current = {
value: valueRef,
version: versionRef,
listeners: []
};
}
return Provider;
react_utilities_1.useIsomorphicLayoutEffect(() => {
valueRef.current = props.value;
versionRef.current += 1;
scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, () => {
contextValue.current.listeners.forEach(listener => {
listener([versionRef.current, props.value]);
});
});
}, [props.value]);
return React.createElement(Original, {
value: contextValue.current
}, props.children);
};
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Provider.displayName = 'ContextSelector.Provider';
}
return Provider;
};
const createContext = (defaultValue) => {
const context = React.createContext({
value: { current: defaultValue },
version: { current: -1 },
listeners: [],
});
context.Provider = createProvider(context.Provider);
// We don't support Consumer API
delete context.Consumer;
return context;
const createContext = defaultValue => {
const context = React.createContext({
value: {
current: defaultValue
},
version: {
current: -1
},
listeners: []
});
context.Provider = createProvider(context.Provider); // We don't support Consumer API
delete context.Consumer;
return context;
};
exports.createContext = createContext;
//# sourceMappingURL=createContext.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useHasParentContext = exports.useContextSelector = exports.createContext = void 0;
const tslib_1 = require("tslib");
var createContext_1 = require("./createContext");
Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return createContext_1.createContext; } });
var useContextSelector_1 = require("./useContextSelector");
Object.defineProperty(exports, "useContextSelector", { enumerable: true, get: function () { return useContextSelector_1.useContextSelector; } });
var useHasParentContext_1 = require("./useHasParentContext");
Object.defineProperty(exports, "useHasParentContext", { enumerable: true, get: function () { return useHasParentContext_1.useHasParentContext; } });
const tslib_1 = /*#__PURE__*/require("tslib");
var createContext_1 = /*#__PURE__*/require("./createContext");
Object.defineProperty(exports, "createContext", {
enumerable: true,
get: function () {
return createContext_1.createContext;
}
});
var useContextSelector_1 = /*#__PURE__*/require("./useContextSelector");
Object.defineProperty(exports, "useContextSelector", {
enumerable: true,
get: function () {
return useContextSelector_1.useContextSelector;
}
});
var useHasParentContext_1 = /*#__PURE__*/require("./useHasParentContext");
Object.defineProperty(exports, "useHasParentContext", {
enumerable: true,
get: function () {
return useHasParentContext_1.useHasParentContext;
}
});
tslib_1.__exportStar(require("./types"), exports);
//# sourceMappingURL=index.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
//# sourceMappingURL=types.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useContextSelector = void 0;
const react_utilities_1 = require("@fluentui/react-utilities");
const React = require("react");
const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
const React = /*#__PURE__*/require("react");
/**

@@ -11,47 +16,65 @@ * This hook returns context selected value by selector.

*/
const useContextSelector = (context, selector) => {
const contextValue = React.useContext(context);
const { value: { current: value }, version: { current: version }, listeners, } = contextValue;
const selected = selector(value);
const [state, dispatch] = React.useReducer((prevState, payload) => {
if (!payload) {
// early bail out when is dispatched during render
return [value, selected];
}
if (payload[0] <= version) {
if (objectIs(prevState[1], selected)) {
return prevState; // bail out
}
return [value, selected];
}
try {
if (objectIs(prevState[0], payload[1])) {
return prevState; // do not update
}
const nextSelected = selector(payload[1]);
if (objectIs(prevState[1], nextSelected)) {
return prevState; // do not update
}
return [payload[1], nextSelected];
}
catch (e) {
// ignored (stale props or some other reason)
}
// explicitly spread to enforce typing
return [prevState[0], prevState[1]]; // schedule update
}, [value, selected]);
if (!objectIs(state[1], selected)) {
// schedule re-render
// this is safe because it's self contained
dispatch(undefined);
const contextValue = React.useContext(context);
const {
value: {
current: value
},
version: {
current: version
},
listeners
} = contextValue;
const selected = selector(value);
const [state, dispatch] = React.useReducer((prevState, payload) => {
if (!payload) {
// early bail out when is dispatched during render
return [value, selected];
}
react_utilities_1.useIsomorphicLayoutEffect(() => {
listeners.push(dispatch);
return () => {
const index = listeners.indexOf(dispatch);
listeners.splice(index, 1);
};
}, [listeners]);
return state[1];
if (payload[0] <= version) {
if (objectIs(prevState[1], selected)) {
return prevState; // bail out
}
return [value, selected];
}
try {
if (objectIs(prevState[0], payload[1])) {
return prevState; // do not update
}
const nextSelected = selector(payload[1]);
if (objectIs(prevState[1], nextSelected)) {
return prevState; // do not update
}
return [payload[1], nextSelected];
} catch (e) {// ignored (stale props or some other reason)
} // explicitly spread to enforce typing
return [prevState[0], prevState[1]]; // schedule update
}, [value, selected]);
if (!objectIs(state[1], selected)) {
// schedule re-render
// this is safe because it's self contained
dispatch(undefined);
}
react_utilities_1.useIsomorphicLayoutEffect(() => {
listeners.push(dispatch);
return () => {
const index = listeners.indexOf(dispatch);
listeners.splice(index, 1);
};
}, [listeners]);
return state[1];
};
exports.useContextSelector = useContextSelector;

@@ -63,11 +86,12 @@ /**

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function is(x, y) {
return ((x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const objectIs =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
;
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore fallback to native if it exists (not in IE11)
typeof Object.is === 'function' ? Object.is : is;
//# sourceMappingURL=useContextSelector.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useHasParentContext = void 0;
const React = require("react");
const React = /*#__PURE__*/require("react");
/**

@@ -12,10 +16,15 @@ * Utility hook for contexts created by react-context-selector to determine if a parent context exists

*/
function useHasParentContext(context) {
const contextValue = React.useContext(context);
if (contextValue.version) {
return contextValue.version.current !== -1;
}
return false;
const contextValue = React.useContext(context);
if (contextValue.version) {
return contextValue.version.current !== -1;
}
return false;
}
exports.useHasParentContext = useHasParentContext;
//# sourceMappingURL=useHasParentContext.js.map
import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
import * as React from 'react';
import { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';
const createProvider = (Original) => {
const Provider = props => {
// Holds an actual "props.value"
const valueRef = React.useRef(props.value);
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
const versionRef = React.useRef(0);
// A stable object, is used to avoid context updates via mutation of its values.
const contextValue = React.useRef();
if (!contextValue.current) {
contextValue.current = {
value: valueRef,
version: versionRef,
listeners: [],
};
}
useIsomorphicLayoutEffect(() => {
valueRef.current = props.value;
versionRef.current += 1;
runWithPriority(NormalPriority, () => {
contextValue.current.listeners.forEach(listener => {
listener([versionRef.current, props.value]);
});
});
}, [props.value]);
return React.createElement(Original, { value: contextValue.current }, props.children);
};
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Provider.displayName = 'ContextSelector.Provider';
const createProvider = Original => {
const Provider = props => {
// Holds an actual "props.value"
const valueRef = React.useRef(props.value); // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
const versionRef = React.useRef(0); // A stable object, is used to avoid context updates via mutation of its values.
const contextValue = React.useRef();
if (!contextValue.current) {
contextValue.current = {
value: valueRef,
version: versionRef,
listeners: []
};
}
return Provider;
useIsomorphicLayoutEffect(() => {
valueRef.current = props.value;
versionRef.current += 1;
runWithPriority(NormalPriority, () => {
contextValue.current.listeners.forEach(listener => {
listener([versionRef.current, props.value]);
});
});
}, [props.value]);
return /*#__PURE__*/React.createElement(Original, {
value: contextValue.current
}, props.children);
};
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Provider.displayName = 'ContextSelector.Provider';
}
return Provider;
};
export const createContext = (defaultValue) => {
const context = React.createContext({
value: { current: defaultValue },
version: { current: -1 },
listeners: [],
});
context.Provider = createProvider(context.Provider);
// We don't support Consumer API
delete context.Consumer;
return context;
export const createContext = defaultValue => {
const context = /*#__PURE__*/React.createContext({
value: {
current: defaultValue
},
version: {
current: -1
},
listeners: []
});
context.Provider = createProvider(context.Provider); // We don't support Consumer API
delete context.Consumer;
return context;
};
//# sourceMappingURL=createContext.js.map

@@ -8,46 +8,62 @@ import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';

*/
export const useContextSelector = (context, selector) => {
const contextValue = React.useContext(context);
const { value: { current: value }, version: { current: version }, listeners, } = contextValue;
const selected = selector(value);
const [state, dispatch] = React.useReducer((prevState, payload) => {
if (!payload) {
// early bail out when is dispatched during render
return [value, selected];
}
if (payload[0] <= version) {
if (objectIs(prevState[1], selected)) {
return prevState; // bail out
}
return [value, selected];
}
try {
if (objectIs(prevState[0], payload[1])) {
return prevState; // do not update
}
const nextSelected = selector(payload[1]);
if (objectIs(prevState[1], nextSelected)) {
return prevState; // do not update
}
return [payload[1], nextSelected];
}
catch (e) {
// ignored (stale props or some other reason)
}
// explicitly spread to enforce typing
return [prevState[0], prevState[1]]; // schedule update
}, [value, selected]);
if (!objectIs(state[1], selected)) {
// schedule re-render
// this is safe because it's self contained
dispatch(undefined);
const contextValue = React.useContext(context);
const {
value: {
current: value
},
version: {
current: version
},
listeners
} = contextValue;
const selected = selector(value);
const [state, dispatch] = React.useReducer((prevState, payload) => {
if (!payload) {
// early bail out when is dispatched during render
return [value, selected];
}
useIsomorphicLayoutEffect(() => {
listeners.push(dispatch);
return () => {
const index = listeners.indexOf(dispatch);
listeners.splice(index, 1);
};
}, [listeners]);
return state[1];
if (payload[0] <= version) {
if (objectIs(prevState[1], selected)) {
return prevState; // bail out
}
return [value, selected];
}
try {
if (objectIs(prevState[0], payload[1])) {
return prevState; // do not update
}
const nextSelected = selector(payload[1]);
if (objectIs(prevState[1], nextSelected)) {
return prevState; // do not update
}
return [payload[1], nextSelected];
} catch (e) {// ignored (stale props or some other reason)
} // explicitly spread to enforce typing
return [prevState[0], prevState[1]]; // schedule update
}, [value, selected]);
if (!objectIs(state[1], selected)) {
// schedule re-render
// this is safe because it's self contained
dispatch(undefined);
}
useIsomorphicLayoutEffect(() => {
listeners.push(dispatch);
return () => {
const index = listeners.indexOf(dispatch);
listeners.splice(index, 1);
};
}, [listeners]);
return state[1];
};

@@ -59,11 +75,12 @@ /**

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function is(x, y) {
return ((x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const objectIs =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
;
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore fallback to native if it exists (not in IE11)
typeof Object.is === 'function' ? Object.is : is;
//# sourceMappingURL=useContextSelector.js.map

@@ -9,9 +9,12 @@ import * as React from 'react';

*/
export function useHasParentContext(context) {
const contextValue = React.useContext(context);
if (contextValue.version) {
return contextValue.version.current !== -1;
}
return false;
const contextValue = React.useContext(context);
if (contextValue.version) {
return contextValue.version.current !== -1;
}
return false;
}
//# sourceMappingURL=useHasParentContext.js.map
{
"name": "@fluentui/react-context-selector",
"version": "0.0.0-nightly57a9843f2620211119.1",
"version": "0.0.0-nightly58c310891320211216.1",
"description": "React useContextSelector hook in userland",

@@ -22,3 +22,4 @@ "main": "lib-commonjs/index.js",

"docs": "api-extractor run --config=config/api-extractor.local.json --local",
"build:local": "tsc -p . --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output dist/react-context-selector/src && yarn docs"
"build:local": "tsc -p ./tsconfig.lib.json --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output ./dist/packages/react-context-selector/src && yarn docs",
"type-check": "tsc -b tsconfig.json"
},

@@ -38,3 +39,3 @@ "devDependencies": {

"dependencies": {
"@fluentui/react-utilities": "0.0.0-nightly57a9843f2620211119.1",
"@fluentui/react-utilities": "0.0.0-nightly58c310891320211216.1",
"scheduler": "^0.20.1",

@@ -41,0 +42,0 @@ "tslib": "^2.1.0"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc