1 line
9.2 KiB
JSON
1 line
9.2 KiB
JSON
|
{"ast":null,"code":"import _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\nimport useEvent from \"rc-util/es/hooks/useEvent\";\nimport useLayoutEffect from \"rc-util/es/hooks/useLayoutEffect\";\nimport isEqual from \"rc-util/es/isEqual\";\nimport * as React from 'react';\nimport { unstable_batchedUpdates } from 'react-dom';\nexport function createContext(defaultValue) {\n var Context = /*#__PURE__*/React.createContext(undefined);\n var Provider = function Provider(_ref) {\n var value = _ref.value,\n children = _ref.children;\n var valueRef = React.useRef(value);\n valueRef.current = value;\n var _React$useState = React.useState(function () {\n return {\n getValue: function getValue() {\n return valueRef.current;\n },\n listeners: new Set()\n };\n }),\n _React$useState2 = _slicedToArray(_React$useState, 1),\n context = _React$useState2[0];\n useLayoutEffect(function () {\n unstable_batchedUpdates(function () {\n context.listeners.forEach(function (listener) {\n listener(value);\n });\n });\n }, [value]);\n return /*#__PURE__*/React.createElement(Context.Provider, {\n value: context\n }, children);\n };\n return {\n Context: Context,\n Provider: Provider,\n defaultValue: defaultValue\n };\n}\n\n/** e.g. useSelect(userContext) => user */\n\n/** e.g. useSelect(userContext, user => user.name) => user.name */\n\n/** e.g. useSelect(userContext, ['name', 'age']) => user { name, age } */\n\n/** e.g. useSelect(userContext, 'name') => user.name */\n\nexport function useContext(holder, selector) {\n var eventSelector = useEvent(typeof selector === 'function' ? selector : function (ctx) {\n if (selector === undefined) {\n return ctx;\n }\n if (!Array.isArray(selector)) {\n return ctx[selector];\n }\n var obj = {};\n selector.forEach(function (key) {\n obj[key] = ctx[key];\n });\n return obj;\n });\n var context = React.useContext(holder === null || holder === void 0 ? void 0 : holder.Context);\n var _ref2 = context || {},\n listeners = _ref2.listeners,\n getValue = _ref2.getValue;\n var valueRef = React.useRef();\n valueRef.current = eventSelector(context ? getValue() : holder === null || holder === void 0 ? void 0 : holder.defaultValue);\n var _React$useState3 = React.useState({}),\n _React$useState4 = _slicedToArray(_React$useState3, 2),\n forceUpdate = _React$useState4[1];\n useLayoutEffect(function () {\n if (!context) {\n return;\n }\n function trigger(nextValue) {\n var nextSelectorValue = eventSelector(nextValue);\n if (!isEqual(valueRef.current, nextSelectorValue, true)) {\n forceUpdate({});\n }\n }\n listeners.add(trigger);\n return function () {\n listeners.delete(trigger);\n };\n }, [context]);\n return valueRef.current;\n}","map":{"version":3,"names":["_slicedToArray","useEvent","useLayoutEffect","isEqual","React","unstable_batchedUpdates","createContext","defaultValue","Context","undefined","Provider","_ref","value","children","valueRef","useRef","current","_React$useState","useState","getValue","listeners","Set","_React$useState2","context","forEach","listener","createElement","useContext","holder","selector","eventSelector","ctx","Array","isArray","obj","key","_ref2","_React$useState3","_React$useState4","forceUpdate","trigger","nextValue","nextSelectorValue","add","delete"],"sources":["C:/Users/Аришина)/source/repos/PromoCursed/node_modules/@rc-component/context/es/context.js"],"sourcesContent":["import _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\nimport useEvent from \"rc-util/es/hooks/useEvent\";\nimport useLayoutEffect from \"rc-util/es/hooks/useLayoutEffect\";\nimport isEqual from \"rc-util/es/isEqual\";\nimport * as React from 'react';\nimport { unstable_batchedUpdates } from 'react-dom';\nexport function createContext(defaultValue) {\n var Context = /*#__PURE__*/React.createContext(undefined);\n var Provi
|