value-enhancer
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -9,4 +9,7 @@ import type { ValCompare } from "./typings"; | ||
export declare type CombineValTransform<TDerivedValue = any, TValues extends readonly any[] = any[], TMeta = any> = (newValues: TValues, oldValues?: TValues, meta?: TMeta) => TDerivedValue; | ||
export declare type Combine = <TDerivedValue = any, TValInputs extends readonly Val[] = Val[], TMeta = ExtractValMeta<TValInputs[number]>>(valInputs: readonly [...TValInputs], transform: CombineValTransform<TDerivedValue, [ | ||
...TValInputsValueTuple<TValInputs> | ||
], TMeta>, compare?: ValCompare<TDerivedValue>, meta?: TMeta) => Val<TDerivedValue, TMeta>; | ||
export declare function combine<TDerivedValue = any, TValInputs extends readonly Val[] = Val[], TMeta = ExtractValMeta<TValInputs[number]>>(valInputs: readonly [...TValInputs], transform: CombineValTransform<TDerivedValue, [ | ||
...TValInputsValueTuple<TValInputs> | ||
], TMeta>, compare?: ValCompare<TDerivedValue>, meta?: TMeta): Val<TDerivedValue, TMeta>; |
import type { SideEffectManager } from "side-effect-manager"; | ||
import type { Val } from "./val"; | ||
import type { Combine } from "./combine"; | ||
import { Val } from "./val"; | ||
import type { ValCompare } from "./typings"; | ||
export declare type BindSideEffect = <TVal extends Val>(val: TVal) => TVal; | ||
export declare function createSideEffectBinder(sideEffect: SideEffectManager): BindSideEffect; | ||
export declare type CreateVal = <TValue = any, TMeta = any>(value: TValue, compare?: ValCompare<TValue>) => Val<TValue, TMeta>; | ||
export declare function createSideEffectBinder(sideEffect: SideEffectManager): { | ||
bindSideEffect: BindSideEffect; | ||
combine: Combine; | ||
createVal: CreateVal; | ||
}; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e=Object.defineProperty,s=("undefined"!=typeof require&&require,(s,r,t)=>(((s,r,t)=>{r in s?e(s,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[r]=t})(s,"symbol"!=typeof r?r+"":r,t),t));Object.defineProperty(exports,"__esModule",{value:!0}),exports[Symbol.toStringTag]="Module";class r{constructor(e,r){s(this,"_value"),s(this,"_beforeDestroys"),s(this,"_subscribers"),this._value=e,r&&(this.compare=r)}get value(){return this._value}setValue(e,s){if(!this.compare(e,this._value)){const r=this._value;this._value=e,this._subscribers&&this._subscribers.forEach((t=>t(e,r,s)))}}reaction(e){return this._subscribers||(this._subscribers=new Set),this._subscribers.add(e),()=>{this._subscribers&&this._subscribers.delete(e)}}subscribe(e,s){const r=this.reaction(e);return e(this._value,void 0,s),r}derive(e,s,t){const o=new r(e(this.value,void 0,t),s),i=this.reaction(((s,r,t)=>{o.setValue(e(s,r,t))}));return o.addBeforeDestroy(i),o}destroy(){this._beforeDestroys&&(this._beforeDestroys.forEach((e=>e())),this._beforeDestroys.clear()),this._subscribers&&this._subscribers.clear()}addBeforeDestroy(e){return this._beforeDestroys||(this._beforeDestroys=new Set),this._beforeDestroys.add(e),()=>{this._beforeDestroys&&this._beforeDestroys.delete(e)}}compare(e,s){return e===s}}function t(e,s,r){var t;Object.defineProperties(e,{[s]:{get:()=>r.value},[`$${s}`]:{value:r},[`set${t=s,t[0].toUpperCase()+t.slice(1)}`]:{value:e=>r.setValue(e)}})}exports.Val=r,exports.combine=function(e,s,t,o){let i=e.map((e=>e.value));const a=new r(s(i,void 0,o),t);return e.forEach(((e,r)=>{const t=e.reaction(((e,t)=>{const o=i.slice();o[r]=e;const u=i;i=o,a.setValue(s(o,u,t),t)}));a.addBeforeDestroy(t)})),a},exports.createSideEffectBinder=function(e){return function(s){const r=e.addDisposer((()=>{s.destroy()}));return s.addBeforeDestroy((()=>{e.remove(r)})),s}},exports.enhanceVal=t,exports.withValueEnhancer=function(e,s){Object.keys(s).forEach((r=>{t(e,r,s[r])}))}; | ||
"use strict";var e=Object.defineProperty,s=("undefined"!=typeof require&&require,(s,r,t)=>(((s,r,t)=>{r in s?e(s,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[r]=t})(s,"symbol"!=typeof r?r+"":r,t),t));Object.defineProperty(exports,"__esModule",{value:!0}),exports[Symbol.toStringTag]="Module";class r{constructor(e,r){s(this,"_value"),s(this,"_beforeDestroys"),s(this,"_subscribers"),this._value=e,r&&(this.compare=r)}get value(){return this._value}setValue(e,s){if(!this.compare(e,this._value)){const r=this._value;this._value=e,this._subscribers&&this._subscribers.forEach((t=>t(e,r,s)))}}reaction(e){return this._subscribers||(this._subscribers=new Set),this._subscribers.add(e),()=>{this._subscribers&&this._subscribers.delete(e)}}subscribe(e,s){const r=this.reaction(e);return e(this._value,void 0,s),r}derive(e,s,t){const o=new r(e(this.value,void 0,t),s),i=this.reaction(((s,r,t)=>{o.setValue(e(s,r,t))}));return o.addBeforeDestroy(i),o}destroy(){this._beforeDestroys&&(this._beforeDestroys.forEach((e=>e())),this._beforeDestroys.clear()),this._subscribers&&this._subscribers.clear()}addBeforeDestroy(e){return this._beforeDestroys||(this._beforeDestroys=new Set),this._beforeDestroys.add(e),()=>{this._beforeDestroys&&this._beforeDestroys.delete(e)}}compare(e,s){return e===s}}function t(e,s,t,o){let i=e.map((e=>e.value));const a=new r(s(i,void 0,o),t);return e.forEach(((e,r)=>{const t=e.reaction(((e,t)=>{const o=i.slice();o[r]=e;const c=i;i=o,a.setValue(s(o,c,t),t)}));a.addBeforeDestroy(t)})),a}function o(e,s,r){var t;Object.defineProperties(e,{[s]:{get:()=>r.value},[`$${s}`]:{value:r},[`set${t=s,t[0].toUpperCase()+t.slice(1)}`]:{value:e=>r.setValue(e)}})}exports.Val=r,exports.combine=t,exports.createSideEffectBinder=function(e){const s=s=>{const r=e.addDisposer((()=>{s.destroy()}));return s.addBeforeDestroy((()=>{e.remove(r)})),s};return{bindSideEffect:s,combine:(e,r,o,i)=>s(t(e,r,o,i)),createVal:(e,t)=>s(new r(e,t))}},exports.enhanceVal=o,exports.withValueEnhancer=function(e,s){Object.keys(s).forEach((r=>{o(e,r,s[r])}))}; | ||
//# sourceMappingURL=value-enhancer.cjs.js.map |
@@ -1,2 +0,2 @@ | ||
var e=Object.defineProperty,s=("undefined"!=typeof require&&require,(s,r,t)=>(((s,r,t)=>{r in s?e(s,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[r]=t})(s,"symbol"!=typeof r?r+"":r,t),t));class r{constructor(e,r){s(this,"_value"),s(this,"_beforeDestroys"),s(this,"_subscribers"),this._value=e,r&&(this.compare=r)}get value(){return this._value}setValue(e,s){if(!this.compare(e,this._value)){const r=this._value;this._value=e,this._subscribers&&this._subscribers.forEach((t=>t(e,r,s)))}}reaction(e){return this._subscribers||(this._subscribers=new Set),this._subscribers.add(e),()=>{this._subscribers&&this._subscribers.delete(e)}}subscribe(e,s){const r=this.reaction(e);return e(this._value,void 0,s),r}derive(e,s,t){const i=new r(e(this.value,void 0,t),s),o=this.reaction(((s,r,t)=>{i.setValue(e(s,r,t))}));return i.addBeforeDestroy(o),i}destroy(){this._beforeDestroys&&(this._beforeDestroys.forEach((e=>e())),this._beforeDestroys.clear()),this._subscribers&&this._subscribers.clear()}addBeforeDestroy(e){return this._beforeDestroys||(this._beforeDestroys=new Set),this._beforeDestroys.add(e),()=>{this._beforeDestroys&&this._beforeDestroys.delete(e)}}compare(e,s){return e===s}}function t(e,s,t,i){let o=e.map((e=>e.value));const u=new r(s(o,void 0,i),t);return e.forEach(((e,r)=>{const t=e.reaction(((e,t)=>{const i=o.slice();i[r]=e;const a=o;o=i,u.setValue(s(i,a,t),t)}));u.addBeforeDestroy(t)})),u}function i(e,s){Object.keys(s).forEach((r=>{o(e,r,s[r])}))}function o(e,s,r){var t;Object.defineProperties(e,{[s]:{get:()=>r.value},[`$${s}`]:{value:r},[`set${t=s,t[0].toUpperCase()+t.slice(1)}`]:{value:e=>r.setValue(e)}})}function u(e){return function(s){const r=e.addDisposer((()=>{s.destroy()}));return s.addBeforeDestroy((()=>{e.remove(r)})),s}}export{r as Val,t as combine,u as createSideEffectBinder,o as enhanceVal,i as withValueEnhancer}; | ||
var e=Object.defineProperty,s=("undefined"!=typeof require&&require,(s,r,t)=>(((s,r,t)=>{r in s?e(s,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[r]=t})(s,"symbol"!=typeof r?r+"":r,t),t));class r{constructor(e,r){s(this,"_value"),s(this,"_beforeDestroys"),s(this,"_subscribers"),this._value=e,r&&(this.compare=r)}get value(){return this._value}setValue(e,s){if(!this.compare(e,this._value)){const r=this._value;this._value=e,this._subscribers&&this._subscribers.forEach((t=>t(e,r,s)))}}reaction(e){return this._subscribers||(this._subscribers=new Set),this._subscribers.add(e),()=>{this._subscribers&&this._subscribers.delete(e)}}subscribe(e,s){const r=this.reaction(e);return e(this._value,void 0,s),r}derive(e,s,t){const i=new r(e(this.value,void 0,t),s),o=this.reaction(((s,r,t)=>{i.setValue(e(s,r,t))}));return i.addBeforeDestroy(o),i}destroy(){this._beforeDestroys&&(this._beforeDestroys.forEach((e=>e())),this._beforeDestroys.clear()),this._subscribers&&this._subscribers.clear()}addBeforeDestroy(e){return this._beforeDestroys||(this._beforeDestroys=new Set),this._beforeDestroys.add(e),()=>{this._beforeDestroys&&this._beforeDestroys.delete(e)}}compare(e,s){return e===s}}function t(e,s,t,i){let o=e.map((e=>e.value));const c=new r(s(o,void 0,i),t);return e.forEach(((e,r)=>{const t=e.reaction(((e,t)=>{const i=o.slice();i[r]=e;const a=o;o=i,c.setValue(s(i,a,t),t)}));c.addBeforeDestroy(t)})),c}function i(e,s){Object.keys(s).forEach((r=>{o(e,r,s[r])}))}function o(e,s,r){var t;Object.defineProperties(e,{[s]:{get:()=>r.value},[`$${s}`]:{value:r},[`set${t=s,t[0].toUpperCase()+t.slice(1)}`]:{value:e=>r.setValue(e)}})}function c(e){const s=s=>{const r=e.addDisposer((()=>{s.destroy()}));return s.addBeforeDestroy((()=>{e.remove(r)})),s};return{bindSideEffect:s,combine:(e,r,i,o)=>s(t(e,r,i,o)),createVal:(e,t)=>s(new r(e,t))}}export{r as Val,t as combine,c as createSideEffectBinder,o as enhanceVal,i as withValueEnhancer}; | ||
//# sourceMappingURL=value-enhancer.es.js.map |
{ | ||
"name": "value-enhancer", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -22,2 +22,17 @@ import type { ValCompare } from "./typings"; | ||
export type Combine = < | ||
TDerivedValue = any, | ||
TValInputs extends readonly Val[] = Val[], | ||
TMeta = ExtractValMeta<TValInputs[number]> | ||
>( | ||
valInputs: readonly [...TValInputs], | ||
transform: CombineValTransform< | ||
TDerivedValue, | ||
[...TValInputsValueTuple<TValInputs>], | ||
TMeta | ||
>, | ||
compare?: ValCompare<TDerivedValue>, | ||
meta?: TMeta | ||
) => Val<TDerivedValue, TMeta>; | ||
export function combine< | ||
@@ -24,0 +39,0 @@ TDerivedValue = any, |
import type { SideEffectManager } from "side-effect-manager"; | ||
import type { Val } from "./val"; | ||
import { combine as combineOrigin } from "./combine"; | ||
import type { Combine } from "./combine"; | ||
import { Val } from "./val"; | ||
import type { ValCompare } from "./typings"; | ||
export type BindSideEffect = <TVal extends Val>(val: TVal) => TVal; | ||
export function createSideEffectBinder( | ||
sideEffect: SideEffectManager | ||
): BindSideEffect { | ||
return function bindSideEffect<TVal extends Val>(val: TVal): TVal { | ||
export type CreateVal = <TValue = any, TMeta = any>( | ||
value: TValue, | ||
compare?: ValCompare<TValue> | ||
) => Val<TValue, TMeta>; | ||
export function createSideEffectBinder(sideEffect: SideEffectManager): { | ||
bindSideEffect: BindSideEffect; | ||
combine: Combine; | ||
createVal: CreateVal; | ||
} { | ||
const bindSideEffect: BindSideEffect = val => { | ||
const disposerID = sideEffect.addDisposer(() => { | ||
@@ -18,2 +28,16 @@ val.destroy(); | ||
}; | ||
const combine: Combine = (valInputs, transform, compare, meta) => { | ||
return bindSideEffect(combineOrigin(valInputs, transform, compare, meta)); | ||
}; | ||
const createVal: CreateVal = (value, compare) => { | ||
return bindSideEffect(new Val(value, compare)); | ||
}; | ||
return { | ||
bindSideEffect, | ||
combine, | ||
createVal, | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43854
393