@frui.ts/views
Advanced tools
Comparing version 1.0.0-rc.1 to 1.0.0-rc.2
var C = Object.defineProperty; | ||
var M = (t, e, n) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n; | ||
var l = (t, e, n) => (M(t, typeof e != "symbol" ? e + "" : e, n), n), y = (t, e, n) => { | ||
var f = (t, e, n) => (M(t, typeof e != "symbol" ? e + "" : e, n), n), y = (t, e, n) => { | ||
if (!e.has(t)) | ||
throw TypeError("Cannot " + n); | ||
}; | ||
var o = (t, e, n) => (y(t, e, "read from private field"), n ? n.call(t) : e.get(t)), h = (t, e, n) => { | ||
var c = (t, e, n) => (y(t, e, "read from private field"), n ? n.call(t) : e.get(t)), h = (t, e, n) => { | ||
if (e.has(t)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
e instanceof WeakSet ? e.add(t) : e.set(t, n); | ||
}, m = (t, e, n, r) => (y(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n); | ||
}, m = (t, e, n, i) => (y(t, e, "write to private field"), i ? i.call(t, n) : e.set(t, n), n); | ||
import { isObservableMap as B, isObservable as N, isObservableProp as T, action as w, get as j, makeObservable as F } from "mobx"; | ||
import s, { useRef as E, useEffect as v } from "react"; | ||
import { isMap as L, ensureObservableProperty as I } from "@frui.ts/helpers"; | ||
import s, { useEffect as v, useRef as E, useState as L } from "react"; | ||
import { isMap as S, ensureObservableProperty as I } from "@frui.ts/helpers"; | ||
import { memoize as O } from "lodash-es"; | ||
import { Mutex as $ } from "async-mutex"; | ||
import { Route as H, RootRoute as S, Outlet as W } from "@tanstack/react-router"; | ||
import { observer as G } from "mobx-react-lite"; | ||
function U(t) { | ||
const { target: e, property: n, onValueChanged: r, ...i } = t; | ||
return i; | ||
import { Route as H, RootRoute as W, Outlet as G } from "@tanstack/react-router"; | ||
import { observer as U } from "mobx-react-lite"; | ||
function X(t) { | ||
const { target: e, property: n, onValueChanged: i, ...r } = t; | ||
return r; | ||
} | ||
@@ -31,10 +31,10 @@ function _(t, e, n = !0) { | ||
if (!N(t) || !T(t, e)) { | ||
if (L(t)) | ||
if (S(t)) | ||
return t.get(e); | ||
{ | ||
const r = t[e]; | ||
const i = t[e]; | ||
if (n) | ||
w(I)(t, e, r); | ||
w(I)(t, e, i); | ||
else | ||
return r; | ||
return i; | ||
} | ||
@@ -44,18 +44,18 @@ } | ||
} | ||
function P(t, e, n) { | ||
function z(t, e, n) { | ||
t && e && w(I)(t, e, n); | ||
} | ||
function ue(t) { | ||
return [_(t.target, t.property), (r) => { | ||
return [_(t.target, t.property), (i) => { | ||
var a; | ||
const i = r; | ||
P(t.target, t.property, i), t.target && t.property && ((a = t.onValueChanged) == null || a.call(t, i, t.property, t.target)); | ||
const r = i; | ||
z(t.target, t.property, r), t.target && t.property && ((a = t.onValueChanged) == null || a.call(t, r, t.property, t.target)); | ||
}]; | ||
} | ||
var X = Object.defineProperty, k = Object.getOwnPropertyDescriptor, q = (t, e, n, r) => { | ||
for (var i = r > 1 ? void 0 : r ? k(e, n) : e, a = t.length - 1, d; a >= 0; a--) | ||
(d = t[a]) && (i = (r ? d(e, n, i) : d(i)) || i); | ||
return r && i && X(e, n, i), i; | ||
var k = Object.defineProperty, q = Object.getOwnPropertyDescriptor, J = (t, e, n, i) => { | ||
for (var r = i > 1 ? void 0 : i ? q(e, n) : e, a = t.length - 1, o; a >= 0; a--) | ||
(o = t[a]) && (r = (i ? o(e, n, r) : o(r)) || r); | ||
return i && r && k(e, n, r), r; | ||
}; | ||
class J extends s.Component { | ||
class K extends s.Component { | ||
constructor(e) { | ||
@@ -75,3 +75,3 @@ super(e), F(this); | ||
get inheritedProps() { | ||
return U(this.props); | ||
return X(this.props); | ||
} | ||
@@ -87,14 +87,14 @@ /** Returns value of the bound property */ | ||
setValue(e) { | ||
const { target: n, property: r, onValueChanged: i } = this.props, a = e; | ||
P(n, r, a), i == null || i(a, r, n); | ||
const { target: n, property: i, onValueChanged: r } = this.props, a = e; | ||
z(n, i, a), r == null || r(a, i, n); | ||
} | ||
} | ||
q([ | ||
J([ | ||
w.bound | ||
], J.prototype, "setValue", 1); | ||
function le(t, e) { | ||
], K.prototype, "setValue", 1); | ||
function fe(t, e) { | ||
const n = t; | ||
n.defaultProps ? Object.assign(n.defaultProps, e) : n.defaultProps = e; | ||
} | ||
function K(t) { | ||
function Q(t) { | ||
return (e) => { | ||
@@ -105,10 +105,10 @@ const n = e.currentTarget.dataset.id; | ||
} | ||
const fe = O(K); | ||
function de(t) { | ||
return O((n) => (r) => { | ||
const i = r.currentTarget.dataset[t]; | ||
i !== void 0 && n(i); | ||
const de = O(Q); | ||
function he(t) { | ||
return O((n) => (i) => { | ||
const r = i.currentTarget.dataset[t]; | ||
r !== void 0 && n(r); | ||
}); | ||
} | ||
function he(t) { | ||
function me(t) { | ||
return function(e) { | ||
@@ -118,13 +118,13 @@ e.preventDefault(), e.stopPropagation(), t(); | ||
} | ||
var u, c; | ||
class z { | ||
var u, l; | ||
class P { | ||
constructor(e) { | ||
h(this, u, void 0); | ||
h(this, c, new $()); | ||
l(this, "_isInitialized", !1); | ||
l(this, "_isActive", !1); | ||
h(this, l, new $()); | ||
f(this, "_isInitialized", !1); | ||
f(this, "_isActive", !1); | ||
this.factory = e; | ||
} | ||
get instance() { | ||
return o(this, u) ?? m(this, u, this.factory()), o(this, u); | ||
return c(this, u) ?? m(this, u, this.factory()), c(this, u); | ||
} | ||
@@ -135,3 +135,3 @@ resetInstance() { | ||
async initialize(e) { | ||
await o(this, c).runExclusive(async () => { | ||
await c(this, l).runExclusive(async () => { | ||
await this.callInitialize(e); | ||
@@ -141,7 +141,7 @@ }); | ||
async callInitialize(e) { | ||
var n, r; | ||
this._isInitialized || (await ((r = (n = this.instance).onInitialize) == null ? void 0 : r.call(n, e)), this._isInitialized = !0); | ||
var n, i; | ||
this._isInitialized || (await ((i = (n = this.instance).onInitialize) == null ? void 0 : i.call(n, e)), this._isInitialized = !0); | ||
} | ||
async activate(e) { | ||
await o(this, c).runExclusive(async () => { | ||
await c(this, l).runExclusive(async () => { | ||
await this.callInitialize(e), await this.callActivate(e); | ||
@@ -151,7 +151,7 @@ }); | ||
async callActivate(e) { | ||
var n, r; | ||
this._isActive || (await ((r = (n = this.instance).onActivate) == null ? void 0 : r.call(n, e)), this._isActive = !0); | ||
var n, i; | ||
this._isActive || (await ((i = (n = this.instance).onActivate) == null ? void 0 : i.call(n, e)), this._isActive = !0); | ||
} | ||
async navigate(e) { | ||
await o(this, c).runExclusive(async () => { | ||
await c(this, l).runExclusive(async () => { | ||
await this.callInitialize(e), await this.callActivate(e), await this.callNavigate(e); | ||
@@ -161,7 +161,7 @@ }); | ||
async callNavigate(e) { | ||
var n, r; | ||
await ((r = (n = this.instance).onNavigate) == null ? void 0 : r.call(n, e)); | ||
var n, i; | ||
await ((i = (n = this.instance).onNavigate) == null ? void 0 : i.call(n, e)); | ||
} | ||
async deactivate(e) { | ||
await o(this, c).runExclusive(async () => { | ||
await c(this, l).runExclusive(async () => { | ||
await this.callDeactivate(e); | ||
@@ -171,7 +171,7 @@ }); | ||
async callDeactivate(e) { | ||
var n, r; | ||
this._isActive && (await ((r = (n = this.instance).onDeactivate) == null ? void 0 : r.call(n, e)), this._isActive = !1); | ||
var n, i; | ||
this._isActive && (await ((i = (n = this.instance).onDeactivate) == null ? void 0 : i.call(n, e)), this._isActive = !1); | ||
} | ||
async close(e) { | ||
await o(this, c).runExclusive(async () => { | ||
await c(this, l).runExclusive(async () => { | ||
await this.callDeactivate(e), this.resetInstance(); | ||
@@ -181,10 +181,18 @@ }); | ||
} | ||
u = new WeakMap(), c = new WeakMap(); | ||
function me(t, e, n) { | ||
const r = E(new z(t)), i = E(e); | ||
return i.current = e, v(() => (r.current.initialize(i.current), () => { | ||
r.current.close(i.current); | ||
u = new WeakMap(), l = new WeakMap(); | ||
function ve(...t) { | ||
v( | ||
() => () => { | ||
t.forEach((e) => e.dispose()); | ||
}, | ||
[] | ||
); | ||
} | ||
function we(t, e, n) { | ||
const i = E(new P(t)), [r, a] = L(!1), o = E(e); | ||
return o.current = e, v(() => (i.current.initialize(o.current).then(() => a(!0)), () => { | ||
i.current.close(o.current); | ||
}), []), v(() => { | ||
r.current.navigate(i.current); | ||
}, n ?? [e]), r.current.instance; | ||
i.current.navigate(o.current); | ||
}, n ?? [e]), { vm: i.current.instance, initialized: r }; | ||
} | ||
@@ -195,11 +203,11 @@ const b = Object.freeze({ | ||
}); | ||
class Q extends s.PureComponent { | ||
class Y extends s.PureComponent { | ||
constructor() { | ||
super(...arguments); | ||
l(this, "state", b); | ||
l(this, "resetErrorBoundary", () => { | ||
f(this, "state", b); | ||
f(this, "resetErrorBoundary", () => { | ||
const { onReset: n } = this.props; | ||
if (n) { | ||
const { error: r, errorInfo: i } = this.state; | ||
n(r, i); | ||
const { error: i, errorInfo: r } = this.state; | ||
n(i, r); | ||
} | ||
@@ -209,9 +217,9 @@ this.setState(b); | ||
} | ||
componentDidCatch(n, r) { | ||
var i, a; | ||
(a = (i = this.props).onError) == null || a.call(i, n, r), this.setState({ error: n, errorInfo: r }); | ||
componentDidCatch(n, i) { | ||
var r, a; | ||
(a = (r = this.props).onError) == null || a.call(r, n, i), this.setState({ error: n, errorInfo: i }); | ||
} | ||
render() { | ||
if (this.state.error) { | ||
const { error: n, errorInfo: r } = this.state, { fallback: i } = this.props, a = typeof i == "function" ? i({ error: n, errorInfo: r, resetError: this.resetErrorBoundary }) : i; | ||
const { error: n, errorInfo: i } = this.state, { fallback: r } = this.props, a = typeof r == "function" ? r({ error: n, errorInfo: i, resetError: this.resetErrorBoundary }) : r; | ||
return a || /* @__PURE__ */ s.createElement("p", null, "Something went wrong :-("); | ||
@@ -222,28 +230,28 @@ } | ||
} | ||
const p = "default", f = /* @__PURE__ */ new Map(); | ||
function Y(t, e, n = p) { | ||
const r = f.get(e); | ||
return r ? r[n] = t : f.set(e, { [n]: t }), t; | ||
const p = "default", d = /* @__PURE__ */ new Map(); | ||
function Z(t, e, n = p) { | ||
const i = d.get(e); | ||
return i ? i[n] = t : d.set(e, { [n]: t }), t; | ||
} | ||
function Z(t, e = p) { | ||
const n = f.get(t); | ||
function x(t, e = p) { | ||
const n = d.get(t); | ||
if (!n) | ||
throw new Error("No view has been registered for view model " + t.name); | ||
const r = n[e]; | ||
if (!r) | ||
const i = n[e]; | ||
if (!i) | ||
throw new Error(`No view has been registered for view model ${t.name} and context ${e}`); | ||
return r; | ||
return i; | ||
} | ||
function x(t, e = p) { | ||
const n = f.get(t); | ||
function ee(t, e = p) { | ||
const n = d.get(t); | ||
if (!n) | ||
return null; | ||
const r = n[e]; | ||
return r || null; | ||
const i = n[e]; | ||
return i || null; | ||
} | ||
const g = (t) => { | ||
const { vm: e, children: n, context: r } = t; | ||
const { vm: e, children: n, context: i } = t; | ||
if (!e) | ||
return /* @__PURE__ */ s.createElement(s.Fragment, null, n); | ||
const i = e.constructor, a = n === void 0 ? Z(i, r) : x(i, r); | ||
const r = e.constructor, a = n === void 0 ? x(r, i) : ee(r, i); | ||
return a ? /* @__PURE__ */ s.createElement(a, { vm: e }) : /* @__PURE__ */ s.createElement(s.Fragment, null, n); | ||
@@ -253,4 +261,4 @@ }; | ||
const D = (t) => { | ||
const { onError: e, onReset: n, fallback: r, ...i } = t; | ||
return /* @__PURE__ */ s.createElement(Q, { onError: e, onReset: n, fallback: r }, /* @__PURE__ */ s.createElement(g, { ...i })); | ||
const { onError: e, onReset: n, fallback: i, ...r } = t; | ||
return /* @__PURE__ */ s.createElement(Y, { onError: e, onReset: n, fallback: i }, /* @__PURE__ */ s.createElement(g, { ...r })); | ||
}; | ||
@@ -261,67 +269,60 @@ D.displayName = "View.ErrorBoundary"; | ||
function R(t) { | ||
const e = new z(t); | ||
const e = new P(t); | ||
let n = 0; | ||
return { | ||
async onLoad(r) { | ||
async onLoad(i) { | ||
await e.initialize({ | ||
isPreload: r.preload, | ||
params: r.params, | ||
search: r.search | ||
}), r.preload || (n++, e.navigate({ params: r.params, search: r.search })); | ||
isPreload: i.preload, | ||
params: i.params, | ||
search: i.search | ||
}), i.preload || (n++, e.navigate({ params: i.params, search: i.search })); | ||
}, | ||
onLoaded() { | ||
return (r) => { | ||
--n === 0 && e.close({ params: r.params, search: r.search }); | ||
return (i) => { | ||
--n === 0 && e.close({ params: i.params, search: i.search }); | ||
}; | ||
}, | ||
component: () => /* @__PURE__ */ s.createElement(A, { vm: e.instance }, /* @__PURE__ */ s.createElement(W, null)) | ||
component: () => /* @__PURE__ */ s.createElement(A, { vm: e.instance }, /* @__PURE__ */ s.createElement(G, null)) | ||
}; | ||
} | ||
function ve(t, e) { | ||
const n = R(t), r = Object.assign(e, n); | ||
return new H(r); | ||
function pe(t, e) { | ||
const n = R(t), i = Object.assign(e, n); | ||
return new H(i); | ||
} | ||
function we(t, e) { | ||
const n = R(t), r = Object.assign(e, n); | ||
return new S(r); | ||
function ge(t, e) { | ||
const n = R(t), i = Object.assign(e, n); | ||
return new W(i); | ||
} | ||
function ee(t) { | ||
return G(({ vm: e }) => t(e)); | ||
function te(t) { | ||
return U(({ vm: e }) => t(e)); | ||
} | ||
function pe(t, e, n) { | ||
function ye(t, e, n) { | ||
return n ? V(t, e, n) : V(t, void 0, e); | ||
} | ||
function V(t, e, n) { | ||
const r = ee(n); | ||
return Y(r, t, e), r; | ||
const i = te(n); | ||
return Z(i, t, e), i; | ||
} | ||
function ge(...t) { | ||
v( | ||
() => () => { | ||
t.forEach((e) => e.dispose()); | ||
}, | ||
[] | ||
); | ||
} | ||
export { | ||
J as BindingComponent, | ||
K as BindingComponent, | ||
A as View, | ||
le as assignDefaultProps, | ||
we as buildRootRoute, | ||
ve as buildRoute, | ||
fe as createDataIdHandler, | ||
de as createMemoizedHandler, | ||
ee as createViewComponent, | ||
P as ViewModelLifecycleManager, | ||
fe as assignDefaultProps, | ||
ge as buildRootRoute, | ||
pe as buildRoute, | ||
de as createDataIdHandler, | ||
he as createMemoizedHandler, | ||
te as createViewComponent, | ||
_ as getValue, | ||
Z as getView, | ||
U as omitBindingProps, | ||
he as preventDefault, | ||
Y as registerView, | ||
pe as registerViewComponent, | ||
P as setValue, | ||
x as tryGetView, | ||
x as getView, | ||
X as omitBindingProps, | ||
me as preventDefault, | ||
Z as registerView, | ||
ye as registerViewComponent, | ||
z as setValue, | ||
ee as tryGetView, | ||
ue as useBinding, | ||
ge as useDisposable, | ||
me as useViewModel | ||
ve as useDisposable, | ||
we as useViewModel | ||
}; | ||
//# sourceMappingURL=fruits-views.es.js.map |
@@ -1,2 +0,2 @@ | ||
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("mobx"),require("react"),require("@frui.ts/helpers"),require("lodash-es"),require("async-mutex"),require("@tanstack/react-router"),require("mobx-react-lite")):typeof define=="function"&&define.amd?define(["exports","mobx","react","@frui.ts/helpers","lodash-es","async-mutex","@tanstack/react-router","mobx-react-lite"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r["fruits-views"]={},r.mobx,r.React,r.helpers,r.lodashEs,r.asyncMutex,r.reactRouter,r.mobxReactLite))})(this,function(r,o,a,f,I,L,v,S){var d,c;"use strict";var ee=Object.defineProperty;var te=(r,o,a)=>o in r?ee(r,o,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[o]=a;var h=(r,o,a)=>(te(r,typeof o!="symbol"?o+"":o,a),a),q=(r,o,a)=>{if(!o.has(r))throw TypeError("Cannot "+a)};var l=(r,o,a)=>(q(r,o,"read from private field"),a?a.call(r):o.get(r)),b=(r,o,a)=>{if(o.has(r))throw TypeError("Cannot add the same private member more than once");o instanceof WeakSet?o.add(r):o.set(r,a)},O=(r,o,a,f)=>(q(r,o,"write to private field"),f?f.call(r,a):o.set(r,a),a);function P(t){const{target:e,property:n,onValueChanged:i,...s}=t;return s}function m(t,e,n=!0){if(!t)throw new Error(`Cannot read property '${e}', because target has not been set`);if(e===void 0)throw new Error(`'property' prop has not been set for target '${t}'`);if(o.isObservableMap(t))return t.get(e);if(!o.isObservable(t)||!o.isObservableProp(t,e)){if(f.isMap(t))return t.get(e);{const i=t[e];if(n)o.action(f.ensureObservableProperty)(t,e,i);else return i}}return o.get(t,e)}function p(t,e,n){t&&e&&o.action(f.ensureObservableProperty)(t,e,n)}function F(t){return[m(t.target,t.property),i=>{var u;const s=i;p(t.target,t.property,s),t.target&&t.property&&((u=t.onValueChanged)==null||u.call(t,s,t.property,t.target))}]}var H=Object.defineProperty,$=Object.getOwnPropertyDescriptor,k=(t,e,n,i)=>{for(var s=i>1?void 0:i?$(e,n):e,u=t.length-1,E;u>=0;u--)(E=t[u])&&(s=(i?E(e,n,s):E(s))||s);return i&&s&&H(e,n,s),s};class _ extends a.Component{constructor(e){super(e),o.makeObservable(this)}get inheritedProps(){return P(this.props)}get value(){if(!this.props.target){console.warn("'target' has not been set");return}return m(this.props.target,this.props.property)}setValue(e){const{target:n,property:i,onValueChanged:s}=this.props,u=e;p(n,i,u),s==null||s(u,i,n)}}k([o.action.bound],_.prototype,"setValue",1);function G(t,e){const n=t;n.defaultProps?Object.assign(n.defaultProps,e):n.defaultProps=e}function W(t){return e=>{const n=e.currentTarget.dataset.id;n!==void 0&&t(+n)}}const U=I.memoize(W);function X(t){const e=n=>i=>{const s=i.currentTarget.dataset[t];s!==void 0&&n(s)};return I.memoize(e)}function J(t){return function(e){e.preventDefault(),e.stopPropagation(),t()}}class z{constructor(e){b(this,d,void 0);b(this,c,new L.Mutex);h(this,"_isInitialized",!1);h(this,"_isActive",!1);this.factory=e}get instance(){return l(this,d)??O(this,d,this.factory()),l(this,d)}resetInstance(){O(this,d,void 0),this._isInitialized=!1,this._isActive=!1}async initialize(e){await l(this,c).runExclusive(async()=>{await this.callInitialize(e)})}async callInitialize(e){var n,i;this._isInitialized||(await((i=(n=this.instance).onInitialize)==null?void 0:i.call(n,e)),this._isInitialized=!0)}async activate(e){await l(this,c).runExclusive(async()=>{await this.callInitialize(e),await this.callActivate(e)})}async callActivate(e){var n,i;this._isActive||(await((i=(n=this.instance).onActivate)==null?void 0:i.call(n,e)),this._isActive=!0)}async navigate(e){await l(this,c).runExclusive(async()=>{await this.callInitialize(e),await this.callActivate(e),await this.callNavigate(e)})}async callNavigate(e){var n,i;await((i=(n=this.instance).onNavigate)==null?void 0:i.call(n,e))}async deactivate(e){await l(this,c).runExclusive(async()=>{await this.callDeactivate(e)})}async callDeactivate(e){var n,i;this._isActive&&(await((i=(n=this.instance).onDeactivate)==null?void 0:i.call(n,e)),this._isActive=!1)}async close(e){await l(this,c).runExclusive(async()=>{await this.callDeactivate(e),this.resetInstance()})}}d=new WeakMap,c=new WeakMap;function K(t,e,n){const i=a.useRef(new z(t)),s=a.useRef(e);return s.current=e,a.useEffect(()=>(i.current.initialize(s.current),()=>{i.current.close(s.current)}),[]),a.useEffect(()=>{i.current.navigate(s.current)},n??[e]),i.current.instance}const D=Object.freeze({errorInfo:null,error:null});class Q extends a.PureComponent{constructor(){super(...arguments);h(this,"state",D);h(this,"resetErrorBoundary",()=>{const{onReset:n}=this.props;if(n){const{error:i,errorInfo:s}=this.state;n(i,s)}this.setState(D)})}componentDidCatch(n,i){var s,u;(u=(s=this.props).onError)==null||u.call(s,n,i),this.setState({error:n,errorInfo:i})}render(){if(this.state.error){const{error:n,errorInfo:i}=this.state,{fallback:s}=this.props,u=typeof s=="function"?s({error:n,errorInfo:i,resetError:this.resetErrorBoundary}):s;return u||a.createElement("p",null,"Something went wrong :-(")}return a.createElement(a.Fragment,null,this.props.children)}}const g="default",w=new Map;function M(t,e,n=g){const i=w.get(e);return i?i[n]=t:w.set(e,{[n]:t}),t}function A(t,e=g){const n=w.get(t);if(!n)throw new Error("No view has been registered for view model "+t.name);const i=n[e];if(!i)throw new Error(`No view has been registered for view model ${t.name} and context ${e}`);return i}function C(t,e=g){const n=w.get(t);if(!n)return null;const i=n[e];return i||null}const y=t=>{const{vm:e,children:n,context:i}=t;if(!e)return a.createElement(a.Fragment,null,n);const s=e.constructor,u=n===void 0?A(s,i):C(s,i);return u?a.createElement(u,{vm:e}):a.createElement(a.Fragment,null,n)};y.displayName="View";const B=t=>{const{onError:e,onReset:n,fallback:i,...s}=t;return a.createElement(Q,{onError:e,onReset:n,fallback:i},a.createElement(y,{...s}))};B.displayName="View.ErrorBoundary";const V=y;V.ErrorBoundary=B;function T(t){const e=new z(t);let n=0;return{async onLoad(i){await e.initialize({isPreload:i.preload,params:i.params,search:i.search}),i.preload||(n++,e.navigate({params:i.params,search:i.search}))},onLoaded(){return i=>{--n===0&&e.close({params:i.params,search:i.search})}},component:()=>a.createElement(V,{vm:e.instance},a.createElement(v.Outlet,null))}}function Y(t,e){const n=T(t),i=Object.assign(e,n);return new v.Route(i)}function Z(t,e){const n=T(t),i=Object.assign(e,n);return new v.RootRoute(i)}function N(t){return S.observer(({vm:e})=>t(e))}function R(t,e,n){return n?j(t,e,n):j(t,void 0,e)}function j(t,e,n){const i=N(n);return M(i,t,e),i}function x(...t){a.useEffect(()=>()=>{t.forEach(e=>e.dispose())},[])}r.BindingComponent=_,r.View=V,r.assignDefaultProps=G,r.buildRootRoute=Z,r.buildRoute=Y,r.createDataIdHandler=U,r.createMemoizedHandler=X,r.createViewComponent=N,r.getValue=m,r.getView=A,r.omitBindingProps=P,r.preventDefault=J,r.registerView=M,r.registerViewComponent=R,r.setValue=p,r.tryGetView=C,r.useBinding=F,r.useDisposable=x,r.useViewModel=K,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}); | ||
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("mobx"),require("react"),require("@frui.ts/helpers"),require("lodash-es"),require("async-mutex"),require("@tanstack/react-router"),require("mobx-react-lite")):typeof define=="function"&&define.amd?define(["exports","mobx","react","@frui.ts/helpers","lodash-es","async-mutex","@tanstack/react-router","mobx-react-lite"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r["fruits-views"]={},r.mobx,r.React,r.helpers,r.lodashEs,r.asyncMutex,r.reactRouter,r.mobxReactLite))})(this,function(r,o,s,d,P,L,m,S){var h,c;"use strict";var ee=Object.defineProperty;var te=(r,o,s)=>o in r?ee(r,o,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[o]=s;var w=(r,o,s)=>(te(r,typeof o!="symbol"?o+"":o,s),s),q=(r,o,s)=>{if(!o.has(r))throw TypeError("Cannot "+s)};var f=(r,o,s)=>(q(r,o,"read from private field"),s?s.call(r):o.get(r)),I=(r,o,s)=>{if(o.has(r))throw TypeError("Cannot add the same private member more than once");o instanceof WeakSet?o.add(r):o.set(r,s)},O=(r,o,s,d)=>(q(r,o,"write to private field"),d?d.call(r,s):o.set(r,s),s);function z(t){const{target:e,property:n,onValueChanged:i,...a}=t;return a}function p(t,e,n=!0){if(!t)throw new Error(`Cannot read property '${e}', because target has not been set`);if(e===void 0)throw new Error(`'property' prop has not been set for target '${t}'`);if(o.isObservableMap(t))return t.get(e);if(!o.isObservable(t)||!o.isObservableProp(t,e)){if(d.isMap(t))return t.get(e);{const i=t[e];if(n)o.action(d.ensureObservableProperty)(t,e,i);else return i}}return o.get(t,e)}function g(t,e,n){t&&e&&o.action(d.ensureObservableProperty)(t,e,n)}function F(t){return[p(t.target,t.property),i=>{var u;const a=i;g(t.target,t.property,a),t.target&&t.property&&((u=t.onValueChanged)==null||u.call(t,a,t.property,t.target))}]}var H=Object.defineProperty,$=Object.getOwnPropertyDescriptor,k=(t,e,n,i)=>{for(var a=i>1?void 0:i?$(e,n):e,u=t.length-1,l;u>=0;u--)(l=t[u])&&(a=(i?l(e,n,a):l(a))||a);return i&&a&&H(e,n,a),a};class _ extends s.Component{constructor(e){super(e),o.makeObservable(this)}get inheritedProps(){return z(this.props)}get value(){if(!this.props.target){console.warn("'target' has not been set");return}return p(this.props.target,this.props.property)}setValue(e){const{target:n,property:i,onValueChanged:a}=this.props,u=e;g(n,i,u),a==null||a(u,i,n)}}k([o.action.bound],_.prototype,"setValue",1);function G(t,e){const n=t;n.defaultProps?Object.assign(n.defaultProps,e):n.defaultProps=e}function W(t){return e=>{const n=e.currentTarget.dataset.id;n!==void 0&&t(+n)}}const U=P.memoize(W);function X(t){const e=n=>i=>{const a=i.currentTarget.dataset[t];a!==void 0&&n(a)};return P.memoize(e)}function J(t){return function(e){e.preventDefault(),e.stopPropagation(),t()}}class y{constructor(e){I(this,h,void 0);I(this,c,new L.Mutex);w(this,"_isInitialized",!1);w(this,"_isActive",!1);this.factory=e}get instance(){return f(this,h)??O(this,h,this.factory()),f(this,h)}resetInstance(){O(this,h,void 0),this._isInitialized=!1,this._isActive=!1}async initialize(e){await f(this,c).runExclusive(async()=>{await this.callInitialize(e)})}async callInitialize(e){var n,i;this._isInitialized||(await((i=(n=this.instance).onInitialize)==null?void 0:i.call(n,e)),this._isInitialized=!0)}async activate(e){await f(this,c).runExclusive(async()=>{await this.callInitialize(e),await this.callActivate(e)})}async callActivate(e){var n,i;this._isActive||(await((i=(n=this.instance).onActivate)==null?void 0:i.call(n,e)),this._isActive=!0)}async navigate(e){await f(this,c).runExclusive(async()=>{await this.callInitialize(e),await this.callActivate(e),await this.callNavigate(e)})}async callNavigate(e){var n,i;await((i=(n=this.instance).onNavigate)==null?void 0:i.call(n,e))}async deactivate(e){await f(this,c).runExclusive(async()=>{await this.callDeactivate(e)})}async callDeactivate(e){var n,i;this._isActive&&(await((i=(n=this.instance).onDeactivate)==null?void 0:i.call(n,e)),this._isActive=!1)}async close(e){await f(this,c).runExclusive(async()=>{await this.callDeactivate(e),this.resetInstance()})}}h=new WeakMap,c=new WeakMap;function K(...t){s.useEffect(()=>()=>{t.forEach(e=>e.dispose())},[])}function Q(t,e,n){const i=s.useRef(new y(t)),[a,u]=s.useState(!1),l=s.useRef(e);return l.current=e,s.useEffect(()=>(i.current.initialize(l.current).then(()=>u(!0)),()=>{i.current.close(l.current)}),[]),s.useEffect(()=>{i.current.navigate(l.current)},n??[e]),{vm:i.current.instance,initialized:a}}const D=Object.freeze({errorInfo:null,error:null});class Y extends s.PureComponent{constructor(){super(...arguments);w(this,"state",D);w(this,"resetErrorBoundary",()=>{const{onReset:n}=this.props;if(n){const{error:i,errorInfo:a}=this.state;n(i,a)}this.setState(D)})}componentDidCatch(n,i){var a,u;(u=(a=this.props).onError)==null||u.call(a,n,i),this.setState({error:n,errorInfo:i})}render(){if(this.state.error){const{error:n,errorInfo:i}=this.state,{fallback:a}=this.props,u=typeof a=="function"?a({error:n,errorInfo:i,resetError:this.resetErrorBoundary}):a;return u||s.createElement("p",null,"Something went wrong :-(")}return s.createElement(s.Fragment,null,this.props.children)}}const V="default",v=new Map;function M(t,e,n=V){const i=v.get(e);return i?i[n]=t:v.set(e,{[n]:t}),t}function A(t,e=V){const n=v.get(t);if(!n)throw new Error("No view has been registered for view model "+t.name);const i=n[e];if(!i)throw new Error(`No view has been registered for view model ${t.name} and context ${e}`);return i}function C(t,e=V){const n=v.get(t);if(!n)return null;const i=n[e];return i||null}const E=t=>{const{vm:e,children:n,context:i}=t;if(!e)return s.createElement(s.Fragment,null,n);const a=e.constructor,u=n===void 0?A(a,i):C(a,i);return u?s.createElement(u,{vm:e}):s.createElement(s.Fragment,null,n)};E.displayName="View";const B=t=>{const{onError:e,onReset:n,fallback:i,...a}=t;return s.createElement(Y,{onError:e,onReset:n,fallback:i},s.createElement(E,{...a}))};B.displayName="View.ErrorBoundary";const b=E;b.ErrorBoundary=B;function T(t){const e=new y(t);let n=0;return{async onLoad(i){await e.initialize({isPreload:i.preload,params:i.params,search:i.search}),i.preload||(n++,e.navigate({params:i.params,search:i.search}))},onLoaded(){return i=>{--n===0&&e.close({params:i.params,search:i.search})}},component:()=>s.createElement(b,{vm:e.instance},s.createElement(m.Outlet,null))}}function Z(t,e){const n=T(t),i=Object.assign(e,n);return new m.Route(i)}function R(t,e){const n=T(t),i=Object.assign(e,n);return new m.RootRoute(i)}function N(t){return S.observer(({vm:e})=>t(e))}function x(t,e,n){return n?j(t,e,n):j(t,void 0,e)}function j(t,e,n){const i=N(n);return M(i,t,e),i}r.BindingComponent=_,r.View=b,r.ViewModelLifecycleManager=y,r.assignDefaultProps=G,r.buildRootRoute=R,r.buildRoute=Z,r.createDataIdHandler=U,r.createMemoizedHandler=X,r.createViewComponent=N,r.getValue=p,r.getView=A,r.omitBindingProps=z,r.preventDefault=J,r.registerView=M,r.registerViewComponent=x,r.setValue=g,r.tryGetView=C,r.useBinding=F,r.useDisposable=K,r.useViewModel=Q,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}); | ||
//# sourceMappingURL=fruits-views.umd.js.map |
import type { DependencyList } from "react"; | ||
import type { IViewModel } from "../types"; | ||
export declare function useViewModel<TContext, TViewModel extends IViewModel<TContext>>(factory: () => TViewModel, context: TContext, dependencies?: DependencyList): TViewModel; | ||
export declare function useViewModel<TContext, TViewModel extends IViewModel<TContext>>(factory: () => TViewModel, context: TContext, dependencies?: DependencyList): { | ||
vm: TViewModel; | ||
initialized: boolean; | ||
}; |
@@ -8,2 +8,4 @@ export * from "./binding/bindingComponent"; | ||
export { default as preventDefault } from "./helpers/preventDefault"; | ||
export * from "./helpers/viewModelLifecycleManager"; | ||
export * from "./hooks/useDisposable"; | ||
export * from "./hooks/useViewModel"; | ||
@@ -14,4 +16,3 @@ export * from "./router/router"; | ||
export * from "./view/helpers"; | ||
export * from "./hooks/useDisposable"; | ||
export { default as View } from "./view/view"; | ||
export * from "./view/viewLocator"; |
@@ -16,2 +16,3 @@ import type React from "react"; | ||
onDeactivate?(context: TContext): Promise<unknown> | unknown; | ||
isVm?: true; | ||
} |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "1.0.0-rc.1", | ||
"version": "1.0.0-rc.2", | ||
"description": "Frui.ts presentation related components based on React", | ||
@@ -48,4 +48,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@frui.ts/helpers": "^1.0.0-rc.1", | ||
"@tanstack/react-router": "^0.0.1-beta.82", | ||
"@frui.ts/helpers": "^1.0.0-rc.2", | ||
"@tanstack/react-router": "^0.0.1-beta.83", | ||
"async-mutex": "^0.4.0", | ||
@@ -61,3 +61,3 @@ "inversify": "^6.0.1", | ||
}, | ||
"gitHead": "c17e77c7246207827d09d34cadbb54b07ebbd82e" | ||
"gitHead": "0a6acd2dea7f4c925c56aae7717802e3b8aa7e7a" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
302485
1132