madoi-client-react
Version:
A React Utilities for Madoi Client
2 lines (1 loc) • 3.58 kB
JavaScript
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i["madoi-react"]={},i.React))})(this,function(i,o){"use strict";var q=Object.defineProperty;var x=(i,o,p)=>o in i?q(i,o,{enumerable:!0,configurable:!0,writable:!0,value:p}):i[o]=p;var _=(i,o,p)=>x(i,typeof o!="symbol"?o+"":o,p);function p(e){return(n,r,f)=>{n[r].madoiMethodConfig_={...n[r].madoiMethodConfig_?n[r].madoiMethodConfig_:{},...e}}}function M(e){return n=>{n.madoiClassConfig_={className:e}}}const j={serialized:!0};function R(e=j){return p({distributed:e})}function C(){return p({changeState:{}})}const P={maxInterval:5e3,minInterval:3e3};function v(e=P){return p({getState:e})}function w(){return p({setState:{}})}var E=Object.defineProperty,N=Object.getOwnPropertyDescriptor,h=(e,n,r,f)=>{for(var c=f>1?void 0:f?N(n,r):n,S=e.length-1,l;S>=0;S--)(l=e[S])&&(c=(f?l(n,r,c):l(c))||c);return f&&c&&E(n,r,c),c};function L(){return(e,n,r)=>{e[n].madoiReactSuppressRender_={}}}function y(e){return typeof e=="function"?e():e}function D(e,n){return typeof n=="function"?n(e):n}let m=class{constructor(e){_(this,"state");this.state=e}updateState(e){this.state=e}setState(e){this.state=e}getState(){return this.state}};h([R(),C()],m.prototype,"updateState",1),h([w()],m.prototype,"setState",1),h([v()],m.prototype,"getState",1),m=h([M("MadoiReactInnerState")],m);function I(e,n){var l;const r=o.useRef(null),f=o.useRef(null),[c,S]=o.useState();return r.current===null&&(r.current=y(n)),o.useEffect(()=>{if(f.current!==null)return;const t=new m(r.current);f.current=t;let d=null;for(let u of Object.getOwnPropertyNames(Object.getPrototypeOf(t))){const s=t[u].madoiMethodConfig_;s&&s.getState&&(d=t[u])}if(d==null)throw new Error(`${typeof t} must declare @GetState method.`);for(let u of Object.getOwnPropertyNames(Object.getPrototypeOf(t))){const s=t[u].madoiMethodConfig_;if(s){if(s.changeState){const a=t[u],g=function(){a.apply(t,arguments),S(d.apply(t))};g.madoiMethodConfig_=s,t[u]=g}else if(s.setState){const a=t[u],g=function(){a.apply(t,arguments),S(d.apply(t))};g.madoiMethodConfig_=s,t[u]=g}}}e.register(t)},[]),[((l=f.current)==null?void 0:l.getState())||r.current,t=>{var d,u;(u=f.current)==null||u.updateState(D((d=f.current)==null?void 0:d.getState(),t))}]}function T(e,n,r=!0){const f=o.useRef(null),c=o.useRef(!1),[S,l]=o.useState();return f.current===null&&(f.current=y(n)),o.useEffect(()=>{if(c.current)return;const t=f.current;let d=null;for(let u of Object.getOwnPropertyNames(Object.getPrototypeOf(t))){const s=t[u].madoiMethodConfig_;s&&s.getState&&(d=t[u])}for(let u of Object.getOwnPropertyNames(Object.getPrototypeOf(t))){let s=t[u].madoiReactSuppressRender_;typeof s>"u"&&(s=!1);const a=t[u].madoiMethodConfig_;if(a&&(a.distributed||a.changeState||a.setState||a.enterRoomAllowed||a.leaveRoomDone||a.peerEntered||a.peerProfileUpdated||a.peerLeaved)){const g=t[u],O=function(){let b={changed:!s};g.apply(t,[...arguments,b]),r&&b.changed&&(console.debug("[madoi-react] fire render for",t,g.name,s),l(d?d.apply(t):new Object))};O.madoiMethodConfig_=a,t[u]=O}}e.register(t),c.current=!0},[]),f.current}function V(e,n){for(const r of Object.keys(n))e.addEventListener(r,n[r]);return()=>{for(const r of Object.keys(n))e.removeEventListener(r,n[r])}}function A(...e){return()=>e.forEach(n=>n())}i.SuppressRender=L,i.bundleCleanups=A,i.eventListnersEffect=V,i.useSharedModel=T,i.useSharedState=I,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});