@domonda/plumb
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -6,2 +6,15 @@ # Change Log | ||
# [2.1.0](https://github.com/domonda/domonda-js/compare/@domonda/plumb@2.0.1...@domonda/plumb@2.1.0) (2019-08-29) | ||
### Features | ||
* **equality:** use `fast-equals` ([e86cb8d](https://github.com/domonda/domonda-js/commit/e86cb8d)) | ||
* **tags:** implement ([9202861](https://github.com/domonda/domonda-js/commit/9202861)) | ||
* **tags:** introduce generics and require tags ([5c89552](https://github.com/domonda/domonda-js/commit/5c89552)) | ||
## [2.0.1](https://github.com/domonda/domonda-js/compare/@domonda/plumb@2.0.0...@domonda/plumb@2.0.1) (2019-08-02) | ||
@@ -8,0 +21,0 @@ |
@@ -7,2 +7,2 @@ /** | ||
import { PlumbProps, Plumb } from './Plumb'; | ||
export declare function createPlumb<T>(initialState: T, props?: PlumbProps<T>): Plumb<T>; | ||
export declare function createPlumb<S, T>(initialState: S, props?: PlumbProps<S, T>): Plumb<S, T>; |
@@ -10,7 +10,7 @@ "use strict"; | ||
const { transformer, skipInitialTransform } = props; | ||
let disposeHandlers = []; | ||
const disposeHandlers = []; | ||
let disposed = false; | ||
let internalState = initialState; | ||
if (transformer && !skipInitialTransform) { | ||
internalState = transformer(internalState); | ||
internalState = transformer(internalState, undefined); | ||
} | ||
@@ -34,3 +34,3 @@ // when the chained plumb triggers next it internally transforms | ||
} | ||
function next(state) { | ||
function next(state, tag) { | ||
if (disposed) { | ||
@@ -43,3 +43,3 @@ throw new Error('cannot send a value through a disposed plumb'); | ||
if (transformer !== skipTransformer) { | ||
internalState = transformer(internalState); | ||
internalState = transformer(internalState, tag); | ||
} | ||
@@ -49,14 +49,14 @@ } | ||
if (transformer) { | ||
internalState = transformer(internalState); | ||
internalState = transformer(internalState, tag); | ||
} | ||
for (const subscriber of subscribers) { | ||
if (typeof subscriber === 'function') { | ||
subscriber(internalState); | ||
subscriber(internalState, tag); | ||
} | ||
else if (subscriber.next) { | ||
subscriber.next(internalState); | ||
subscriber.next(internalState, tag); | ||
} | ||
} | ||
} | ||
function chain(props) { | ||
function chain(props, chainTag) { | ||
if (disposed) { | ||
@@ -70,3 +70,3 @@ throw new Error('cannot chain a disposed plumb'); | ||
}; | ||
function memoSelector(state) { | ||
const memoSelector = (state) => { | ||
if (memoData.state !== state) { | ||
@@ -77,7 +77,7 @@ memoData.state = state; | ||
return memoData.selectedState; | ||
} | ||
function parentTransformer(state) { | ||
}; | ||
function parentTransformer(state, tag) { | ||
let selectedState = memoSelector(state); | ||
if (chainTransformer) { | ||
selectedState = chainTransformer(selectedState); | ||
selectedState = chainTransformer(selectedState, tag); | ||
} | ||
@@ -91,3 +91,3 @@ return updater(state, selectedState); | ||
const chained = createPlumb(memoSelector(internalState), { | ||
transformer: (selectedState) => { | ||
transformer: (selectedState, tag) => { | ||
if (chainTransformer) { | ||
@@ -98,6 +98,5 @@ if (chainSkipInitialTransform && isInitialTransform) { | ||
else { | ||
selectedState = chainTransformer(selectedState); | ||
selectedState = chainTransformer(selectedState, tag); | ||
} | ||
} | ||
isInitialTransform = false; | ||
if (!parentNext) { | ||
@@ -108,17 +107,19 @@ const nextState = updater(internalState, selectedState); | ||
skipTransformer = parentTransformer; | ||
next(nextState); | ||
next(nextState, isInitialTransform ? chainTag : tag); | ||
skipTransformer = null; | ||
chainedNext = false; | ||
isInitialTransform = false; | ||
return memoSelector(internalState); | ||
} | ||
} | ||
isInitialTransform = false; | ||
return selectedState; | ||
}, | ||
}); | ||
const subscription = subscribe((state) => { | ||
const subscription = subscribe((state, tag) => { | ||
if (!chainedNext) { | ||
const selectedState = memoSelector(state); | ||
if (filter(selectedState)) { | ||
if (filter(selectedState, tag)) { | ||
parentNext = true; | ||
chained.next(selectedState); | ||
chained.next(selectedState, tag); | ||
parentNext = false; | ||
@@ -125,0 +126,0 @@ } |
export * from './Plumb'; | ||
export * from './createPlumb'; | ||
export * from './equality'; |
@@ -7,2 +7,1 @@ "use strict"; | ||
__export(require("./createPlumb")); | ||
__export(require("./equality")); |
{ | ||
"name": "@domonda/plumb", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Simple state management inspired by Observables.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
/** | ||
* | ||
* Plumb.d | ||
* Plumb | ||
* | ||
@@ -9,27 +9,27 @@ */ | ||
} | ||
export declare type Subscriber<T> = ((state: Readonly<T>) => void) | ({ | ||
export declare type Subscriber<S, T> = ((state: Readonly<S>, tag: T) => void) | ({ | ||
dispose?: () => void; | ||
next?: (state: Readonly<T>) => void; | ||
next?: (state: Readonly<S>, tag: T) => void; | ||
}); | ||
export declare type Subscription = Disposable; | ||
export declare type Transformer<T> = (state: Readonly<T>) => T; | ||
export declare type Filter<T> = (state: T) => boolean; | ||
export declare type Selector<T, K> = (state: Readonly<T>) => K; | ||
export declare type Updater<T, K> = (state: Readonly<T>, selectedState: Readonly<K>) => T; | ||
export interface ChainProps<T, K> extends PlumbProps<K> { | ||
selector: Selector<T, K>; | ||
updater: Updater<T, K>; | ||
filter?: Filter<K>; | ||
export declare type Transformer<S, T> = (state: Readonly<S>, tag: T | undefined) => S; | ||
export declare type Filter<S, T> = (state: S, tag: T) => boolean; | ||
export declare type Selector<S, K> = (state: Readonly<S>) => K; | ||
export declare type Updater<S, K> = (state: Readonly<S>, selectedState: Readonly<K>) => S; | ||
export interface ChainProps<S, K, T> extends PlumbProps<K, T> { | ||
selector: Selector<S, K>; | ||
updater: Updater<S, K>; | ||
filter?: Filter<K, T>; | ||
} | ||
export interface PlumbProps<T> { | ||
transformer?: Transformer<T>; | ||
export interface PlumbProps<S, T> { | ||
transformer?: Transformer<S, T>; | ||
skipInitialTransform?: boolean; | ||
} | ||
export interface Plumb<T> extends Disposable { | ||
readonly state: T; | ||
readonly subscribers: Subscriber<T>[]; | ||
export interface Plumb<S, T> extends Disposable { | ||
readonly state: S; | ||
readonly subscribers: Subscriber<S, T>[]; | ||
readonly disposed: boolean; | ||
chain: <K>(props: ChainProps<T, K>) => Plumb<K>; | ||
next: (state: T) => void; | ||
subscribe: (subscriber: Subscriber<T>) => Subscription; | ||
chain: <K>(props: ChainProps<S, K, T>, tag: T) => Plumb<K, T>; | ||
next: (state: S, tag: T) => void; | ||
subscribe: (subscriber: Subscriber<S, T>) => Subscription; | ||
} |
"use strict"; | ||
/** | ||
* | ||
* Plumb.d | ||
* Plumb | ||
* | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
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
11478
9
235