value-enhancer
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -1,2 +0,2 @@ | ||
"use strict";var E=Object.defineProperty,y=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var l=(r,e,s)=>e in r?E(r,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[e]=s,_=(r,e)=>{for(var s in e||(e={}))$.call(e,s)&&l(r,s,e[s]);if(p)for(var s of p(e))j.call(e,s)&&l(r,s,e[s]);return r},d=(r,e)=>y(r,z(e));var u=(r,e,s)=>(l(r,typeof e!="symbol"?e+"":e,s),s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class O{constructor(e){u(this,"_subscribers");u(this,"_bSub");u(this,"_bSubDisposer");this._bSub=e}get size(){return this._subscribers?this._subscribers.size:0}invoke(e,s){this._subscribers&&this._subscribers.forEach(i=>i(e,s))}add(e){this._bSub&&(!this._subscribers||this._subscribers.size<=0)&&(this._bSubDisposer=this._bSub()),this._subscribers||(this._subscribers=new Set),this._subscribers.add(e)}remove(e){if(this._subscribers&&this._subscribers.delete(e),this._subscribers&&this._subscribers.size<=0&&this._bSubDisposer){const s=this._bSubDisposer;this._bSubDisposer=null,s()}}clear(){if(this._subscribers&&this._subscribers.clear(),this._bSubDisposer){const e=this._bSubDisposer;this._bSubDisposer=null,e()}}destroy(){this.clear()}}class h{constructor(e,s){u(this,"_subscribers");u(this,"_value");this._value=e;let i;if(s&&(s.compare&&(this.compare=s.compare),s.beforeSubscribe)){const t=s.beforeSubscribe,b=this._setValue.bind(this);i=()=>t(b)}this._subscribers=new O(i)}_setValue(e,s){this.compare(e,this._value)||(this._value=e,this._subscribers.invoke(e,s))}get value(){return this._value}reaction(e){return this._subscribers.add(e),()=>{this._subscribers.remove(e)}}subscribe(e,s){const i=this.reaction(e);return e(this._value,s),i}destroy(){this._subscribers.destroy()}unsubscribe(e){this._subscribers.remove(e)}compare(e,s){return e===s}}class M extends h{constructor(){super(...arguments);u(this,"setValue",this._setValue)}}class S extends h{constructor(s,i,t={}){super(i(s.value),d(_({},t),{beforeSubscribe:b=>{const c=s.subscribe((a,o)=>b(i(a),o));if(t.beforeSubscribe){const a=t.beforeSubscribe(b);if(a)return()=>{c(),a()}}return c}}));u(this,"_srcValue");this._srcValue=()=>i(s.value)}get value(){if(this._subscribers.size<=0){const s=this._srcValue();return this.compare(s,this._value)?this._value:s}return this._value}}function R(r,e=i=>i,s={}){return new S(r,e,s)}class v extends h{constructor(s,i,t={}){super(i(f(s)),d(_({},t),{beforeSubscribe:b=>{let c=f(s);b(i(c));const a=s.map((n,D)=>n.reaction((m,w)=>{c=c.slice(),c[D]=m,b(i(c),w)})),o=()=>a.forEach(n=>n());if(t.beforeSubscribe){const n=t.beforeSubscribe(b);if(n)return()=>{o(),n()}}return o}}));u(this,"_srcValue");this._srcValue=()=>i(f(s))}get value(){if(this._subscribers.size<=0){const s=this._srcValue();return this.compare(s,this._value)?this._value:s}return this._value}}function f(r){return r.map(x)}function x(r){return r.value}function C(r,e=i=>i,s={}){return new v(r,e,s)}function P(r,e){Object.keys(e).forEach(s=>{g(r,s,e[s])})}function g(r,e,s){return Object.defineProperties(r,{[e]:{get(){return s.value}},[`_${e}$`]:{value:s}}),r}function I(r,e){Object.keys(e).forEach(s=>{V(r,s,e[s])})}function V(r,e,s){return Object.defineProperties(r,{[e]:{get(){return s.value},set(i){s.setValue(i)}},[`_${e}$`]:{value:s},[`set${T(e)}`]:{value:(i,t)=>s.setValue(i,t)}}),r}function T(r){return r[0].toUpperCase()+r.slice(1)}class U{constructor(){u(this,"vals",new Set)}attach(e){return this.vals.add(e),e}detach(e){return this.vals.delete(e),e}destroy(){this.vals.forEach(q),this.vals.clear()}}function q(r){r.destroy()}exports.CombinedVal=v;exports.DerivedVal=S;exports.ReadonlyVal=h;exports.Val=M;exports.ValManager=U;exports.bindInstance=V;exports.combine=C;exports.derive=R;exports.withReadonlyValueEnhancer=P;exports.withValueEnhancer=I; | ||
"use strict";var E=Object.defineProperty,z=Object.defineProperties;var y=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var l=(r,e,s)=>e in r?E(r,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[e]=s,_=(r,e)=>{for(var s in e||(e={}))$.call(e,s)&&l(r,s,e[s]);if(p)for(var s of p(e))j.call(e,s)&&l(r,s,e[s]);return r},d=(r,e)=>z(r,y(e));var u=(r,e,s)=>(l(r,typeof e!="symbol"?e+"":e,s),s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class O{constructor(e){u(this,"_subscribers");u(this,"_bSub");u(this,"_bSubDisposer");this._bSub=e}get size(){return this._subscribers?this._subscribers.size:0}invoke(e,s){this._subscribers&&this._subscribers.forEach(i=>i(e,s))}add(e){this._bSub&&(!this._subscribers||this._subscribers.size<=0)&&(this._bSubDisposer=this._bSub()),this._subscribers||(this._subscribers=new Set),this._subscribers.add(e)}remove(e){if(this._subscribers&&this._subscribers.delete(e),this._subscribers&&this._subscribers.size<=0&&this._bSubDisposer){const s=this._bSubDisposer;this._bSubDisposer=null,s()}}clear(){if(this._subscribers&&this._subscribers.clear(),this._bSubDisposer){const e=this._bSubDisposer;this._bSubDisposer=null,e()}}destroy(){this.clear()}}class h{constructor(e,s){u(this,"_subscribers");u(this,"_value");this._value=e;let i;if(s&&(s.compare&&(this.compare=s.compare),s.beforeSubscribe)){const t=s.beforeSubscribe,b=this._setValue.bind(this);i=()=>t(b)}this._subscribers=new O(i)}_setValue(e,s){this.compare(e,this._value)||(this._value=e,this._subscribers.invoke(e,s))}get value(){return this._value}reaction(e){return this._subscribers.add(e),()=>{this._subscribers.remove(e)}}subscribe(e,s){const i=this.reaction(e);return e(this._value,s),i}destroy(){this._subscribers.destroy()}unsubscribe(e){this._subscribers.remove(e)}compare(e,s){return e===s}}class R extends h{constructor(){super(...arguments);u(this,"setValue",this._setValue)}}class S extends h{constructor(s,i,t={}){super(i(s.value),d(_({},t),{beforeSubscribe:b=>{const c=s.subscribe((a,o)=>b(i(a),o));if(t.beforeSubscribe){const a=t.beforeSubscribe(b);if(a)return()=>{c(),a()}}return c}}));u(this,"_srcValue");this._srcValue=()=>i(s.value)}get value(){if(this._subscribers.size<=0){const s=this._srcValue();return this.compare(s,this._value)?this._value:s}return this._value}}function x(r,e=i=>i,s={}){return new S(r,e,s)}class V extends h{constructor(s,i,t={}){super(i(f(s)),d(_({},t),{beforeSubscribe:b=>{let c=f(s);b(i(c));const a=s.map((n,D)=>n.reaction((m,w)=>{c=c.slice(),c[D]=m,b(i(c),w)})),o=()=>a.forEach(n=>n());if(t.beforeSubscribe){const n=t.beforeSubscribe(b);if(n)return()=>{o(),n()}}return o}}));u(this,"_srcValue");this._srcValue=()=>i(f(s))}get value(){if(this._subscribers.size<=0){const s=this._srcValue();return this.compare(s,this._value)?this._value:s}return this._value}}function f(r){return r.map(C)}function C(r){return r.value}function P(r,e=i=>i,s={}){return new V(r,e,s)}function M(r,e,s){Object.keys(e).forEach(i=>{I(r,i,e[i]),s&&s.attach(e[i])})}function I(r,e,s){return Object.defineProperties(r,{[e]:{get(){return s.value}},[`_${e}$`]:{value:s}}),r}function T(r,e,s){Object.keys(e).forEach(i=>{v(r,i,e[i]),s&&s.attach(e[i])})}function v(r,e,s){return Object.defineProperties(r,{[e]:{get(){return s.value},set(i){s.setValue(i)}},[`_${e}$`]:{value:s},[`set${U(e)}`]:{value:(i,t)=>s.setValue(i,t)}}),r}function U(r){return r[0].toUpperCase()+r.slice(1)}class q{constructor(){u(this,"vals",new Set)}attach(e){return this.vals.add(e),e}detach(e){return this.vals.delete(e),e}destroy(){this.vals.forEach(A),this.vals.clear()}}function A(r){r.destroy()}exports.CombinedVal=V;exports.DerivedVal=S;exports.ReadonlyVal=h;exports.Val=R;exports.ValManager=q;exports.bindInstance=v;exports.combine=P;exports.derive=x;exports.withReadonlyValueEnhancer=M;exports.withValueEnhancer=T; | ||
//# sourceMappingURL=value-enhancer.cjs.js.map |
@@ -203,5 +203,8 @@ var __defProp = Object.defineProperty; | ||
} | ||
function withReadonlyValueEnhancer(instance, config) { | ||
function withReadonlyValueEnhancer(instance, config, valManager) { | ||
Object.keys(config).forEach((key) => { | ||
bindInstance$1(instance, key, config[key]); | ||
if (valManager) { | ||
valManager.attach(config[key]); | ||
} | ||
}); | ||
@@ -222,5 +225,8 @@ } | ||
} | ||
function withValueEnhancer(instance, config) { | ||
function withValueEnhancer(instance, config, valManager) { | ||
Object.keys(config).forEach((key) => { | ||
bindInstance(instance, key, config[key]); | ||
if (valManager) { | ||
valManager.attach(config[key]); | ||
} | ||
}); | ||
@@ -227,0 +233,0 @@ } |
import type { ExtractValValue } from "./combine"; | ||
import type { ReadonlyVal } from "./readonly-val"; | ||
import type { ValManager } from "./val-manager"; | ||
declare type IntersectionFromUnion<TUnion> = (TUnion extends any ? (arg: TUnion) => void : never) extends (arg: infer TArg) => void ? TArg : never; | ||
@@ -43,3 +44,3 @@ declare type ExtractReadonlyValKeys<TInstance, TKey = keyof TInstance> = TKey extends Extract<keyof TInstance, string> ? TInstance[TKey] extends ReadonlyVal ? TKey : never : never; | ||
*/ | ||
export declare function withReadonlyValueEnhancer<TInstance, TConfig extends ReadonlyValEnhancerConfig>(instance: TInstance, config: TConfig): void; | ||
export declare function withReadonlyValueEnhancer<TInstance, TConfig extends ReadonlyValEnhancerConfig>(instance: TInstance, config: TConfig, valManager?: ValManager): void; | ||
export {}; |
import type { ExtractValMeta, ExtractValValue } from "./combine"; | ||
import type { Val } from "./val"; | ||
import type { ValManager } from "./val-manager"; | ||
declare type IntersectionFromUnion<TUnion> = (TUnion extends any ? (arg: TUnion) => void : never) extends (arg: infer TArg) => void ? TArg : never; | ||
@@ -45,3 +46,3 @@ declare type ExtractValKeys<TInstance, TKey = keyof TInstance> = TKey extends Extract<keyof TInstance, string> ? TInstance[TKey] extends Val ? TKey : never : never; | ||
*/ | ||
export declare function withValueEnhancer<TInstance, TConfig extends ValEnhancerConfig>(instance: TInstance, config: TConfig): void; | ||
export declare function withValueEnhancer<TInstance, TConfig extends ValEnhancerConfig>(instance: TInstance, config: TConfig, valManager?: ValManager): void; | ||
export declare type BindVal = <TKey extends string, TValue, TMeta>(key: TKey, val: Val<TValue, TMeta>) => Val<TValue, TMeta>; | ||
@@ -48,0 +49,0 @@ /** |
{ | ||
"name": "value-enhancer", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -152,1 +152,63 @@ # value-enhancer | ||
``` | ||
Combined with `withValueEnhancer` or `withReadonlyValueEnhancer`: | ||
```js | ||
import type { ValEnhancedResult } from "value-enhancer"; | ||
import { Val, withValueEnhancer, ValManager } from "value-enhancer"; | ||
class Obj { | ||
constructor() { | ||
this.valManager = new ValManager(); | ||
const apple$ = new Val("apple"); | ||
const banana$ = new Val("banana"); | ||
withValueEnhancer( | ||
this, | ||
{ | ||
apple: apple$, | ||
banana: banana$, | ||
}, | ||
this.valManager | ||
); | ||
} | ||
destroy() { | ||
this.valManager.destroy(); | ||
} | ||
} | ||
``` | ||
And `valManager` may keep private with [`side-effect-manager`](https://github.com/crimx/side-effect-manager): | ||
```js | ||
import type { ValEnhancedResult } from "value-enhancer"; | ||
import { Val, withValueEnhancer, ValManager } from "value-enhancer"; | ||
import { SideEffectManager } from "side-effect-manager"; | ||
class Obj { | ||
constructor() { | ||
this.sideEffect = new SideEffectManager(); | ||
const valManager = new ValManager(); | ||
this.sideEffect.addDisposer(() => valManager.destroy()); | ||
const apple$ = new Val("apple"); | ||
const banana$ = new Val("banana"); | ||
withValueEnhancer( | ||
this, | ||
{ | ||
apple: apple$, | ||
banana: banana$, | ||
}, | ||
valManager | ||
); | ||
} | ||
destroy() { | ||
this.sideEffect.flushAll(); | ||
} | ||
} | ||
``` |
@@ -5,2 +5,3 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
import type { ReadonlyVal } from "./readonly-val"; | ||
import type { ValManager } from "./val-manager"; | ||
@@ -76,5 +77,8 @@ type IntersectionFromUnion<TUnion> = ( | ||
TConfig extends ReadonlyValEnhancerConfig | ||
>(instance: TInstance, config: TConfig): void { | ||
>(instance: TInstance, config: TConfig, valManager?: ValManager): void { | ||
Object.keys(config).forEach(key => { | ||
bindInstance(instance, key, config[key]); | ||
if (valManager) { | ||
valManager.attach(config[key]); | ||
} | ||
}); | ||
@@ -81,0 +85,0 @@ } |
@@ -5,2 +5,3 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
import type { Val } from "./val"; | ||
import type { ValManager } from "./val-manager"; | ||
@@ -82,6 +83,10 @@ type IntersectionFromUnion<TUnion> = ( | ||
instance: TInstance, | ||
config: TConfig | ||
config: TConfig, | ||
valManager?: ValManager | ||
): void { | ||
Object.keys(config).forEach(key => { | ||
bindInstance(instance, key, config[key]); | ||
if (valManager) { | ||
valManager.attach(config[key]); | ||
} | ||
}); | ||
@@ -88,0 +93,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94006
1095
214