stated-bean
Advanced tools
Comparing version 0.5.1 to 0.5.2
384
lib/cjs.js
@@ -13,19 +13,65 @@ 'use strict'; | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry() { | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (provider) { | ||
if (provider.bean !== undefined) { | ||
return provider.bean; | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var obj; | ||
if (!('bean' in provider)) { | ||
// eslint-disable-next-line new-cap | ||
return new provider.type(); | ||
obj = new provider.type(); | ||
} | ||
else { | ||
obj = provider.bean; | ||
} | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, obj)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, obj); | ||
} | ||
}; | ||
DefaultBeanFactory.prototype.remove = function () { | ||
// | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
// @internal | ||
this._registry = new StatedBeanRegistry(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (type, identity) { | ||
return this._registry.get(type, identity); | ||
}; | ||
DefaultBeanFactory.prototype.register = function (provider) { | ||
this._registry.register(provider); | ||
}; | ||
DefaultBeanFactory.prototype.remove = function (type, identity) { | ||
this._registry.remove(type, identity); | ||
}; | ||
return DefaultBeanFactory; | ||
@@ -107,18 +153,2 @@ }()); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
function getPropertiesWithoutFunction(obj) { | ||
return Object.keys(obj).filter(function (key) { return typeof obj[key] !== 'function'; }); | ||
} | ||
var CountableSubject = /** @class */ (function (_super) { | ||
@@ -133,2 +163,3 @@ tslib.__extends(CountableSubject, _super); | ||
var _this = this; | ||
var self = this; | ||
var observable = new rxjs.Observable(function (_subscriber) { | ||
@@ -138,3 +169,3 @@ var _subscription = _super.prototype._subscribe.call(_this, _subscriber); | ||
_subscription.unsubscribe(); | ||
_this._counter$.next(_this.observers.length); | ||
_this._counter$.next(self.observers.length); | ||
}; | ||
@@ -152,2 +183,13 @@ }); | ||
var NoSuchBeanDefinitionError = /** @class */ (function (_super) { | ||
tslib.__extends(NoSuchBeanDefinitionError, _super); | ||
function NoSuchBeanDefinitionError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return NoSuchBeanDefinitionError; | ||
}(Error)); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function getProperty(obj, key) { | ||
@@ -174,3 +216,2 @@ if (key in obj) { | ||
function BeanObserver(_container, _provider) { | ||
var _this = this; | ||
this._container = _container; | ||
@@ -182,27 +223,10 @@ this._provider = _provider; | ||
if (beanMeta === undefined) { | ||
this._beanMeta = { | ||
name: this._provider.identity, | ||
target: this._provider.type, | ||
statedFields: getPropertiesWithoutFunction(this._provider.bean).map(function (key) { return ({ | ||
name: key, | ||
target: _this._provider.type, | ||
}); }), | ||
}; | ||
this._proxyBean = this._observePlainObject(this._provider.bean); | ||
throw new NoSuchBeanDefinitionError(this._provider.type.name); | ||
} | ||
else { | ||
this._beanMeta = beanMeta; | ||
this._proxyBean = this._observe(this._provider.bean); | ||
this._beanMeta = beanMeta; | ||
if (isBeanContainerAware(this.bean)) { | ||
this.bean.setBeanContainer(this._container); | ||
} | ||
if (isBeanContainerAware(this.proxy)) { | ||
this.proxy.setBeanContainer(this._container); | ||
} | ||
this._observe(); | ||
} | ||
Object.defineProperty(BeanObserver.prototype, "proxy", { | ||
get: function () { | ||
return this._proxyBean; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BeanObserver.prototype, "bean", { | ||
@@ -218,4 +242,4 @@ get: function () { | ||
this.effect$.complete(); | ||
if (isDisposableBean(this.proxy)) { | ||
this.proxy.destroy(); | ||
if (isDisposableBean(this.bean)) { | ||
this.bean.destroy(); | ||
} | ||
@@ -233,41 +257,22 @@ }; | ||
}; | ||
BeanObserver.prototype._observe = function (bean) { | ||
BeanObserver.prototype._observe = function () { | ||
var _this = this; | ||
this._defineStatedBean(); | ||
var fields = this._beanMeta.statedFields || []; | ||
// const proxyBean = this._createProxyBean(bean, fields); | ||
var proxyBean = new Proxy(bean, {}); | ||
this._defineStatedBean(proxyBean); | ||
fields.forEach(function (field) { | ||
_this._observeBeanField(proxyBean, field); | ||
}); | ||
setTimeout(function () { | ||
if (_this._beanMeta.postMethod != null && | ||
_this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = _this._beanMeta.postMethod.descriptor.value; | ||
f.apply(proxyBean); | ||
} | ||
else if (isInitializingBean(proxyBean)) { | ||
proxyBean.postProvided(); | ||
} | ||
}, 0); | ||
return proxyBean; | ||
fields.forEach(function (field) { return _this._observeBeanField(field); }); | ||
if (this._beanMeta.postMethod != null && | ||
this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = this._beanMeta.postMethod.descriptor.value; | ||
f.apply(this.bean); | ||
} | ||
else if (isInitializingBean(this.bean)) { | ||
this.bean.postProvided(); | ||
} | ||
}; | ||
BeanObserver.prototype._observePlainObject = function (bean) { | ||
var proxyBean = this._observe(bean); | ||
Object.keys(proxyBean).forEach(function (key) { | ||
if (typeof proxyBean[key] === 'function') { | ||
Object.defineProperty(proxyBean, key, { | ||
value: proxyBean[key].bind(proxyBean), | ||
}); | ||
} | ||
}); | ||
return proxyBean; | ||
}; | ||
// @internal | ||
BeanObserver.prototype._defineStatedBean = function (bean) { | ||
BeanObserver.prototype._defineStatedBean = function () { | ||
var self = this; | ||
Object.defineProperty(bean, StatedBeanSymbol, { | ||
Object.defineProperty(this.bean, StatedBeanSymbol, { | ||
value: { | ||
identity: self._provider.identity, | ||
type: self._provider.type, | ||
name: self._provider.identity, | ||
container: self._container, | ||
@@ -285,16 +290,16 @@ forceUpdate: function (field) { | ||
// @internal | ||
BeanObserver.prototype._observeBeanField = function (bean, fieldMeta) { | ||
BeanObserver.prototype._observeBeanField = function (fieldMeta) { | ||
var proxyField = Symbol(fieldMeta.name.toString() + '_v'); | ||
Object.defineProperty(bean, proxyField, { | ||
Object.defineProperty(this.bean, proxyField, { | ||
writable: true, | ||
value: bean[fieldMeta.name], | ||
value: this.bean[fieldMeta.name], | ||
}); | ||
var self = this; | ||
Object.defineProperty(bean, fieldMeta.name.toString(), { | ||
Object.defineProperty(this.bean, fieldMeta.name.toString(), { | ||
set: function (value) { | ||
bean[proxyField] = value; | ||
self.bean[proxyField] = value; | ||
self.publishStateAction(fieldMeta, value); | ||
}, | ||
get: function () { | ||
return bean[proxyField]; | ||
return self.bean[proxyField]; | ||
}, | ||
@@ -307,63 +312,2 @@ }); | ||
/** | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry(_container) { | ||
this._container = _container; | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
Object.defineProperty(StatedBeanRegistry.prototype, "beanFactory", { | ||
get: function () { | ||
return this._container.application.getBeanFactory(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var _this = this; | ||
var bean = this.beanFactory.get(provider); | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var strictProvider = { | ||
type: provider.type, | ||
bean: bean, | ||
identity: beanIdentity, | ||
}; | ||
var beanObserver = new BeanObserver(this._container, strictProvider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
_this.remove(strictProvider.type, strictProvider.identity); | ||
} | ||
}); | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, beanObserver)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, beanObserver); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* `StatedBeanContainer` is responsible for registering and managing `bean` and observing its `@Stated()` property changes. | ||
@@ -373,2 +317,4 @@ */ | ||
function StatedBeanContainer(parent, app) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._beanObservers = new WeakMap(); | ||
this._parent = parent; | ||
@@ -384,9 +330,9 @@ if (app != null) { | ||
} | ||
this._registry = new StatedBeanRegistry(this); | ||
} | ||
StatedBeanContainer.prototype.destroy = function () { | ||
// container destroy | ||
this._beanObservers = new WeakMap(); | ||
}; | ||
StatedBeanContainer.prototype.getBeanRegistry = function () { | ||
return this._registry; | ||
StatedBeanContainer.prototype.getBeanFactory = function () { | ||
return this._app.getBeanFactory(); | ||
}; | ||
@@ -406,38 +352,30 @@ StatedBeanContainer.prototype.getBeanIdentity = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
var bean = this.getBeanObserver(type, identity); | ||
var bean = this.getBeanFactory().get(type, identity); | ||
if (bean == null && this.parent) { | ||
bean = this.parent.getBeanObserver(type, name); | ||
bean = this.parent.getBean(type, name); | ||
} | ||
if (bean !== undefined) { | ||
return bean.proxy; | ||
} | ||
return undefined; | ||
return bean; | ||
}; | ||
StatedBeanContainer.prototype.getBeanObserver = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
return this.getBeanRegistry().get(type, identity); | ||
StatedBeanContainer.prototype.getBeanObserver = function (bean) { | ||
return this._beanObservers.get(bean); | ||
}; | ||
StatedBeanContainer.prototype.register = function (provider) { | ||
var beanFactory = this.getBeanRegistry(); | ||
var beanFactory = this.getBeanFactory(); | ||
if (provider !== undefined) { | ||
beanFactory.register(provider); | ||
if (beanFactory.get(provider.type, provider.identity) === undefined) { | ||
beanFactory.register(provider); | ||
} | ||
} | ||
}; | ||
StatedBeanContainer.prototype.registerAndObserve = function (provider) { | ||
try { | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
var observer = this.getBeanObserver(provider.type, this.getBeanIdentity(provider.type, provider.identity)); | ||
if (observer === undefined) { | ||
throw new Error('observer is undefined'); | ||
} | ||
return observer; | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
return this._observeBean({ | ||
type: provider.type, | ||
bean: bean, | ||
identity: this.getBeanIdentity(provider.type, provider.identity), | ||
}); | ||
}; | ||
@@ -447,5 +385,26 @@ StatedBeanContainer.prototype.hasBean = function (type, name) { | ||
}; | ||
StatedBeanContainer.prototype.remove = function (type, name) { | ||
this.getBeanRegistry().remove(type, name); | ||
StatedBeanContainer.prototype.remove = function (type, bean, name) { | ||
if (this._beanObservers) { | ||
this._beanObservers.delete(bean); | ||
} | ||
this.getBeanFactory().remove(type, name); | ||
}; | ||
StatedBeanContainer.prototype._observeBean = function (provider) { | ||
var _this = this; | ||
var bean = provider.bean; | ||
if (this._beanObservers.has(bean)) { | ||
return this._beanObservers.get(bean); | ||
} | ||
var beanObserver = new BeanObserver(this, provider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
if (_this._beanObservers) { | ||
_this._beanObservers.delete(bean); | ||
} | ||
} | ||
}); | ||
this._beanObservers.set(bean, beanObserver); | ||
return beanObserver; | ||
}; | ||
Object.defineProperty(StatedBeanContainer.prototype, "parent", { | ||
@@ -586,2 +545,12 @@ get: function () { | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
/** | ||
@@ -607,24 +576,24 @@ * | ||
if (isStatedBean(this)) { | ||
var _a = this[StatedBeanSymbol], container_1 = _a.container, type_1 = _a.type, identity_1 = _a.identity; | ||
var container = this[StatedBeanSymbol].container; | ||
var observer_1 = container.getBeanObserver(this); | ||
var emitEffectAction_1 = function (action) { | ||
var observer = container_1.getBeanObserver(type_1, identity_1); | ||
if (observer !== undefined) { | ||
observer.effect$.next(tslib.__assign({ effect: effectName }, action)); | ||
if (observer_1 !== undefined) { | ||
observer_1.effect$.next(tslib.__assign({ loading: false, error: null, data: null, effect: effectName }, action)); | ||
} | ||
}; | ||
emitEffectAction_1({ loading: true, data: null, error: null }); | ||
emitEffectAction_1({ loading: true }); | ||
var result = originalMethod.apply(this, args); | ||
if (isPromise(result)) { | ||
return result | ||
result | ||
.then(function (data) { | ||
emitEffectAction_1({ loading: false, data: data, error: null }); | ||
emitEffectAction_1({ data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, data: null, error: e }); | ||
throw e; | ||
emitEffectAction_1({ error: e }); | ||
return e; | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result, error: null }); | ||
emitEffectAction_1({ data: result }); | ||
} | ||
@@ -719,6 +688,7 @@ return result; | ||
var observer = React.useState(function () { | ||
return container.getBeanObserver(type, option.name); | ||
var bean = container.getBean(type, name); | ||
return container.getBeanObserver(bean); | ||
})[0]; | ||
if (observer === undefined) { | ||
throw new Error("bean[" + type.name + "] observer is undefined"); | ||
throw new Error("bean[" + type + "] observer is undefined"); | ||
} | ||
@@ -733,3 +703,3 @@ var subscription = React.useState(function () { | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
@@ -785,14 +755,5 @@ | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
function useObserveEffect(bean, effect) { | ||
@@ -805,9 +766,2 @@ var StateBeanContext = getStatedBeanContext(); | ||
} | ||
var identity; | ||
if (isStatedBean(bean)) { | ||
identity = bean[StatedBeanSymbol].identity; | ||
} | ||
else { | ||
throw new Error('bean is not a StatedBean'); | ||
} | ||
var _a = React.useState(function () { | ||
@@ -827,3 +781,3 @@ return { | ||
var subscription = React.useState(function () { | ||
var observer = container.getBeanObserver(bean.constructor, identity); | ||
var observer = container.getBeanObserver(bean); | ||
if (observer === undefined) { | ||
@@ -856,3 +810,2 @@ throw new Error('bean observer is undefined'); | ||
exports.boundMethod = boundMethod; | ||
exports.getPropertiesWithoutFunction = getPropertiesWithoutFunction; | ||
exports.getProperty = getProperty; | ||
@@ -862,6 +815,3 @@ exports.getStatedBeanContext = getStatedBeanContext; | ||
exports.isDisposableBean = isDisposableBean; | ||
exports.isFunction = isFunction; | ||
exports.isInitializingBean = isInitializingBean; | ||
exports.isPromise = isPromise; | ||
exports.isStatedBean = isStatedBean; | ||
exports.useBean = useBean; | ||
@@ -868,0 +818,0 @@ exports.useContainer = useContainer; |
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var StateBeanMetaStorage,rxjs=require("rxjs"),tslib=require("tslib"),React=require("react"),React__default=_interopDefault(React),DefaultBeanFactory=function(){function e(){}return e.prototype.get=function(e){return void 0!==e.bean?e.bean:new e.type},e.prototype.remove=function(){},e}(),StatedBeanApplication=function(){function e(){this._beanFactory=new DefaultBeanFactory,this._debug=!1}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e.prototype.setDebug=function(e){this._debug=e},e.prototype.isDebug=function(){return this._debug},e}(),StatedBeanMetaStorage=function(){function e(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._tempPostMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},e}(),getMetadataStorage=function(){return StateBeanMetaStorage||(StateBeanMetaStorage=new StatedBeanMetaStorage)},StatedBeanSymbol=Symbol("stated-bean"),ForceUpdate=Symbol("stated-bean-force-update");function isFunction(e){return"function"==typeof e}function isStatedBean(e){return Object.hasOwnProperty.call(e,StatedBeanSymbol)}function isPromise(e){return!!e&&"function"==typeof e.then}function getPropertiesWithoutFunction(e){return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var CountableSubject=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new rxjs.Subject,t}return tslib.__extends(t,e),t.prototype._subscribe=function(t){var n=this,r=new rxjs.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}(rxjs.Subject);function getProperty(e,t){if(t in e)return e[t]}function isInitializingBean(e){var t=getProperty(e,"postProvided");return void 0!==t&&"function"==typeof t}function isDisposableBean(e){var t=getProperty(e,"destroy");return void 0!==t&&"function"==typeof t}function isBeanContainerAware(e){var t=getProperty(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}var statedBeanContext,BeanObserver=function(){function e(e,t){var n=this;this._container=e,this._provider=t,this.state$=new CountableSubject,this.effect$=new rxjs.Subject;var r=getMetadataStorage().getBeanMeta(this._provider.type);void 0===r?(this._beanMeta={name:this._provider.identity,target:this._provider.type,statedFields:getPropertiesWithoutFunction(this._provider.bean).map((function(e){return{name:e,target:n._provider.type}}))},this._proxyBean=this._observePlainObject(this._provider.bean)):(this._beanMeta=r,this._proxyBean=this._observe(this._provider.bean)),isBeanContainerAware(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),isDisposableBean(this.proxy)&&this.proxy.destroy()},e.prototype.publishStateAction=function(e,t){var n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(e){var t=this,n=this._beanMeta.statedFields||[],r=new Proxy(e,{});return this._defineStatedBean(r),n.forEach((function(e){t._observeBeanField(r,e)})),setTimeout((function(){null!=t._beanMeta.postMethod&&void 0!==t._beanMeta.postMethod.descriptor?t._beanMeta.postMethod.descriptor.value.apply(r):isInitializingBean(r)&&r.postProvided()}),0),r},e.prototype._observePlainObject=function(e){var t=this._observe(e);return Object.keys(t).forEach((function(e){"function"==typeof t[e]&&Object.defineProperty(t,e,{value:t[e].bind(t)})})),t},e.prototype._defineStatedBean=function(e){var t=this;Object.defineProperty(e,StatedBeanSymbol,{value:{identity:t._provider.identity,type:t._provider.type,container:t._container,forceUpdate:function(e){var n=(t._beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==n&&t.publishStateAction(n,t.bean[e])}}})},e.prototype._observeBeanField=function(e,t){var n=Symbol(t.name.toString()+"_v");Object.defineProperty(e,n,{writable:!0,value:e[t.name]});var r=this;Object.defineProperty(e,t.name.toString(),{set:function(o){e[n]=o,r.publishStateAction(t,o)},get:function(){return e[n]}})},e}(),StatedBeanRegistry=function(){function e(e){this._container=e,this._beans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.get=function(e,t){var n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)},e.prototype.register=function(e){var t=this,n=this.beanFactory.get(e),r=e.identity||e.type.name,o={type:e.type,bean:n,identity:r},a=new BeanObserver(this._container,o);a.state$.subscribeCount((function(e){0===e&&(a.destroy(),t.remove(o.type,o.identity))}));var i=this._beans.get(r);void 0===i?this._beans.set(r,(new WeakMap).set(e.type,a)):i.set(e.type,a)},e.prototype.remove=function(e,t){var n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)},e}(),StatedBeanContainer=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 StatedBeanApplication,this._registry=new StatedBeanRegistry(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getBeanIdentity=function(e,t){return t||this.getBeanMetaName(e)||e.name},e.prototype.getBeanMetaName=function(e){var t=this.getBeanMeta(e);return void 0===t?void 0:t.name},e.prototype.getBeanMeta=function(e){return getMetadataStorage().getBeanMeta(e)},e.prototype.getBean=function(e,t){var n=this.getBeanIdentity(e,t),r=this.getBeanObserver(e,n);if(null==r&&this.parent&&(r=this.parent.getBeanObserver(e,t)),void 0!==r)return r.proxy},e.prototype.getBeanObserver=function(e,t){var n=this.getBeanIdentity(e,t);return this.getBeanRegistry().get(e,n)},e.prototype.register=function(e){var t=this.getBeanRegistry();void 0!==e&&t.register(e)},e.prototype.registerAndObserve=function(e){try{if(this.register(e),void 0===this.getBean(e.type,e.identity))throw new Error("bean is undefined");var t=this.getBeanObserver(e.type,this.getBeanIdentity(e.type,e.identity));if(void 0===t)throw new Error("observer is undefined");return t}catch(e){throw console.error(e),e}},e.prototype.hasBean=function(e,t){return void 0!==this.getBean(e,t)},e.prototype.remove=function(e,t){this.getBeanRegistry().remove(e,t)},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 boundMethod(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 boundClass(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,boundMethod(e,t,n))}})),e}function Stated(){return function(e,t){getMetadataStorage().collectStatedField({name:t,target:e.constructor})}}function StatedBean(e){return function(t){boundClass(t),Object.defineProperty(t,StatedBeanSymbol,{writable:!1,value:!0}),getMetadataStorage().collectStatedBean({name:e||t.name,target:t})}}function PostProvided(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),getMetadataStorage().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}}function Effect(e){return function(t,n,r){void 0===r&&(r=Object.getOwnPropertyDescriptor(t,n));var o=e||n,a=r.value;return r.value=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(isStatedBean(this)){var n=this[StatedBeanSymbol],r=n.container,i=n.type,s=n.identity,u=function(e){var t=r.getBeanObserver(i,s);void 0!==t&&t.effect$.next(tslib.__assign({effect:o},e))};u({loading:!0,data:null,error:null});var c=a.apply(this,e);return isPromise(c)?c.then((function(e){return u({loading:!1,data:e,error:null}),e})).catch((function(e){throw u({loading:!1,data:null,error:e}),e})):(u({loading:!1,data:c,error:null}),c)}return a.apply(this,e)},r}}function getStatedBeanContext(){return statedBeanContext||(statedBeanContext=React__default.createContext({})),statedBeanContext}function useContainer(e){var t=e.providers,n=e.application,r=getStatedBeanContext(),o=React.useContext(r),a=React.useState((function(){var e=new StatedBeanContainer(o.container,n);return(t||[]).forEach((function(t){isFunction(t)?e.registerAndObserve({type:t}).state$.subscribe():"type"in t&&e.registerAndObserve(t).state$.subscribe()})),e}))[0];return React.useEffect((function(){return function(){a.destroy()}}),[a]),a}var StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,o=getStatedBeanContext(),a=useContainer({providers:t,application:n});return React__default.createElement(o.Provider,{value:{container:a}},r)},StatedBeanConsumer=function(e){var t=e.children,n=getStatedBeanContext();return React__default.createElement(n.Consumer,null,(function(e){return t(e)}))};function useInject(e,t){void 0===t&&(t={});var n=getStatedBeanContext(),r=React.useContext(n),o=React.useState(0)[1],a=React.useCallback((function(e){var n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&o((function(e){return e+1}))}),[t.fields]),i=r.container;if(void 0===i)throw new Error("not found container");var s=React.useState((function(){return i.getBeanObserver(e,t.name)}))[0];if(void 0===s)throw new Error("bean["+e.name+"] observer is undefined");var u=React.useState((function(){return s.state$.subscribe(a)}))[0];return React.useEffect((function(){return function(){u.unsubscribe()}}),[u]),s.proxy}function useBean(e,t){var n=getStatedBeanContext(),r=React.useContext(n),o=React.useState(0)[1],a=React.useCallback((function(e){o((function(e){return e+1}))}),[]),i=r.container;if(void 0===i)throw new Error("not found stated bean container.");var s=React.useState((function(){var n,r;if(isFunction(e)&&!isStatedBean(e)){var o=(r=e()).constructor;if("Object"===o.name&&null==t)throw new Error("plain object bean must be named.");n={type:o,bean:r,identity:t}}else n={type:e,identity:t};return i.registerAndObserve(n)}))[0],u=React.useState((function(){return s.state$.subscribe(a)}))[0];return React.useEffect((function(){return function(){return u.unsubscribe()}}),[u]),s.proxy}function useObserveEffect(e,t){var n,r=getStatedBeanContext(),o=React.useContext(r).container;if(void 0===o)throw new Error("not found container");if(!isStatedBean(e))throw new Error("bean is not a StatedBean");n=e[StatedBeanSymbol].identity;var a=React.useState((function(){return{loading:!1,error:null,data:null,effect:t}})),i=a[0],s=a[1],u=React.useCallback((function(e){e.effect===t&&s(e)}),[t]),c=React.useState((function(){var t=o.getBeanObserver(e.constructor,n);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(u)}))[0];return React.useEffect((function(){return function(){c.unsubscribe()}}),[c]),i}exports.BeanObserver=BeanObserver,exports.DefaultBeanFactory=DefaultBeanFactory,exports.Effect=Effect,exports.ForceUpdate=ForceUpdate,exports.PostProvided=PostProvided,exports.Stated=Stated,exports.StatedBean=StatedBean,exports.StatedBeanApplication=StatedBeanApplication,exports.StatedBeanConsumer=StatedBeanConsumer,exports.StatedBeanContainer=StatedBeanContainer,exports.StatedBeanProvider=StatedBeanProvider,exports.StatedBeanSymbol=StatedBeanSymbol,exports.boundClass=boundClass,exports.boundMethod=boundMethod,exports.getPropertiesWithoutFunction=getPropertiesWithoutFunction,exports.getProperty=getProperty,exports.getStatedBeanContext=getStatedBeanContext,exports.isBeanContainerAware=isBeanContainerAware,exports.isDisposableBean=isDisposableBean,exports.isFunction=isFunction,exports.isInitializingBean=isInitializingBean,exports.isPromise=isPromise,exports.isStatedBean=isStatedBean,exports.useBean=useBean,exports.useContainer=useContainer,exports.useInject=useInject,exports.useObserveEffect=useObserveEffect; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var StateBeanMetaStorage,rxjs=require("rxjs"),tslib=require("tslib"),React=require("react"),React__default=_interopDefault(React),StatedBeanRegistry=function(){function e(){this._beans=new Map}return e.prototype.get=function(e,t){var n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)},e.prototype.register=function(e){var t;t="bean"in e?e.bean:new e.type;var n=e.identity||e.type.name,r=this._beans.get(n);void 0===r?this._beans.set(n,(new WeakMap).set(e.type,t)):r.set(e.type,t)},e.prototype.remove=function(e,t){var n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)},e}(),DefaultBeanFactory=function(){function e(){this._registry=new StatedBeanRegistry}return e.prototype.get=function(e,t){return this._registry.get(e,t)},e.prototype.register=function(e){this._registry.register(e)},e.prototype.remove=function(e,t){this._registry.remove(e,t)},e}(),StatedBeanApplication=function(){function e(){this._beanFactory=new DefaultBeanFactory,this._debug=!1}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e.prototype.setDebug=function(e){this._debug=e},e.prototype.isDebug=function(){return this._debug},e}(),StatedBeanMetaStorage=function(){function e(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._tempPostMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},e}(),getMetadataStorage=function(){return StateBeanMetaStorage||(StateBeanMetaStorage=new StatedBeanMetaStorage)},CountableSubject=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._counter$=new rxjs.Subject,t}return tslib.__extends(t,e),t.prototype._subscribe=function(t){var n=this,r=this,a=new rxjs.Observable((function(t){var a=e.prototype._subscribe.call(n,t);return function(){a.unsubscribe(),n._counter$.next(r.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),a},t.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},t}(rxjs.Subject),NoSuchBeanDefinitionError=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return tslib.__extends(t,e),t}(Error),StatedBeanSymbol=Symbol("stated-bean"),ForceUpdate=Symbol("stated-bean-force-update");function getProperty(e,t){if(t in e)return e[t]}function isInitializingBean(e){var t=getProperty(e,"postProvided");return void 0!==t&&"function"==typeof t}function isDisposableBean(e){var t=getProperty(e,"destroy");return void 0!==t&&"function"==typeof t}function isBeanContainerAware(e){var t=getProperty(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}var statedBeanContext,BeanObserver=function(){function e(e,t){this._container=e,this._provider=t,this.state$=new CountableSubject,this.effect$=new rxjs.Subject;var n=getMetadataStorage().getBeanMeta(this._provider.type);if(void 0===n)throw new NoSuchBeanDefinitionError(this._provider.type.name);this._beanMeta=n,isBeanContainerAware(this.bean)&&this.bean.setBeanContainer(this._container),this._observe()}return Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),isDisposableBean(this.bean)&&this.bean.destroy()},e.prototype.publishStateAction=function(e,t){var n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(){var e=this;(this._defineStatedBean(),(this._beanMeta.statedFields||[]).forEach((function(t){return e._observeBeanField(t)})),null!=this._beanMeta.postMethod&&void 0!==this._beanMeta.postMethod.descriptor)?this._beanMeta.postMethod.descriptor.value.apply(this.bean):isInitializingBean(this.bean)&&this.bean.postProvided()},e.prototype._defineStatedBean=function(){var e=this;Object.defineProperty(this.bean,StatedBeanSymbol,{value:{name:e._provider.identity,container:e._container,forceUpdate:function(t){var n=(e._beanMeta.statedFields||[]).find((function(e){return e.name===t}));void 0!==n&&e.publishStateAction(n,e.bean[t])}}})},e.prototype._observeBeanField=function(e){var t=Symbol(e.name.toString()+"_v");Object.defineProperty(this.bean,t,{writable:!0,value:this.bean[e.name]});var n=this;Object.defineProperty(this.bean,e.name.toString(),{set:function(r){n.bean[t]=r,n.publishStateAction(e,r)},get:function(){return n.bean[t]}})},e}(),StatedBeanContainer=function(){function e(e,t){this._beanObservers=new WeakMap,this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new StatedBeanApplication}return e.prototype.destroy=function(){this._beanObservers=new WeakMap},e.prototype.getBeanFactory=function(){return this._app.getBeanFactory()},e.prototype.getBeanIdentity=function(e,t){return t||this.getBeanMetaName(e)||e.name},e.prototype.getBeanMetaName=function(e){var t=this.getBeanMeta(e);return void 0===t?void 0:t.name},e.prototype.getBeanMeta=function(e){return getMetadataStorage().getBeanMeta(e)},e.prototype.getBean=function(e,t){var n=this.getBeanIdentity(e,t),r=this.getBeanFactory().get(e,n);return null==r&&this.parent&&(r=this.parent.getBean(e,t)),r},e.prototype.getBeanObserver=function(e){return this._beanObservers.get(e)},e.prototype.register=function(e){var t=this.getBeanFactory();void 0!==e&&void 0===t.get(e.type,e.identity)&&t.register(e)},e.prototype.registerAndObserve=function(e){this.register(e);var t=this.getBean(e.type,e.identity);if(void 0===t)throw new Error("bean is undefined");return this._observeBean({type:e.type,bean:t,identity:this.getBeanIdentity(e.type,e.identity)})},e.prototype.hasBean=function(e,t){return void 0!==this.getBean(e,t)},e.prototype.remove=function(e,t,n){this._beanObservers&&this._beanObservers.delete(t),this.getBeanFactory().remove(e,n)},e.prototype._observeBean=function(e){var t=this,n=e.bean;if(this._beanObservers.has(n))return this._beanObservers.get(n);var r=new BeanObserver(this,e);return r.state$.subscribeCount((function(e){0===e&&(r.destroy(),t._beanObservers&&t._beanObservers.delete(n))})),this._beanObservers.set(n,r),r},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 boundMethod(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 a=!1;return{configurable:!0,get:function(){if(a||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;var n=r.bind(this);return a=!0,Object.defineProperty(this,t,{configurable:!0,get:function(){return n},set:function(e){r=e,delete this[t]}}),a=!1,n},set:function(e){r=e}}}function boundClass(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,boundMethod(e,t,n))}})),e}function Stated(){return function(e,t){getMetadataStorage().collectStatedField({name:t,target:e.constructor})}}function StatedBean(e){return function(t){boundClass(t),Object.defineProperty(t,StatedBeanSymbol,{writable:!1,value:!0}),getMetadataStorage().collectStatedBean({name:e||t.name,target:t})}}function PostProvided(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),getMetadataStorage().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}}function isFunction(e){return"function"==typeof e}function isStatedBean(e){return Object.hasOwnProperty.call(e,StatedBeanSymbol)}function isPromise(e){return!!e&&"function"==typeof e.then}function Effect(e){return function(t,n,r){void 0===r&&(r=Object.getOwnPropertyDescriptor(t,n));var a=e||n,o=r.value;return r.value=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(isStatedBean(this)){var n=this[StatedBeanSymbol].container,r=n.getBeanObserver(this),i=function(e){void 0!==r&&r.effect$.next(tslib.__assign({loading:!1,error:null,data:null,effect:a},e))};i({loading:!0});var s=o.apply(this,e);return isPromise(s)?s.then((function(e){return i({data:e}),e})).catch((function(e){return i({error:e}),e})):i({data:s}),s}return o.apply(this,e)},r}}function getStatedBeanContext(){return statedBeanContext||(statedBeanContext=React__default.createContext({})),statedBeanContext}function useContainer(e){var t=e.providers,n=e.application,r=getStatedBeanContext(),a=React.useContext(r),o=React.useState((function(){var e=new StatedBeanContainer(a.container,n);return(t||[]).forEach((function(t){isFunction(t)?e.registerAndObserve({type:t}).state$.subscribe():"type"in t&&e.registerAndObserve(t).state$.subscribe()})),e}))[0];return React.useEffect((function(){return function(){o.destroy()}}),[o]),o}var StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,a=getStatedBeanContext(),o=useContainer({providers:t,application:n});return React__default.createElement(a.Provider,{value:{container:o}},r)},StatedBeanConsumer=function(e){var t=e.children,n=getStatedBeanContext();return React__default.createElement(n.Consumer,null,(function(e){return t(e)}))};function useInject(e,t){void 0===t&&(t={});var n=getStatedBeanContext(),r=React.useContext(n),a=React.useState(0)[1],o=React.useCallback((function(e){var n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&a((function(e){return e+1}))}),[t.fields]),i=r.container;if(void 0===i)throw new Error("not found container");var s=React.useState((function(){var t=i.getBean(e,name);return i.getBeanObserver(t)}))[0];if(void 0===s)throw new Error("bean["+e+"] observer is undefined");var u=React.useState((function(){return s.state$.subscribe(o)}))[0];return React.useEffect((function(){return function(){u.unsubscribe()}}),[u]),s.bean}function useBean(e,t){var n=getStatedBeanContext(),r=React.useContext(n),a=React.useState(0)[1],o=React.useCallback((function(e){a((function(e){return e+1}))}),[]),i=r.container;if(void 0===i)throw new Error("not found stated bean container.");var s=React.useState((function(){var n,r;if(isFunction(e)&&!isStatedBean(e)){var a=(r=e()).constructor;if("Object"===a.name&&null==t)throw new Error("plain object bean must be named.");n={type:a,bean:r,identity:t}}else n={type:e,identity:t};return i.registerAndObserve(n)}))[0],u=React.useState((function(){return s.state$.subscribe(o)}))[0];return React.useEffect((function(){return function(){return u.unsubscribe()}}),[u]),s.bean}function useObserveEffect(e,t){var n=getStatedBeanContext(),r=React.useContext(n).container;if(void 0===r)throw new Error("not found container");var a=React.useState((function(){return{loading:!1,error:null,data:null,effect:t}})),o=a[0],i=a[1],s=React.useCallback((function(e){e.effect===t&&i(e)}),[t]),u=React.useState((function(){var t=r.getBeanObserver(e);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(s)}))[0];return React.useEffect((function(){return function(){u.unsubscribe()}}),[u]),o}exports.BeanObserver=BeanObserver,exports.DefaultBeanFactory=DefaultBeanFactory,exports.Effect=Effect,exports.ForceUpdate=ForceUpdate,exports.PostProvided=PostProvided,exports.Stated=Stated,exports.StatedBean=StatedBean,exports.StatedBeanApplication=StatedBeanApplication,exports.StatedBeanConsumer=StatedBeanConsumer,exports.StatedBeanContainer=StatedBeanContainer,exports.StatedBeanProvider=StatedBeanProvider,exports.StatedBeanSymbol=StatedBeanSymbol,exports.boundClass=boundClass,exports.boundMethod=boundMethod,exports.getProperty=getProperty,exports.getStatedBeanContext=getStatedBeanContext,exports.isBeanContainerAware=isBeanContainerAware,exports.isDisposableBean=isDisposableBean,exports.isInitializingBean=isInitializingBean,exports.useBean=useBean,exports.useContainer=useContainer,exports.useInject=useInject,exports.useObserveEffect=useObserveEffect; |
@@ -5,11 +5,2 @@ import { getStatedBeanContext } from '../context'; | ||
import { useCallback, useContext, useEffect, useState } from 'react'; | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
export function useObserveEffect(bean, effect) { | ||
@@ -16,0 +7,0 @@ var StateBeanContext = getStatedBeanContext(); |
@@ -5,3 +5,2 @@ export * from './core'; | ||
export * from './context'; | ||
export * from './utils'; | ||
//# sourceMappingURL=index.js.map |
@@ -5,16 +5,44 @@ import { Subject, Observable } from 'rxjs'; | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
class DefaultBeanFactory { | ||
get(provider) { | ||
if (provider.bean !== undefined) { | ||
return provider.bean; | ||
class StatedBeanRegistry { | ||
constructor() { | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
get(type, identity) { | ||
const typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
} | ||
register(provider) { | ||
let obj; | ||
if (!('bean' in provider)) { | ||
// eslint-disable-next-line new-cap | ||
return new provider.type(); | ||
obj = new provider.type(); | ||
} | ||
else { | ||
obj = provider.bean; | ||
} | ||
const beanIdentity = provider.identity || provider.type.name; | ||
const typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, obj)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, obj); | ||
} | ||
} | ||
remove() { | ||
// | ||
remove(type, identity) { | ||
const typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
} | ||
@@ -24,2 +52,21 @@ } | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
*/ | ||
class DefaultBeanFactory { | ||
constructor() { | ||
// @internal | ||
this._registry = new StatedBeanRegistry(); | ||
} | ||
get(type, identity) { | ||
return this._registry.get(type, identity); | ||
} | ||
register(provider) { | ||
this._registry.register(provider); | ||
} | ||
remove(type, identity) { | ||
this._registry.remove(type, identity); | ||
} | ||
} | ||
/** | ||
* | ||
@@ -93,18 +140,2 @@ * @export | ||
const StatedBeanSymbol = Symbol('stated-bean'); | ||
const ForceUpdate = Symbol('stated-bean-force-update'); | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
function getPropertiesWithoutFunction(obj) { | ||
return Object.keys(obj).filter((key) => typeof obj[key] !== 'function'); | ||
} | ||
class CountableSubject extends Subject { | ||
@@ -116,2 +147,3 @@ constructor() { | ||
_subscribe(subscriber) { | ||
const self = this; | ||
const observable = new Observable(_subscriber => { | ||
@@ -121,3 +153,3 @@ const _subscription = super._subscribe(_subscriber); | ||
_subscription.unsubscribe(); | ||
this._counter$.next(this.observers.length); | ||
this._counter$.next(self.observers.length); | ||
}; | ||
@@ -134,2 +166,8 @@ }); | ||
class NoSuchBeanDefinitionError extends Error { | ||
} | ||
const StatedBeanSymbol = Symbol('stated-bean'); | ||
const ForceUpdate = Symbol('stated-bean-force-update'); | ||
function getProperty(obj, key) { | ||
@@ -162,23 +200,10 @@ if (key in obj) { | ||
if (beanMeta === undefined) { | ||
this._beanMeta = { | ||
name: this._provider.identity, | ||
target: this._provider.type, | ||
statedFields: getPropertiesWithoutFunction(this._provider.bean).map(key => ({ | ||
name: key, | ||
target: this._provider.type, | ||
})), | ||
}; | ||
this._proxyBean = this._observePlainObject(this._provider.bean); | ||
throw new NoSuchBeanDefinitionError(this._provider.type.name); | ||
} | ||
else { | ||
this._beanMeta = beanMeta; | ||
this._proxyBean = this._observe(this._provider.bean); | ||
this._beanMeta = beanMeta; | ||
if (isBeanContainerAware(this.bean)) { | ||
this.bean.setBeanContainer(this._container); | ||
} | ||
if (isBeanContainerAware(this.proxy)) { | ||
this.proxy.setBeanContainer(this._container); | ||
} | ||
this._observe(); | ||
} | ||
get proxy() { | ||
return this._proxyBean; | ||
} | ||
get bean() { | ||
@@ -190,4 +215,4 @@ return this._provider.bean; | ||
this.effect$.complete(); | ||
if (isDisposableBean(this.proxy)) { | ||
this.proxy.destroy(); | ||
if (isDisposableBean(this.bean)) { | ||
this.bean.destroy(); | ||
} | ||
@@ -205,40 +230,21 @@ } | ||
} | ||
_observe(bean) { | ||
_observe() { | ||
this._defineStatedBean(); | ||
const fields = this._beanMeta.statedFields || []; | ||
// const proxyBean = this._createProxyBean(bean, fields); | ||
const proxyBean = new Proxy(bean, {}); | ||
this._defineStatedBean(proxyBean); | ||
fields.forEach(field => { | ||
this._observeBeanField(proxyBean, field); | ||
}); | ||
setTimeout(() => { | ||
if (this._beanMeta.postMethod != null && | ||
this._beanMeta.postMethod.descriptor !== undefined) { | ||
const f = this._beanMeta.postMethod.descriptor.value; | ||
f.apply(proxyBean); | ||
} | ||
else if (isInitializingBean(proxyBean)) { | ||
proxyBean.postProvided(); | ||
} | ||
}, 0); | ||
return proxyBean; | ||
fields.forEach(field => this._observeBeanField(field)); | ||
if (this._beanMeta.postMethod != null && | ||
this._beanMeta.postMethod.descriptor !== undefined) { | ||
const f = this._beanMeta.postMethod.descriptor.value; | ||
f.apply(this.bean); | ||
} | ||
else if (isInitializingBean(this.bean)) { | ||
this.bean.postProvided(); | ||
} | ||
} | ||
_observePlainObject(bean) { | ||
const proxyBean = this._observe(bean); | ||
Object.keys(proxyBean).forEach((key) => { | ||
if (typeof proxyBean[key] === 'function') { | ||
Object.defineProperty(proxyBean, key, { | ||
value: proxyBean[key].bind(proxyBean), | ||
}); | ||
} | ||
}); | ||
return proxyBean; | ||
} | ||
// @internal | ||
_defineStatedBean(bean) { | ||
_defineStatedBean() { | ||
const self = this; | ||
Object.defineProperty(bean, StatedBeanSymbol, { | ||
Object.defineProperty(this.bean, StatedBeanSymbol, { | ||
value: { | ||
identity: self._provider.identity, | ||
type: self._provider.type, | ||
name: self._provider.identity, | ||
container: self._container, | ||
@@ -256,16 +262,16 @@ forceUpdate: function (field) { | ||
// @internal | ||
_observeBeanField(bean, fieldMeta) { | ||
_observeBeanField(fieldMeta) { | ||
const proxyField = Symbol(fieldMeta.name.toString() + '_v'); | ||
Object.defineProperty(bean, proxyField, { | ||
Object.defineProperty(this.bean, proxyField, { | ||
writable: true, | ||
value: bean[fieldMeta.name], | ||
value: this.bean[fieldMeta.name], | ||
}); | ||
const self = this; | ||
Object.defineProperty(bean, fieldMeta.name.toString(), { | ||
Object.defineProperty(this.bean, fieldMeta.name.toString(), { | ||
set(value) { | ||
bean[proxyField] = value; | ||
self.bean[proxyField] = value; | ||
self.publishStateAction(fieldMeta, value); | ||
}, | ||
get() { | ||
return bean[proxyField]; | ||
return self.bean[proxyField]; | ||
}, | ||
@@ -277,57 +283,2 @@ }); | ||
/** | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
class StatedBeanRegistry { | ||
constructor(_container) { | ||
this._container = _container; | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
get beanFactory() { | ||
return this._container.application.getBeanFactory(); | ||
} | ||
get(type, identity) { | ||
const typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
} | ||
register(provider) { | ||
const bean = this.beanFactory.get(provider); | ||
const beanIdentity = provider.identity || provider.type.name; | ||
const strictProvider = { | ||
type: provider.type, | ||
bean, | ||
identity: beanIdentity, | ||
}; | ||
const beanObserver = new BeanObserver(this._container, strictProvider); | ||
beanObserver.state$.subscribeCount(count => { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
this.remove(strictProvider.type, strictProvider.identity); | ||
} | ||
}); | ||
const typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, beanObserver)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, beanObserver); | ||
} | ||
} | ||
remove(type, identity) { | ||
const typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
} | ||
} | ||
/** | ||
* `StatedBeanContainer` is responsible for registering and managing `bean` and observing its `@Stated()` property changes. | ||
@@ -337,2 +288,4 @@ */ | ||
constructor(parent, app) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._beanObservers = new WeakMap(); | ||
this._parent = parent; | ||
@@ -348,9 +301,9 @@ if (app != null) { | ||
} | ||
this._registry = new StatedBeanRegistry(this); | ||
} | ||
destroy() { | ||
// container destroy | ||
this._beanObservers = new WeakMap(); | ||
} | ||
getBeanRegistry() { | ||
return this._registry; | ||
getBeanFactory() { | ||
return this._app.getBeanFactory(); | ||
} | ||
@@ -370,38 +323,30 @@ getBeanIdentity(type, name) { | ||
const identity = this.getBeanIdentity(type, name); | ||
let bean = this.getBeanObserver(type, identity); | ||
let bean = this.getBeanFactory().get(type, identity); | ||
if (bean == null && this.parent) { | ||
bean = this.parent.getBeanObserver(type, name); | ||
bean = this.parent.getBean(type, name); | ||
} | ||
if (bean !== undefined) { | ||
return bean.proxy; | ||
} | ||
return undefined; | ||
return bean; | ||
} | ||
getBeanObserver(type, name) { | ||
const identity = this.getBeanIdentity(type, name); | ||
return this.getBeanRegistry().get(type, identity); | ||
getBeanObserver(bean) { | ||
return this._beanObservers.get(bean); | ||
} | ||
register(provider) { | ||
const beanFactory = this.getBeanRegistry(); | ||
const beanFactory = this.getBeanFactory(); | ||
if (provider !== undefined) { | ||
beanFactory.register(provider); | ||
if (beanFactory.get(provider.type, provider.identity) === undefined) { | ||
beanFactory.register(provider); | ||
} | ||
} | ||
} | ||
registerAndObserve(provider) { | ||
try { | ||
this.register(provider); | ||
const bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
const observer = this.getBeanObserver(provider.type, this.getBeanIdentity(provider.type, provider.identity)); | ||
if (observer === undefined) { | ||
throw new Error('observer is undefined'); | ||
} | ||
return observer; | ||
this.register(provider); | ||
const bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
return this._observeBean({ | ||
type: provider.type, | ||
bean, | ||
identity: this.getBeanIdentity(provider.type, provider.identity), | ||
}); | ||
} | ||
@@ -411,5 +356,25 @@ hasBean(type, name) { | ||
} | ||
remove(type, name) { | ||
this.getBeanRegistry().remove(type, name); | ||
remove(type, bean, name) { | ||
if (this._beanObservers) { | ||
this._beanObservers.delete(bean); | ||
} | ||
this.getBeanFactory().remove(type, name); | ||
} | ||
_observeBean(provider) { | ||
const bean = provider.bean; | ||
if (this._beanObservers.has(bean)) { | ||
return this._beanObservers.get(bean); | ||
} | ||
const beanObserver = new BeanObserver(this, provider); | ||
beanObserver.state$.subscribeCount(count => { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
if (this._beanObservers) { | ||
this._beanObservers.delete(bean); | ||
} | ||
} | ||
}); | ||
this._beanObservers.set(bean, beanObserver); | ||
return beanObserver; | ||
} | ||
get parent() { | ||
@@ -541,2 +506,12 @@ return this._parent; | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
/** | ||
@@ -558,24 +533,24 @@ * | ||
if (isStatedBean(this)) { | ||
const { container, type, identity } = this[StatedBeanSymbol]; | ||
const container = this[StatedBeanSymbol].container; | ||
const observer = container.getBeanObserver(this); | ||
const emitEffectAction = (action) => { | ||
const observer = container.getBeanObserver(type, identity); | ||
if (observer !== undefined) { | ||
observer.effect$.next(Object.assign({ effect: effectName }, action)); | ||
observer.effect$.next(Object.assign({ loading: false, error: null, data: null, effect: effectName }, action)); | ||
} | ||
}; | ||
emitEffectAction({ loading: true, data: null, error: null }); | ||
emitEffectAction({ loading: true }); | ||
const result = originalMethod.apply(this, args); | ||
if (isPromise(result)) { | ||
return result | ||
result | ||
.then((data) => { | ||
emitEffectAction({ loading: false, data, error: null }); | ||
emitEffectAction({ data }); | ||
return data; | ||
}) | ||
.catch((e) => { | ||
emitEffectAction({ loading: false, data: null, error: e }); | ||
throw e; | ||
emitEffectAction({ error: e }); | ||
return e; | ||
}); | ||
} | ||
else { | ||
emitEffectAction({ loading: false, data: result, error: null }); | ||
emitEffectAction({ data: result }); | ||
} | ||
@@ -666,6 +641,7 @@ return result; | ||
const [observer] = useState(() => { | ||
return container.getBeanObserver(type, option.name); | ||
const bean = container.getBean(type, name); | ||
return container.getBeanObserver(bean); | ||
}); | ||
if (observer === undefined) { | ||
throw new Error(`bean[${type.name}] observer is undefined`); | ||
throw new Error(`bean[${type}] observer is undefined`); | ||
} | ||
@@ -680,3 +656,3 @@ const [subscription] = useState(() => { | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
@@ -732,14 +708,5 @@ | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
function useObserveEffect(bean, effect) { | ||
@@ -752,9 +719,2 @@ const StateBeanContext = getStatedBeanContext(); | ||
} | ||
let identity; | ||
if (isStatedBean(bean)) { | ||
identity = bean[StatedBeanSymbol].identity; | ||
} | ||
else { | ||
throw new Error('bean is not a StatedBean'); | ||
} | ||
const [effectState, setEffectState] = useState(() => { | ||
@@ -765,3 +725,3 @@ return { | ||
data: null, | ||
effect: effect, | ||
effect, | ||
}; | ||
@@ -775,3 +735,3 @@ }); | ||
const [subscription] = useState(() => { | ||
const observer = container.getBeanObserver(bean.constructor, identity); | ||
const observer = container.getBeanObserver(bean); | ||
if (observer === undefined) { | ||
@@ -790,2 +750,2 @@ throw new Error('bean observer is undefined'); | ||
export { BeanObserver, DefaultBeanFactory, Effect, ForceUpdate, PostProvided, Stated, StatedBean, StatedBeanApplication, StatedBeanConsumer, StatedBeanContainer, StatedBeanProvider, StatedBeanSymbol, boundClass, boundMethod, getPropertiesWithoutFunction, getProperty, getStatedBeanContext, isBeanContainerAware, isDisposableBean, isFunction, isInitializingBean, isPromise, isStatedBean, useBean, useContainer, useInject, useObserveEffect }; | ||
export { BeanObserver, DefaultBeanFactory, Effect, ForceUpdate, PostProvided, Stated, StatedBean, StatedBeanApplication, StatedBeanConsumer, StatedBeanContainer, StatedBeanProvider, StatedBeanSymbol, boundClass, boundMethod, getProperty, getStatedBeanContext, isBeanContainerAware, isDisposableBean, isInitializingBean, useBean, useContainer, useInject, useObserveEffect }; |
@@ -1,1 +0,1 @@ | ||
import{Subject as e,Observable as t}from"rxjs";import n,{useContext as r,useState as i,useEffect as o,useCallback as s}from"react";class a{get(e){return void 0!==e.bean?e.bean:new e.type}remove(){}}class c{constructor(){this._beanFactory=new a,this._debug=!1}getBeanFactory(){return this._beanFactory}setBeanFactory(e){return this._beanFactory=e,this}setDebug(e){this._debug=e}isDebug(){return this._debug}}class p{constructor(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}collectStatedBean(e){const t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)}collectStatedField(e){const t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])}collectPostProvided(e){this._tempPostMethod.set(e.target,e)}getBeanMeta(e){return this._beans.get(e)}clear(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}}let d;const u=()=>d||(d=new p),h=Symbol("stated-bean"),l=Symbol("stated-bean-force-update");function b(e){return"function"==typeof e}function y(e){return Object.hasOwnProperty.call(e,h)}function f(e){return!!e&&"function"==typeof e.then}function g(e){return Object.keys(e).filter(t=>"function"!=typeof e[t])}class v extends e{constructor(){super(...arguments),this._counter$=new e}_subscribe(e){const n=new t(e=>{const t=super._subscribe(e);return()=>{t.unsubscribe(),this._counter$.next(this.observers.length)}}).subscribe(e);return this._counter$.next(this.observers.length),n}subscribeCount(e){this._counter$.subscribe(e)}}function _(e,t){if(t in e)return e[t]}function m(e){const t=_(e,"postProvided");return void 0!==t&&"function"==typeof t}function w(e){const t=_(e,"destroy");return void 0!==t&&"function"==typeof t}function B(e){const t=_(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}class O{constructor(t,n){this._container=t,this._provider=n,this.state$=new v,this.effect$=new e;const r=u().getBeanMeta(this._provider.type);void 0===r?(this._beanMeta={name:this._provider.identity,target:this._provider.type,statedFields:g(this._provider.bean).map(e=>({name:e,target:this._provider.type}))},this._proxyBean=this._observePlainObject(this._provider.bean)):(this._beanMeta=r,this._proxyBean=this._observe(this._provider.bean)),B(this.proxy)&&this.proxy.setBeanContainer(this._container)}get proxy(){return this._proxyBean}get bean(){return this._provider.bean}destroy(){this.state$.complete(),this.effect$.complete(),w(this.proxy)&&this.proxy.destroy()}publishStateAction(e,t){const n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)}_observe(e){const t=this._beanMeta.statedFields||[],n=new Proxy(e,{});return this._defineStatedBean(n),t.forEach(e=>{this._observeBeanField(n,e)}),setTimeout(()=>{if(null!=this._beanMeta.postMethod&&void 0!==this._beanMeta.postMethod.descriptor){this._beanMeta.postMethod.descriptor.value.apply(n)}else m(n)&&n.postProvided()},0),n}_observePlainObject(e){const t=this._observe(e);return Object.keys(t).forEach(e=>{"function"==typeof t[e]&&Object.defineProperty(t,e,{value:t[e].bind(t)})}),t}_defineStatedBean(e){const t=this;Object.defineProperty(e,h,{value:{identity:t._provider.identity,type:t._provider.type,container:t._container,forceUpdate:function(e){const n=(t._beanMeta.statedFields||[]).find(t=>t.name===e);void 0!==n&&t.publishStateAction(n,t.bean[e])}}})}_observeBeanField(e,t){const n=Symbol(t.name.toString()+"_v");Object.defineProperty(e,n,{writable:!0,value:e[t.name]});const r=this;Object.defineProperty(e,t.name.toString(),{set(i){e[n]=i,r.publishStateAction(t,i)},get:()=>e[n]})}}class M{constructor(e){this._container=e,this._beans=new Map}get beanFactory(){return this._container.application.getBeanFactory()}get(e,t){const n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)}register(e){const t=this.beanFactory.get(e),n=e.identity||e.type.name,r={type:e.type,bean:t,identity:n},i=new O(this._container,r);i.state$.subscribeCount(e=>{0===e&&(i.destroy(),this.remove(r.type,r.identity))});const o=this._beans.get(n);void 0===o?this._beans.set(n,(new WeakMap).set(e.type,i)):o.set(e.type,i)}remove(e,t){const n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)}}class P{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 c,this._registry=new M(this)}destroy(){}getBeanRegistry(){return this._registry}getBeanIdentity(e,t){return t||this.getBeanMetaName(e)||e.name}getBeanMetaName(e){const t=this.getBeanMeta(e);return void 0===t?void 0:t.name}getBeanMeta(e){return u().getBeanMeta(e)}getBean(e,t){const n=this.getBeanIdentity(e,t);let r=this.getBeanObserver(e,n);if(null==r&&this.parent&&(r=this.parent.getBeanObserver(e,t)),void 0!==r)return r.proxy}getBeanObserver(e,t){const n=this.getBeanIdentity(e,t);return this.getBeanRegistry().get(e,n)}register(e){const t=this.getBeanRegistry();void 0!==e&&t.register(e)}registerAndObserve(e){try{if(this.register(e),void 0===this.getBean(e.type,e.identity))throw new Error("bean is undefined");const t=this.getBeanObserver(e.type,this.getBeanIdentity(e.type,e.identity));if(void 0===t)throw new Error("observer is undefined");return t}catch(e){throw console.error(e),e}}hasBean(e,t){return void 0!==this.getBean(e,t)}remove(e,t){this.getBeanRegistry().remove(e,t)}get parent(){return this._parent}get application(){return this._app}}function j(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 i=!1;return{configurable:!0,get(){if(i||this===e.prototype||Object.hasOwnProperty.call(this,t)||"function"!=typeof r)return r;const n=r.bind(this);return i=!0,Object.defineProperty(this,t,{configurable:!0,get:()=>n,set(e){r=e,delete this[t]}}),i=!1,n},set(e){r=e}}}function F(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,j(e,t,n))}),e}function x(){return(e,t)=>{u().collectStatedField({name:t,target:e.constructor})}}function $(e){return t=>{F(t),Object.defineProperty(t,h,{writable:!1,value:!0}),u().collectStatedBean({name:e||t.name,target:t})}}function S(){return(e,t,n)=>(void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),u().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n)}function E(e){return(t,n,r)=>{void 0===r&&(r=Object.getOwnPropertyDescriptor(t,n));const i=e||n,o=r.value;return r.value=function(...e){if(y(this)){const{container:t,type:n,identity:r}=this[h],s=e=>{const o=t.getBeanObserver(n,r);void 0!==o&&o.effect$.next(Object.assign({effect:i},e))};s({loading:!0,data:null,error:null});const a=o.apply(this,e);return f(a)?a.then(e=>(s({loading:!1,data:e,error:null}),e)).catch(e=>{throw s({loading:!1,data:null,error:e}),e}):(s({loading:!1,data:a,error:null}),a)}return o.apply(this,e)},r}}let k;function T(){return k||(k=n.createContext({})),k}function A({providers:e,application:t}){const n=T(),s=r(n),[a]=i(()=>{const n=new P(s.container,t);return(e||[]).forEach(e=>{b(e)?n.registerAndObserve({type:e}).state$.subscribe():"type"in e&&n.registerAndObserve(e).state$.subscribe()}),n});return o(()=>()=>{a.destroy()},[a]),a}const R=({providers:e,application:t,children:r})=>{const i=T(),o=A({providers:e,application:t});return n.createElement(i.Provider,{value:{container:o}},r)},W=({children:e})=>{const t=T();return n.createElement(t.Consumer,null,t=>e(t))};function C(e,t={}){const n=T(),a=r(n),[,c]=i(0),p=s(e=>{const n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&c(e=>e+1)},[t.fields]),d=a.container;if(void 0===d)throw new Error("not found container");const[u]=i(()=>d.getBeanObserver(e,t.name));if(void 0===u)throw new Error(`bean[${e.name}] observer is undefined`);const[h]=i(()=>u.state$.subscribe(p));return o(()=>()=>{h.unsubscribe()},[h]),u.proxy}function D(e,t){const n=T(),a=r(n),[,c]=i(0),p=s(e=>{c(e=>e+1)},[]),d=a.container;if(void 0===d)throw new Error("not found stated bean container.");const[u]=i(()=>{let n,r;if(b(e)&&!y(e)){const i=(r=e()).constructor;if("Object"===i.name&&null==t)throw new Error("plain object bean must be named.");n={type:i,bean:r,identity:t}}else n={type:e,identity:t};return d.registerAndObserve(n)}),[h]=i(()=>u.state$.subscribe(p));return o(()=>()=>h.unsubscribe(),[h]),u.proxy}function I(e,t){const n=T(),a=r(n).container;if(void 0===a)throw new Error("not found container");let c;if(!y(e))throw new Error("bean is not a StatedBean");c=e[h].identity;const[p,d]=i(()=>({loading:!1,error:null,data:null,effect:t})),u=s(e=>{e.effect===t&&d(e)},[t]),[l]=i(()=>{const t=a.getBeanObserver(e.constructor,c);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(u)});return o(()=>()=>{l.unsubscribe()},[l]),p}export{O as BeanObserver,a as DefaultBeanFactory,E as Effect,l as ForceUpdate,S as PostProvided,x as Stated,$ as StatedBean,c as StatedBeanApplication,W as StatedBeanConsumer,P as StatedBeanContainer,R as StatedBeanProvider,h as StatedBeanSymbol,F as boundClass,j as boundMethod,g as getPropertiesWithoutFunction,_ as getProperty,T as getStatedBeanContext,B as isBeanContainerAware,w as isDisposableBean,b as isFunction,m as isInitializingBean,f as isPromise,y as isStatedBean,D as useBean,A as useContainer,C as useInject,I as useObserveEffect}; | ||
import{Subject as e,Observable as t}from"rxjs";import n,{useContext as r,useState as s,useEffect as i,useCallback as o}from"react";class a{constructor(){this._beans=new Map}get(e,t){const n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)}register(e){let t;t="bean"in e?e.bean:new e.type;const n=e.identity||e.type.name,r=this._beans.get(n);void 0===r?this._beans.set(n,(new WeakMap).set(e.type,t)):r.set(e.type,t)}remove(e,t){const n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)}}class c{constructor(){this._registry=new a}get(e,t){return this._registry.get(e,t)}register(e){this._registry.register(e)}remove(e,t){this._registry.remove(e,t)}}class u{constructor(){this._beanFactory=new c,this._debug=!1}getBeanFactory(){return this._beanFactory}setBeanFactory(e){return this._beanFactory=e,this}setDebug(e){this._debug=e}isDebug(){return this._debug}}class b{constructor(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}collectStatedBean(e){const t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)}collectStatedField(e){const t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])}collectPostProvided(e){this._tempPostMethod.set(e.target,e)}getBeanMeta(e){return this._beans.get(e)}clear(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}}let d;const h=()=>d||(d=new b);class p extends e{constructor(){super(...arguments),this._counter$=new e}_subscribe(e){const n=this,r=new t(e=>{const t=super._subscribe(e);return()=>{t.unsubscribe(),this._counter$.next(n.observers.length)}}).subscribe(e);return this._counter$.next(this.observers.length),r}subscribeCount(e){this._counter$.subscribe(e)}}class l extends Error{}const f=Symbol("stated-bean"),y=Symbol("stated-bean-force-update");function g(e,t){if(t in e)return e[t]}function v(e){const t=g(e,"postProvided");return void 0!==t&&"function"==typeof t}function _(e){const t=g(e,"destroy");return void 0!==t&&"function"==typeof t}function m(e){const t=g(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}class w{constructor(t,n){this._container=t,this._provider=n,this.state$=new p,this.effect$=new e;const r=h().getBeanMeta(this._provider.type);if(void 0===r)throw new l(this._provider.type.name);this._beanMeta=r,m(this.bean)&&this.bean.setBeanContainer(this._container),this._observe()}get bean(){return this._provider.bean}destroy(){this.state$.complete(),this.effect$.complete(),_(this.bean)&&this.bean.destroy()}publishStateAction(e,t){const n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)}_observe(){if(this._defineStatedBean(),(this._beanMeta.statedFields||[]).forEach(e=>this._observeBeanField(e)),null!=this._beanMeta.postMethod&&void 0!==this._beanMeta.postMethod.descriptor){this._beanMeta.postMethod.descriptor.value.apply(this.bean)}else v(this.bean)&&this.bean.postProvided()}_defineStatedBean(){const e=this;Object.defineProperty(this.bean,f,{value:{name:e._provider.identity,container:e._container,forceUpdate:function(t){const n=(e._beanMeta.statedFields||[]).find(e=>e.name===t);void 0!==n&&e.publishStateAction(n,e.bean[t])}}})}_observeBeanField(e){const t=Symbol(e.name.toString()+"_v");Object.defineProperty(this.bean,t,{writable:!0,value:this.bean[e.name]});const n=this;Object.defineProperty(this.bean,e.name.toString(),{set(r){n.bean[t]=r,n.publishStateAction(e,r)},get:()=>n.bean[t]})}}class O{constructor(e,t){this._beanObservers=new WeakMap,this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u}destroy(){this._beanObservers=new WeakMap}getBeanFactory(){return this._app.getBeanFactory()}getBeanIdentity(e,t){return t||this.getBeanMetaName(e)||e.name}getBeanMetaName(e){const t=this.getBeanMeta(e);return void 0===t?void 0:t.name}getBeanMeta(e){return h().getBeanMeta(e)}getBean(e,t){const n=this.getBeanIdentity(e,t);let r=this.getBeanFactory().get(e,n);return null==r&&this.parent&&(r=this.parent.getBean(e,t)),r}getBeanObserver(e){return this._beanObservers.get(e)}register(e){const t=this.getBeanFactory();void 0!==e&&void 0===t.get(e.type,e.identity)&&t.register(e)}registerAndObserve(e){this.register(e);const t=this.getBean(e.type,e.identity);if(void 0===t)throw new Error("bean is undefined");return this._observeBean({type:e.type,bean:t,identity:this.getBeanIdentity(e.type,e.identity)})}hasBean(e,t){return void 0!==this.getBean(e,t)}remove(e,t,n){this._beanObservers&&this._beanObservers.delete(t),this.getBeanFactory().remove(e,n)}_observeBean(e){const t=e.bean;if(this._beanObservers.has(t))return this._beanObservers.get(t);const n=new w(this,e);return n.state$.subscribeCount(e=>{0===e&&(n.destroy(),this._beanObservers&&this._beanObservers.delete(t))}),this._beanObservers.set(t,n),n}get parent(){return this._parent}get application(){return this._app}}function B(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 M(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,B(e,t,n))}),e}function P(){return(e,t)=>{h().collectStatedField({name:t,target:e.constructor})}}function F(e){return t=>{M(t),Object.defineProperty(t,f,{writable:!1,value:!0}),h().collectStatedBean({name:e||t.name,target:t})}}function j(){return(e,t,n)=>(void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),h().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n)}function $(e){return"function"==typeof e}function S(e){return Object.hasOwnProperty.call(e,f)}function E(e){return(t,n,r)=>{void 0===r&&(r=Object.getOwnPropertyDescriptor(t,n));const s=e||n,i=r.value;return r.value=function(...e){if(S(this)){const n=this[f].container.getBeanObserver(this),r=e=>{void 0!==n&&n.effect$.next(Object.assign({loading:!1,error:null,data:null,effect:s},e))};r({loading:!0});const o=i.apply(this,e);return(t=o)&&"function"==typeof t.then?o.then(e=>(r({data:e}),e)).catch(e=>(r({error:e}),e)):r({data:o}),o}var t;return i.apply(this,e)},r}}let x;function k(){return x||(x=n.createContext({})),x}function W({providers:e,application:t}){const n=k(),o=r(n),[a]=s(()=>{const n=new O(o.container,t);return(e||[]).forEach(e=>{$(e)?n.registerAndObserve({type:e}).state$.subscribe():"type"in e&&n.registerAndObserve(e).state$.subscribe()}),n});return i(()=>()=>{a.destroy()},[a]),a}const A=({providers:e,application:t,children:r})=>{const s=k(),i=W({providers:e,application:t});return n.createElement(s.Provider,{value:{container:i}},r)},T=({children:e})=>{const t=k();return n.createElement(t.Consumer,null,t=>e(t))};function C(e,t={}){const n=k(),a=r(n),[,c]=s(0),u=o(e=>{const n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&c(e=>e+1)},[t.fields]),b=a.container;if(void 0===b)throw new Error("not found container");const[d]=s(()=>{const t=b.getBean(e,name);return b.getBeanObserver(t)});if(void 0===d)throw new Error(`bean[${e}] observer is undefined`);const[h]=s(()=>d.state$.subscribe(u));return i(()=>()=>{h.unsubscribe()},[h]),d.bean}function D(e,t){const n=k(),a=r(n),[,c]=s(0),u=o(e=>{c(e=>e+1)},[]),b=a.container;if(void 0===b)throw new Error("not found stated bean container.");const[d]=s(()=>{let n,r;if($(e)&&!S(e)){const s=(r=e()).constructor;if("Object"===s.name&&null==t)throw new Error("plain object bean must be named.");n={type:s,bean:r,identity:t}}else n={type:e,identity:t};return b.registerAndObserve(n)}),[h]=s(()=>d.state$.subscribe(u));return i(()=>()=>h.unsubscribe(),[h]),d.bean}function I(e,t){const n=k(),a=r(n).container;if(void 0===a)throw new Error("not found container");const[c,u]=s(()=>({loading:!1,error:null,data:null,effect:t})),b=o(e=>{e.effect===t&&u(e)},[t]),[d]=s(()=>{const t=a.getBeanObserver(e);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(b)});return i(()=>()=>{d.unsubscribe()},[d]),c}export{w as BeanObserver,c as DefaultBeanFactory,E as Effect,y as ForceUpdate,j as PostProvided,P as Stated,F as StatedBean,u as StatedBeanApplication,T as StatedBeanConsumer,O as StatedBeanContainer,A as StatedBeanProvider,f as StatedBeanSymbol,M as boundClass,B as boundMethod,g as getProperty,k as getStatedBeanContext,m as isBeanContainerAware,_ as isDisposableBean,v as isInitializingBean,D as useBean,W as useContainer,C as useInject,I as useObserveEffect}; |
382
lib/esm.js
@@ -6,19 +6,65 @@ import { Subject, Observable } from 'rxjs'; | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry() { | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (provider) { | ||
if (provider.bean !== undefined) { | ||
return provider.bean; | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var obj; | ||
if (!('bean' in provider)) { | ||
// eslint-disable-next-line new-cap | ||
return new provider.type(); | ||
obj = new provider.type(); | ||
} | ||
else { | ||
obj = provider.bean; | ||
} | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, obj)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, obj); | ||
} | ||
}; | ||
DefaultBeanFactory.prototype.remove = function () { | ||
// | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
// @internal | ||
this._registry = new StatedBeanRegistry(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (type, identity) { | ||
return this._registry.get(type, identity); | ||
}; | ||
DefaultBeanFactory.prototype.register = function (provider) { | ||
this._registry.register(provider); | ||
}; | ||
DefaultBeanFactory.prototype.remove = function (type, identity) { | ||
this._registry.remove(type, identity); | ||
}; | ||
return DefaultBeanFactory; | ||
@@ -100,18 +146,2 @@ }()); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
function getPropertiesWithoutFunction(obj) { | ||
return Object.keys(obj).filter(function (key) { return typeof obj[key] !== 'function'; }); | ||
} | ||
var CountableSubject = /** @class */ (function (_super) { | ||
@@ -126,2 +156,3 @@ __extends(CountableSubject, _super); | ||
var _this = this; | ||
var self = this; | ||
var observable = new Observable(function (_subscriber) { | ||
@@ -131,3 +162,3 @@ var _subscription = _super.prototype._subscribe.call(_this, _subscriber); | ||
_subscription.unsubscribe(); | ||
_this._counter$.next(_this.observers.length); | ||
_this._counter$.next(self.observers.length); | ||
}; | ||
@@ -145,2 +176,13 @@ }); | ||
var NoSuchBeanDefinitionError = /** @class */ (function (_super) { | ||
__extends(NoSuchBeanDefinitionError, _super); | ||
function NoSuchBeanDefinitionError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return NoSuchBeanDefinitionError; | ||
}(Error)); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function getProperty(obj, key) { | ||
@@ -167,3 +209,2 @@ if (key in obj) { | ||
function BeanObserver(_container, _provider) { | ||
var _this = this; | ||
this._container = _container; | ||
@@ -175,27 +216,10 @@ this._provider = _provider; | ||
if (beanMeta === undefined) { | ||
this._beanMeta = { | ||
name: this._provider.identity, | ||
target: this._provider.type, | ||
statedFields: getPropertiesWithoutFunction(this._provider.bean).map(function (key) { return ({ | ||
name: key, | ||
target: _this._provider.type, | ||
}); }), | ||
}; | ||
this._proxyBean = this._observePlainObject(this._provider.bean); | ||
throw new NoSuchBeanDefinitionError(this._provider.type.name); | ||
} | ||
else { | ||
this._beanMeta = beanMeta; | ||
this._proxyBean = this._observe(this._provider.bean); | ||
this._beanMeta = beanMeta; | ||
if (isBeanContainerAware(this.bean)) { | ||
this.bean.setBeanContainer(this._container); | ||
} | ||
if (isBeanContainerAware(this.proxy)) { | ||
this.proxy.setBeanContainer(this._container); | ||
} | ||
this._observe(); | ||
} | ||
Object.defineProperty(BeanObserver.prototype, "proxy", { | ||
get: function () { | ||
return this._proxyBean; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BeanObserver.prototype, "bean", { | ||
@@ -211,4 +235,4 @@ get: function () { | ||
this.effect$.complete(); | ||
if (isDisposableBean(this.proxy)) { | ||
this.proxy.destroy(); | ||
if (isDisposableBean(this.bean)) { | ||
this.bean.destroy(); | ||
} | ||
@@ -226,41 +250,22 @@ }; | ||
}; | ||
BeanObserver.prototype._observe = function (bean) { | ||
BeanObserver.prototype._observe = function () { | ||
var _this = this; | ||
this._defineStatedBean(); | ||
var fields = this._beanMeta.statedFields || []; | ||
// const proxyBean = this._createProxyBean(bean, fields); | ||
var proxyBean = new Proxy(bean, {}); | ||
this._defineStatedBean(proxyBean); | ||
fields.forEach(function (field) { | ||
_this._observeBeanField(proxyBean, field); | ||
}); | ||
setTimeout(function () { | ||
if (_this._beanMeta.postMethod != null && | ||
_this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = _this._beanMeta.postMethod.descriptor.value; | ||
f.apply(proxyBean); | ||
} | ||
else if (isInitializingBean(proxyBean)) { | ||
proxyBean.postProvided(); | ||
} | ||
}, 0); | ||
return proxyBean; | ||
fields.forEach(function (field) { return _this._observeBeanField(field); }); | ||
if (this._beanMeta.postMethod != null && | ||
this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = this._beanMeta.postMethod.descriptor.value; | ||
f.apply(this.bean); | ||
} | ||
else if (isInitializingBean(this.bean)) { | ||
this.bean.postProvided(); | ||
} | ||
}; | ||
BeanObserver.prototype._observePlainObject = function (bean) { | ||
var proxyBean = this._observe(bean); | ||
Object.keys(proxyBean).forEach(function (key) { | ||
if (typeof proxyBean[key] === 'function') { | ||
Object.defineProperty(proxyBean, key, { | ||
value: proxyBean[key].bind(proxyBean), | ||
}); | ||
} | ||
}); | ||
return proxyBean; | ||
}; | ||
// @internal | ||
BeanObserver.prototype._defineStatedBean = function (bean) { | ||
BeanObserver.prototype._defineStatedBean = function () { | ||
var self = this; | ||
Object.defineProperty(bean, StatedBeanSymbol, { | ||
Object.defineProperty(this.bean, StatedBeanSymbol, { | ||
value: { | ||
identity: self._provider.identity, | ||
type: self._provider.type, | ||
name: self._provider.identity, | ||
container: self._container, | ||
@@ -278,16 +283,16 @@ forceUpdate: function (field) { | ||
// @internal | ||
BeanObserver.prototype._observeBeanField = function (bean, fieldMeta) { | ||
BeanObserver.prototype._observeBeanField = function (fieldMeta) { | ||
var proxyField = Symbol(fieldMeta.name.toString() + '_v'); | ||
Object.defineProperty(bean, proxyField, { | ||
Object.defineProperty(this.bean, proxyField, { | ||
writable: true, | ||
value: bean[fieldMeta.name], | ||
value: this.bean[fieldMeta.name], | ||
}); | ||
var self = this; | ||
Object.defineProperty(bean, fieldMeta.name.toString(), { | ||
Object.defineProperty(this.bean, fieldMeta.name.toString(), { | ||
set: function (value) { | ||
bean[proxyField] = value; | ||
self.bean[proxyField] = value; | ||
self.publishStateAction(fieldMeta, value); | ||
}, | ||
get: function () { | ||
return bean[proxyField]; | ||
return self.bean[proxyField]; | ||
}, | ||
@@ -300,63 +305,2 @@ }); | ||
/** | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry(_container) { | ||
this._container = _container; | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
Object.defineProperty(StatedBeanRegistry.prototype, "beanFactory", { | ||
get: function () { | ||
return this._container.application.getBeanFactory(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var _this = this; | ||
var bean = this.beanFactory.get(provider); | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var strictProvider = { | ||
type: provider.type, | ||
bean: bean, | ||
identity: beanIdentity, | ||
}; | ||
var beanObserver = new BeanObserver(this._container, strictProvider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
_this.remove(strictProvider.type, strictProvider.identity); | ||
} | ||
}); | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, beanObserver)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, beanObserver); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* `StatedBeanContainer` is responsible for registering and managing `bean` and observing its `@Stated()` property changes. | ||
@@ -366,2 +310,4 @@ */ | ||
function StatedBeanContainer(parent, app) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._beanObservers = new WeakMap(); | ||
this._parent = parent; | ||
@@ -377,9 +323,9 @@ if (app != null) { | ||
} | ||
this._registry = new StatedBeanRegistry(this); | ||
} | ||
StatedBeanContainer.prototype.destroy = function () { | ||
// container destroy | ||
this._beanObservers = new WeakMap(); | ||
}; | ||
StatedBeanContainer.prototype.getBeanRegistry = function () { | ||
return this._registry; | ||
StatedBeanContainer.prototype.getBeanFactory = function () { | ||
return this._app.getBeanFactory(); | ||
}; | ||
@@ -399,38 +345,30 @@ StatedBeanContainer.prototype.getBeanIdentity = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
var bean = this.getBeanObserver(type, identity); | ||
var bean = this.getBeanFactory().get(type, identity); | ||
if (bean == null && this.parent) { | ||
bean = this.parent.getBeanObserver(type, name); | ||
bean = this.parent.getBean(type, name); | ||
} | ||
if (bean !== undefined) { | ||
return bean.proxy; | ||
} | ||
return undefined; | ||
return bean; | ||
}; | ||
StatedBeanContainer.prototype.getBeanObserver = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
return this.getBeanRegistry().get(type, identity); | ||
StatedBeanContainer.prototype.getBeanObserver = function (bean) { | ||
return this._beanObservers.get(bean); | ||
}; | ||
StatedBeanContainer.prototype.register = function (provider) { | ||
var beanFactory = this.getBeanRegistry(); | ||
var beanFactory = this.getBeanFactory(); | ||
if (provider !== undefined) { | ||
beanFactory.register(provider); | ||
if (beanFactory.get(provider.type, provider.identity) === undefined) { | ||
beanFactory.register(provider); | ||
} | ||
} | ||
}; | ||
StatedBeanContainer.prototype.registerAndObserve = function (provider) { | ||
try { | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
var observer = this.getBeanObserver(provider.type, this.getBeanIdentity(provider.type, provider.identity)); | ||
if (observer === undefined) { | ||
throw new Error('observer is undefined'); | ||
} | ||
return observer; | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
return this._observeBean({ | ||
type: provider.type, | ||
bean: bean, | ||
identity: this.getBeanIdentity(provider.type, provider.identity), | ||
}); | ||
}; | ||
@@ -440,5 +378,26 @@ StatedBeanContainer.prototype.hasBean = function (type, name) { | ||
}; | ||
StatedBeanContainer.prototype.remove = function (type, name) { | ||
this.getBeanRegistry().remove(type, name); | ||
StatedBeanContainer.prototype.remove = function (type, bean, name) { | ||
if (this._beanObservers) { | ||
this._beanObservers.delete(bean); | ||
} | ||
this.getBeanFactory().remove(type, name); | ||
}; | ||
StatedBeanContainer.prototype._observeBean = function (provider) { | ||
var _this = this; | ||
var bean = provider.bean; | ||
if (this._beanObservers.has(bean)) { | ||
return this._beanObservers.get(bean); | ||
} | ||
var beanObserver = new BeanObserver(this, provider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
if (_this._beanObservers) { | ||
_this._beanObservers.delete(bean); | ||
} | ||
} | ||
}); | ||
this._beanObservers.set(bean, beanObserver); | ||
return beanObserver; | ||
}; | ||
Object.defineProperty(StatedBeanContainer.prototype, "parent", { | ||
@@ -579,2 +538,12 @@ get: function () { | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
/** | ||
@@ -600,24 +569,24 @@ * | ||
if (isStatedBean(this)) { | ||
var _a = this[StatedBeanSymbol], container_1 = _a.container, type_1 = _a.type, identity_1 = _a.identity; | ||
var container = this[StatedBeanSymbol].container; | ||
var observer_1 = container.getBeanObserver(this); | ||
var emitEffectAction_1 = function (action) { | ||
var observer = container_1.getBeanObserver(type_1, identity_1); | ||
if (observer !== undefined) { | ||
observer.effect$.next(__assign({ effect: effectName }, action)); | ||
if (observer_1 !== undefined) { | ||
observer_1.effect$.next(__assign({ loading: false, error: null, data: null, effect: effectName }, action)); | ||
} | ||
}; | ||
emitEffectAction_1({ loading: true, data: null, error: null }); | ||
emitEffectAction_1({ loading: true }); | ||
var result = originalMethod.apply(this, args); | ||
if (isPromise(result)) { | ||
return result | ||
result | ||
.then(function (data) { | ||
emitEffectAction_1({ loading: false, data: data, error: null }); | ||
emitEffectAction_1({ data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, data: null, error: e }); | ||
throw e; | ||
emitEffectAction_1({ error: e }); | ||
return e; | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result, error: null }); | ||
emitEffectAction_1({ data: result }); | ||
} | ||
@@ -712,6 +681,7 @@ return result; | ||
var observer = useState(function () { | ||
return container.getBeanObserver(type, option.name); | ||
var bean = container.getBean(type, name); | ||
return container.getBeanObserver(bean); | ||
})[0]; | ||
if (observer === undefined) { | ||
throw new Error("bean[" + type.name + "] observer is undefined"); | ||
throw new Error("bean[" + type + "] observer is undefined"); | ||
} | ||
@@ -726,3 +696,3 @@ var subscription = useState(function () { | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
@@ -778,14 +748,5 @@ | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
function useObserveEffect(bean, effect) { | ||
@@ -798,9 +759,2 @@ var StateBeanContext = getStatedBeanContext(); | ||
} | ||
var identity; | ||
if (isStatedBean(bean)) { | ||
identity = bean[StatedBeanSymbol].identity; | ||
} | ||
else { | ||
throw new Error('bean is not a StatedBean'); | ||
} | ||
var _a = useState(function () { | ||
@@ -820,3 +774,3 @@ return { | ||
var subscription = useState(function () { | ||
var observer = container.getBeanObserver(bean.constructor, identity); | ||
var observer = container.getBeanObserver(bean); | ||
if (observer === undefined) { | ||
@@ -835,2 +789,2 @@ throw new Error('bean observer is undefined'); | ||
export { BeanObserver, DefaultBeanFactory, Effect, ForceUpdate, PostProvided, Stated, StatedBean, StatedBeanApplication, StatedBeanConsumer, StatedBeanContainer, StatedBeanProvider, StatedBeanSymbol, boundClass, boundMethod, getPropertiesWithoutFunction, getProperty, getStatedBeanContext, isBeanContainerAware, isDisposableBean, isFunction, isInitializingBean, isPromise, isStatedBean, useBean, useContainer, useInject, useObserveEffect }; | ||
export { BeanObserver, DefaultBeanFactory, Effect, ForceUpdate, PostProvided, Stated, StatedBean, StatedBeanApplication, StatedBeanConsumer, StatedBeanContainer, StatedBeanProvider, StatedBeanSymbol, boundClass, boundMethod, getProperty, getStatedBeanContext, isBeanContainerAware, isDisposableBean, isInitializingBean, useBean, useContainer, useInject, useObserveEffect }; |
@@ -1,1 +0,1 @@ | ||
import{Subject as t,Observable as e}from"rxjs";import{__extends as n,__assign as r}from"tslib";import o,{useContext as i,useState as a,useEffect as u,useCallback as s}from"react";var c,p=function(){function t(){}return t.prototype.get=function(t){return void 0!==t.bean?t.bean:new t.type},t.prototype.remove=function(){},t}(),f=function(){function t(){this._beanFactory=new p,this._debug=!1}return t.prototype.getBeanFactory=function(){return this._beanFactory},t.prototype.setBeanFactory=function(t){return this._beanFactory=t,this},t.prototype.setDebug=function(t){this._debug=t},t.prototype.isDebug=function(){return this._debug},t}(),d=function(){function t(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return t.prototype.collectStatedBean=function(t){var e=t.target,n=this._tempTypeFields.get(e);t.statedFields=n,t.postMethod=this._tempPostMethod.get(e),this._beans.set(e,t),this._tempTypeFields.delete(e),this._tempPostMethod.delete(e)},t.prototype.collectStatedField=function(t){var e=t.target,n=this._tempTypeFields.get(e);n?n.push(t):this._tempTypeFields.set(e,[t])},t.prototype.collectPostProvided=function(t){this._tempPostMethod.set(t.target,t)},t.prototype.getBeanMeta=function(t){return this._beans.get(t)},t.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},t}(),y=function(){return c||(c=new d)},b=Symbol("stated-bean"),h=Symbol("stated-bean-force-update");function l(t){return"function"==typeof t}function v(t){return Object.hasOwnProperty.call(t,b)}function g(t){return!!t&&"function"==typeof t.then}function _(t){return Object.keys(t).filter((function(e){return"function"!=typeof t[e]}))}var m=function(r){function o(){var e=null!==r&&r.apply(this,arguments)||this;return e._counter$=new t,e}return n(o,r),o.prototype._subscribe=function(t){var n=this,o=new e((function(t){var e=r.prototype._subscribe.call(n,t);return function(){e.unsubscribe(),n._counter$.next(n.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),o},o.prototype.subscribeCount=function(t){this._counter$.subscribe(t)},o}(t);function w(t,e){if(e in t)return t[e]}function O(t){var e=w(t,"postProvided");return void 0!==e&&"function"==typeof e}function B(t){var e=w(t,"destroy");return void 0!==e&&"function"==typeof e}function M(t){var e=w(t,"setBeanContainer");return void 0!==e&&"function"==typeof e}var P,j=function(){function e(e,n){var r=this;this._container=e,this._provider=n,this.state$=new m,this.effect$=new t;var o=y().getBeanMeta(this._provider.type);void 0===o?(this._beanMeta={name:this._provider.identity,target:this._provider.type,statedFields:_(this._provider.bean).map((function(t){return{name:t,target:r._provider.type}}))},this._proxyBean=this._observePlainObject(this._provider.bean)):(this._beanMeta=o,this._proxyBean=this._observe(this._provider.bean)),M(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),B(this.proxy)&&this.proxy.destroy()},e.prototype.publishStateAction=function(t,e){var n={bean:this.bean,nextValue:e,prevValue:this.bean[t.name],fieldMeta:t,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(t){var e=this,n=this._beanMeta.statedFields||[],r=new Proxy(t,{});return this._defineStatedBean(r),n.forEach((function(t){e._observeBeanField(r,t)})),setTimeout((function(){null!=e._beanMeta.postMethod&&void 0!==e._beanMeta.postMethod.descriptor?e._beanMeta.postMethod.descriptor.value.apply(r):O(r)&&r.postProvided()}),0),r},e.prototype._observePlainObject=function(t){var e=this._observe(t);return Object.keys(e).forEach((function(t){"function"==typeof e[t]&&Object.defineProperty(e,t,{value:e[t].bind(e)})})),e},e.prototype._defineStatedBean=function(t){var e=this;Object.defineProperty(t,b,{value:{identity:e._provider.identity,type:e._provider.type,container:e._container,forceUpdate:function(t){var n=(e._beanMeta.statedFields||[]).find((function(e){return e.name===t}));void 0!==n&&e.publishStateAction(n,e.bean[t])}}})},e.prototype._observeBeanField=function(t,e){var n=Symbol(e.name.toString()+"_v");Object.defineProperty(t,n,{writable:!0,value:t[e.name]});var r=this;Object.defineProperty(t,e.name.toString(),{set:function(o){t[n]=o,r.publishStateAction(e,o)},get:function(){return t[n]}})},e}(),F=function(){function t(t){this._container=t,this._beans=new Map}return Object.defineProperty(t.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),t.prototype.get=function(t,e){var n=this._beans.get(e||t.name);return void 0===n?void 0:n.get(t)},t.prototype.register=function(t){var e=this,n=this.beanFactory.get(t),r=t.identity||t.type.name,o={type:t.type,bean:n,identity:r},i=new j(this._container,o);i.state$.subscribeCount((function(t){0===t&&(i.destroy(),e.remove(o.type,o.identity))}));var a=this._beans.get(r);void 0===a?this._beans.set(r,(new WeakMap).set(t.type,i)):a.set(t.type,i)},t.prototype.remove=function(t,e){var n=this._beans.get(e||t.name);void 0!==n&&n.delete(t)},t}(),x=function(){function t(t,e){this._parent=t,null!=e?this._app=e:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new f,this._registry=new F(this)}return t.prototype.destroy=function(){},t.prototype.getBeanRegistry=function(){return this._registry},t.prototype.getBeanIdentity=function(t,e){return e||this.getBeanMetaName(t)||t.name},t.prototype.getBeanMetaName=function(t){var e=this.getBeanMeta(t);return void 0===e?void 0:e.name},t.prototype.getBeanMeta=function(t){return y().getBeanMeta(t)},t.prototype.getBean=function(t,e){var n=this.getBeanIdentity(t,e),r=this.getBeanObserver(t,n);if(null==r&&this.parent&&(r=this.parent.getBeanObserver(t,e)),void 0!==r)return r.proxy},t.prototype.getBeanObserver=function(t,e){var n=this.getBeanIdentity(t,e);return this.getBeanRegistry().get(t,n)},t.prototype.register=function(t){var e=this.getBeanRegistry();void 0!==t&&e.register(t)},t.prototype.registerAndObserve=function(t){try{if(this.register(t),void 0===this.getBean(t.type,t.identity))throw new Error("bean is undefined");var e=this.getBeanObserver(t.type,this.getBeanIdentity(t.type,t.identity));if(void 0===e)throw new Error("observer is undefined");return e}catch(t){throw console.error(t),t}},t.prototype.hasBean=function(t,e){return void 0!==this.getBean(t,e)},t.prototype.remove=function(t,e){this.getBeanRegistry().remove(t,e)},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),t}();function S(t,e,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===t.prototype||Object.hasOwnProperty.call(this,e)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,e,{configurable:!0,get:function(){return n},set:function(t){r=t,delete this[e]}}),o=!1,n},set:function(t){r=t}}}function E(t){var e;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?e=Reflect.ownKeys(t.prototype):(e=Object.getOwnPropertyNames(t.prototype),"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(t.prototype)))),e.forEach((function(e){if("constructor"!==e){var n=Object.getOwnPropertyDescriptor(t.prototype,e);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(t.prototype,e,S(t,e,n))}})),t}function $(){return function(t,e){y().collectStatedField({name:e,target:t.constructor})}}function k(t){return function(e){E(e),Object.defineProperty(e,b,{writable:!1,value:!0}),y().collectStatedBean({name:t||e.name,target:e})}}function T(){return function(t,e,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(t,e)),y().collectPostProvided({name:e,target:t.constructor,descriptor:n}),n}}function A(t){return function(e,n,o){void 0===o&&(o=Object.getOwnPropertyDescriptor(e,n));var i=t||n,a=o.value;return o.value=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(v(this)){var n=this[b],o=n.container,u=n.type,s=n.identity,c=function(t){var e=o.getBeanObserver(u,s);void 0!==e&&e.effect$.next(r({effect:i},t))};c({loading:!0,data:null,error:null});var p=a.apply(this,t);return g(p)?p.then((function(t){return c({loading:!1,data:t,error:null}),t})).catch((function(t){throw c({loading:!1,data:null,error:t}),t})):(c({loading:!1,data:p,error:null}),p)}return a.apply(this,t)},o}}function R(){return P||(P=o.createContext({})),P}function W(t){var e=t.providers,n=t.application,r=R(),o=i(r),s=a((function(){var t=new x(o.container,n);return(e||[]).forEach((function(e){l(e)?t.registerAndObserve({type:e}).state$.subscribe():"type"in e&&t.registerAndObserve(e).state$.subscribe()})),t}))[0];return u((function(){return function(){s.destroy()}}),[s]),s}var C=function(t){var e=t.providers,n=t.application,r=t.children,i=R(),a=W({providers:e,application:n});return o.createElement(i.Provider,{value:{container:a}},r)},D=function(t){var e=t.children,n=R();return o.createElement(n.Consumer,null,(function(t){return e(t)}))};function I(t,e){void 0===e&&(e={});var n=R(),r=i(n),o=a(0)[1],c=s((function(t){var n=t.fieldMeta.name;(null==e.fields||0===e.fields.length||e.fields.includes(n))&&o((function(t){return t+1}))}),[e.fields]),p=r.container;if(void 0===p)throw new Error("not found container");var f=a((function(){return p.getBeanObserver(t,e.name)}))[0];if(void 0===f)throw new Error("bean["+t.name+"] observer is undefined");var d=a((function(){return f.state$.subscribe(c)}))[0];return u((function(){return function(){d.unsubscribe()}}),[d]),f.proxy}function N(t,e){var n=R(),r=i(n),o=a(0)[1],c=s((function(t){o((function(t){return t+1}))}),[]),p=r.container;if(void 0===p)throw new Error("not found stated bean container.");var f=a((function(){var n,r;if(l(t)&&!v(t)){var o=(r=t()).constructor;if("Object"===o.name&&null==e)throw new Error("plain object bean must be named.");n={type:o,bean:r,identity:e}}else n={type:t,identity:e};return p.registerAndObserve(n)}))[0],d=a((function(){return f.state$.subscribe(c)}))[0];return u((function(){return function(){return d.unsubscribe()}}),[d]),f.proxy}function K(t,e){var n,r=R(),o=i(r).container;if(void 0===o)throw new Error("not found container");if(!v(t))throw new Error("bean is not a StatedBean");n=t[b].identity;var c=a((function(){return{loading:!1,error:null,data:null,effect:e}})),p=c[0],f=c[1],d=s((function(t){t.effect===e&&f(t)}),[e]),y=a((function(){var e=o.getBeanObserver(t.constructor,n);if(void 0===e)throw new Error("bean observer is undefined");return e.effect$.subscribe(d)}))[0];return u((function(){return function(){y.unsubscribe()}}),[y]),p}export{j as BeanObserver,p as DefaultBeanFactory,A as Effect,h as ForceUpdate,T as PostProvided,$ as Stated,k as StatedBean,f as StatedBeanApplication,D as StatedBeanConsumer,x as StatedBeanContainer,C as StatedBeanProvider,b as StatedBeanSymbol,E as boundClass,S as boundMethod,_ as getPropertiesWithoutFunction,w as getProperty,R as getStatedBeanContext,M as isBeanContainerAware,B as isDisposableBean,l as isFunction,O as isInitializingBean,g as isPromise,v as isStatedBean,N as useBean,W as useContainer,I as useInject,K as useObserveEffect}; | ||
import{Subject as t,Observable as e}from"rxjs";import{__extends as n,__assign as r}from"tslib";import o,{useContext as i,useState as a,useEffect as s,useCallback as u}from"react";var c,p=function(){function t(){this._beans=new Map}return t.prototype.get=function(t,e){var n=this._beans.get(e||t.name);return void 0===n?void 0:n.get(t)},t.prototype.register=function(t){var e;e="bean"in t?t.bean:new t.type;var n=t.identity||t.type.name,r=this._beans.get(n);void 0===r?this._beans.set(n,(new WeakMap).set(t.type,e)):r.set(t.type,e)},t.prototype.remove=function(t,e){var n=this._beans.get(e||t.name);void 0!==n&&n.delete(t)},t}(),f=function(){function t(){this._registry=new p}return t.prototype.get=function(t,e){return this._registry.get(t,e)},t.prototype.register=function(t){this._registry.register(t)},t.prototype.remove=function(t,e){this._registry.remove(t,e)},t}(),b=function(){function t(){this._beanFactory=new f,this._debug=!1}return t.prototype.getBeanFactory=function(){return this._beanFactory},t.prototype.setBeanFactory=function(t){return this._beanFactory=t,this},t.prototype.setDebug=function(t){this._debug=t},t.prototype.isDebug=function(){return this._debug},t}(),d=function(){function t(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return t.prototype.collectStatedBean=function(t){var e=t.target,n=this._tempTypeFields.get(e);t.statedFields=n,t.postMethod=this._tempPostMethod.get(e),this._beans.set(e,t),this._tempTypeFields.delete(e),this._tempPostMethod.delete(e)},t.prototype.collectStatedField=function(t){var e=t.target,n=this._tempTypeFields.get(e);n?n.push(t):this._tempTypeFields.set(e,[t])},t.prototype.collectPostProvided=function(t){this._tempPostMethod.set(t.target,t)},t.prototype.getBeanMeta=function(t){return this._beans.get(t)},t.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},t}(),h=function(){return c||(c=new d)},y=function(r){function o(){var e=null!==r&&r.apply(this,arguments)||this;return e._counter$=new t,e}return n(o,r),o.prototype._subscribe=function(t){var n=this,o=this,i=new e((function(t){var e=r.prototype._subscribe.call(n,t);return function(){e.unsubscribe(),n._counter$.next(o.observers.length)}})).subscribe(t);return this._counter$.next(this.observers.length),i},o.prototype.subscribeCount=function(t){this._counter$.subscribe(t)},o}(t),v=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e}(Error),l=Symbol("stated-bean"),g=Symbol("stated-bean-force-update");function _(t,e){if(e in t)return t[e]}function m(t){var e=_(t,"postProvided");return void 0!==e&&"function"==typeof e}function w(t){var e=_(t,"destroy");return void 0!==e&&"function"==typeof e}function O(t){var e=_(t,"setBeanContainer");return void 0!==e&&"function"==typeof e}var B,M=function(){function e(e,n){this._container=e,this._provider=n,this.state$=new y,this.effect$=new t;var r=h().getBeanMeta(this._provider.type);if(void 0===r)throw new v(this._provider.type.name);this._beanMeta=r,O(this.bean)&&this.bean.setBeanContainer(this._container),this._observe()}return Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),w(this.bean)&&this.bean.destroy()},e.prototype.publishStateAction=function(t,e){var n={bean:this.bean,nextValue:e,prevValue:this.bean[t.name],fieldMeta:t,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(){var t=this;(this._defineStatedBean(),(this._beanMeta.statedFields||[]).forEach((function(e){return t._observeBeanField(e)})),null!=this._beanMeta.postMethod&&void 0!==this._beanMeta.postMethod.descriptor)?this._beanMeta.postMethod.descriptor.value.apply(this.bean):m(this.bean)&&this.bean.postProvided()},e.prototype._defineStatedBean=function(){var t=this;Object.defineProperty(this.bean,l,{value:{name:t._provider.identity,container:t._container,forceUpdate:function(e){var n=(t._beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==n&&t.publishStateAction(n,t.bean[e])}}})},e.prototype._observeBeanField=function(t){var e=Symbol(t.name.toString()+"_v");Object.defineProperty(this.bean,e,{writable:!0,value:this.bean[t.name]});var n=this;Object.defineProperty(this.bean,t.name.toString(),{set:function(r){n.bean[e]=r,n.publishStateAction(t,r)},get:function(){return n.bean[e]}})},e}(),P=function(){function t(t,e){this._beanObservers=new WeakMap,this._parent=t,null!=e?this._app=e:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new b}return t.prototype.destroy=function(){this._beanObservers=new WeakMap},t.prototype.getBeanFactory=function(){return this._app.getBeanFactory()},t.prototype.getBeanIdentity=function(t,e){return e||this.getBeanMetaName(t)||t.name},t.prototype.getBeanMetaName=function(t){var e=this.getBeanMeta(t);return void 0===e?void 0:e.name},t.prototype.getBeanMeta=function(t){return h().getBeanMeta(t)},t.prototype.getBean=function(t,e){var n=this.getBeanIdentity(t,e),r=this.getBeanFactory().get(t,n);return null==r&&this.parent&&(r=this.parent.getBean(t,e)),r},t.prototype.getBeanObserver=function(t){return this._beanObservers.get(t)},t.prototype.register=function(t){var e=this.getBeanFactory();void 0!==t&&void 0===e.get(t.type,t.identity)&&e.register(t)},t.prototype.registerAndObserve=function(t){this.register(t);var e=this.getBean(t.type,t.identity);if(void 0===e)throw new Error("bean is undefined");return this._observeBean({type:t.type,bean:e,identity:this.getBeanIdentity(t.type,t.identity)})},t.prototype.hasBean=function(t,e){return void 0!==this.getBean(t,e)},t.prototype.remove=function(t,e,n){this._beanObservers&&this._beanObservers.delete(e),this.getBeanFactory().remove(t,n)},t.prototype._observeBean=function(t){var e=this,n=t.bean;if(this._beanObservers.has(n))return this._beanObservers.get(n);var r=new M(this,t);return r.state$.subscribeCount((function(t){0===t&&(r.destroy(),e._beanObservers&&e._beanObservers.delete(n))})),this._beanObservers.set(n,r),r},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"application",{get:function(){return this._app},enumerable:!0,configurable:!0}),t}();function F(t,e,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===t.prototype||Object.hasOwnProperty.call(this,e)||"function"!=typeof r)return r;var n=r.bind(this);return o=!0,Object.defineProperty(this,e,{configurable:!0,get:function(){return n},set:function(t){r=t,delete this[e]}}),o=!1,n},set:function(t){r=t}}}function j(t){var e;return"undefined"!=typeof Reflect&&"function"==typeof Reflect.ownKeys?e=Reflect.ownKeys(t.prototype):(e=Object.getOwnPropertyNames(t.prototype),"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(t.prototype)))),e.forEach((function(e){if("constructor"!==e){var n=Object.getOwnPropertyDescriptor(t.prototype,e);void 0!==n&&"function"==typeof n.value&&Object.defineProperty(t.prototype,e,F(t,e,n))}})),t}function S(){return function(t,e){h().collectStatedField({name:e,target:t.constructor})}}function $(t){return function(e){j(e),Object.defineProperty(e,l,{writable:!1,value:!0}),h().collectStatedBean({name:t||e.name,target:e})}}function E(){return function(t,e,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(t,e)),h().collectPostProvided({name:e,target:t.constructor,descriptor:n}),n}}function k(t){return"function"==typeof t}function W(t){return Object.hasOwnProperty.call(t,l)}function x(t){return!!t&&"function"==typeof t.then}function A(t){return function(e,n,o){void 0===o&&(o=Object.getOwnPropertyDescriptor(e,n));var i=t||n,a=o.value;return o.value=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(W(this)){var n=this[l].container,o=n.getBeanObserver(this),s=function(t){void 0!==o&&o.effect$.next(r({loading:!1,error:null,data:null,effect:i},t))};s({loading:!0});var u=a.apply(this,t);return x(u)?u.then((function(t){return s({data:t}),t})).catch((function(t){return s({error:t}),t})):s({data:u}),u}return a.apply(this,t)},o}}function T(){return B||(B=o.createContext({})),B}function C(t){var e=t.providers,n=t.application,r=T(),o=i(r),u=a((function(){var t=new P(o.container,n);return(e||[]).forEach((function(e){k(e)?t.registerAndObserve({type:e}).state$.subscribe():"type"in e&&t.registerAndObserve(e).state$.subscribe()})),t}))[0];return s((function(){return function(){u.destroy()}}),[u]),u}var D=function(t){var e=t.providers,n=t.application,r=t.children,i=T(),a=C({providers:e,application:n});return o.createElement(i.Provider,{value:{container:a}},r)},I=function(t){var e=t.children,n=T();return o.createElement(n.Consumer,null,(function(t){return e(t)}))};function N(t,e){void 0===e&&(e={});var n=T(),r=i(n),o=a(0)[1],c=u((function(t){var n=t.fieldMeta.name;(null==e.fields||0===e.fields.length||e.fields.includes(n))&&o((function(t){return t+1}))}),[e.fields]),p=r.container;if(void 0===p)throw new Error("not found container");var f=a((function(){var e=p.getBean(t,name);return p.getBeanObserver(e)}))[0];if(void 0===f)throw new Error("bean["+t+"] observer is undefined");var b=a((function(){return f.state$.subscribe(c)}))[0];return s((function(){return function(){b.unsubscribe()}}),[b]),f.bean}function R(t,e){var n=T(),r=i(n),o=a(0)[1],c=u((function(t){o((function(t){return t+1}))}),[]),p=r.container;if(void 0===p)throw new Error("not found stated bean container.");var f=a((function(){var n,r;if(k(t)&&!W(t)){var o=(r=t()).constructor;if("Object"===o.name&&null==e)throw new Error("plain object bean must be named.");n={type:o,bean:r,identity:e}}else n={type:t,identity:e};return p.registerAndObserve(n)}))[0],b=a((function(){return f.state$.subscribe(c)}))[0];return s((function(){return function(){return b.unsubscribe()}}),[b]),f.bean}function K(t,e){var n=T(),r=i(n).container;if(void 0===r)throw new Error("not found container");var o=a((function(){return{loading:!1,error:null,data:null,effect:e}})),c=o[0],p=o[1],f=u((function(t){t.effect===e&&p(t)}),[e]),b=a((function(){var e=r.getBeanObserver(t);if(void 0===e)throw new Error("bean observer is undefined");return e.effect$.subscribe(f)}))[0];return s((function(){return function(){b.unsubscribe()}}),[b]),c}export{M as BeanObserver,f as DefaultBeanFactory,A as Effect,g as ForceUpdate,E as PostProvided,S as Stated,$ as StatedBean,b as StatedBeanApplication,I as StatedBeanConsumer,P as StatedBeanContainer,D as StatedBeanProvider,l as StatedBeanSymbol,j as boundClass,F as boundMethod,_ as getProperty,T as getStatedBeanContext,O as isBeanContainerAware,w as isDisposableBean,m as isInitializingBean,R as useBean,C as useContainer,N as useInject,K as useObserveEffect}; |
@@ -1,12 +0,3 @@ | ||
import { EffectAction, FunctionPropertyNames } from '../types'; | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
export declare function useObserveEffect<T>(bean: T, effect: FunctionPropertyNames<T> | string | symbol): EffectAction; | ||
import { EffectAction } from '../types'; | ||
export declare function useObserveEffect<T>(bean: T, effect: keyof T & (string | symbol)): EffectAction; | ||
//# sourceMappingURL=useObserveEffect.d.ts.map |
@@ -6,3 +6,2 @@ export * from './core'; | ||
export * from './types'; | ||
export * from './utils'; | ||
//# sourceMappingURL=index.d.ts.map |
export declare type ClassType<T = unknown> = new (...args: unknown[]) => T; | ||
export declare type InstanceType<T extends ClassType> = T extends ClassType<infer R> ? R : never; | ||
export declare type FunctionPropertyNames<T> = { | ||
[K in keyof T]: T[K] extends Function ? K : never; | ||
}[keyof T]; | ||
//# sourceMappingURL=ClassType.d.ts.map |
384
lib/umd.js
@@ -10,19 +10,65 @@ (function (global, factory) { | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry() { | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (provider) { | ||
if (provider.bean !== undefined) { | ||
return provider.bean; | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var obj; | ||
if (!('bean' in provider)) { | ||
// eslint-disable-next-line new-cap | ||
return new provider.type(); | ||
obj = new provider.type(); | ||
} | ||
else { | ||
obj = provider.bean; | ||
} | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, obj)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, obj); | ||
} | ||
}; | ||
DefaultBeanFactory.prototype.remove = function () { | ||
// | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* the default `BeanFactory` by the class `new` constructor. | ||
*/ | ||
var DefaultBeanFactory = /** @class */ (function () { | ||
function DefaultBeanFactory() { | ||
// @internal | ||
this._registry = new StatedBeanRegistry(); | ||
} | ||
DefaultBeanFactory.prototype.get = function (type, identity) { | ||
return this._registry.get(type, identity); | ||
}; | ||
DefaultBeanFactory.prototype.register = function (provider) { | ||
this._registry.register(provider); | ||
}; | ||
DefaultBeanFactory.prototype.remove = function (type, identity) { | ||
this._registry.remove(type, identity); | ||
}; | ||
return DefaultBeanFactory; | ||
@@ -104,18 +150,2 @@ }()); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
function getPropertiesWithoutFunction(obj) { | ||
return Object.keys(obj).filter(function (key) { return typeof obj[key] !== 'function'; }); | ||
} | ||
var CountableSubject = /** @class */ (function (_super) { | ||
@@ -130,2 +160,3 @@ tslib.__extends(CountableSubject, _super); | ||
var _this = this; | ||
var self = this; | ||
var observable = new rxjs.Observable(function (_subscriber) { | ||
@@ -135,3 +166,3 @@ var _subscription = _super.prototype._subscribe.call(_this, _subscriber); | ||
_subscription.unsubscribe(); | ||
_this._counter$.next(_this.observers.length); | ||
_this._counter$.next(self.observers.length); | ||
}; | ||
@@ -149,2 +180,13 @@ }); | ||
var NoSuchBeanDefinitionError = /** @class */ (function (_super) { | ||
tslib.__extends(NoSuchBeanDefinitionError, _super); | ||
function NoSuchBeanDefinitionError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return NoSuchBeanDefinitionError; | ||
}(Error)); | ||
var StatedBeanSymbol = Symbol('stated-bean'); | ||
var ForceUpdate = Symbol('stated-bean-force-update'); | ||
function getProperty(obj, key) { | ||
@@ -171,3 +213,2 @@ if (key in obj) { | ||
function BeanObserver(_container, _provider) { | ||
var _this = this; | ||
this._container = _container; | ||
@@ -179,27 +220,10 @@ this._provider = _provider; | ||
if (beanMeta === undefined) { | ||
this._beanMeta = { | ||
name: this._provider.identity, | ||
target: this._provider.type, | ||
statedFields: getPropertiesWithoutFunction(this._provider.bean).map(function (key) { return ({ | ||
name: key, | ||
target: _this._provider.type, | ||
}); }), | ||
}; | ||
this._proxyBean = this._observePlainObject(this._provider.bean); | ||
throw new NoSuchBeanDefinitionError(this._provider.type.name); | ||
} | ||
else { | ||
this._beanMeta = beanMeta; | ||
this._proxyBean = this._observe(this._provider.bean); | ||
this._beanMeta = beanMeta; | ||
if (isBeanContainerAware(this.bean)) { | ||
this.bean.setBeanContainer(this._container); | ||
} | ||
if (isBeanContainerAware(this.proxy)) { | ||
this.proxy.setBeanContainer(this._container); | ||
} | ||
this._observe(); | ||
} | ||
Object.defineProperty(BeanObserver.prototype, "proxy", { | ||
get: function () { | ||
return this._proxyBean; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BeanObserver.prototype, "bean", { | ||
@@ -215,4 +239,4 @@ get: function () { | ||
this.effect$.complete(); | ||
if (isDisposableBean(this.proxy)) { | ||
this.proxy.destroy(); | ||
if (isDisposableBean(this.bean)) { | ||
this.bean.destroy(); | ||
} | ||
@@ -230,41 +254,22 @@ }; | ||
}; | ||
BeanObserver.prototype._observe = function (bean) { | ||
BeanObserver.prototype._observe = function () { | ||
var _this = this; | ||
this._defineStatedBean(); | ||
var fields = this._beanMeta.statedFields || []; | ||
// const proxyBean = this._createProxyBean(bean, fields); | ||
var proxyBean = new Proxy(bean, {}); | ||
this._defineStatedBean(proxyBean); | ||
fields.forEach(function (field) { | ||
_this._observeBeanField(proxyBean, field); | ||
}); | ||
setTimeout(function () { | ||
if (_this._beanMeta.postMethod != null && | ||
_this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = _this._beanMeta.postMethod.descriptor.value; | ||
f.apply(proxyBean); | ||
} | ||
else if (isInitializingBean(proxyBean)) { | ||
proxyBean.postProvided(); | ||
} | ||
}, 0); | ||
return proxyBean; | ||
fields.forEach(function (field) { return _this._observeBeanField(field); }); | ||
if (this._beanMeta.postMethod != null && | ||
this._beanMeta.postMethod.descriptor !== undefined) { | ||
var f = this._beanMeta.postMethod.descriptor.value; | ||
f.apply(this.bean); | ||
} | ||
else if (isInitializingBean(this.bean)) { | ||
this.bean.postProvided(); | ||
} | ||
}; | ||
BeanObserver.prototype._observePlainObject = function (bean) { | ||
var proxyBean = this._observe(bean); | ||
Object.keys(proxyBean).forEach(function (key) { | ||
if (typeof proxyBean[key] === 'function') { | ||
Object.defineProperty(proxyBean, key, { | ||
value: proxyBean[key].bind(proxyBean), | ||
}); | ||
} | ||
}); | ||
return proxyBean; | ||
}; | ||
// @internal | ||
BeanObserver.prototype._defineStatedBean = function (bean) { | ||
BeanObserver.prototype._defineStatedBean = function () { | ||
var self = this; | ||
Object.defineProperty(bean, StatedBeanSymbol, { | ||
Object.defineProperty(this.bean, StatedBeanSymbol, { | ||
value: { | ||
identity: self._provider.identity, | ||
type: self._provider.type, | ||
name: self._provider.identity, | ||
container: self._container, | ||
@@ -282,16 +287,16 @@ forceUpdate: function (field) { | ||
// @internal | ||
BeanObserver.prototype._observeBeanField = function (bean, fieldMeta) { | ||
BeanObserver.prototype._observeBeanField = function (fieldMeta) { | ||
var proxyField = Symbol(fieldMeta.name.toString() + '_v'); | ||
Object.defineProperty(bean, proxyField, { | ||
Object.defineProperty(this.bean, proxyField, { | ||
writable: true, | ||
value: bean[fieldMeta.name], | ||
value: this.bean[fieldMeta.name], | ||
}); | ||
var self = this; | ||
Object.defineProperty(bean, fieldMeta.name.toString(), { | ||
Object.defineProperty(this.bean, fieldMeta.name.toString(), { | ||
set: function (value) { | ||
bean[proxyField] = value; | ||
self.bean[proxyField] = value; | ||
self.publishStateAction(fieldMeta, value); | ||
}, | ||
get: function () { | ||
return bean[proxyField]; | ||
return self.bean[proxyField]; | ||
}, | ||
@@ -304,63 +309,2 @@ }); | ||
/** | ||
* The named and types bean storage with `Map<string | symbol, WeakMap<ClassType, unknown>>`. | ||
* | ||
* @export | ||
* @class StatedBeanRegistry | ||
*/ | ||
var StatedBeanRegistry = /** @class */ (function () { | ||
function StatedBeanRegistry(_container) { | ||
this._container = _container; | ||
// @internal | ||
this._beans = new Map(); | ||
} | ||
Object.defineProperty(StatedBeanRegistry.prototype, "beanFactory", { | ||
get: function () { | ||
return this._container.application.getBeanFactory(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
StatedBeanRegistry.prototype.get = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans === undefined) { | ||
return undefined; | ||
} | ||
else { | ||
return typedBeans.get(type); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.register = function (provider) { | ||
var _this = this; | ||
var bean = this.beanFactory.get(provider); | ||
var beanIdentity = provider.identity || provider.type.name; | ||
var strictProvider = { | ||
type: provider.type, | ||
bean: bean, | ||
identity: beanIdentity, | ||
}; | ||
var beanObserver = new BeanObserver(this._container, strictProvider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
_this.remove(strictProvider.type, strictProvider.identity); | ||
} | ||
}); | ||
var typedBeans = this._beans.get(beanIdentity); | ||
if (typedBeans === undefined) { | ||
this._beans.set(beanIdentity, new WeakMap().set(provider.type, beanObserver)); | ||
} | ||
else { | ||
typedBeans.set(provider.type, beanObserver); | ||
} | ||
}; | ||
StatedBeanRegistry.prototype.remove = function (type, identity) { | ||
var typedBeans = this._beans.get(identity || type.name); | ||
if (typedBeans !== undefined) { | ||
typedBeans.delete(type); | ||
} | ||
}; | ||
return StatedBeanRegistry; | ||
}()); | ||
/** | ||
* `StatedBeanContainer` is responsible for registering and managing `bean` and observing its `@Stated()` property changes. | ||
@@ -370,2 +314,4 @@ */ | ||
function StatedBeanContainer(parent, app) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._beanObservers = new WeakMap(); | ||
this._parent = parent; | ||
@@ -381,9 +327,9 @@ if (app != null) { | ||
} | ||
this._registry = new StatedBeanRegistry(this); | ||
} | ||
StatedBeanContainer.prototype.destroy = function () { | ||
// container destroy | ||
this._beanObservers = new WeakMap(); | ||
}; | ||
StatedBeanContainer.prototype.getBeanRegistry = function () { | ||
return this._registry; | ||
StatedBeanContainer.prototype.getBeanFactory = function () { | ||
return this._app.getBeanFactory(); | ||
}; | ||
@@ -403,38 +349,30 @@ StatedBeanContainer.prototype.getBeanIdentity = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
var bean = this.getBeanObserver(type, identity); | ||
var bean = this.getBeanFactory().get(type, identity); | ||
if (bean == null && this.parent) { | ||
bean = this.parent.getBeanObserver(type, name); | ||
bean = this.parent.getBean(type, name); | ||
} | ||
if (bean !== undefined) { | ||
return bean.proxy; | ||
} | ||
return undefined; | ||
return bean; | ||
}; | ||
StatedBeanContainer.prototype.getBeanObserver = function (type, name) { | ||
var identity = this.getBeanIdentity(type, name); | ||
return this.getBeanRegistry().get(type, identity); | ||
StatedBeanContainer.prototype.getBeanObserver = function (bean) { | ||
return this._beanObservers.get(bean); | ||
}; | ||
StatedBeanContainer.prototype.register = function (provider) { | ||
var beanFactory = this.getBeanRegistry(); | ||
var beanFactory = this.getBeanFactory(); | ||
if (provider !== undefined) { | ||
beanFactory.register(provider); | ||
if (beanFactory.get(provider.type, provider.identity) === undefined) { | ||
beanFactory.register(provider); | ||
} | ||
} | ||
}; | ||
StatedBeanContainer.prototype.registerAndObserve = function (provider) { | ||
try { | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
var observer = this.getBeanObserver(provider.type, this.getBeanIdentity(provider.type, provider.identity)); | ||
if (observer === undefined) { | ||
throw new Error('observer is undefined'); | ||
} | ||
return observer; | ||
this.register(provider); | ||
var bean = this.getBean(provider.type, provider.identity); | ||
if (bean === undefined) { | ||
throw new Error('bean is undefined'); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
return this._observeBean({ | ||
type: provider.type, | ||
bean: bean, | ||
identity: this.getBeanIdentity(provider.type, provider.identity), | ||
}); | ||
}; | ||
@@ -444,5 +382,26 @@ StatedBeanContainer.prototype.hasBean = function (type, name) { | ||
}; | ||
StatedBeanContainer.prototype.remove = function (type, name) { | ||
this.getBeanRegistry().remove(type, name); | ||
StatedBeanContainer.prototype.remove = function (type, bean, name) { | ||
if (this._beanObservers) { | ||
this._beanObservers.delete(bean); | ||
} | ||
this.getBeanFactory().remove(type, name); | ||
}; | ||
StatedBeanContainer.prototype._observeBean = function (provider) { | ||
var _this = this; | ||
var bean = provider.bean; | ||
if (this._beanObservers.has(bean)) { | ||
return this._beanObservers.get(bean); | ||
} | ||
var beanObserver = new BeanObserver(this, provider); | ||
beanObserver.state$.subscribeCount(function (count) { | ||
if (count === 0) { | ||
beanObserver.destroy(); | ||
if (_this._beanObservers) { | ||
_this._beanObservers.delete(bean); | ||
} | ||
} | ||
}); | ||
this._beanObservers.set(bean, beanObserver); | ||
return beanObserver; | ||
}; | ||
Object.defineProperty(StatedBeanContainer.prototype, "parent", { | ||
@@ -583,2 +542,12 @@ get: function () { | ||
function isFunction(func) { | ||
return typeof func === 'function'; | ||
} | ||
function isStatedBean(obj) { | ||
return Object.hasOwnProperty.call(obj, StatedBeanSymbol); | ||
} | ||
function isPromise(value) { | ||
return !!value && typeof value.then === 'function'; | ||
} | ||
/** | ||
@@ -604,24 +573,24 @@ * | ||
if (isStatedBean(this)) { | ||
var _a = this[StatedBeanSymbol], container_1 = _a.container, type_1 = _a.type, identity_1 = _a.identity; | ||
var container = this[StatedBeanSymbol].container; | ||
var observer_1 = container.getBeanObserver(this); | ||
var emitEffectAction_1 = function (action) { | ||
var observer = container_1.getBeanObserver(type_1, identity_1); | ||
if (observer !== undefined) { | ||
observer.effect$.next(tslib.__assign({ effect: effectName }, action)); | ||
if (observer_1 !== undefined) { | ||
observer_1.effect$.next(tslib.__assign({ loading: false, error: null, data: null, effect: effectName }, action)); | ||
} | ||
}; | ||
emitEffectAction_1({ loading: true, data: null, error: null }); | ||
emitEffectAction_1({ loading: true }); | ||
var result = originalMethod.apply(this, args); | ||
if (isPromise(result)) { | ||
return result | ||
result | ||
.then(function (data) { | ||
emitEffectAction_1({ loading: false, data: data, error: null }); | ||
emitEffectAction_1({ data: data }); | ||
return data; | ||
}) | ||
.catch(function (e) { | ||
emitEffectAction_1({ loading: false, data: null, error: e }); | ||
throw e; | ||
emitEffectAction_1({ error: e }); | ||
return e; | ||
}); | ||
} | ||
else { | ||
emitEffectAction_1({ loading: false, data: result, error: null }); | ||
emitEffectAction_1({ data: result }); | ||
} | ||
@@ -716,6 +685,7 @@ return result; | ||
var observer = React.useState(function () { | ||
return container.getBeanObserver(type, option.name); | ||
var bean = container.getBean(type, name); | ||
return container.getBeanObserver(bean); | ||
})[0]; | ||
if (observer === undefined) { | ||
throw new Error("bean[" + type.name + "] observer is undefined"); | ||
throw new Error("bean[" + type + "] observer is undefined"); | ||
} | ||
@@ -730,3 +700,3 @@ var subscription = React.useState(function () { | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
@@ -782,14 +752,5 @@ | ||
}, [subscription]); | ||
return observer.proxy; | ||
return observer.bean; | ||
} | ||
/** | ||
* Observe the effects in the bean | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} bean | ||
* @param {FunctionPropertyNames<T>} effect | ||
* @returns {EffectAction} | ||
*/ | ||
function useObserveEffect(bean, effect) { | ||
@@ -802,9 +763,2 @@ var StateBeanContext = getStatedBeanContext(); | ||
} | ||
var identity; | ||
if (isStatedBean(bean)) { | ||
identity = bean[StatedBeanSymbol].identity; | ||
} | ||
else { | ||
throw new Error('bean is not a StatedBean'); | ||
} | ||
var _a = React.useState(function () { | ||
@@ -824,3 +778,3 @@ return { | ||
var subscription = React.useState(function () { | ||
var observer = container.getBeanObserver(bean.constructor, identity); | ||
var observer = container.getBeanObserver(bean); | ||
if (observer === undefined) { | ||
@@ -853,3 +807,2 @@ throw new Error('bean observer is undefined'); | ||
exports.boundMethod = boundMethod; | ||
exports.getPropertiesWithoutFunction = getPropertiesWithoutFunction; | ||
exports.getProperty = getProperty; | ||
@@ -859,6 +812,3 @@ exports.getStatedBeanContext = getStatedBeanContext; | ||
exports.isDisposableBean = isDisposableBean; | ||
exports.isFunction = isFunction; | ||
exports.isInitializingBean = isInitializingBean; | ||
exports.isPromise = isPromise; | ||
exports.isStatedBean = isStatedBean; | ||
exports.useBean = useBean; | ||
@@ -865,0 +815,0 @@ exports.useContainer = useContainer; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("rxjs"),require("tslib"),require("react")):"function"==typeof define&&define.amd?define(["exports","rxjs","tslib","react"],t):t((e=e||self).StatedBean={},e.rxjs,e.tslib,e.React)}(this,(function(e,t,n,r){"use strict";var o,i="default"in r?r.default:r,a=function(){function e(){}return e.prototype.get=function(e){return void 0!==e.bean?e.bean:new e.type},e.prototype.remove=function(){},e}(),u=function(){function e(){this._beanFactory=new a,this._debug=!1}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e.prototype.setDebug=function(e){this._debug=e},e.prototype.isDebug=function(){return this._debug},e}(),s=function(){function e(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._tempPostMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},e}(),c=function(){return o||(o=new s)},p=Symbol("stated-bean"),f=Symbol("stated-bean-force-update");function d(e){return"function"==typeof e}function y(e){return Object.hasOwnProperty.call(e,p)}function b(e){return!!e&&"function"==typeof e.then}function l(e){return Object.keys(e).filter((function(t){return"function"!=typeof e[t]}))}var h=function(e){function r(){var n=null!==e&&e.apply(this,arguments)||this;return n._counter$=new t.Subject,n}return n.__extends(r,e),r.prototype._subscribe=function(n){var r=this,o=new t.Observable((function(t){var n=e.prototype._subscribe.call(r,t);return function(){n.unsubscribe(),r._counter$.next(r.observers.length)}})).subscribe(n);return this._counter$.next(this.observers.length),o},r.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},r}(t.Subject);function v(e,t){if(t in e)return e[t]}function g(e){var t=v(e,"postProvided");return void 0!==t&&"function"==typeof t}function _(e){var t=v(e,"destroy");return void 0!==t&&"function"==typeof t}function m(e){var t=v(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}var B,w=function(){function e(e,n){var r=this;this._container=e,this._provider=n,this.state$=new h,this.effect$=new t.Subject;var o=c().getBeanMeta(this._provider.type);void 0===o?(this._beanMeta={name:this._provider.identity,target:this._provider.type,statedFields:l(this._provider.bean).map((function(e){return{name:e,target:r._provider.type}}))},this._proxyBean=this._observePlainObject(this._provider.bean)):(this._beanMeta=o,this._proxyBean=this._observe(this._provider.bean)),m(this.proxy)&&this.proxy.setBeanContainer(this._container)}return Object.defineProperty(e.prototype,"proxy",{get:function(){return this._proxyBean},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),_(this.proxy)&&this.proxy.destroy()},e.prototype.publishStateAction=function(e,t){var n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(e){var t=this,n=this._beanMeta.statedFields||[],r=new Proxy(e,{});return this._defineStatedBean(r),n.forEach((function(e){t._observeBeanField(r,e)})),setTimeout((function(){null!=t._beanMeta.postMethod&&void 0!==t._beanMeta.postMethod.descriptor?t._beanMeta.postMethod.descriptor.value.apply(r):g(r)&&r.postProvided()}),0),r},e.prototype._observePlainObject=function(e){var t=this._observe(e);return Object.keys(t).forEach((function(e){"function"==typeof t[e]&&Object.defineProperty(t,e,{value:t[e].bind(t)})})),t},e.prototype._defineStatedBean=function(e){var t=this;Object.defineProperty(e,p,{value:{identity:t._provider.identity,type:t._provider.type,container:t._container,forceUpdate:function(e){var n=(t._beanMeta.statedFields||[]).find((function(t){return t.name===e}));void 0!==n&&t.publishStateAction(n,t.bean[e])}}})},e.prototype._observeBeanField=function(e,t){var n=Symbol(t.name.toString()+"_v");Object.defineProperty(e,n,{writable:!0,value:e[t.name]});var r=this;Object.defineProperty(e,t.name.toString(),{set:function(o){e[n]=o,r.publishStateAction(t,o)},get:function(){return e[n]}})},e}(),O=function(){function e(e){this._container=e,this._beans=new Map}return Object.defineProperty(e.prototype,"beanFactory",{get:function(){return this._container.application.getBeanFactory()},enumerable:!0,configurable:!0}),e.prototype.get=function(e,t){var n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)},e.prototype.register=function(e){var t=this,n=this.beanFactory.get(e),r=e.identity||e.type.name,o={type:e.type,bean:n,identity:r},i=new w(this._container,o);i.state$.subscribeCount((function(e){0===e&&(i.destroy(),t.remove(o.type,o.identity))}));var a=this._beans.get(r);void 0===a?this._beans.set(r,(new WeakMap).set(e.type,i)):a.set(e.type,i)},e.prototype.remove=function(e,t){var n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)},e}(),P=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 O(this)}return e.prototype.destroy=function(){},e.prototype.getBeanRegistry=function(){return this._registry},e.prototype.getBeanIdentity=function(e,t){return t||this.getBeanMetaName(e)||e.name},e.prototype.getBeanMetaName=function(e){var t=this.getBeanMeta(e);return void 0===t?void 0:t.name},e.prototype.getBeanMeta=function(e){return c().getBeanMeta(e)},e.prototype.getBean=function(e,t){var n=this.getBeanIdentity(e,t),r=this.getBeanObserver(e,n);if(null==r&&this.parent&&(r=this.parent.getBeanObserver(e,t)),void 0!==r)return r.proxy},e.prototype.getBeanObserver=function(e,t){var n=this.getBeanIdentity(e,t);return this.getBeanRegistry().get(e,n)},e.prototype.register=function(e){var t=this.getBeanRegistry();void 0!==e&&t.register(e)},e.prototype.registerAndObserve=function(e){try{if(this.register(e),void 0===this.getBean(e.type,e.identity))throw new Error("bean is undefined");var t=this.getBeanObserver(e.type,this.getBeanIdentity(e.type,e.identity));if(void 0===t)throw new Error("observer is undefined");return t}catch(e){throw console.error(e),e}},e.prototype.hasBean=function(e,t){return void 0!==this.getBean(e,t)},e.prototype.remove=function(e,t){this.getBeanRegistry().remove(e,t)},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 M(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}function j(){return B||(B=i.createContext({})),B}function x(e){var t=e.providers,n=e.application,o=j(),i=r.useContext(o),a=r.useState((function(){var e=new P(i.container,n);return(t||[]).forEach((function(t){d(t)?e.registerAndObserve({type:t}).state$.subscribe():"type"in t&&e.registerAndObserve(t).state$.subscribe()})),e}))[0];return r.useEffect((function(){return function(){a.destroy()}}),[a]),a}e.BeanObserver=w,e.DefaultBeanFactory=a,e.Effect=function(e){return function(t,r,o){void 0===o&&(o=Object.getOwnPropertyDescriptor(t,r));var i=e||r,a=o.value;return o.value=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(y(this)){var r=this[p],o=r.container,u=r.type,s=r.identity,c=function(e){var t=o.getBeanObserver(u,s);void 0!==t&&t.effect$.next(n.__assign({effect:i},e))};c({loading:!0,data:null,error:null});var f=a.apply(this,e);return b(f)?f.then((function(e){return c({loading:!1,data:e,error:null}),e})).catch((function(e){throw c({loading:!1,data:null,error:e}),e})):(c({loading:!1,data:f,error:null}),f)}return a.apply(this,e)},o}},e.ForceUpdate=f,e.PostProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),c().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},e.Stated=function(){return function(e,t){c().collectStatedField({name:t,target:e.constructor})}},e.StatedBean=function(e){return function(t){M(t),Object.defineProperty(t,p,{writable:!1,value:!0}),c().collectStatedBean({name:e||t.name,target:t})}},e.StatedBeanApplication=u,e.StatedBeanConsumer=function(e){var t=e.children,n=j();return i.createElement(n.Consumer,null,(function(e){return t(e)}))},e.StatedBeanContainer=P,e.StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,o=j(),a=x({providers:t,application:n});return i.createElement(o.Provider,{value:{container:a}},r)},e.StatedBeanSymbol=p,e.boundClass=M,e.boundMethod=S,e.getPropertiesWithoutFunction=l,e.getProperty=v,e.getStatedBeanContext=j,e.isBeanContainerAware=m,e.isDisposableBean=_,e.isFunction=d,e.isInitializingBean=g,e.isPromise=b,e.isStatedBean=y,e.useBean=function(e,t){var n=j(),o=r.useContext(n),i=r.useState(0)[1],a=r.useCallback((function(e){i((function(e){return e+1}))}),[]),u=o.container;if(void 0===u)throw new Error("not found stated bean container.");var s=r.useState((function(){var n,r;if(d(e)&&!y(e)){var o=(r=e()).constructor;if("Object"===o.name&&null==t)throw new Error("plain object bean must be named.");n={type:o,bean:r,identity:t}}else n={type:e,identity:t};return u.registerAndObserve(n)}))[0],c=r.useState((function(){return s.state$.subscribe(a)}))[0];return r.useEffect((function(){return function(){return c.unsubscribe()}}),[c]),s.proxy},e.useContainer=x,e.useInject=function(e,t){void 0===t&&(t={});var n=j(),o=r.useContext(n),i=r.useState(0)[1],a=r.useCallback((function(e){var n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&i((function(e){return e+1}))}),[t.fields]),u=o.container;if(void 0===u)throw new Error("not found container");var s=r.useState((function(){return u.getBeanObserver(e,t.name)}))[0];if(void 0===s)throw new Error("bean["+e.name+"] observer is undefined");var c=r.useState((function(){return s.state$.subscribe(a)}))[0];return r.useEffect((function(){return function(){c.unsubscribe()}}),[c]),s.proxy},e.useObserveEffect=function(e,t){var n,o=j(),i=r.useContext(o).container;if(void 0===i)throw new Error("not found container");if(!y(e))throw new Error("bean is not a StatedBean");n=e[p].identity;var a=r.useState((function(){return{loading:!1,error:null,data:null,effect:t}})),u=a[0],s=a[1],c=r.useCallback((function(e){e.effect===t&&s(e)}),[t]),f=r.useState((function(){var t=i.getBeanObserver(e.constructor,n);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(c)}))[0];return r.useEffect((function(){return function(){f.unsubscribe()}}),[f]),u},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("rxjs"),require("tslib"),require("react")):"function"==typeof define&&define.amd?define(["exports","rxjs","tslib","react"],t):t((e=e||self).StatedBean={},e.rxjs,e.tslib,e.React)}(this,(function(e,t,n,r){"use strict";var o,i="default"in r?r.default:r,a=function(){function e(){this._beans=new Map}return e.prototype.get=function(e,t){var n=this._beans.get(t||e.name);return void 0===n?void 0:n.get(e)},e.prototype.register=function(e){var t;t="bean"in e?e.bean:new e.type;var n=e.identity||e.type.name,r=this._beans.get(n);void 0===r?this._beans.set(n,(new WeakMap).set(e.type,t)):r.set(e.type,t)},e.prototype.remove=function(e,t){var n=this._beans.get(t||e.name);void 0!==n&&n.delete(e)},e}(),s=function(){function e(){this._registry=new a}return e.prototype.get=function(e,t){return this._registry.get(e,t)},e.prototype.register=function(e){this._registry.register(e)},e.prototype.remove=function(e,t){this._registry.remove(e,t)},e}(),u=function(){function e(){this._beanFactory=new s,this._debug=!1}return e.prototype.getBeanFactory=function(){return this._beanFactory},e.prototype.setBeanFactory=function(e){return this._beanFactory=e,this},e.prototype.setDebug=function(e){this._debug=e},e.prototype.isDebug=function(){return this._debug},e}(),c=function(){function e(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap}return e.prototype.collectStatedBean=function(e){var t=e.target,n=this._tempTypeFields.get(t);e.statedFields=n,e.postMethod=this._tempPostMethod.get(t),this._beans.set(t,e),this._tempTypeFields.delete(t),this._tempPostMethod.delete(t)},e.prototype.collectStatedField=function(e){var t=e.target,n=this._tempTypeFields.get(t);n?n.push(e):this._tempTypeFields.set(t,[e])},e.prototype.collectPostProvided=function(e){this._tempPostMethod.set(e.target,e)},e.prototype.getBeanMeta=function(e){return this._beans.get(e)},e.prototype.clear=function(){this._beans=new WeakMap,this._tempTypeFields=new WeakMap,this._tempPostMethod=new WeakMap},e}(),p=function(){return o||(o=new c)},f=function(e){function r(){var n=null!==e&&e.apply(this,arguments)||this;return n._counter$=new t.Subject,n}return n.__extends(r,e),r.prototype._subscribe=function(n){var r=this,o=this,i=new t.Observable((function(t){var n=e.prototype._subscribe.call(r,t);return function(){n.unsubscribe(),r._counter$.next(o.observers.length)}})).subscribe(n);return this._counter$.next(this.observers.length),i},r.prototype.subscribeCount=function(e){this._counter$.subscribe(e)},r}(t.Subject),d=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return n.__extends(t,e),t}(Error),b=Symbol("stated-bean"),h=Symbol("stated-bean-force-update");function l(e,t){if(t in e)return e[t]}function y(e){var t=l(e,"postProvided");return void 0!==t&&"function"==typeof t}function v(e){var t=l(e,"destroy");return void 0!==t&&"function"==typeof t}function g(e){var t=l(e,"setBeanContainer");return void 0!==t&&"function"==typeof t}var _,m=function(){function e(e,n){this._container=e,this._provider=n,this.state$=new f,this.effect$=new t.Subject;var r=p().getBeanMeta(this._provider.type);if(void 0===r)throw new d(this._provider.type.name);this._beanMeta=r,g(this.bean)&&this.bean.setBeanContainer(this._container),this._observe()}return Object.defineProperty(e.prototype,"bean",{get:function(){return this._provider.bean},enumerable:!0,configurable:!0}),e.prototype.destroy=function(){this.state$.complete(),this.effect$.complete(),v(this.bean)&&this.bean.destroy()},e.prototype.publishStateAction=function(e,t){var n={bean:this.bean,nextValue:t,prevValue:this.bean[e.name],fieldMeta:e,beanMeta:this._beanMeta};this.state$.next(n)},e.prototype._observe=function(){var e=this;(this._defineStatedBean(),(this._beanMeta.statedFields||[]).forEach((function(t){return e._observeBeanField(t)})),null!=this._beanMeta.postMethod&&void 0!==this._beanMeta.postMethod.descriptor)?this._beanMeta.postMethod.descriptor.value.apply(this.bean):y(this.bean)&&this.bean.postProvided()},e.prototype._defineStatedBean=function(){var e=this;Object.defineProperty(this.bean,b,{value:{name:e._provider.identity,container:e._container,forceUpdate:function(t){var n=(e._beanMeta.statedFields||[]).find((function(e){return e.name===t}));void 0!==n&&e.publishStateAction(n,e.bean[t])}}})},e.prototype._observeBeanField=function(e){var t=Symbol(e.name.toString()+"_v");Object.defineProperty(this.bean,t,{writable:!0,value:this.bean[e.name]});var n=this;Object.defineProperty(this.bean,e.name.toString(),{set:function(r){n.bean[t]=r,n.publishStateAction(e,r)},get:function(){return n.bean[t]}})},e}(),w=function(){function e(e,t){this._beanObservers=new WeakMap,this._parent=e,null!=t?this._app=t:null!=this._parent&&null!=this._parent.application?this._app=this._parent.application:this._app=new u}return e.prototype.destroy=function(){this._beanObservers=new WeakMap},e.prototype.getBeanFactory=function(){return this._app.getBeanFactory()},e.prototype.getBeanIdentity=function(e,t){return t||this.getBeanMetaName(e)||e.name},e.prototype.getBeanMetaName=function(e){var t=this.getBeanMeta(e);return void 0===t?void 0:t.name},e.prototype.getBeanMeta=function(e){return p().getBeanMeta(e)},e.prototype.getBean=function(e,t){var n=this.getBeanIdentity(e,t),r=this.getBeanFactory().get(e,n);return null==r&&this.parent&&(r=this.parent.getBean(e,t)),r},e.prototype.getBeanObserver=function(e){return this._beanObservers.get(e)},e.prototype.register=function(e){var t=this.getBeanFactory();void 0!==e&&void 0===t.get(e.type,e.identity)&&t.register(e)},e.prototype.registerAndObserve=function(e){this.register(e);var t=this.getBean(e.type,e.identity);if(void 0===t)throw new Error("bean is undefined");return this._observeBean({type:e.type,bean:t,identity:this.getBeanIdentity(e.type,e.identity)})},e.prototype.hasBean=function(e,t){return void 0!==this.getBean(e,t)},e.prototype.remove=function(e,t,n){this._beanObservers&&this._beanObservers.delete(t),this.getBeanFactory().remove(e,n)},e.prototype._observeBean=function(e){var t=this,n=e.bean;if(this._beanObservers.has(n))return this._beanObservers.get(n);var r=new m(this,e);return r.state$.subscribeCount((function(e){0===e&&(r.destroy(),t._beanObservers&&t._beanObservers.delete(n))})),this._beanObservers.set(n,r),r},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 B(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 O(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,B(e,t,n))}})),e}function M(e){return"function"==typeof e}function S(e){return Object.hasOwnProperty.call(e,b)}function P(e){return!!e&&"function"==typeof e.then}function j(){return _||(_=i.createContext({})),_}function F(e){var t=e.providers,n=e.application,o=j(),i=r.useContext(o),a=r.useState((function(){var e=new w(i.container,n);return(t||[]).forEach((function(t){M(t)?e.registerAndObserve({type:t}).state$.subscribe():"type"in t&&e.registerAndObserve(t).state$.subscribe()})),e}))[0];return r.useEffect((function(){return function(){a.destroy()}}),[a]),a}e.BeanObserver=m,e.DefaultBeanFactory=s,e.Effect=function(e){return function(t,r,o){void 0===o&&(o=Object.getOwnPropertyDescriptor(t,r));var i=e||r,a=o.value;return o.value=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(S(this)){var r=this[b].container,o=r.getBeanObserver(this),s=function(e){void 0!==o&&o.effect$.next(n.__assign({loading:!1,error:null,data:null,effect:i},e))};s({loading:!0});var u=a.apply(this,e);return P(u)?u.then((function(e){return s({data:e}),e})).catch((function(e){return s({error:e}),e})):s({data:u}),u}return a.apply(this,e)},o}},e.ForceUpdate=h,e.PostProvided=function(){return function(e,t,n){return void 0===n&&(n=Object.getOwnPropertyDescriptor(e,t)),p().collectPostProvided({name:t,target:e.constructor,descriptor:n}),n}},e.Stated=function(){return function(e,t){p().collectStatedField({name:t,target:e.constructor})}},e.StatedBean=function(e){return function(t){O(t),Object.defineProperty(t,b,{writable:!1,value:!0}),p().collectStatedBean({name:e||t.name,target:t})}},e.StatedBeanApplication=u,e.StatedBeanConsumer=function(e){var t=e.children,n=j();return i.createElement(n.Consumer,null,(function(e){return t(e)}))},e.StatedBeanContainer=w,e.StatedBeanProvider=function(e){var t=e.providers,n=e.application,r=e.children,o=j(),a=F({providers:t,application:n});return i.createElement(o.Provider,{value:{container:a}},r)},e.StatedBeanSymbol=b,e.boundClass=O,e.boundMethod=B,e.getProperty=l,e.getStatedBeanContext=j,e.isBeanContainerAware=g,e.isDisposableBean=v,e.isInitializingBean=y,e.useBean=function(e,t){var n=j(),o=r.useContext(n),i=r.useState(0)[1],a=r.useCallback((function(e){i((function(e){return e+1}))}),[]),s=o.container;if(void 0===s)throw new Error("not found stated bean container.");var u=r.useState((function(){var n,r;if(M(e)&&!S(e)){var o=(r=e()).constructor;if("Object"===o.name&&null==t)throw new Error("plain object bean must be named.");n={type:o,bean:r,identity:t}}else n={type:e,identity:t};return s.registerAndObserve(n)}))[0],c=r.useState((function(){return u.state$.subscribe(a)}))[0];return r.useEffect((function(){return function(){return c.unsubscribe()}}),[c]),u.bean},e.useContainer=F,e.useInject=function(e,t){void 0===t&&(t={});var n=j(),o=r.useContext(n),i=r.useState(0)[1],a=r.useCallback((function(e){var n=e.fieldMeta.name;(null==t.fields||0===t.fields.length||t.fields.includes(n))&&i((function(e){return e+1}))}),[t.fields]),s=o.container;if(void 0===s)throw new Error("not found container");var u=r.useState((function(){var t=s.getBean(e,name);return s.getBeanObserver(t)}))[0];if(void 0===u)throw new Error("bean["+e+"] observer is undefined");var c=r.useState((function(){return u.state$.subscribe(a)}))[0];return r.useEffect((function(){return function(){c.unsubscribe()}}),[c]),u.bean},e.useObserveEffect=function(e,t){var n=j(),o=r.useContext(n).container;if(void 0===o)throw new Error("not found container");var i=r.useState((function(){return{loading:!1,error:null,data:null,effect:t}})),a=i[0],s=i[1],u=r.useCallback((function(e){e.effect===t&&s(e)}),[t]),c=r.useState((function(){var t=o.getBeanObserver(e);if(void 0===t)throw new Error("bean observer is undefined");return t.effect$.subscribe(u)}))[0];return r.useEffect((function(){return function(){c.unsubscribe()}}),[c]),a},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "stated-bean", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"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
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
161
474097
4379