madoi-client-react
Version:
A React Utilities for Madoi Client
158 lines (157 loc) • 4.22 kB
JavaScript
var M = Object.defineProperty;
var j = (e, t, n) => t in e ? M(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
var S = (e, t, n) => j(e, typeof t != "symbol" ? t + "" : t, n);
import { useRef as d, useState as h, useEffect as O } from "react";
function C(e = {}) {
return (t) => {
t.madoiClassConfig_ = e;
};
}
const P = {
type: "beforeExec",
maxLog: 0,
allowedTo: ["USER"]
};
function w(e = P) {
const t = e;
return t.type || (t.type = "beforeExec"), t.maxLog || (t.maxLog = 0), (n, r, a) => {
const f = { share: t };
n[r].madoiMethodConfig_ = f;
};
}
const E = {
maxInterval: 5e3
};
function v(e = E) {
const t = e;
return (n, r, a) => {
const f = { getState: t };
n[r].madoiMethodConfig_ = f;
};
}
function x(e = {}) {
const t = e;
return (n, r, a) => {
const f = { setState: t };
n[r].madoiMethodConfig_ = f;
};
}
var L = Object.defineProperty, N = Object.getOwnPropertyDescriptor, m = (e, t, n, r) => {
for (var a = r > 1 ? void 0 : r ? N(t, n) : t, f = e.length - 1, p; f >= 0; f--)
(p = e[f]) && (a = (r ? p(t, n, a) : p(a)) || a);
return r && a && L(t, n, a), a;
};
function _(e) {
return typeof e == "function" ? e() : e;
}
function R(e, t) {
return typeof t == "function" ? t(e) : t;
}
let g = class {
constructor(e) {
S(this, "state");
this.state = e;
}
updateState(e) {
this.state = e;
}
setState(e) {
this.state = e;
}
getState() {
return this.state;
}
};
m([
w()
], g.prototype, "updateState", 1);
m([
x()
], g.prototype, "setState", 1);
m([
v()
], g.prototype, "getState", 1);
g = m([
C({ className: "State" })
], g);
function A(e, t) {
var p;
const n = d(null), r = d(null), [a, f] = h();
return n.current === null && (n.current = _(t)), O(() => {
if (r.current !== null) return;
const o = new g(n.current);
r.current = o;
let u = null;
for (let s of Object.getOwnPropertyNames(Object.getPrototypeOf(o))) {
const i = o[s].madoiMethodConfig_;
i && i.getState && (u = o[s]);
}
if (u == null)
throw new Error(`${typeof o} must declare @GetState method.`);
for (let s of Object.getOwnPropertyNames(Object.getPrototypeOf(o))) {
const i = o[s].madoiMethodConfig_;
if (i) {
if (i.share) {
const l = o[s], c = function() {
l.apply(o, arguments), f(u.apply(o));
};
c.madoiMethodConfig_ = i, o[s] = c;
} else if (i.setState) {
const l = o[s], c = function() {
l.apply(o, arguments), f(u.apply(o));
};
c.madoiMethodConfig_ = i, o[s] = c;
}
}
}
e.register(o);
}, []), [
((p = r.current) == null ? void 0 : p.getState()) || n.current,
(o) => {
var u, s;
(s = r.current) == null || s.updateState(
R((u = r.current) == null ? void 0 : u.getState(), o)
);
}
];
}
function U(e, t, n = !0) {
const r = d(null), a = d(!1), [f, p] = h();
return r.current === null && (r.current = _(t)), O(() => {
var s, i;
if (a.current) return;
const o = r.current;
let u = null;
for (let l of Object.getOwnPropertyNames(Object.getPrototypeOf(o))) {
const c = (s = o[l]) == null ? void 0 : s.madoiMethodConfig_;
c && c.getState && (u = o[l]);
}
for (let l of Object.getOwnPropertyNames(Object.getPrototypeOf(o))) {
const c = (i = o[l]) == null ? void 0 : i.madoiMethodConfig_;
if (c && (c.share || c.setState || c.enterRoomAllowed || c.leaveRoomDone || c.peerEntered || c.peerProfileUpdated || c.peerLeaved)) {
const b = o[l], y = function() {
b.apply(o, arguments), n && p(u ? u.apply(o) : new Object());
};
y.madoiMethodConfig_ = c, o[l] = y;
}
}
e.register(o), a.current = !0;
}, []), r.current;
}
function B(e, t) {
for (const n of Object.keys(t))
e.addEventListener(n, t[n]);
return () => {
for (const n of Object.keys(t))
e.removeEventListener(n, t[n]);
};
}
function F(...e) {
return () => e.forEach((t) => t());
}
export {
F as bundleCleanups,
B as eventListnersEffect,
U as useSharedModel,
A as useSharedState
};