UNPKG

use-context-selector

Version:
3 lines (2 loc) 2.7 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react"),require("scheduler"),require("react-native")):"function"==typeof define&&define.amd?define(["exports","react","scheduler","react-native"],r):r((e=e||self).useContextSelector={},e.react,e.scheduler,e.reactNative)}(this,function(e,r,t,n){function o(){return(o=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var u=Symbol(),c=Symbol(),i="undefined"==typeof window||/ServerSideRendering/.test(window.navigator&&window.navigator.userAgent)?r.useEffect:r.useLayoutEffect,s=function(e){return e};function a(e,t){var n=r.useContext(e)[u];if("object"==typeof process&&"production"!==process.env.NODE_ENV&&!n)throw new Error("useContextSelector requires special context");var c=n.v.current,s=n.n.current,a=n.l,f=t(c),l=r.useReducer(function(e,r){if(s<r[0]){try{if(2===r.length&&(Object.is(e.value,r[1])||Object.is(e.selected,t(r[1]))))return e}catch(e){}return o({},e)}return Object.is(e.value,c)||Object.is(e.selected,f)?e:{value:c,selected:f}},{value:c,selected:f}),d=l[0],v=l[1];return i(function(){return a.add(v),function(){a.delete(v)}},[a]),d.selected}e.BridgeProvider=function(e){var t=e.value,n=e.children,o=e.context[c];if("object"==typeof process&&"production"!==process.env.NODE_ENV&&!o)throw new Error("BridgeProvider requires special context");return r.createElement(o,{value:t},n)},e.createContext=function(e){var o,s,a=r.createContext(((o={})[u]={v:{current:e},n:{current:-1},l:new Set,u:function(e){return e()}},o));return a[c]=a.Provider,a.Provider=(s=a.Provider,function(e){var o=e.value,c=e.children,a=r.useRef(o),f=r.useRef(0),l=r.useRef();if(!l.current){var d,v=new Set;l.current=((d={})[u]={v:a,n:f,l:v,u:function(e){n.unstable_batchedUpdates(function(){f.current+=1,v.forEach(function(e){return e([f.current])}),e()})}},d)}return i(function(){a.current=o,f.current+=1,t.unstable_runWithPriority(t.unstable_NormalPriority,function(){l.current[u].l.forEach(function(e){e([f.current,o])})})},[o]),r.createElement(s,{value:l.current},c)}),delete a.Consumer,a},e.useBridgeValue=function(e){var t=r.useContext(e);if("object"==typeof process&&"production"!==process.env.NODE_ENV&&!t[u])throw new Error("useBridgeValue requires special context");return t},e.useContext=function(e){return a(e,s)},e.useContextSelector=a,e.useContextUpdate=function(e){var t=r.useContext(e)[u];if("object"==typeof process&&"production"!==process.env.NODE_ENV&&!t)throw new Error("useContextUpdate requires special context");return t.u}}); //# sourceMappingURL=index.native.umd.js.map