UNPKG

preact-scoped-model

Version:
35 lines (34 loc) 6.75 kB
"use strict"; var O=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var me=Object.getOwnPropertyNames,R=Object.getOwnPropertySymbols;var L=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable;var q=(o,e,t)=>e in o?O(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,J=(o,e)=>{for(var t in e||(e={}))L.call(e,t)&&q(o,t,e[t]);if(R)for(var t of R(e))G.call(e,t)&&q(o,t,e[t]);return o};var K=(o,e)=>{var t={};for(var r in o)L.call(o,r)&&e.indexOf(r)<0&&(t[r]=o[r]);if(o!=null&&R)for(var r of R(o))e.indexOf(r)<0&&G.call(o,r)&&(t[r]=o[r]);return t};var fe=(o,e)=>{for(var t in e)O(o,t,{get:e[t],enumerable:!0})},Se=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of me(e))!L.call(o,l)&&l!==t&&O(o,l,{get:()=>e[l],enumerable:!(r=ue(e,l))||r.enumerable});return o};var Me=o=>Se(O({},"__esModule",{value:!0}),o);var Te={};fe(Te,{createNullaryModel:()=>u,createPropsSelectorModel:()=>w,createReducerModel:()=>k,createSelector:()=>E,createSelectorOnce:()=>B,createSelectors:()=>D,createSnapshot:()=>j,createStateModel:()=>U,createValue:()=>I,createValueOnce:()=>H,default:()=>ye,useScopedModelExists:()=>z,useSelector:()=>m,useSelectorOnce:()=>h,useSelectors:()=>T,useSnapshot:()=>b,useValue:()=>N,useValueOnce:()=>f});module.exports=Me(Te);var y=require("preact"),S=require("preact/hooks"),W=require("preact/compat"),X=require("@lyonph/preact-hooks");var P=class{constructor(){this.alive=!0;this.initialized=!1;this.listeners=new Set}subscribe(e){return this.alive&&this.listeners.add(e),()=>{this.alive&&this.listeners.delete(e)}}consume(e){if(this.alive){this.ref={current:e};for(let t of this.listeners.keys())t(e)}}hydrate(e){this.alive&&!this.initialized&&(this.ref={current:e})}hasValue(){return!!this.ref}get value(){if(this.ref==null)throw new Error("Unexpected missing model reference.");return this.ref.current}destroy(){this.alive&&(this.listeners.clear(),this.alive=!1)}};var Q=0;function A(){let o=Q;return Q+=1,o}var i=class extends Error{constructor(t){super(` The scoped model '${t}' is missing from the ancestor component tree. Make sure that the model's Provider is mounted first before trying to access the model's state. `);this.modelName=t}};function xe(o,e){return!Object.is(o,e)}function c(o,e={}){var Y;let t=(0,y.createContext)(null),r=A(),l=e.displayName||`ScopedModel-${r}`,p=(Y=e.shouldNotify)!=null?Y:xe,C=(0,W.memo)(F=>{let n=(0,S.useContext)(t);if(!n)throw new i(l);let M=o(F);return n.hydrate(M),(0,S.useEffect)(()=>{n.initialized=!0},[n]),(0,S.useEffect)(()=>{let x=n.hasValue();(x&&p(n.value,M)||!x)&&n.consume(M)},[n,M]),null},e.shouldUpdate),_=M=>{var x=M,{children:F}=x,n=K(x,["children"]);let g=(0,X.useConstant)(()=>new P);return(0,S.useEffect)(()=>()=>{g.destroy()},[g]),(0,y.h)(t.Provider,{value:g},(0,y.h)(C,J({},n)),F)};return _.defaultProps=e.defaultProps,{context:t,Provider:_,displayName:l}}var Pe=()=>!0;function u(o,e){return c(o,{displayName:e==null?void 0:e.displayName,shouldUpdate:Pe})}var Z=require("preact/hooks");function U(o,e){return u(()=>(0,Z.useState)(o),e)}var ee=require("preact/hooks");function k(o,e,t){return u(()=>(0,ee.useReducer)(o,e),t)}function w(o){return c(e=>e,o)}function d(o,e){return!Object.is(o,e)}function oe(o,e,t=d){if(o===e)return!1;if(o.length!==e.length)return!0;for(let r=0;r<o.length;r+=1)if(t(o[r],e[r]))return!0;return!1}var re=require("preact/hooks"),v=require("@lyonph/preact-hooks");var te=require("preact/hooks");function a(o){let e=(0,te.useContext)(o.context);if(!e)throw new i(o.displayName);return e}function m(o,e,t=d){let r=a(o),l=(0,v.useConditionalMemo)(()=>({read:()=>e(r.value),subscribe:s=>r.subscribe(s),shouldUpdate:t}),{notifier:r,shouldUpdate:t},(s,C)=>s.notifier!==C.notifier||s.shouldUpdate!==C.shouldUpdate),p=(0,v.useSubscription)(l);return(0,re.useDebugValue)(p),p}function E(o,e,t=d){return()=>m(o,e,t)}var le=require("preact/hooks"),pe=require("@lyonph/preact-hooks");function T(o,e,t=d){let r=(0,pe.useConditionalCallback)((p,s)=>oe(p,s,t),t),l=m(o,e,r);return(0,le.useDebugValue)(l),l}function D(o,e,t=d){return()=>T(o,e,t)}var de=require("preact/hooks"),V=require("@lyonph/preact-hooks");function N(o,e=d){let t=a(o),r=(0,V.useConditionalMemo)(()=>({read:()=>t.value,subscribe:p=>t.subscribe(p),shouldUpdate:e}),{notifier:t,shouldUpdate:e},(p,s)=>p.notifier!==s.notifier||p.shouldUpdate!==s.shouldUpdate),l=(0,V.useSubscription)(r);return(0,de.useDebugValue)(l),l}function I(o,e=d){return()=>N(o,e)}var se=require("preact/hooks");function f(o){let{value:e}=a(o);return(0,se.useDebugValue)(e),e}function H(o){return()=>f(o)}var ne=require("preact/hooks");function $(o,e){(0,ne.useEffect)(()=>{let t=!0,r=p=>{t&&e(p)},l=o.subscribe(r);return()=>{t=!1,l()}},[o,e])}function b(o,e){let t=a(o);$(t,e)}function j(o,e){return()=>{b(o,e)}}var ae=require("preact/hooks"),ie=require("@lyonph/preact-hooks");function h(o,e){let t=f(o),r=(0,ie.useConditionalMemo)(()=>e(t),{baseValue:t,selector:e},(l,p)=>l.baseValue!==p.baseValue||l.selector!==p.selector);return(0,ae.useDebugValue)(r),r}function B(o,e){return()=>h(o,e)}var ce=require("preact/hooks");function z(o){return!!(0,ce.useContext)(o.context)}var ye=c;0&&(module.exports={createNullaryModel,createPropsSelectorModel,createReducerModel,createSelector,createSelectorOnce,createSelectors,createSnapshot,createStateModel,createValue,createValueOnce,useScopedModelExists,useSelector,useSelectorOnce,useSelectors,useSnapshot,useValue,useValueOnce}); /** * @license * MIT License * * Copyright (c) 2021 Alexis Munsayac * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * @author Alexis Munsayac <alexis.munsayac@gmail.com> * @copyright Alexis Munsayac 2021 */ //# sourceMappingURL=index.js.map