@typeheim/fire-rx
Advanced tools
Comparing version 0.0.0-beta.18 to 0.0.0-beta.19
@@ -7,3 +7,2 @@ export * from './src/DestroyEvent'; | ||
export * from './src/ReactivePromise'; | ||
export * from './src/StatefulStream'; | ||
export * from './src/ValueStream'; | ||
export * from './src/StatefulProducer'; |
@@ -12,4 +12,3 @@ "use strict"; | ||
__export(require("./src/ReactivePromise")); | ||
__export(require("./src/StatefulStream")); | ||
__export(require("./src/ValueStream")); | ||
__export(require("./src/StatefulProducer")); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@typeheim/fire-rx", | ||
"version": "0.0.0-beta.18", | ||
"version": "0.0.0-beta.19", | ||
"description": "Firebase authentication library", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "6349c6686dbb291301ac4d97c7b29e5d48ac5b60" | ||
"gitHead": "c086e5155d73a7cb0876714674d741a4a8c61910" | ||
} |
# FireRx | ||
RxJS on steroids. Adds memory safety and garbage collection features to work with subjects and subscriptions. | ||
Adds streams that behave both like subjects behave and promises to support async/await. | ||
Adds subjects that behave both like subjects and promises to support async/await. | ||
## StatefulSubject | ||
StatefulSubject extends ReplaySubject from RxJS and adds memory safety and garbage collection. | ||
StatefulSubject acts as ReplaySubject and Promise so that you can use async/await operators on it as well as regular Subject methods. | ||
Adds memory safety and garbage collection automatically calling unsubscribe on subscriptions. | ||
```typescript | ||
@@ -13,13 +15,2 @@ import { StatefulSubject } from '@typeheim/fire-rx' | ||
subject.next(5) // emits to all subscriptions 5 | ||
subject.stop() // completes subject and unsubscribe all subscriptions | ||
``` | ||
## StatefulStream | ||
StatefulStream extends StatefulSubject and adds Promise interface so that you can use async/await operators on it. | ||
```typescript | ||
import { StatefulStream } from '@typeheim/fire-rx' | ||
let subject = new StatefulStream<number>(1) | ||
subject.next(5) | ||
@@ -34,28 +25,2 @@ await subject // returns 5 | ||
## ValueSubject | ||
ValueSubject extends BehaviorSubject from RxJS and adds memory safety and garbage collection. | ||
```typescript | ||
import { ValueSubject } from '@typeheim/fire-rx' | ||
let subject = new ValueSubject<number>(0) | ||
subject.next(5) // emits to all subscriptions 5 | ||
subject.stop() // completes subject and unsubscribe all subscriptions | ||
``` | ||
## ValueStream | ||
ValueStream extends ValueSubject from RxJS and adds Promise interface so that you can use async/await operators on it. | ||
```typescript | ||
import { ValueStream } from '@typeheim/fire-rx' | ||
let subject = new ValueStream<number>(0) | ||
subject.next(5) | ||
await subject // returns 5 | ||
subject.next(6) | ||
await subject // returns 6 | ||
``` | ||
## ReactivePromise | ||
@@ -84,2 +49,29 @@ ReactivePromise acts as a regular Promise but additionally let you use `subscribe` and `pipe` methods. ReactivePromise, like | ||
## StatefulProducer | ||
StatefulProducer extends ReplaySubject from RxJS and adds memory safety and garbage collection. | ||
```typescript | ||
import { StatefulProducer } from '@typeheim/fire-rx' | ||
let subject = new StatefulProducer<number>(1) | ||
subject.next(5) // emits to all subscriptions 5 | ||
subject.stop() // completes subject and unsubscribe all subscriptions | ||
``` | ||
## ValueSubject | ||
ValueSubject extends BehaviorSubject from RxJS and adds memory safety, garbage collection and Promise interface so that you can use async/await operators on it. | ||
```typescript | ||
import { ValueSubject } from '@typeheim/fire-rx' | ||
let subject = new ValueSubject<number>(0) | ||
subject.next(5) | ||
await subject // returns 5 | ||
subject.next(6) | ||
await subject // returns 6 | ||
subject.stop() // completes subject and unsubscribe all subscriptions | ||
``` | ||
## SubscriptionsHub | ||
@@ -86,0 +78,0 @@ SubscriptionsHub represents a hub of subscriptions that let you massively unsubscribe them at once. It might be useful to trigger |
@@ -1,31 +0,29 @@ | ||
import { ReplaySubject } from 'rxjs'; | ||
import { Subscribable } from './contracts'; | ||
import { SubscriptionsHub } from './SubscriptionsHub'; | ||
export declare class StatefulSubject<T> extends ReplaySubject<T> { | ||
protected _emitsCount: number; | ||
protected hub: SubscriptionsHub; | ||
get emitsCount(): number; | ||
get subscriptionsCount(): number; | ||
next(value?: T): void; | ||
import { StatefulProducer } from './StatefulProducer'; | ||
export declare class StatefulSubject<T> extends StatefulProducer<T> { | ||
protected _internalPromise: Promise<T>; | ||
/** | ||
* @deprecated internal method | ||
* Completes subject and clean up resources | ||
*/ | ||
_subscribe(subscriber: any): import("rxjs").Subscription; | ||
stop(): void; | ||
protected get internalPromise(): Promise<T>; | ||
/** | ||
* Subscribe to a destruction event to complete and unsubscribe as it | ||
* emits | ||
* Attaches callbacks for the resolution and/or rejection of the Promise. | ||
* @param onfulfilled The callback to execute when the Promise is resolved. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
emitUntil(destroyEvent: Subscribable<any>): this; | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>; | ||
/** | ||
* Completes subject and clean up resources | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
stop(): void; | ||
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>; | ||
/** | ||
* Completes subject with error and unsubscribe all subscriptions | ||
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The | ||
* resolved value cannot be modified from the callback. | ||
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
fail(error: any): void; | ||
/** | ||
* @deprecated | ||
*/ | ||
close(): void; | ||
finally(onfinally?: (() => void) | undefined | null): Promise<T>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const rxjs_1 = require("rxjs"); | ||
const SubscriptionsHub_1 = require("./SubscriptionsHub"); | ||
class StatefulSubject extends rxjs_1.ReplaySubject { | ||
constructor() { | ||
super(...arguments); | ||
this._emitsCount = 0; | ||
this.hub = new SubscriptionsHub_1.SubscriptionsHub(); | ||
} | ||
get emitsCount() { | ||
return this._emitsCount; | ||
} | ||
get subscriptionsCount() { | ||
return this.hub.count; | ||
} | ||
next(value) { | ||
this._emitsCount++; | ||
super.next(value); | ||
} | ||
const StatefulProducer_1 = require("./StatefulProducer"); | ||
class StatefulSubject extends StatefulProducer_1.StatefulProducer { | ||
/** | ||
* @deprecated internal method | ||
* Completes subject and clean up resources | ||
*/ | ||
_subscribe(subscriber) { | ||
let sub = super._subscribe(subscriber); | ||
this.hub.add(sub); | ||
return sub; | ||
stop() { | ||
this._internalPromise = null; | ||
super.stop(); | ||
} | ||
/** | ||
* Subscribe to a destruction event to complete and unsubscribe as it | ||
* emits | ||
*/ | ||
emitUntil(destroyEvent) { | ||
destroyEvent.subscribe(() => { | ||
this.stop(); | ||
}); | ||
return this; | ||
get internalPromise() { | ||
if (!this._internalPromise) { | ||
// in order for promise to properly return values from subject, it's required to "resolve" each "next" value | ||
// and to keep behavior consistent, there's a storage variable "lastValue" that will be resolved on subject completion | ||
let lastValue = null; | ||
this._internalPromise = new Promise((resolve, reject) => { | ||
this.subscribe({ | ||
next: (data) => { | ||
lastValue = data; | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
resolve(data); | ||
}, | ||
error: (error) => { | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
reject(error); | ||
}, | ||
complete: () => { | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
resolve(lastValue); | ||
}, | ||
}); | ||
}); | ||
} | ||
return this._internalPromise; | ||
} | ||
/** | ||
* Completes subject and clean up resources | ||
* Attaches callbacks for the resolution and/or rejection of the Promise. | ||
* @param onfulfilled The callback to execute when the Promise is resolved. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
stop() { | ||
if (!this.isStopped) { | ||
this.complete(); | ||
} | ||
this.hub.unsubscribe(); | ||
if (!this.closed) { | ||
this.unsubscribe(); | ||
} | ||
then(onfulfilled, onrejected) { | ||
return this.internalPromise.then(onfulfilled); | ||
} | ||
/** | ||
* Completes subject with error and unsubscribe all subscriptions | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
fail(error) { | ||
this.error(error); | ||
this.hub.unsubscribe(); | ||
if (!this.closed) { | ||
this.unsubscribe(); | ||
} | ||
catch(onrejected) { | ||
return this.internalPromise.catch(onrejected); | ||
} | ||
/** | ||
* @deprecated | ||
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The | ||
* resolved value cannot be modified from the callback. | ||
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
close() { | ||
this.stop(); | ||
finally(onfinally) { | ||
return this.internalPromise.finally(onfinally); | ||
} | ||
@@ -67,0 +66,0 @@ } |
import { BehaviorSubject, Subscribable } from 'rxjs'; | ||
import { SubscriptionsHub } from './SubscriptionsHub'; | ||
export declare class ValueSubject<T> extends BehaviorSubject<T> { | ||
protected _internalPromise: Promise<T>; | ||
protected _emitsCount: number; | ||
@@ -30,2 +31,23 @@ protected hub: SubscriptionsHub; | ||
close(): void; | ||
protected get internalPromise(): Promise<T>; | ||
/** | ||
* Attaches callbacks for the resolution and/or rejection of the Promise. | ||
* @param onfulfilled The callback to execute when the Promise is resolved. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>; | ||
/** | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>; | ||
/** | ||
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The | ||
* resolved value cannot be modified from the callback. | ||
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
finally(onfinally?: (() => void) | undefined | null): Promise<T>; | ||
} |
@@ -50,2 +50,3 @@ "use strict"; | ||
} | ||
this._internalPromise = null; | ||
} | ||
@@ -68,4 +69,61 @@ /** | ||
} | ||
// | ||
// Promise interface | ||
// | ||
get internalPromise() { | ||
if (!this._internalPromise) { | ||
// in order for promise to properly return values from subject, it's required to "resolve" each "next" value | ||
// and to keep behavior consistent, there's a storage variable "lastValue" that will be resolved on subject completion | ||
let lastValue = null; | ||
this._internalPromise = new Promise((resolve, reject) => { | ||
this.subscribe({ | ||
next: (data) => { | ||
lastValue = data; | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
resolve(data); | ||
}, | ||
error: (error) => { | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
reject(error); | ||
}, | ||
complete: () => { | ||
// promise should return only one value and then being destroyed | ||
this._internalPromise = null; | ||
resolve(lastValue); | ||
}, | ||
}); | ||
}); | ||
} | ||
return this._internalPromise; | ||
} | ||
/** | ||
* Attaches callbacks for the resolution and/or rejection of the Promise. | ||
* @param onfulfilled The callback to execute when the Promise is resolved. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
then(onfulfilled, onrejected) { | ||
return this.internalPromise.then(onfulfilled); | ||
} | ||
/** | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
catch(onrejected) { | ||
return this.internalPromise.catch(onrejected); | ||
} | ||
/** | ||
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The | ||
* resolved value cannot be modified from the callback. | ||
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
finally(onfinally) { | ||
return this.internalPromise.finally(onfinally); | ||
} | ||
} | ||
exports.ValueSubject = ValueSubject; | ||
//# sourceMappingURL=ValueSubject.js.map |
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
43598
36
739
121