stated-bean
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -5,2 +5,9 @@ # Changelog | ||
### [0.9.1](https://github.com/mjolnirjs/stated-bean/compare/v0.9.0...v0.9.1) (2020-04-14) | ||
### Features | ||
* effect observable performance ([#158](https://github.com/mjolnirjs/stated-bean/issues/158)) ([c569dc5](https://github.com/mjolnirjs/stated-bean/commit/c569dc5db777de2cfb42dfb4847d6c24dacf3f1a)) | ||
## [0.9.0](https://github.com/mjolnirjs/stated-bean/compare/v0.8.6...v0.9.0) (2020-03-07) | ||
@@ -7,0 +14,0 @@ |
@@ -856,17 +856,13 @@ 'use strict'; | ||
.then(function (data) { | ||
emitEffectAction_1({ data: data }); | ||
emitEffectAction_1({ loading: false, error: null, data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, error: e }); | ||
emitEffectAction_1({ loading: false, error: e, data: undefined }); | ||
throw e; | ||
}) | ||
.finally(function () { | ||
emitEffectAction_1({ loading: false }); | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result }); | ||
throw new Error('Effect must decorated for a Promise function'); | ||
} | ||
return result; | ||
} | ||
@@ -1140,9 +1136,7 @@ return originalMethod.apply(this, args); | ||
data: null, | ||
effect: effect, | ||
}; | ||
}), effectState = _a[0], setEffectState = _a[1]; | ||
var listener = React.useCallback(function (action) { | ||
console.log(action.effectTarget === effect); | ||
if (action.effect === effect || action.effectTarget === effect) { | ||
setEffectState(action); | ||
setEffectState({ loading: action.loading, error: action.error, data: action.data }); | ||
} | ||
@@ -1149,0 +1143,0 @@ }, [effect]); |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,n=require("react"),r=(e=n)&&"object"==typeof e&&"default"in e?e.default:e,o=require("rxjs"),i=require("tslib");function a(){return t||(t=r.createContext({})),t}var s=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),u=function(){function e(){this._beanFactory=new s}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),c=Symbol("stated-bean-class"),p=Symbol("stated-bean-wrapper"),f=Symbol("stated-bean-force-update");function b(e){return"function"==typeof e}function l(e){return Object.hasOwnProperty.call(e,c)}function y(e){if(Object.hasOwnProperty.call(e,p))return Reflect.get(e,p)}function d(e){return!!e&&"function"==typeof e.then}function h(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var v=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new o.Subject,t}return i.__extends(t,e),t.prototype._subscribe=function(t){var n=this,r=new o.Observable((function(t){var r=e.prototype._subscribe.call(n,t);return function(){r.unsubscribe(),n._counter$.next(n.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),r},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(o.Subject),g=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new v}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function _(e,t){if(t in e)return e[t]}function m(e){var t=_(e,"afterProvided");return void 0!==t&&"function"==typeof t}function B(e){var t=_(e,"destroy");return void 0!==t&&"function"==typeof t}function O(e){var t=_(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function x(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function P(e,t){if(x(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!x(e[a],t[a]))return!1}return!0}var j=function(){function e(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new v,this.effect$=new o.Subject,this.props$=new o.Subject,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),O(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),B(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):m(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=y(e);void 0===n&&(n=new g(this._container,this.beanDefinition.beanName),Object.defineProperty(e,p,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,n){var r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new o.BehaviorSubject(r)):Reflect.set(e,t.name,r)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;P(a.getValue(),o)||a.next(o)}else if(!P(i,o)){var s=String(t.name),u=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));u&&"function"==typeof u?u.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),w=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new j(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=y(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),M=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u,this._registry=new w(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function S(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function F(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,S(e,t,n))}})),e}var E,T=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),N=function(){return E||(E=new T)},$=Symbol("UN_NAMED_BEAN"),C=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=N().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(h(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=N().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==$},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return b(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function D(e){var t=e.providers,r=e.application,o=a(),i=n.useContext(o),s=n.useState((function(){var e=new M(i.container,r);return(t||[]).forEach((function(t){var n;n=b(t)?l(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new C(n)).state$.subscribe()})),e}))[0];return n.useEffect((function(){return function(){s.destroy()}}),[s]),s}exports.AfterProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),N().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},exports.BeanDefinition=C,exports.BeanObserver=j,exports.BeanWrapper=g,exports.DefaultBeanFactory=s,exports.Effect=function(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var r=t,o=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var a=y(this);if(void 0!==a){var s=function(t){var n=a.beanObserver;void 0!==n&&n.effect$.next(i.__assign({effect:r,effectTarget:Reflect.get(e,r)},t))};s({loading:!0,error:null});var u=o.apply(this,t);return d(u)?u.then((function(e){return s({data:e}),e})).catch((function(e){throw s({loading:!1,error:e}),e})).finally((function(){s({loading:!1})})):(s({loading:!1,data:u}),u)}return o.apply(this,t)},n}},exports.ForceUpdate=f,exports.ObservableProps=function(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),N().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}},exports.Props=function(e){return function(t,n){N().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}},exports.Stated=function(){return function(e,t){N().collectStatedField({name:t,target:e.constructor})}},exports.StatedBean=function(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;F(t),Object.defineProperty(t,c,{writable:!1,value:!0}),N().collectStatedBean({name:n,target:t,singleton:r})}},exports.StatedBeanApplication=u,exports.StatedBeanClass=c,exports.StatedBeanConsumer=function(e){var t=e.children,n=a();return r.createElement(n.Consumer,null,(function(e){return t(e)}))},exports.StatedBeanContainer=M,exports.StatedBeanProvider=function(e){var t=e.providers,n=e.application,o=e.children,i=a(),s=D({providers:t,application:n});return r.createElement(i.Provider,{value:{container:s}},o)},exports.StatedBeanWrapper=p,exports.UN_NAMED_BEAN=$,exports.boundClass=F,exports.boundMethod=S,exports.getBeanWrapper=y,exports.getPropertiesWithoutFunction=h,exports.getProperty=_,exports.getStatedBeanContext=a,exports.isBeanContainerAware=O,exports.isDisposableBean=B,exports.isFunction=b,exports.isInitializingBean=m,exports.isPromise=d,exports.isStatedBeanClass=l,exports.useBean=function(e,t){var r,o,i=a(),s=n.useContext(i),u=n.useState(0)[1];void 0!==t&&("object"==typeof t?(r=t.name,o=t.props):"string"!=typeof t&&"symbol"!=typeof t||(r=t));var c=n.useCallback((function(e){u((function(e){return e+1}))}),[]),p=s.container;if(void 0===p)throw new Error("not found stated bean container.");var f=n.useState((function(){var t;if(b(e)&&!l(e)){var n=e;t={type:n.constructor,factory:n,name:r,props:o}}else t={type:e,name:r,props:o};return p.register(new C(t))}))[0],y=n.useState((function(){return f.state$.subscribe(c)}))[0];return n.useEffect((function(){f.props$.next(o)}),[o,f]),n.useEffect((function(){return function(){return y.unsubscribe()}}),[y]),n.useDebugValue(f,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),f.proxy},exports.useContainer=D,exports.useInject=function(e){var t=a(),r=n.useContext(t),o=n.useState(0)[1],i="object"==typeof e?e.type:e,s="object"==typeof e?e:{},u=s.name,c=s.observedFields,p=n.useCallback((function(e){(null==c||0===c.length||c.includes(e.fieldMeta.name))&&o((function(e){return e+1}))}),[c]),f=r.container;if(void 0===f)throw new Error("not found container");var b=n.useState((function(){var e;if(void 0!==u)e=f.getNamedObserver(u);else if(void 0!==i){var t=f.getTypedObserver(i);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+i.name+"] found.");e=t[0]}}return e}))[0];if(void 0===b)throw new Error("bean observer is undefined.");var l=n.useState((function(){return b.state$.subscribe(p)}))[0];return n.useEffect((function(){return function(){l.unsubscribe()}}),[l]),b.proxy},exports.useObservable=function(e){var t=n.useState((function(){return e instanceof o.BehaviorSubject?e.getValue():null})),r=t[0],i=t[1],a=n.useState(e),s=a[0],u=a[1];return n.useEffect((function(){b(e)||u(e)}),[e]),n.useEffect((function(){var e;return s instanceof o.Observable&&(e=s.subscribe(i)),function(){e&&e.unsubscribe()}}),[s]),r},exports.useObserveEffect=function(e,t){var r=a();if(void 0===n.useContext(r).container)throw new Error("not found container");var o=n.useState((function(){return{loading:!1,error:null,data:null,effect:t}})),i=o[0],s=o[1],u=n.useCallback((function(e){console.log(e.effectTarget===t),e.effect!==t&&e.effectTarget!==t||s(e)}),[t]),c=n.useState((function(){var t=y(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(u)}))[0];return n.useEffect((function(){return function(){c.unsubscribe()}}),[c]),i}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,n=require("react"),r=(e=n)&&"object"==typeof e&&"default"in e?e.default:e,o=require("rxjs"),i=require("tslib");function a(){return t||(t=r.createContext({})),t}var s=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),u=function(){function e(){this._beanFactory=new s}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),c=Symbol("stated-bean-class"),p=Symbol("stated-bean-wrapper"),f=Symbol("stated-bean-force-update");function b(e){return"function"==typeof e}function l(e){return Object.hasOwnProperty.call(e,c)}function d(e){if(Object.hasOwnProperty.call(e,p))return Reflect.get(e,p)}function y(e){return!!e&&"function"==typeof e.then}function h(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var v=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new o.Subject,t}return i.__extends(t,e),t.prototype._subscribe=function(t){var n=this,r=new o.Observable((function(t){var r=e.prototype._subscribe.call(n,t);return function(){r.unsubscribe(),n._counter$.next(n.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),r},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(o.Subject),g=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new v}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function _(e,t){if(t in e)return e[t]}function m(e){var t=_(e,"afterProvided");return void 0!==t&&"function"==typeof t}function B(e){var t=_(e,"destroy");return void 0!==t&&"function"==typeof t}function O(e){var t=_(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function x(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function P(e,t){if(x(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!x(e[a],t[a]))return!1}return!0}var w=function(){function e(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new v,this.effect$=new o.Subject,this.props$=new o.Subject,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),O(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),B(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):m(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=d(e);void 0===n&&(n=new g(this._container,this.beanDefinition.beanName),Object.defineProperty(e,p,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,n){var r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new o.BehaviorSubject(r)):Reflect.set(e,t.name,r)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;P(a.getValue(),o)||a.next(o)}else if(!P(i,o)){var s=String(t.name),u=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));u&&"function"==typeof u?u.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),j=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new w(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=d(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),M=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u,this._registry=new j(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function S(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function F(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,S(e,t,n))}})),e}var E,T=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),N=function(){return E||(E=new T)},$=Symbol("UN_NAMED_BEAN"),C=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=N().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(h(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=N().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==$},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return b(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function D(e){var t=e.providers,r=e.application,o=a(),i=n.useContext(o),s=n.useState((function(){var e=new M(i.container,r);return(t||[]).forEach((function(t){var n;n=b(t)?l(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new C(n)).state$.subscribe()})),e}))[0];return n.useEffect((function(){return function(){s.destroy()}}),[s]),s}exports.AfterProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),N().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},exports.BeanDefinition=C,exports.BeanObserver=w,exports.BeanWrapper=g,exports.DefaultBeanFactory=s,exports.Effect=function(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var r=t,o=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var a=d(this);if(void 0!==a){var s=function(t){var n=a.beanObserver;void 0!==n&&n.effect$.next(i.__assign({effect:r,effectTarget:Reflect.get(e,r)},t))};s({loading:!0,error:null});var u=o.apply(this,t);if(y(u))return u.then((function(e){return s({loading:!1,error:null,data:e}),e})).catch((function(e){throw s({loading:!1,error:e,data:void 0}),e}));throw new Error("Effect must decorated for a Promise function")}return o.apply(this,t)},n}},exports.ForceUpdate=f,exports.ObservableProps=function(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),N().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}},exports.Props=function(e){return function(t,n){N().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}},exports.Stated=function(){return function(e,t){N().collectStatedField({name:t,target:e.constructor})}},exports.StatedBean=function(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;F(t),Object.defineProperty(t,c,{writable:!1,value:!0}),N().collectStatedBean({name:n,target:t,singleton:r})}},exports.StatedBeanApplication=u,exports.StatedBeanClass=c,exports.StatedBeanConsumer=function(e){var t=e.children,n=a();return r.createElement(n.Consumer,null,(function(e){return t(e)}))},exports.StatedBeanContainer=M,exports.StatedBeanProvider=function(e){var t=e.providers,n=e.application,o=e.children,i=a(),s=D({providers:t,application:n});return r.createElement(i.Provider,{value:{container:s}},o)},exports.StatedBeanWrapper=p,exports.UN_NAMED_BEAN=$,exports.boundClass=F,exports.boundMethod=S,exports.getBeanWrapper=d,exports.getPropertiesWithoutFunction=h,exports.getProperty=_,exports.getStatedBeanContext=a,exports.isBeanContainerAware=O,exports.isDisposableBean=B,exports.isFunction=b,exports.isInitializingBean=m,exports.isPromise=y,exports.isStatedBeanClass=l,exports.useBean=function(e,t){var r,o,i=a(),s=n.useContext(i),u=n.useState(0)[1];void 0!==t&&("object"==typeof t?(r=t.name,o=t.props):"string"!=typeof t&&"symbol"!=typeof t||(r=t));var c=n.useCallback((function(e){u((function(e){return e+1}))}),[]),p=s.container;if(void 0===p)throw new Error("not found stated bean container.");var f=n.useState((function(){var t;if(b(e)&&!l(e)){var n=e;t={type:n.constructor,factory:n,name:r,props:o}}else t={type:e,name:r,props:o};return p.register(new C(t))}))[0],d=n.useState((function(){return f.state$.subscribe(c)}))[0];return n.useEffect((function(){f.props$.next(o)}),[o,f]),n.useEffect((function(){return function(){return d.unsubscribe()}}),[d]),n.useDebugValue(f,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),f.proxy},exports.useContainer=D,exports.useInject=function(e){var t=a(),r=n.useContext(t),o=n.useState(0)[1],i="object"==typeof e?e.type:e,s="object"==typeof e?e:{},u=s.name,c=s.observedFields,p=n.useCallback((function(e){(null==c||0===c.length||c.includes(e.fieldMeta.name))&&o((function(e){return e+1}))}),[c]),f=r.container;if(void 0===f)throw new Error("not found container");var b=n.useState((function(){var e;if(void 0!==u)e=f.getNamedObserver(u);else if(void 0!==i){var t=f.getTypedObserver(i);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+i.name+"] found.");e=t[0]}}return e}))[0];if(void 0===b)throw new Error("bean observer is undefined.");var l=n.useState((function(){return b.state$.subscribe(p)}))[0];return n.useEffect((function(){return function(){l.unsubscribe()}}),[l]),b.proxy},exports.useObservable=function(e){var t=n.useState((function(){return e instanceof o.BehaviorSubject?e.getValue():null})),r=t[0],i=t[1],a=n.useState(e),s=a[0],u=a[1];return n.useEffect((function(){b(e)||u(e)}),[e]),n.useEffect((function(){var e;return s instanceof o.Observable&&(e=s.subscribe(i)),function(){e&&e.unsubscribe()}}),[s]),r},exports.useObserveEffect=function(e,t){var r=a();if(void 0===n.useContext(r).container)throw new Error("not found container");var o=n.useState((function(){return{loading:!1,error:null,data:null}})),i=o[0],s=o[1],u=n.useCallback((function(e){e.effect!==t&&e.effectTarget!==t||s({loading:e.loading,error:e.error,data:e.data})}),[t]),c=n.useState((function(){var t=d(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(u)}))[0];return n.useEffect((function(){return function(){c.unsubscribe()}}),[c]),i}; |
@@ -36,17 +36,13 @@ import { __assign } from "tslib"; | ||
.then(function (data) { | ||
emitEffectAction_1({ data: data }); | ||
emitEffectAction_1({ loading: false, error: null, data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, error: e }); | ||
emitEffectAction_1({ loading: false, error: e, data: undefined }); | ||
throw e; | ||
}) | ||
.finally(function () { | ||
emitEffectAction_1({ loading: false }); | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result }); | ||
throw new Error('Effect must decorated for a Promise function'); | ||
} | ||
return result; | ||
} | ||
@@ -53,0 +49,0 @@ return originalMethod.apply(this, args); |
@@ -25,9 +25,7 @@ import { useCallback, useContext, useEffect, useState } from 'react'; | ||
data: null, | ||
effect: effect, | ||
}; | ||
}), effectState = _a[0], setEffectState = _a[1]; | ||
var listener = useCallback(function (action) { | ||
console.log(action.effectTarget === effect); | ||
if (action.effect === effect || action.effectTarget === effect) { | ||
setEffectState(action); | ||
setEffectState({ loading: action.loading, error: action.error, data: action.data }); | ||
} | ||
@@ -34,0 +32,0 @@ }, [effect]); |
@@ -745,17 +745,13 @@ import React, { useContext, useState, useEffect, useCallback, useDebugValue } from 'react'; | ||
.then(data => { | ||
emitEffectAction({ data: data }); | ||
emitEffectAction({ loading: false, error: null, data: data }); | ||
return data; | ||
}) | ||
.catch((e) => { | ||
emitEffectAction({ loading: false, error: e }); | ||
emitEffectAction({ loading: false, error: e, data: undefined }); | ||
throw e; | ||
}) | ||
.finally(() => { | ||
emitEffectAction({ loading: false }); | ||
}); | ||
} | ||
else { | ||
emitEffectAction({ loading: false, data: result }); | ||
throw new Error('Effect must decorated for a Promise function'); | ||
} | ||
return result; | ||
} | ||
@@ -1029,9 +1025,7 @@ return originalMethod.apply(this, args); | ||
data: null, | ||
effect: effect, | ||
}; | ||
}); | ||
const listener = useCallback((action) => { | ||
console.log(action.effectTarget === effect); | ||
if (action.effect === effect || action.effectTarget === effect) { | ||
setEffectState(action); | ||
setEffectState({ loading: action.loading, error: action.error, data: action.data }); | ||
} | ||
@@ -1038,0 +1032,0 @@ }, [effect]); |
@@ -1,1 +0,1 @@ | ||
import e,{useContext as t,useState as n,useEffect as r,useCallback as s,useDebugValue as o}from"react";import{Subject as i,Observable as a,BehaviorSubject as c}from"rxjs";let p;function u(){return p||(p=e.createContext({})),p}const h=({children:t})=>{const n=u();return e.createElement(n.Consumer,null,e=>t(e))};class l{createBean(e){if(e.isFactoryBean){return e.getFactory()(e.props)}return new e.beanType(e.props)}destroyBean(){}}class b{constructor(){this._beanFactory=new l}getBeanFactory(){return this._beanFactory}setBeanFactory(e){return this._beanFactory=e,this}}const d=Symbol("stated-bean-class"),f=Symbol("stated-bean-wrapper"),y=Symbol("stated-bean-force-update");function g(e){return"function"==typeof e}function _(e){return Object.hasOwnProperty.call(e,d)}function v(e){if(Object.hasOwnProperty.call(e,f))return Reflect.get(e,f)}function m(e){return!!e&&"function"==typeof e.then}function B(e){if(e)return Object.keys(e).filter(t=>"function"!=typeof e[t])}class w extends i{constructor(){super(...arguments),this._counter$=new i}_subscribe(e){const t=new a(e=>{const t=super._subscribe(e);return()=>{t.unsubscribe(),this._counter$.next(this.observers.length)}}).subscribe(e);return this._counter$.next(this.observers.length),t}subscribeCount(e){this._counter$.subscribe(e)}complete(){super.complete(),this._counter$.complete()}}class M{constructor(e,t){this._container=e,this._beanName=t,this.state$=new w}get beanObserver(){return this._container.getNamedObserver(this._beanName)}get beanDefinition(){return this.beanObserver.beanDefinition}get beanMeta(){return this.beanDefinition.beanMeta}forceUpdate(e){const t=(this.beanMeta.statedFields||[]).find(t=>t.name===e);void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])}}function O(e,t){if(t in e)return e[t]}function F(e){const t=O(e,"afterProvided");return void 0!==t&&"function"==typeof t}function P(e){const t=O(e,"destroy");return void 0!==t&&"function"==typeof t}function x(e){const t=O(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function j(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function $(e,t){if(j(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(const r of n)if(!Object.prototype.hasOwnProperty.call(t,r)||!j(e[r],t[r]))return!1;return!0}class T{constructor(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new w,this.effect$=new i,this.props$=new i,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});const r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),x(this.proxy)&&this.proxy.setBeanContainer(this._container)}get beanDefinition(){return this._beanDefinition}get proxy(){return this._proxyBean}get origin(){return this._bean}get beanMeta(){return this._beanMeta}destroy(){this.state$.complete(),this.effect$.complete(),P(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()}publishStateAction(e,t){const n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)}_observe(){const e=this._defineStatedBean(this.proxy),t=this.beanMeta.propsFields;return void 0!==t&&(t.forEach(e=>{this._initPropsField(this.proxy,e,this.beanDefinition.props)}),this.props$.subscribe(e=>{t.forEach(t=>{this._updatePropsField(this.proxy,t,e)})})),setTimeout(()=>{if(null!=this.beanMeta.postMethod&&void 0!==this.beanMeta.postMethod.descriptor){this.beanMeta.postMethod.descriptor.value.apply(this.proxy)}else F(this.proxy)&&this.proxy.afterProvided()},0),e}_defineStatedBean(e){let t=v(e);if(void 0===t){t=new M(this._container,this.beanDefinition.beanName),Object.defineProperty(e,f,{value:t}),(this.beanMeta.statedFields||[]).forEach(n=>{this._observeBeanField(t,e,n)})}return t}_observeBeanField(e,t,n){const r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set(s){t[r]=s;const o={bean:t,nextValue:s,prevValue:t[n.name],fieldMeta:n};e.state$.next(o)},get:()=>t[r]})}_initPropsField(e,t,n){const r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new c(r)):Reflect.set(e,t.name,r)}_updatePropsField(e,t,n){const r=e,s=void 0===n?void 0:n[t.prop],o=Reflect.get(r,t.name);if(t.observable){const e=o;$(e.getValue(),s)||e.next(s)}else if(!$(o,s)){const n=String(t.name),o=Reflect.get(r,"set"+n.charAt(0).toUpperCase()+n.slice(1));o&&"function"==typeof o?o.apply(e,[s]):Reflect.set(e,t.name,s),Reflect.set(e,t.name,s)}}}class S{constructor(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}get beanFactory(){return this._container.application.getBeanFactory()}getTypedBean(e){return this._typedBeans.get(e)}getNamedBean(e){return this._namedBeans.get(e)}register(e){const t=this.getNamedBean(e.beanName);if(void 0!==t)return t;const n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n}createBeanObserver(e){const t=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(t);const n=new T(t,this._container,e);n.state$.subscribeCount(e=>{0===e&&(this.remove(n),n.destroy())});const r=v(t);return void 0!==r&&r.state$.subscribeCount(n=>{0===n&&(this.beanFactory.destroyBean(e,t),r.state$.complete())}),n}remove(e){const t=e.beanDefinition;this._namedBeans.delete(t.beanName);const n=this._typedBeans.get(t.beanType);if(void 0!==n){const t=n.indexOf(e);n.splice(t,1)}}_addTypedBean(e,t){const n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)}}class N{constructor(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new b,this._registry=new S(this)}destroy(){}getBeanRegistry(){return this._registry}getTypedObserver(e){let t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t}getNamedObserver(e){let t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t}register(e){return this.getBeanRegistry().register(e)}get parent(){return this._parent}get application(){return this._app}}function E(e,t,n){let r=n.value;if("function"!=typeof r)throw new TypeError(`@boundMethod decorator can only be applied to methods not: ${typeof r}`);let s=!1;return{configurable:!0,get(){if(s||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;const n=r.bind(this);return s=!0,Object.defineProperty(this,t,{configurable:!0,get:()=>n,set(e){r=e,delete this[t]}}),s=!1,n},set(e){r=e}}}function D(e){let t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach(t=>{if("constructor"===t)return;const n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,E(e,t,n))}),e}class R{constructor(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}collectStatedBean(e){const t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)}collectStatedField(e){const t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])}collectPropsField(e){const t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])}collectPostProvided(e){this._postMethod.set(e.target,e)}getBeanMeta(e){return this._beans.get(e)}clear(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}}let k;const W=()=>k||(k=new R),C=Symbol("UN_NAMED_BEAN");class V{constructor(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(`${Date.now()}`),!this.isFactoryBean){const e=W().getBeanMeta(this.beanType);if(void 0===e)throw new Error("bean metadata is undefined.");this._beanMeta=e}}extractFactoryBeanInfo(e){this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach(t=>{"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})}),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(B(e)||[]).map(e=>({name:e,target:this._factoryBeanType}))}:this._factoryBeanMeta=W().getBeanMeta(this._factoryBeanType)}getFactory(){return this._beanProvider.factory}get factoryBeanType(){return this._factoryBeanType}get beanType(){return this._beanProvider.type}get isSingleton(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)}get beanName(){const e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName}get beanMeta(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta}get isNamedBean(){return this.beanName!==C}get isFactoryBean(){return g(this._beanProvider.factory)}get isPlainObject(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name}get props(){return this._beanProvider.props}}function A({providers:e,application:s}){const o=u(),i=t(o),[a]=n(()=>{const t=new N(i.container,s);return(e||[]).forEach(e=>{let n;n=g(e)?_(e)?{type:e}:{type:e.constructor,factory:e}:e,t.register(new V(n)).state$.subscribe()}),t});return r(()=>()=>{a.destroy()},[a]),a}const U=({providers:t,application:n,children:r})=>{const s=u(),o=A({providers:t,application:n});return e.createElement(s.Provider,{value:{container:o}},r)};function I(){return(e,t,n)=>{void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));const r=t,s=n.value;return n.value=function(...e){const t=v(this);if(void 0!==t){const n=e=>{const n=t.beanObserver;void 0!==n&&n.effect$.next(Object.assign({effect:r,effectTarget:Reflect.get(this,r)},e))};n({loading:!0,error:null});const o=s.apply(this,e);return m(o)?o.then(e=>(n({data:e}),e)).catch(e=>{throw n({loading:!1,error:e}),e}).finally(()=>{n({loading:!1})}):(n({loading:!1,data:o}),o)}return s.apply(this,e)},n}}function K(e){return(t,n)=>{let r=e;void 0===r&&(r=String(n),r.endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1)))),W().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}}function q(){return(e,t,n)=>(void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),W().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n)}function z(e){return(t,n)=>{W().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}}function G(){return(e,t)=>{W().collectStatedField({name:t,target:e.constructor})}}function H(e){return t=>{const n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;D(t),Object.defineProperty(t,d,{writable:!1,value:!0}),W().collectStatedBean({name:n,target:t,singleton:r})}}function J(e,i){const a=u(),c=t(a),[,p]=n(0);let h,l;void 0!==i&&("object"==typeof i?(h=i.name,l=i.props):"string"!=typeof i&&"symbol"!=typeof i||(h=i));const b=s(e=>{p(e=>e+1)},[]),d=c.container;if(void 0===d)throw new Error("not found stated bean container.");const[f]=n(()=>{let t;if(g(e)&&!_(e)){const n=e;t={type:n.constructor,factory:n,name:h,props:l}}else t={type:e,name:h,props:l};return d.register(new V(t))}),[y]=n(()=>f.state$.subscribe(b));return r(()=>{f.props$.next(l)},[l,f]),r(()=>()=>y.unsubscribe(),[y]),o(f,e=>({metadata:e.beanMeta,bean:e.origin,id:`${e.beanDefinition.beanType.name}#${String(e.beanDefinition.beanName)}`})),f.proxy}function L(e){const o=u(),i=t(o),[,a]=n(0),c="object"==typeof e?e.type:e,{name:p,observedFields:h}="object"==typeof e?e:{},l=s(e=>{(null==h||0===h.length||h.includes(e.fieldMeta.name))&&a(e=>e+1)},[h]),b=i.container;if(void 0===b)throw new Error("not found container");const[d]=n(()=>{let e;if(void 0!==p)e=b.getNamedObserver(p);else if(void 0!==c){const t=b.getTypedObserver(c);if(void 0!==t){if(!(t.length<=1))throw new Error(`Multiple bean [${c.name}] found.`);e=t[0]}}return e});if(void 0===d)throw new Error("bean observer is undefined.");const[f]=n(()=>d.state$.subscribe(l));return r(()=>()=>{f.unsubscribe()},[f]),d.proxy}function Q(e){const[t,s]=n(()=>e instanceof c?e.getValue():null),[o,i]=n(e);return r(()=>{g(e)||i(e)},[e]),r(()=>{let e;return o instanceof a&&(e=o.subscribe(s)),()=>{e&&e.unsubscribe()}},[o]),t}function X(e,o){const i=u();if(void 0===t(i).container)throw new Error("not found container");const[a,c]=n(()=>({loading:!1,error:null,data:null,effect:o})),p=s(e=>{console.log(e.effectTarget===o),e.effect!==o&&e.effectTarget!==o||c(e)},[o]),[h]=n(()=>{const t=v(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(p)});return r(()=>()=>{h.unsubscribe()},[h]),a}export{q as AfterProvided,V as BeanDefinition,T as BeanObserver,M as BeanWrapper,l as DefaultBeanFactory,I as Effect,y as ForceUpdate,K as ObservableProps,z as Props,G as Stated,H as StatedBean,b as StatedBeanApplication,d as StatedBeanClass,h as StatedBeanConsumer,N as StatedBeanContainer,U as StatedBeanProvider,f as StatedBeanWrapper,C as UN_NAMED_BEAN,D as boundClass,E as boundMethod,v as getBeanWrapper,B as getPropertiesWithoutFunction,O as getProperty,u as getStatedBeanContext,x as isBeanContainerAware,P as isDisposableBean,g as isFunction,F as isInitializingBean,m as isPromise,_ as isStatedBeanClass,J as useBean,A as useContainer,L as useInject,Q as useObservable,X as useObserveEffect}; | ||
import e,{useContext as t,useState as n,useEffect as r,useCallback as s,useDebugValue as o}from"react";import{Subject as i,Observable as a,BehaviorSubject as c}from"rxjs";let p;function u(){return p||(p=e.createContext({})),p}const h=({children:t})=>{const n=u();return e.createElement(n.Consumer,null,e=>t(e))};class l{createBean(e){if(e.isFactoryBean){return e.getFactory()(e.props)}return new e.beanType(e.props)}destroyBean(){}}class d{constructor(){this._beanFactory=new l}getBeanFactory(){return this._beanFactory}setBeanFactory(e){return this._beanFactory=e,this}}const b=Symbol("stated-bean-class"),f=Symbol("stated-bean-wrapper"),y=Symbol("stated-bean-force-update");function g(e){return"function"==typeof e}function _(e){return Object.hasOwnProperty.call(e,b)}function v(e){if(Object.hasOwnProperty.call(e,f))return Reflect.get(e,f)}function m(e){return!!e&&"function"==typeof e.then}function B(e){if(e)return Object.keys(e).filter(t=>"function"!=typeof e[t])}class w extends i{constructor(){super(...arguments),this._counter$=new i}_subscribe(e){const t=new a(e=>{const t=super._subscribe(e);return()=>{t.unsubscribe(),this._counter$.next(this.observers.length)}}).subscribe(e);return this._counter$.next(this.observers.length),t}subscribeCount(e){this._counter$.subscribe(e)}complete(){super.complete(),this._counter$.complete()}}class M{constructor(e,t){this._container=e,this._beanName=t,this.state$=new w}get beanObserver(){return this._container.getNamedObserver(this._beanName)}get beanDefinition(){return this.beanObserver.beanDefinition}get beanMeta(){return this.beanDefinition.beanMeta}forceUpdate(e){const t=(this.beanMeta.statedFields||[]).find(t=>t.name===e);void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])}}function O(e,t){if(t in e)return e[t]}function F(e){const t=O(e,"afterProvided");return void 0!==t&&"function"==typeof t}function P(e){const t=O(e,"destroy");return void 0!==t&&"function"==typeof t}function x(e){const t=O(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function j(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function $(e,t){if(j(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(const r of n)if(!Object.prototype.hasOwnProperty.call(t,r)||!j(e[r],t[r]))return!1;return!0}class T{constructor(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new w,this.effect$=new i,this.props$=new i,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});const r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),x(this.proxy)&&this.proxy.setBeanContainer(this._container)}get beanDefinition(){return this._beanDefinition}get proxy(){return this._proxyBean}get origin(){return this._bean}get beanMeta(){return this._beanMeta}destroy(){this.state$.complete(),this.effect$.complete(),P(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()}publishStateAction(e,t){const n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)}_observe(){const e=this._defineStatedBean(this.proxy),t=this.beanMeta.propsFields;return void 0!==t&&(t.forEach(e=>{this._initPropsField(this.proxy,e,this.beanDefinition.props)}),this.props$.subscribe(e=>{t.forEach(t=>{this._updatePropsField(this.proxy,t,e)})})),setTimeout(()=>{if(null!=this.beanMeta.postMethod&&void 0!==this.beanMeta.postMethod.descriptor){this.beanMeta.postMethod.descriptor.value.apply(this.proxy)}else F(this.proxy)&&this.proxy.afterProvided()},0),e}_defineStatedBean(e){let t=v(e);if(void 0===t){t=new M(this._container,this.beanDefinition.beanName),Object.defineProperty(e,f,{value:t}),(this.beanMeta.statedFields||[]).forEach(n=>{this._observeBeanField(t,e,n)})}return t}_observeBeanField(e,t,n){const r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set(s){t[r]=s;const o={bean:t,nextValue:s,prevValue:t[n.name],fieldMeta:n};e.state$.next(o)},get:()=>t[r]})}_initPropsField(e,t,n){const r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new c(r)):Reflect.set(e,t.name,r)}_updatePropsField(e,t,n){const r=e,s=void 0===n?void 0:n[t.prop],o=Reflect.get(r,t.name);if(t.observable){const e=o;$(e.getValue(),s)||e.next(s)}else if(!$(o,s)){const n=String(t.name),o=Reflect.get(r,"set"+n.charAt(0).toUpperCase()+n.slice(1));o&&"function"==typeof o?o.apply(e,[s]):Reflect.set(e,t.name,s),Reflect.set(e,t.name,s)}}}class S{constructor(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}get beanFactory(){return this._container.application.getBeanFactory()}getTypedBean(e){return this._typedBeans.get(e)}getNamedBean(e){return this._namedBeans.get(e)}register(e){const t=this.getNamedBean(e.beanName);if(void 0!==t)return t;const n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n}createBeanObserver(e){const t=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(t);const n=new T(t,this._container,e);n.state$.subscribeCount(e=>{0===e&&(this.remove(n),n.destroy())});const r=v(t);return void 0!==r&&r.state$.subscribeCount(n=>{0===n&&(this.beanFactory.destroyBean(e,t),r.state$.complete())}),n}remove(e){const t=e.beanDefinition;this._namedBeans.delete(t.beanName);const n=this._typedBeans.get(t.beanType);if(void 0!==n){const t=n.indexOf(e);n.splice(t,1)}}_addTypedBean(e,t){const n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)}}class N{constructor(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new d,this._registry=new S(this)}destroy(){}getBeanRegistry(){return this._registry}getTypedObserver(e){let t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t}getNamedObserver(e){let t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t}register(e){return this.getBeanRegistry().register(e)}get parent(){return this._parent}get application(){return this._app}}function E(e,t,n){let r=n.value;if("function"!=typeof r)throw new TypeError(`@boundMethod decorator can only be applied to methods not: ${typeof r}`);let s=!1;return{configurable:!0,get(){if(s||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;const n=r.bind(this);return s=!0,Object.defineProperty(this,t,{configurable:!0,get:()=>n,set(e){r=e,delete this[t]}}),s=!1,n},set(e){r=e}}}function D(e){let t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach(t=>{if("constructor"===t)return;const n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,E(e,t,n))}),e}class R{constructor(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}collectStatedBean(e){const t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)}collectStatedField(e){const t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])}collectPropsField(e){const t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])}collectPostProvided(e){this._postMethod.set(e.target,e)}getBeanMeta(e){return this._beans.get(e)}clear(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}}let k;const W=()=>k||(k=new R),C=Symbol("UN_NAMED_BEAN");class V{constructor(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(`${Date.now()}`),!this.isFactoryBean){const e=W().getBeanMeta(this.beanType);if(void 0===e)throw new Error("bean metadata is undefined.");this._beanMeta=e}}extractFactoryBeanInfo(e){this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach(t=>{"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})}),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(B(e)||[]).map(e=>({name:e,target:this._factoryBeanType}))}:this._factoryBeanMeta=W().getBeanMeta(this._factoryBeanType)}getFactory(){return this._beanProvider.factory}get factoryBeanType(){return this._factoryBeanType}get beanType(){return this._beanProvider.type}get isSingleton(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)}get beanName(){const e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName}get beanMeta(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta}get isNamedBean(){return this.beanName!==C}get isFactoryBean(){return g(this._beanProvider.factory)}get isPlainObject(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name}get props(){return this._beanProvider.props}}function A({providers:e,application:s}){const o=u(),i=t(o),[a]=n(()=>{const t=new N(i.container,s);return(e||[]).forEach(e=>{let n;n=g(e)?_(e)?{type:e}:{type:e.constructor,factory:e}:e,t.register(new V(n)).state$.subscribe()}),t});return r(()=>()=>{a.destroy()},[a]),a}const U=({providers:t,application:n,children:r})=>{const s=u(),o=A({providers:t,application:n});return e.createElement(s.Provider,{value:{container:o}},r)};function I(){return(e,t,n)=>{void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));const r=t,s=n.value;return n.value=function(...e){const t=v(this);if(void 0!==t){const n=e=>{const n=t.beanObserver;void 0!==n&&n.effect$.next(Object.assign({effect:r,effectTarget:Reflect.get(this,r)},e))};n({loading:!0,error:null});const o=s.apply(this,e);if(m(o))return o.then(e=>(n({loading:!1,error:null,data:e}),e)).catch(e=>{throw n({loading:!1,error:e,data:void 0}),e});throw new Error("Effect must decorated for a Promise function")}return s.apply(this,e)},n}}function K(e){return(t,n)=>{let r=e;void 0===r&&(r=String(n),r.endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1)))),W().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}}function q(){return(e,t,n)=>(void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),W().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n)}function z(e){return(t,n)=>{W().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}}function G(){return(e,t)=>{W().collectStatedField({name:t,target:e.constructor})}}function H(e){return t=>{const n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;D(t),Object.defineProperty(t,b,{writable:!1,value:!0}),W().collectStatedBean({name:n,target:t,singleton:r})}}function J(e,i){const a=u(),c=t(a),[,p]=n(0);let h,l;void 0!==i&&("object"==typeof i?(h=i.name,l=i.props):"string"!=typeof i&&"symbol"!=typeof i||(h=i));const d=s(e=>{p(e=>e+1)},[]),b=c.container;if(void 0===b)throw new Error("not found stated bean container.");const[f]=n(()=>{let t;if(g(e)&&!_(e)){const n=e;t={type:n.constructor,factory:n,name:h,props:l}}else t={type:e,name:h,props:l};return b.register(new V(t))}),[y]=n(()=>f.state$.subscribe(d));return r(()=>{f.props$.next(l)},[l,f]),r(()=>()=>y.unsubscribe(),[y]),o(f,e=>({metadata:e.beanMeta,bean:e.origin,id:`${e.beanDefinition.beanType.name}#${String(e.beanDefinition.beanName)}`})),f.proxy}function L(e){const o=u(),i=t(o),[,a]=n(0),c="object"==typeof e?e.type:e,{name:p,observedFields:h}="object"==typeof e?e:{},l=s(e=>{(null==h||0===h.length||h.includes(e.fieldMeta.name))&&a(e=>e+1)},[h]),d=i.container;if(void 0===d)throw new Error("not found container");const[b]=n(()=>{let e;if(void 0!==p)e=d.getNamedObserver(p);else if(void 0!==c){const t=d.getTypedObserver(c);if(void 0!==t){if(!(t.length<=1))throw new Error(`Multiple bean [${c.name}] found.`);e=t[0]}}return e});if(void 0===b)throw new Error("bean observer is undefined.");const[f]=n(()=>b.state$.subscribe(l));return r(()=>()=>{f.unsubscribe()},[f]),b.proxy}function Q(e){const[t,s]=n(()=>e instanceof c?e.getValue():null),[o,i]=n(e);return r(()=>{g(e)||i(e)},[e]),r(()=>{let e;return o instanceof a&&(e=o.subscribe(s)),()=>{e&&e.unsubscribe()}},[o]),t}function X(e,o){const i=u();if(void 0===t(i).container)throw new Error("not found container");const[a,c]=n(()=>({loading:!1,error:null,data:null})),p=s(e=>{e.effect!==o&&e.effectTarget!==o||c({loading:e.loading,error:e.error,data:e.data})},[o]),[h]=n(()=>{const t=v(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(p)});return r(()=>()=>{h.unsubscribe()},[h]),a}export{q as AfterProvided,V as BeanDefinition,T as BeanObserver,M as BeanWrapper,l as DefaultBeanFactory,I as Effect,y as ForceUpdate,K as ObservableProps,z as Props,G as Stated,H as StatedBean,d as StatedBeanApplication,b as StatedBeanClass,h as StatedBeanConsumer,N as StatedBeanContainer,U as StatedBeanProvider,f as StatedBeanWrapper,C as UN_NAMED_BEAN,D as boundClass,E as boundMethod,v as getBeanWrapper,B as getPropertiesWithoutFunction,O as getProperty,u as getStatedBeanContext,x as isBeanContainerAware,P as isDisposableBean,g as isFunction,F as isInitializingBean,m as isPromise,_ as isStatedBeanClass,J as useBean,A as useContainer,L as useInject,Q as useObservable,X as useObserveEffect}; |
@@ -849,17 +849,13 @@ import React, { useContext, useState, useEffect, useCallback, useDebugValue } from 'react'; | ||
.then(function (data) { | ||
emitEffectAction_1({ data: data }); | ||
emitEffectAction_1({ loading: false, error: null, data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, error: e }); | ||
emitEffectAction_1({ loading: false, error: e, data: undefined }); | ||
throw e; | ||
}) | ||
.finally(function () { | ||
emitEffectAction_1({ loading: false }); | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result }); | ||
throw new Error('Effect must decorated for a Promise function'); | ||
} | ||
return result; | ||
} | ||
@@ -1133,9 +1129,7 @@ return originalMethod.apply(this, args); | ||
data: null, | ||
effect: effect, | ||
}; | ||
}), effectState = _a[0], setEffectState = _a[1]; | ||
var listener = useCallback(function (action) { | ||
console.log(action.effectTarget === effect); | ||
if (action.effect === effect || action.effectTarget === effect) { | ||
setEffectState(action); | ||
setEffectState({ loading: action.loading, error: action.error, data: action.data }); | ||
} | ||
@@ -1142,0 +1136,0 @@ }, [effect]); |
@@ -1,1 +0,1 @@ | ||
import e,{useContext as t,useState as n,useEffect as r,useCallback as o,useDebugValue as i}from"react";import{Subject as a,Observable as s,BehaviorSubject as c}from"rxjs";import{__extends as u,__assign as p}from"tslib";var f;function b(){return f||(f=e.createContext({})),f}var y=function(t){var n=t.children,r=b();return e.createElement(r.Consumer,null,(function(e){return n(e)}))},l=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),d=function(){function e(){this._beanFactory=new l}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),h=Symbol("stated-bean-class"),v=Symbol("stated-bean-wrapper"),g=Symbol("stated-bean-force-update");function _(e){return"function"==typeof e}function m(e){return Object.hasOwnProperty.call(e,h)}function O(e){if(Object.hasOwnProperty.call(e,v))return Reflect.get(e,v)}function B(e){return!!e&&"function"==typeof e.then}function P(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var w=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new a,t}return u(t,e),t.prototype._subscribe=function(t){var n=this,r=new s((function(t){var r=e.prototype._subscribe.call(n,t);return function(){r.unsubscribe(),n._counter$.next(n.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),r},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(a),j=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new w}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function M(e,t){if(t in e)return e[t]}function F(e){var t=M(e,"afterProvided");return void 0!==t&&"function"==typeof t}function x(e){var t=M(e,"destroy");return void 0!==t&&"function"==typeof t}function T(e){var t=M(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function S(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function $(e,t){if(S(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!S(e[a],t[a]))return!1}return!0}var N=function(){function e(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new w,this.effect$=new a,this.props$=new a,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),T(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),x(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):F(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=O(e);void 0===n&&(n=new j(this._container,this.beanDefinition.beanName),Object.defineProperty(e,v,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,n){var r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new c(r)):Reflect.set(e,t.name,r)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;$(a.getValue(),o)||a.next(o)}else if(!$(i,o)){var s=String(t.name),c=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));c&&"function"==typeof c?c.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),E=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new N(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=O(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),D=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new d,this._registry=new E(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function R(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function k(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,R(e,t,n))}})),e}var W,C=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),V=function(){return W||(W=new C)},A=Symbol("UN_NAMED_BEAN"),U=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=V().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(P(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=V().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==A},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return _(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function I(e){var o=e.providers,i=e.application,a=b(),s=t(a),c=n((function(){var e=new D(s.container,i);return(o||[]).forEach((function(t){var n;n=_(t)?m(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new U(n)).state$.subscribe()})),e}))[0];return r((function(){return function(){c.destroy()}}),[c]),c}var K=function(t){var n=t.providers,r=t.application,o=t.children,i=b(),a=I({providers:n,application:r});return e.createElement(i.Provider,{value:{container:a}},o)};function q(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var r=t,o=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var i=O(this);if(void 0!==i){var a=function(t){var n=i.beanObserver;void 0!==n&&n.effect$.next(p({effect:r,effectTarget:Reflect.get(e,r)},t))};a({loading:!0,error:null});var s=o.apply(this,t);return B(s)?s.then((function(e){return a({data:e}),e})).catch((function(e){throw a({loading:!1,error:e}),e})).finally((function(){a({loading:!1})})):(a({loading:!1,data:s}),s)}return o.apply(this,t)},n}}function z(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),V().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}}function G(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),V().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}}function H(e){return function(t,n){V().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}}function J(){return function(e,t){V().collectStatedField({name:t,target:e.constructor})}}function L(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;k(t),Object.defineProperty(t,h,{writable:!1,value:!0}),V().collectStatedBean({name:n,target:t,singleton:r})}}function Q(e,a){var s,c,u=b(),p=t(u),f=n(0)[1];void 0!==a&&("object"==typeof a?(s=a.name,c=a.props):"string"!=typeof a&&"symbol"!=typeof a||(s=a));var y=o((function(e){f((function(e){return e+1}))}),[]),l=p.container;if(void 0===l)throw new Error("not found stated bean container.");var d=n((function(){var t;if(_(e)&&!m(e)){var n=e;t={type:n.constructor,factory:n,name:s,props:c}}else t={type:e,name:s,props:c};return l.register(new U(t))}))[0],h=n((function(){return d.state$.subscribe(y)}))[0];return r((function(){d.props$.next(c)}),[c,d]),r((function(){return function(){return h.unsubscribe()}}),[h]),i(d,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),d.proxy}function X(e){var i=b(),a=t(i),s=n(0)[1],c="object"==typeof e?e.type:e,u="object"==typeof e?e:{},p=u.name,f=u.observedFields,y=o((function(e){(null==f||0===f.length||f.includes(e.fieldMeta.name))&&s((function(e){return e+1}))}),[f]),l=a.container;if(void 0===l)throw new Error("not found container");var d=n((function(){var e;if(void 0!==p)e=l.getNamedObserver(p);else if(void 0!==c){var t=l.getTypedObserver(c);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+c.name+"] found.");e=t[0]}}return e}))[0];if(void 0===d)throw new Error("bean observer is undefined.");var h=n((function(){return d.state$.subscribe(y)}))[0];return r((function(){return function(){h.unsubscribe()}}),[h]),d.proxy}function Y(e){var t=n((function(){return e instanceof c?e.getValue():null})),o=t[0],i=t[1],a=n(e),u=a[0],p=a[1];return r((function(){_(e)||p(e)}),[e]),r((function(){var e;return u instanceof s&&(e=u.subscribe(i)),function(){e&&e.unsubscribe()}}),[u]),o}function Z(e,i){var a=b();if(void 0===t(a).container)throw new Error("not found container");var s=n((function(){return{loading:!1,error:null,data:null,effect:i}})),c=s[0],u=s[1],p=o((function(e){console.log(e.effectTarget===i),e.effect!==i&&e.effectTarget!==i||u(e)}),[i]),f=n((function(){var t=O(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(p)}))[0];return r((function(){return function(){f.unsubscribe()}}),[f]),c}export{G as AfterProvided,U as BeanDefinition,N as BeanObserver,j as BeanWrapper,l as DefaultBeanFactory,q as Effect,g as ForceUpdate,z as ObservableProps,H as Props,J as Stated,L as StatedBean,d as StatedBeanApplication,h as StatedBeanClass,y as StatedBeanConsumer,D as StatedBeanContainer,K as StatedBeanProvider,v as StatedBeanWrapper,A as UN_NAMED_BEAN,k as boundClass,R as boundMethod,O as getBeanWrapper,P as getPropertiesWithoutFunction,M as getProperty,b as getStatedBeanContext,T as isBeanContainerAware,x as isDisposableBean,_ as isFunction,F as isInitializingBean,B as isPromise,m as isStatedBeanClass,Q as useBean,I as useContainer,X as useInject,Y as useObservable,Z as useObserveEffect}; | ||
import e,{useContext as t,useState as n,useEffect as r,useCallback as o,useDebugValue as i}from"react";import{Subject as a,Observable as s,BehaviorSubject as c}from"rxjs";import{__extends as u,__assign as p}from"tslib";var f;function b(){return f||(f=e.createContext({})),f}var y=function(t){var n=t.children,r=b();return e.createElement(r.Consumer,null,(function(e){return n(e)}))},l=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),d=function(){function e(){this._beanFactory=new l}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),h=Symbol("stated-bean-class"),v=Symbol("stated-bean-wrapper"),g=Symbol("stated-bean-force-update");function m(e){return"function"==typeof e}function _(e){return Object.hasOwnProperty.call(e,h)}function O(e){if(Object.hasOwnProperty.call(e,v))return Reflect.get(e,v)}function B(e){return!!e&&"function"==typeof e.then}function w(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var P=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new a,t}return u(t,e),t.prototype._subscribe=function(t){var n=this,r=new s((function(t){var r=e.prototype._subscribe.call(n,t);return function(){r.unsubscribe(),n._counter$.next(n.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),r},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(a),j=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new P}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function M(e,t){if(t in e)return e[t]}function F(e){var t=M(e,"afterProvided");return void 0!==t&&"function"==typeof t}function x(e){var t=M(e,"destroy");return void 0!==t&&"function"==typeof t}function T(e){var t=M(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function S(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function $(e,t){if(S(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!S(e[a],t[a]))return!1}return!0}var N=function(){function e(e,t,n){this._bean=e,this._container=t,this._beanDefinition=n,this.state$=new P,this.effect$=new a,this.props$=new a,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var r=this._observe();this._stateSubscription=r.state$.subscribe(this.state$),T(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),x(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):F(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=O(e);void 0===n&&(n=new j(this._container,this.beanDefinition.beanName),Object.defineProperty(e,v,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,n){var r=void 0===n?void 0:n[t.prop];t.observable?Reflect.set(e,t.name,new c(r)):Reflect.set(e,t.name,r)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;$(a.getValue(),o)||a.next(o)}else if(!$(i,o)){var s=String(t.name),c=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));c&&"function"==typeof c?c.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),E=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new N(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=O(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),D=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new d,this._registry=new E(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function R(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function k(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,R(e,t,n))}})),e}var W,C=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),V=function(){return W||(W=new C)},A=Symbol("UN_NAMED_BEAN"),U=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=V().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(w(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=V().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==A},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return m(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function I(e){var o=e.providers,i=e.application,a=b(),s=t(a),c=n((function(){var e=new D(s.container,i);return(o||[]).forEach((function(t){var n;n=m(t)?_(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new U(n)).state$.subscribe()})),e}))[0];return r((function(){return function(){c.destroy()}}),[c]),c}var K=function(t){var n=t.providers,r=t.application,o=t.children,i=b(),a=I({providers:n,application:r});return e.createElement(i.Provider,{value:{container:a}},o)};function q(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var r=t,o=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var i=O(this);if(void 0!==i){var a=function(t){var n=i.beanObserver;void 0!==n&&n.effect$.next(p({effect:r,effectTarget:Reflect.get(e,r)},t))};a({loading:!0,error:null});var s=o.apply(this,t);if(B(s))return s.then((function(e){return a({loading:!1,error:null,data:e}),e})).catch((function(e){throw a({loading:!1,error:e,data:void 0}),e}));throw new Error("Effect must decorated for a Promise function")}return o.apply(this,t)},n}}function z(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),V().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}}function G(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),V().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}}function H(e){return function(t,n){V().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}}function J(){return function(e,t){V().collectStatedField({name:t,target:e.constructor})}}function L(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;k(t),Object.defineProperty(t,h,{writable:!1,value:!0}),V().collectStatedBean({name:n,target:t,singleton:r})}}function Q(e,a){var s,c,u=b(),p=t(u),f=n(0)[1];void 0!==a&&("object"==typeof a?(s=a.name,c=a.props):"string"!=typeof a&&"symbol"!=typeof a||(s=a));var y=o((function(e){f((function(e){return e+1}))}),[]),l=p.container;if(void 0===l)throw new Error("not found stated bean container.");var d=n((function(){var t;if(m(e)&&!_(e)){var n=e;t={type:n.constructor,factory:n,name:s,props:c}}else t={type:e,name:s,props:c};return l.register(new U(t))}))[0],h=n((function(){return d.state$.subscribe(y)}))[0];return r((function(){d.props$.next(c)}),[c,d]),r((function(){return function(){return h.unsubscribe()}}),[h]),i(d,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),d.proxy}function X(e){var i=b(),a=t(i),s=n(0)[1],c="object"==typeof e?e.type:e,u="object"==typeof e?e:{},p=u.name,f=u.observedFields,y=o((function(e){(null==f||0===f.length||f.includes(e.fieldMeta.name))&&s((function(e){return e+1}))}),[f]),l=a.container;if(void 0===l)throw new Error("not found container");var d=n((function(){var e;if(void 0!==p)e=l.getNamedObserver(p);else if(void 0!==c){var t=l.getTypedObserver(c);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+c.name+"] found.");e=t[0]}}return e}))[0];if(void 0===d)throw new Error("bean observer is undefined.");var h=n((function(){return d.state$.subscribe(y)}))[0];return r((function(){return function(){h.unsubscribe()}}),[h]),d.proxy}function Y(e){var t=n((function(){return e instanceof c?e.getValue():null})),o=t[0],i=t[1],a=n(e),u=a[0],p=a[1];return r((function(){m(e)||p(e)}),[e]),r((function(){var e;return u instanceof s&&(e=u.subscribe(i)),function(){e&&e.unsubscribe()}}),[u]),o}function Z(e,i){var a=b();if(void 0===t(a).container)throw new Error("not found container");var s=n((function(){return{loading:!1,error:null,data:null}})),c=s[0],u=s[1],p=o((function(e){e.effect!==i&&e.effectTarget!==i||u({loading:e.loading,error:e.error,data:e.data})}),[i]),f=n((function(){var t=O(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(p)}))[0];return r((function(){return function(){f.unsubscribe()}}),[f]),c}export{G as AfterProvided,U as BeanDefinition,N as BeanObserver,j as BeanWrapper,l as DefaultBeanFactory,q as Effect,g as ForceUpdate,z as ObservableProps,H as Props,J as Stated,L as StatedBean,d as StatedBeanApplication,h as StatedBeanClass,y as StatedBeanConsumer,D as StatedBeanContainer,K as StatedBeanProvider,v as StatedBeanWrapper,A as UN_NAMED_BEAN,k as boundClass,R as boundMethod,O as getBeanWrapper,w as getPropertiesWithoutFunction,M as getProperty,b as getStatedBeanContext,T as isBeanContainerAware,x as isDisposableBean,m as isFunction,F as isInitializingBean,B as isPromise,_ as isStatedBeanClass,Q as useBean,I as useContainer,X as useInject,Y as useObservable,Z as useObserveEffect}; |
@@ -12,3 +12,3 @@ import { StatedFieldMeta } from './StatedMetadata'; | ||
error: unknown; | ||
effect: string | symbol; | ||
effect?: string | symbol; | ||
effectTarget?: Function; | ||
@@ -15,0 +15,0 @@ } |
@@ -853,17 +853,13 @@ (function (global, factory) { | ||
.then(function (data) { | ||
emitEffectAction_1({ data: data }); | ||
emitEffectAction_1({ loading: false, error: null, data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, error: e }); | ||
emitEffectAction_1({ loading: false, error: e, data: undefined }); | ||
throw e; | ||
}) | ||
.finally(function () { | ||
emitEffectAction_1({ loading: false }); | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result }); | ||
throw new Error('Effect must decorated for a Promise function'); | ||
} | ||
return result; | ||
} | ||
@@ -1137,9 +1133,7 @@ return originalMethod.apply(this, args); | ||
data: null, | ||
effect: effect, | ||
}; | ||
}), effectState = _a[0], setEffectState = _a[1]; | ||
var listener = React.useCallback(function (action) { | ||
console.log(action.effectTarget === effect); | ||
if (action.effect === effect || action.effectTarget === effect) { | ||
setEffectState(action); | ||
setEffectState({ loading: action.loading, error: action.error, data: action.data }); | ||
} | ||
@@ -1146,0 +1140,0 @@ }, [effect]); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("rxjs"),require("tslib")):"function"==typeof define&&define.amd?define(["exports","react","rxjs","tslib"],t):t((e=e||self).StatedBean={},e.React,e.rxjs,e.tslib)}(this,(function(e,t,n,r){"use strict";var o,i="default"in t?t.default:t;function a(){return o||(o=i.createContext({})),o}var s=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),u=function(){function e(){this._beanFactory=new s}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),c=Symbol("stated-bean-class"),p=Symbol("stated-bean-wrapper"),f=Symbol("stated-bean-force-update");function b(e){return"function"==typeof e}function l(e){return Object.hasOwnProperty.call(e,c)}function d(e){if(Object.hasOwnProperty.call(e,p))return Reflect.get(e,p)}function y(e){return!!e&&"function"==typeof e.then}function h(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var v=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new n.Subject,t}return r.__extends(t,e),t.prototype._subscribe=function(t){var r=this,o=new n.Observable((function(t){var n=e.prototype._subscribe.call(r,t);return function(){n.unsubscribe(),r._counter$.next(r.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),o},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(n.Subject),g=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new v}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function _(e,t){if(t in e)return e[t]}function m(e){var t=_(e,"afterProvided");return void 0!==t&&"function"==typeof t}function B(e){var t=_(e,"destroy");return void 0!==t&&"function"==typeof t}function O(e){var t=_(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function P(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function j(e,t){if(P(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!P(e[a],t[a]))return!1}return!0}var w=function(){function e(e,t,r){this._bean=e,this._container=t,this._beanDefinition=r,this.state$=new v,this.effect$=new n.Subject,this.props$=new n.Subject,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var o=this._observe();this._stateSubscription=o.state$.subscribe(this.state$),O(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),B(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):m(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=d(e);void 0===n&&(n=new g(this._container,this.beanDefinition.beanName),Object.defineProperty(e,p,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,r){var o=void 0===r?void 0:r[t.prop];t.observable?Reflect.set(e,t.name,new n.BehaviorSubject(o)):Reflect.set(e,t.name,o)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;j(a.getValue(),o)||a.next(o)}else if(!j(i,o)){var s=String(t.name),u=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));u&&"function"==typeof u?u.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),M=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new w(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=d(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),S=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u,this._registry=new M(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function F(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function x(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,F(e,t,n))}})),e}var E,T=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),N=function(){return E||(E=new T)},$=Symbol("UN_NAMED_BEAN"),C=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=N().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(h(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=N().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==$},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return b(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function D(e){var n=e.providers,r=e.application,o=a(),i=t.useContext(o),s=t.useState((function(){var e=new S(i.container,r);return(n||[]).forEach((function(t){var n;n=b(t)?l(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new C(n)).state$.subscribe()})),e}))[0];return t.useEffect((function(){return function(){s.destroy()}}),[s]),s}e.AfterProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),N().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},e.BeanDefinition=C,e.BeanObserver=w,e.BeanWrapper=g,e.DefaultBeanFactory=s,e.Effect=function(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var o=t,i=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var a=d(this);if(void 0!==a){var s=function(t){var n=a.beanObserver;void 0!==n&&n.effect$.next(r.__assign({effect:o,effectTarget:Reflect.get(e,o)},t))};s({loading:!0,error:null});var u=i.apply(this,t);return y(u)?u.then((function(e){return s({data:e}),e})).catch((function(e){throw s({loading:!1,error:e}),e})).finally((function(){s({loading:!1})})):(s({loading:!1,data:u}),u)}return i.apply(this,t)},n}},e.ForceUpdate=f,e.ObservableProps=function(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),N().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}},e.Props=function(e){return function(t,n){N().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}},e.Stated=function(){return function(e,t){N().collectStatedField({name:t,target:e.constructor})}},e.StatedBean=function(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;x(t),Object.defineProperty(t,c,{writable:!1,value:!0}),N().collectStatedBean({name:n,target:t,singleton:r})}},e.StatedBeanApplication=u,e.StatedBeanClass=c,e.StatedBeanConsumer=function(e){var t=e.children,n=a();return i.createElement(n.Consumer,null,(function(e){return t(e)}))},e.StatedBeanContainer=S,e.StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,o=a(),s=D({providers:t,application:n});return i.createElement(o.Provider,{value:{container:s}},r)},e.StatedBeanWrapper=p,e.UN_NAMED_BEAN=$,e.boundClass=x,e.boundMethod=F,e.getBeanWrapper=d,e.getPropertiesWithoutFunction=h,e.getProperty=_,e.getStatedBeanContext=a,e.isBeanContainerAware=O,e.isDisposableBean=B,e.isFunction=b,e.isInitializingBean=m,e.isPromise=y,e.isStatedBeanClass=l,e.useBean=function(e,n){var r,o,i=a(),s=t.useContext(i),u=t.useState(0)[1];void 0!==n&&("object"==typeof n?(r=n.name,o=n.props):"string"!=typeof n&&"symbol"!=typeof n||(r=n));var c=t.useCallback((function(e){u((function(e){return e+1}))}),[]),p=s.container;if(void 0===p)throw new Error("not found stated bean container.");var f=t.useState((function(){var t;if(b(e)&&!l(e)){var n=e;t={type:n.constructor,factory:n,name:r,props:o}}else t={type:e,name:r,props:o};return p.register(new C(t))}))[0],d=t.useState((function(){return f.state$.subscribe(c)}))[0];return t.useEffect((function(){f.props$.next(o)}),[o,f]),t.useEffect((function(){return function(){return d.unsubscribe()}}),[d]),t.useDebugValue(f,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),f.proxy},e.useContainer=D,e.useInject=function(e){var n=a(),r=t.useContext(n),o=t.useState(0)[1],i="object"==typeof e?e.type:e,s="object"==typeof e?e:{},u=s.name,c=s.observedFields,p=t.useCallback((function(e){(null==c||0===c.length||c.includes(e.fieldMeta.name))&&o((function(e){return e+1}))}),[c]),f=r.container;if(void 0===f)throw new Error("not found container");var b=t.useState((function(){var e;if(void 0!==u)e=f.getNamedObserver(u);else if(void 0!==i){var t=f.getTypedObserver(i);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+i.name+"] found.");e=t[0]}}return e}))[0];if(void 0===b)throw new Error("bean observer is undefined.");var l=t.useState((function(){return b.state$.subscribe(p)}))[0];return t.useEffect((function(){return function(){l.unsubscribe()}}),[l]),b.proxy},e.useObservable=function(e){var r=t.useState((function(){return e instanceof n.BehaviorSubject?e.getValue():null})),o=r[0],i=r[1],a=t.useState(e),s=a[0],u=a[1];return t.useEffect((function(){b(e)||u(e)}),[e]),t.useEffect((function(){var e;return s instanceof n.Observable&&(e=s.subscribe(i)),function(){e&&e.unsubscribe()}}),[s]),o},e.useObserveEffect=function(e,n){var r=a();if(void 0===t.useContext(r).container)throw new Error("not found container");var o=t.useState((function(){return{loading:!1,error:null,data:null,effect:n}})),i=o[0],s=o[1],u=t.useCallback((function(e){console.log(e.effectTarget===n),e.effect!==n&&e.effectTarget!==n||s(e)}),[n]),c=t.useState((function(){var t=d(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(u)}))[0];return t.useEffect((function(){return function(){c.unsubscribe()}}),[c]),i},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("rxjs"),require("tslib")):"function"==typeof define&&define.amd?define(["exports","react","rxjs","tslib"],t):t((e=e||self).StatedBean={},e.React,e.rxjs,e.tslib)}(this,(function(e,t,n,r){"use strict";var o,i="default"in t?t.default:t;function a(){return o||(o=i.createContext({})),o}var s=function(){function e(){}return e.prototype.createBean=function(e){return e.isFactoryBean?e.getFactory()(e.props):new e.beanType(e.props)},e.prototype.destroyBean=function(){},e}(),u=function(){function e(){this._beanFactory=new s}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e}(),c=Symbol("stated-bean-class"),p=Symbol("stated-bean-wrapper"),f=Symbol("stated-bean-force-update");function b(e){return"function"==typeof e}function l(e){return Object.hasOwnProperty.call(e,c)}function d(e){if(Object.hasOwnProperty.call(e,p))return Reflect.get(e,p)}function y(e){return!!e&&"function"==typeof e.then}function h(e){if(e)return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var v=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new n.Subject,t}return r.__extends(t,e),t.prototype._subscribe=function(t){var r=this,o=new n.Observable((function(t){var n=e.prototype._subscribe.call(r,t);return function(){n.unsubscribe(),r._counter$.next(r.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),o},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t.prototype.complete=function(){e.prototype.complete.call(this),this._counter$.complete()},t}(n.Subject),g=function(){function e(e,t){this._container=e,this._beanName=t,this.state$=new v}return Object.defineProperty(e.prototype,"beanObserver",{get:function(){return this._container.getNamedObserver(this._beanName)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this.beanObserver.beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.beanDefinition.beanMeta},enumerable:!0,configurable:!0}),e.prototype.forceUpdate=function(e){var t=(this.beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==t&&this.beanObserver.publishStateAction(t,this.beanObserver.proxy[e])},e}();function _(e,t){if(t in e)return e[t]}function m(e){var t=_(e,"afterProvided");return void 0!==t&&"function"==typeof t}function B(e){var t=_(e,"destroy");return void 0!==t&&"function"==typeof t}function O(e){var t=_(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}function P(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function j(e,t){if(P(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0,i=n;o<i.length;o++){var a=i[o];if(!Object.prototype.hasOwnProperty.call(t,a)||!P(e[a],t[a]))return!1}return!0}var w=function(){function e(e,t,r){this._bean=e,this._container=t,this._beanDefinition=r,this.state$=new v,this.effect$=new n.Subject,this.props$=new n.Subject,this._beanMeta=this.beanDefinition.beanMeta,this._proxyBean=new Proxy(this.origin,{});var o=this._observe();this._stateSubscription=o.state$.subscribe(this.state$),O(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"beanDefinition",{get:function(){return this._beanDefinition},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"origin",{get:function(){return this._bean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this._beanMeta},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),B(this.proxy)&&this.proxy.destroy(),void 0!==this._stateSubscription&&this._stateSubscription.unsubscribe()},e.prototype.publishStateAction=function(e,t){var n={bean:this.proxy,nextValue:t,prevValue:this.proxy[e.name],fieldMeta:e};this.state$.next(n)},e.prototype._observe=function(){var e=this,t=this._defineStatedBean(this.proxy),n=this.beanMeta.propsFields;return void 0!==n&&(n.forEach((function(t){e._initPropsField(e.proxy,t,e.beanDefinition.props)})),this.props$.subscribe((function(t){n.forEach((function(n){e._updatePropsField(e.proxy,n,t)}))}))),setTimeout((function(){null!=e.beanMeta.postMethod&&void 0!==e.beanMeta.postMethod.descriptor?e.beanMeta.postMethod.descriptor.value.apply(e.proxy):m(e.proxy)&&e.proxy.afterProvided()}),0),t},e.prototype._defineStatedBean=function(e){var t=this,n=d(e);void 0===n&&(n=new g(this._container,this.beanDefinition.beanName),Object.defineProperty(e,p,{value:n}),(this.beanMeta.statedFields||[]).forEach((function(r){t._observeBeanField(n,e,r)})));return n},e.prototype._observeBeanField=function(e,t,n){var r=Symbol(n.name.toString()+"_v");Object.defineProperty(t,r,{writable:!0,value:t[n.name]}),Object.defineProperty(t,n.name.toString(),{set:function(o){t[r]=o;var i={bean:t,nextValue:o,prevValue:t[n.name],fieldMeta:n};e.state$.next(i)},get:function(){return t[r]}})},e.prototype._initPropsField=function(e,t,r){var o=void 0===r?void 0:r[t.prop];t.observable?Reflect.set(e,t.name,new n.BehaviorSubject(o)):Reflect.set(e,t.name,o)},e.prototype._updatePropsField=function(e,t,n){var r=e,o=void 0===n?void 0:n[t.prop],i=Reflect.get(r,t.name);if(t.observable){var a=i;j(a.getValue(),o)||a.next(o)}else if(!j(i,o)){var s=String(t.name),u=Reflect.get(r,"set"+s.charAt(0).toUpperCase()+s.slice(1));u&&"function"==typeof u?u.apply(e,[o]):Reflect.set(e,t.name,o),Reflect.set(e,t.name,o)}},e}(),M=function(){function e(e){this._container=e,this._typedBeans=new WeakMap,this._namedBeans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.getTypedBean=function(e){return this._typedBeans.get(e)},e.prototype.getNamedBean=function(e){return this._namedBeans.get(e)},e.prototype.register=function(e){var t=this.getNamedBean(e.beanName);if(void 0!==t)return t;var n=this.createBeanObserver(e);return this._namedBeans.set(e.beanName,n),this._addTypedBean(e.beanType,n),n},e.prototype.createBeanObserver=function(e){var t=this,n=this.beanFactory.createBean(e);e.isFactoryBean&&e.extractFactoryBeanInfo(n);var r=new w(n,this._container,e);r.state$.subscribeCount((function(e){0===e&&(t.remove(r),r.destroy())}));var o=d(n);return void 0!==o&&o.state$.subscribeCount((function(r){0===r&&(t.beanFactory.destroyBean(e,n),o.state$.complete())})),r},e.prototype.remove=function(e){var t=e.beanDefinition;this._namedBeans.delete(t.beanName);var n=this._typedBeans.get(t.beanType);if(void 0!==n){var r=n.indexOf(e);n.splice(r,1)}},e.prototype._addTypedBean=function(e,t){var n=this._typedBeans.get(e);void 0===n?this._typedBeans.set(e,[t]):n.unshift(t)},e}(),S=function(){function e(e,t){this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u,this._registry=new M(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getTypedObserver=function(e){var t=this.getBeanRegistry().getTypedBean(e);return null==t&&this.parent&&(t=this.parent.getTypedObserver(e)),t},e.prototype.getNamedObserver=function(e){var t=this.getBeanRegistry().getNamedBean(e);return null==t&&this.parent&&(t=this.parent.getNamedObserver(e)),t},e.prototype.register=function(e){return this.getBeanRegistry().register(e)},Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),e}();function F(e,t,n){var r=n.value;if("function"!=typeof r)throw new TypeError("@boundMethod decorator can only be applied to methods not: "+typeof r);var o=!1;return{configurable:!0,get:function(){if(o||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),o=!1,n},set:function(e){r=e}}}function x(e){var t;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?t=Reflect.ownKeys(e.prototype):(t=Object.getOwnPropertyNames(e.prototype),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(e.prototype)))),t.forEach((function(t){if("constructor"!==t){var n=Object.getOwnPropertyDescriptor(e.prototype,t);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(e.prototype,t,F(e,t,n))}})),e}var E,T=function(){function e(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._statedFields.get(t);e.statedFields=n,e.propsFields=this._propsFields.get(t),e.postMethod=this._postMethod.get(t),this._beans.set(t,e),this._statedFields.delete(t),this._propsFields.delete(t),this._postMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._statedFields.get(t);n?n.push(e):this._statedFields.set(t,[e])},e.prototype.collectPropsField=function(e){var t=e.target,n=this._propsFields.get(t);n?n.push(e):this._propsFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._postMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._statedFields=new WeakMap,this._propsFields=new WeakMap,this._postMethod=new WeakMap},e}(),N=function(){return E||(E=new T)},$=Symbol("UN_NAMED_BEAN"),C=function(){function e(e){if(this._beanProvider=e,this._factoryBeanType=void 0,this._factoryBeanMeta=void 0,this._beanName=Symbol(""+Date.now()),!this.isFactoryBean){var t=N().getBeanMeta(this.beanType);if(void 0===t)throw new Error("bean metadata is undefined.");this._beanMeta=t}}return e.prototype.extractFactoryBeanInfo=function(e){var t=this;this._factoryBeanType=e.constructor,this.isPlainObject&&Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),this.isPlainObject?this._factoryBeanMeta={target:this._factoryBeanType,name:this._beanProvider.name,statedFields:(h(e)||[]).map((function(e){return{name:e,target:t._factoryBeanType}}))}:this._factoryBeanMeta=N().getBeanMeta(this._factoryBeanType)},e.prototype.getFactory=function(){return this._beanProvider.factory},Object.defineProperty(e.prototype,"factoryBeanType",{get:function(){return this._factoryBeanType},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanType",{get:function(){return this._beanProvider.type},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isSingleton",{get:function(){return!!(this._beanProvider.singleton||this.beanMeta&&this.beanMeta.singleton)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanName",{get:function(){var e=this._beanProvider.name||(this.beanMeta?this.beanMeta.name:void 0);return this.isSingleton?e||this.beanType.name:e||this._beanName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"beanMeta",{get:function(){return this.isFactoryBean?this._factoryBeanMeta:this._beanMeta},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isNamedBean",{get:function(){return this.beanName!==$},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isFactoryBean",{get:function(){return b(this._beanProvider.factory)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isPlainObject",{get:function(){return this.isFactoryBean&&this.factoryBeanType?"Object"===this.factoryBeanType.name:"Object"===this.beanType.name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"props",{get:function(){return this._beanProvider.props},enumerable:!0,configurable:!0}),e}();function D(e){var n=e.providers,r=e.application,o=a(),i=t.useContext(o),s=t.useState((function(){var e=new S(i.container,r);return(n||[]).forEach((function(t){var n;n=b(t)?l(t)?{type:t}:{type:t.constructor,factory:t}:t,e.register(new C(n)).state$.subscribe()})),e}))[0];return t.useEffect((function(){return function(){s.destroy()}}),[s]),s}e.AfterProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),N().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},e.BeanDefinition=C,e.BeanObserver=w,e.BeanWrapper=g,e.DefaultBeanFactory=s,e.Effect=function(){return function(e,t,n){void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t));var o=t,i=n.value;return n.value=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var a=d(this);if(void 0!==a){var s=function(t){var n=a.beanObserver;void 0!==n&&n.effect$.next(r.__assign({effect:o,effectTarget:Reflect.get(e,o)},t))};s({loading:!0,error:null});var u=i.apply(this,t);if(y(u))return u.then((function(e){return s({loading:!1,error:null,data:e}),e})).catch((function(e){throw s({loading:!1,error:e,data:void 0}),e}));throw new Error("Effect must decorated for a Promise function")}return i.apply(this,t)},n}},e.ForceUpdate=f,e.ObservableProps=function(e){return function(t,n){var r=e;void 0===r&&(r=String(n)).endsWith("$")&&(r=r.slice(0,Math.max(0,r.length-1))),N().collectPropsField({name:n,prop:r,target:t.constructor,observable:!0})}},e.Props=function(e){return function(t,n){N().collectPropsField({name:n,prop:e||String(n),target:t.constructor,observable:!1})}},e.Stated=function(){return function(e,t){N().collectStatedField({name:t,target:e.constructor})}},e.StatedBean=function(e){return function(t){var n="object"==typeof e?e.name:e,r="object"==typeof e&&e.singleton;x(t),Object.defineProperty(t,c,{writable:!1,value:!0}),N().collectStatedBean({name:n,target:t,singleton:r})}},e.StatedBeanApplication=u,e.StatedBeanClass=c,e.StatedBeanConsumer=function(e){var t=e.children,n=a();return i.createElement(n.Consumer,null,(function(e){return t(e)}))},e.StatedBeanContainer=S,e.StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,o=a(),s=D({providers:t,application:n});return i.createElement(o.Provider,{value:{container:s}},r)},e.StatedBeanWrapper=p,e.UN_NAMED_BEAN=$,e.boundClass=x,e.boundMethod=F,e.getBeanWrapper=d,e.getPropertiesWithoutFunction=h,e.getProperty=_,e.getStatedBeanContext=a,e.isBeanContainerAware=O,e.isDisposableBean=B,e.isFunction=b,e.isInitializingBean=m,e.isPromise=y,e.isStatedBeanClass=l,e.useBean=function(e,n){var r,o,i=a(),s=t.useContext(i),u=t.useState(0)[1];void 0!==n&&("object"==typeof n?(r=n.name,o=n.props):"string"!=typeof n&&"symbol"!=typeof n||(r=n));var c=t.useCallback((function(e){u((function(e){return e+1}))}),[]),p=s.container;if(void 0===p)throw new Error("not found stated bean container.");var f=t.useState((function(){var t;if(b(e)&&!l(e)){var n=e;t={type:n.constructor,factory:n,name:r,props:o}}else t={type:e,name:r,props:o};return p.register(new C(t))}))[0],d=t.useState((function(){return f.state$.subscribe(c)}))[0];return t.useEffect((function(){f.props$.next(o)}),[o,f]),t.useEffect((function(){return function(){return d.unsubscribe()}}),[d]),t.useDebugValue(f,(function(e){return{metadata:e.beanMeta,bean:e.origin,id:e.beanDefinition.beanType.name+"#"+String(e.beanDefinition.beanName)}})),f.proxy},e.useContainer=D,e.useInject=function(e){var n=a(),r=t.useContext(n),o=t.useState(0)[1],i="object"==typeof e?e.type:e,s="object"==typeof e?e:{},u=s.name,c=s.observedFields,p=t.useCallback((function(e){(null==c||0===c.length||c.includes(e.fieldMeta.name))&&o((function(e){return e+1}))}),[c]),f=r.container;if(void 0===f)throw new Error("not found container");var b=t.useState((function(){var e;if(void 0!==u)e=f.getNamedObserver(u);else if(void 0!==i){var t=f.getTypedObserver(i);if(void 0!==t){if(!(t.length<=1))throw new Error("Multiple bean ["+i.name+"] found.");e=t[0]}}return e}))[0];if(void 0===b)throw new Error("bean observer is undefined.");var l=t.useState((function(){return b.state$.subscribe(p)}))[0];return t.useEffect((function(){return function(){l.unsubscribe()}}),[l]),b.proxy},e.useObservable=function(e){var r=t.useState((function(){return e instanceof n.BehaviorSubject?e.getValue():null})),o=r[0],i=r[1],a=t.useState(e),s=a[0],u=a[1];return t.useEffect((function(){b(e)||u(e)}),[e]),t.useEffect((function(){var e;return s instanceof n.Observable&&(e=s.subscribe(i)),function(){e&&e.unsubscribe()}}),[s]),o},e.useObserveEffect=function(e,n){var r=a();if(void 0===t.useContext(r).container)throw new Error("not found container");var o=t.useState((function(){return{loading:!1,error:null,data:null}})),i=o[0],s=o[1],u=t.useCallback((function(e){e.effect!==n&&e.effectTarget!==n||s({loading:e.loading,error:e.error,data:e.data})}),[n]),c=t.useState((function(){var t=d(e);if(void 0===t)throw new Error("bean observer is undefined");return t.beanObserver.effect$.subscribe(u)}))[0];return t.useEffect((function(){return function(){c.unsubscribe()}}),[c]),i},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "stated-bean", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "A light but scalable state management library with react hooks", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:mjolnirjs/stated-bean.git", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
407231
6314