Comparing version 0.1.9 to 0.1.10
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class s{constructor(){this.subscribers=new Set,this.subscribersHot=new Set,this.dispatched=!1,this.hot=!1}dispatch(s){const t=this.val;this.val=s,this.dispatched=!0,this.subscribers.forEach(e=>e(s,t))}subscribeNext(s){return this.subscribers.add(s),this.notifyHotSubscribers(),{unsubscribe:()=>{this.subscribers.delete(s)}}}subscribe(s){return this.dispatched&&s(this.val),this.subscribeNext(s)}unsubscribe(s){this.subscribers.delete(s),this.notifyHotSubscribers()}notifyHotSubscribers(){this.subscribers.size>0!=this.hot&&(this.hot=this.subscribers.size>0,this.subscribersHot.forEach(s=>s(this.hot)))}subscribeHot(s){return this.subscribersHot.add(s),{unsubscribe:()=>{this.subscribersHot.delete(s)}}}clear(){this.subscribers.clear(),this.subscribersHot.clear()}value(){return this.val}promise(){return new Promise(s=>{const t=this.subscribe((function(e){t.unsubscribe(),s(e)}))})}map(t){const e=new s,r=s=>{e.dispatch(t(s))};return e.subscribeHot(s=>{s?this.subscribe(r):this.unsubscribe(r)}),e}filter(t){const e=new s,r=s=>{t(s)&&e.dispatch(s)};return e.subscribeHot(s=>{s?this.subscribe(r):this.unsubscribe(r)}),e}}exports.Cuprum=s,exports.combine=function(...t){const e=new s,r=new Set;return e.subscribeHot(s=>{s?t.forEach(s=>{r.add(s.subscribe(()=>{e.dispatch(t.map(s=>s.value()))}))}):r.forEach(s=>s.unsubscribe())}),e},exports.fromEvent=function(t,e){const r=new s,i=s=>{r.dispatch(s)};return r.subscribeHot(s=>{s?t.addEventListener(e,i,!1):t.removeEventListener(e,i,!1)}),r},exports.tap=function(s){return s(this.val),this}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class s{constructor(){this.subscribers=new Set,this.subscribersHot=new Set,this.dispatched=!1,this.hot=!1,this.isSubject=!1}dispatch(s){if(this.isSubject)throw"Can't dispatch on subject";this.internalDispatch(s)}subscribe(s){return this.dispatched&&s(this.val),this.subscribeNext(s)}unsubscribe(s){this.subscribers.delete(s),this.notifyHotSubscribers()}subscribeHot(s){return this.subscribersHot.add(s),{unsubscribe:()=>{this.subscribersHot.delete(s)}}}clear(){this.subscribers.clear(),this.subscribersHot.clear()}value(){return this.val}observable(){const s=this.map(s=>s);return s.isSubject=!0,s}promise(){return new Promise(s=>{const t=this.subscribe((function(e){t.unsubscribe(),s(e)}))})}map(t){const e=new s,i=s=>{e.internalDispatch(t(s))};return e.subscribeHot(s=>{s?this.subscribe(i):this.unsubscribe(i)}),e}filter(t){const e=new s,i=s=>{t(s)&&e.dispatch(s)};return e.subscribeHot(s=>{s?this.subscribe(i):this.unsubscribe(i)}),e}notifyHotSubscribers(){this.subscribers.size>0!=this.hot&&(this.hot=this.subscribers.size>0,this.subscribersHot.forEach(s=>s(this.hot)))}subscribeNext(s){return this.subscribers.add(s),this.notifyHotSubscribers(),{unsubscribe:()=>{this.subscribers.delete(s)}}}internalDispatch(s){const t=this.val;this.val=s,this.dispatched=!0,this.subscribers.forEach(e=>e(s,t))}}exports.Cuprum=s,exports.combine=function(...t){const e=new s,i=new Set;return e.subscribeHot(s=>{s?t.forEach(s=>{i.add(s.subscribe(()=>{e.dispatch(t.map(s=>s.value()))}))}):i.forEach(s=>s.unsubscribe())}),e},exports.fromEvent=function(t,e){const i=new s,r=s=>{i.dispatch(s)};return i.subscribeHot(s=>{s?t.addEventListener(e,r,!1):t.removeEventListener(e,r,!1)}),i},exports.merge=function(...t){const e=new s,i=new Set;return e.subscribeHot(s=>{s?t.forEach(s=>{i.add(s.subscribe(s=>{e.dispatch(s)}))}):i.forEach(s=>s.unsubscribe())}),e},exports.tap=function(s){return s(this.val),this}; | ||
//# sourceMappingURL=bundle.common.min.js.map |
@@ -1,2 +0,2 @@ | ||
class s{constructor(){this.subscribers=new Set,this.subscribersHot=new Set,this.dispatched=!1,this.hot=!1}dispatch(s){const t=this.val;this.val=s,this.dispatched=!0,this.subscribers.forEach(i=>i(s,t))}subscribeNext(s){return this.subscribers.add(s),this.notifyHotSubscribers(),{unsubscribe:()=>{this.subscribers.delete(s)}}}subscribe(s){return this.dispatched&&s(this.val),this.subscribeNext(s)}unsubscribe(s){this.subscribers.delete(s),this.notifyHotSubscribers()}notifyHotSubscribers(){this.subscribers.size>0!=this.hot&&(this.hot=this.subscribers.size>0,this.subscribersHot.forEach(s=>s(this.hot)))}subscribeHot(s){return this.subscribersHot.add(s),{unsubscribe:()=>{this.subscribersHot.delete(s)}}}clear(){this.subscribers.clear(),this.subscribersHot.clear()}value(){return this.val}promise(){return new Promise(s=>{const t=this.subscribe((function(i){t.unsubscribe(),s(i)}))})}map(t){const i=new s,e=s=>{i.dispatch(t(s))};return i.subscribeHot(s=>{s?this.subscribe(e):this.unsubscribe(e)}),i}filter(t){const i=new s,e=s=>{t(s)&&i.dispatch(s)};return i.subscribeHot(s=>{s?this.subscribe(e):this.unsubscribe(e)}),i}}function t(s){return s(this.val),this}function i(t,i){const e=new s,r=s=>{e.dispatch(s)};return e.subscribeHot(s=>{s?t.addEventListener(i,r,!1):t.removeEventListener(i,r,!1)}),e}function e(...t){const i=new s,e=new Set;return i.subscribeHot(s=>{s?t.forEach(s=>{e.add(s.subscribe(()=>{i.dispatch(t.map(s=>s.value()))}))}):e.forEach(s=>s.unsubscribe())}),i}export{s as Cuprum,e as combine,i as fromEvent,t as tap}; | ||
class s{constructor(){this.subscribers=new Set,this.subscribersHot=new Set,this.dispatched=!1,this.hot=!1,this.isSubject=!1}dispatch(s){if(this.isSubject)throw"Can't dispatch on subject";this.internalDispatch(s)}subscribe(s){return this.dispatched&&s(this.val),this.subscribeNext(s)}unsubscribe(s){this.subscribers.delete(s),this.notifyHotSubscribers()}subscribeHot(s){return this.subscribersHot.add(s),{unsubscribe:()=>{this.subscribersHot.delete(s)}}}clear(){this.subscribers.clear(),this.subscribersHot.clear()}value(){return this.val}observable(){const s=this.map(s=>s);return s.isSubject=!0,s}promise(){return new Promise(s=>{const t=this.subscribe((function(i){t.unsubscribe(),s(i)}))})}map(t){const i=new s,e=s=>{i.internalDispatch(t(s))};return i.subscribeHot(s=>{s?this.subscribe(e):this.unsubscribe(e)}),i}filter(t){const i=new s,e=s=>{t(s)&&i.dispatch(s)};return i.subscribeHot(s=>{s?this.subscribe(e):this.unsubscribe(e)}),i}notifyHotSubscribers(){this.subscribers.size>0!=this.hot&&(this.hot=this.subscribers.size>0,this.subscribersHot.forEach(s=>s(this.hot)))}subscribeNext(s){return this.subscribers.add(s),this.notifyHotSubscribers(),{unsubscribe:()=>{this.subscribers.delete(s)}}}internalDispatch(s){const t=this.val;this.val=s,this.dispatched=!0,this.subscribers.forEach(i=>i(s,t))}}function t(s){return s(this.val),this}function i(t,i){const e=new s,r=s=>{e.dispatch(s)};return e.subscribeHot(s=>{s?t.addEventListener(i,r,!1):t.removeEventListener(i,r,!1)}),e}function e(...t){const i=new s,e=new Set;return i.subscribeHot(s=>{s?t.forEach(s=>{e.add(s.subscribe(()=>{i.dispatch(t.map(s=>s.value()))}))}):e.forEach(s=>s.unsubscribe())}),i}function r(...t){const i=new s,e=new Set;return i.subscribeHot(s=>{s?t.forEach(s=>{e.add(s.subscribe(s=>{i.dispatch(s)}))}):e.forEach(s=>s.unsubscribe())}),i}export{s as Cuprum,e as combine,i as fromEvent,r as merge,t as tap}; | ||
//# sourceMappingURL=bundle.min.js.map |
@@ -7,7 +7,6 @@ export declare class Cuprum<T> { | ||
private hot; | ||
private isSubject; | ||
dispatch(value: T): void; | ||
subscribeNext(fn: (value: T) => void): Subscription; | ||
subscribe(fn: (value: T, oldValue?: T) => void): Subscription; | ||
unsubscribe(fn: (value: T, oldValue?: T) => void): void; | ||
private notifyHotSubscribers; | ||
subscribeHot(fn: (value: boolean) => void): { | ||
@@ -18,5 +17,9 @@ unsubscribe: () => void; | ||
value(): T; | ||
observable(): Observable<T>; | ||
promise(): Promise<T>; | ||
map<U>(fn: (val: T) => U): Cuprum<U>; | ||
filter(fn: (val: T) => boolean): Cuprum<T>; | ||
private notifyHotSubscribers; | ||
private subscribeNext; | ||
private internalDispatch; | ||
} | ||
@@ -32,2 +35,10 @@ export declare function tap(fn: (val: any) => any): any; | ||
export declare function combine<T, U, V, W, X, Y, Z>(obs1$: Cuprum<T>, obs2$: Cuprum<U>, obs3$: Cuprum<V>, obs4$: Cuprum<W>, obs5$: Cuprum<X>, obs6$: Cuprum<Y>, obs7$: Cuprum<Z>): Cuprum<[T, U, V, W, X, Y, Z]>; | ||
export declare function merge<T>(obs1$: Cuprum<T>): Cuprum<[T]>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>): Cuprum<T>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>, obs3$: Cuprum<T>): Cuprum<T>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>, obs3$: Cuprum<T>, obs4$: Cuprum<T>): Cuprum<T>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>, obs3$: Cuprum<T>, obs4$: Cuprum<T>, obs5$: Cuprum<T>): Cuprum<T>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>, obs3$: Cuprum<T>, obs4$: Cuprum<T>, obs5$: Cuprum<T>, obs6$: Cuprum<T>): Cuprum<T>; | ||
export declare function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>, obs3$: Cuprum<T>, obs4$: Cuprum<T>, obs5$: Cuprum<T>, obs6$: Cuprum<T>, obs7$: Cuprum<T>): Cuprum<T>; | ||
declare type Observable<T> = Omit<Cuprum<T>, "dispatch">; | ||
interface Subscription { | ||
@@ -34,0 +45,0 @@ unsubscribe: () => void; |
{ | ||
"name": "cuprum", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Tiny state management library", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.common.min.js", |
@@ -7,20 +7,11 @@ export class Cuprum<T> { | ||
private hot = false; | ||
private isSubject = false; | ||
dispatch(value: T) { | ||
const oldValue = this.val; | ||
this.val = value; | ||
this.dispatched = true; | ||
this.subscribers.forEach((fn) => fn(value, oldValue)); | ||
if (this.isSubject) { | ||
throw "Can't dispatch on subject"; | ||
} | ||
this.internalDispatch(value); | ||
} | ||
subscribeNext(fn: (value: T) => void): Subscription { | ||
this.subscribers.add(fn); | ||
this.notifyHotSubscribers(); | ||
return { | ||
unsubscribe: () => { | ||
this.subscribers.delete(fn); | ||
}, | ||
}; | ||
} | ||
subscribe(fn: (value: T, oldValue?: T) => void) { | ||
@@ -38,9 +29,2 @@ if (this.dispatched) { | ||
private notifyHotSubscribers() { | ||
if (this.subscribers.size > 0 != this.hot) { | ||
this.hot = this.subscribers.size > 0; | ||
this.subscribersHot.forEach((fn) => fn(this.hot)); | ||
} | ||
} | ||
subscribeHot(fn: (value: boolean) => void) { | ||
@@ -64,2 +48,8 @@ this.subscribersHot.add(fn); | ||
observable(): Observable<T> { | ||
const observable = this.map((value) => value); | ||
observable.isSubject = true; | ||
return observable; | ||
} | ||
promise() { | ||
@@ -74,2 +64,4 @@ return new Promise<T>((resolve) => { | ||
// TODO: pipe | ||
// TODO: async map | ||
@@ -80,3 +72,3 @@ | ||
const dispatch = (value) => { | ||
event$.dispatch(fn(value)); | ||
event$.internalDispatch(fn(value)); | ||
}; | ||
@@ -109,2 +101,26 @@ event$.subscribeHot((hot) => { | ||
} | ||
private notifyHotSubscribers() { | ||
if (this.subscribers.size > 0 != this.hot) { | ||
this.hot = this.subscribers.size > 0; | ||
this.subscribersHot.forEach((fn) => fn(this.hot)); | ||
} | ||
} | ||
private subscribeNext(fn: (value: T) => void): Subscription { | ||
this.subscribers.add(fn); | ||
this.notifyHotSubscribers(); | ||
return { | ||
unsubscribe: () => { | ||
this.subscribers.delete(fn); | ||
}, | ||
}; | ||
} | ||
private internalDispatch(value: T) { | ||
const oldValue = this.val; | ||
this.val = value; | ||
this.dispatched = true; | ||
this.subscribers.forEach((fn) => fn(value, oldValue)); | ||
} | ||
} | ||
@@ -173,3 +189,3 @@ | ||
export function combine(...cuprumList: Cuprum<any>[]) { | ||
export function combine(...cuprumList: Cuprum<unknown>[]) { | ||
const obs$ = new Cuprum(); | ||
@@ -195,4 +211,64 @@ const subs = new Set<Subscription>(); | ||
export function merge<T>(obs1$: Cuprum<T>): Cuprum<[T]>; | ||
export function merge<T>(obs1$: Cuprum<T>, obs2$: Cuprum<T>): Cuprum<T>; | ||
export function merge<T>( | ||
obs1$: Cuprum<T>, | ||
obs2$: Cuprum<T>, | ||
obs3$: Cuprum<T> | ||
): Cuprum<T>; | ||
export function merge<T>( | ||
obs1$: Cuprum<T>, | ||
obs2$: Cuprum<T>, | ||
obs3$: Cuprum<T>, | ||
obs4$: Cuprum<T> | ||
): Cuprum<T>; | ||
export function merge<T>( | ||
obs1$: Cuprum<T>, | ||
obs2$: Cuprum<T>, | ||
obs3$: Cuprum<T>, | ||
obs4$: Cuprum<T>, | ||
obs5$: Cuprum<T> | ||
): Cuprum<T>; | ||
export function merge<T>( | ||
obs1$: Cuprum<T>, | ||
obs2$: Cuprum<T>, | ||
obs3$: Cuprum<T>, | ||
obs4$: Cuprum<T>, | ||
obs5$: Cuprum<T>, | ||
obs6$: Cuprum<T> | ||
): Cuprum<T>; | ||
export function merge<T>( | ||
obs1$: Cuprum<T>, | ||
obs2$: Cuprum<T>, | ||
obs3$: Cuprum<T>, | ||
obs4$: Cuprum<T>, | ||
obs5$: Cuprum<T>, | ||
obs6$: Cuprum<T>, | ||
obs7$: Cuprum<T> | ||
): Cuprum<T>; | ||
export function merge(...cuprumList: Cuprum<unknown>[]) { | ||
const obs$ = new Cuprum(); | ||
const subs = new Set<Subscription>(); | ||
obs$.subscribeHot((hot) => { | ||
if (hot) { | ||
cuprumList.forEach((obs) => { | ||
subs.add( | ||
obs.subscribe((value) => { | ||
obs$.dispatch(value); | ||
}) | ||
); | ||
}); | ||
} else { | ||
subs.forEach((sub) => sub.unsubscribe()); | ||
} | ||
}); | ||
return obs$; | ||
} | ||
type Observable<T> = Omit<Cuprum<T>, "dispatch">; | ||
interface Subscription { | ||
unsubscribe: () => void; | ||
} |
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
32826
302