Comparing version 3.4.0 to 4.0.0
@@ -8,31 +8,24 @@ import * as erreur from 'erreur'; | ||
declare type UnsubscribeAllMethod = () => void; | ||
interface SubscribeMethod<T> { | ||
(callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed): Unsubscribe; | ||
(subId: string, callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed): Unsubscribe; | ||
} | ||
interface VoidSubscribeMethod { | ||
(callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed): Unsubscribe; | ||
(subId: string, callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed): Unsubscribe; | ||
} | ||
interface IsSubscribedMethod<T> { | ||
(subId: string): boolean; | ||
(callback: SubscriptionCallback<T>): boolean; | ||
} | ||
interface UnsubscribeMethod<T> { | ||
(subId: string): void; | ||
(callback: SubscriptionCallback<T>): void; | ||
} | ||
interface VoidIsSubscribedMethod { | ||
(subId: string): boolean; | ||
(callback: VoidSubscriptionCallback): boolean; | ||
} | ||
interface VoidUnsubscribeMethod { | ||
(subId: string): void; | ||
(callback: VoidSubscriptionCallback): void; | ||
} | ||
declare type SubscribeMethod<T> = (callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type SubscribeByIdMethod<T> = (subId: string, callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type VoidSubscribeMethod = (callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type VoidSubscribeByIdMethod = (subId: string, callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type VoidWatchMethod = (callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type VoidWatchByIdMethod = (subId: string, callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe; | ||
declare type IsSubscribedMethod<T> = (callback: SubscriptionCallback<T>) => boolean; | ||
declare type IsSubscribedByIdMethod = (subId: string) => boolean; | ||
declare type UnsubscribeMethod<T> = (callback: SubscriptionCallback<T>) => void; | ||
declare type UnsubscribeByIdMethod = (subId: string) => void; | ||
declare type VoidIsSubscribedMethod = (callback: VoidSubscriptionCallback) => boolean; | ||
declare type VoidIsSubscribedByIdMethod = (subId: string) => boolean; | ||
declare type VoidUnsubscribeMethod = (callback: VoidSubscriptionCallback) => void; | ||
declare type VoidUnsubscribeByIdMethod = (subId: string) => void; | ||
interface ISubscription<T> { | ||
subscribe: SubscribeMethod<T>; | ||
subscribeById: SubscribeByIdMethod<T>; | ||
unsubscribe: UnsubscribeMethod<T>; | ||
unsubscribeById: UnsubscribeByIdMethod; | ||
isSubscribed: IsSubscribedMethod<T>; | ||
isSubscribedById: IsSubscribedByIdMethod; | ||
unsubscribeAll: UnsubscribeAllMethod; | ||
isSubscribed: IsSubscribedMethod<T>; | ||
size: () => number; | ||
@@ -45,5 +38,8 @@ emit: (newValue: T) => void; | ||
subscribe: VoidSubscribeMethod; | ||
subscribeById: VoidSubscribeByIdMethod; | ||
unsubscribe: VoidUnsubscribeMethod; | ||
unsubscribeById: VoidUnsubscribeByIdMethod; | ||
isSubscribed: VoidIsSubscribedMethod; | ||
isSubscribedById: VoidIsSubscribedByIdMethod; | ||
unsubscribeAll: UnsubscribeAllMethod; | ||
isSubscribed: VoidIsSubscribedMethod; | ||
size: () => number; | ||
@@ -63,3 +59,3 @@ emit: () => void; | ||
maxSubscriptionCount?: number; | ||
maxRecursiveCall?: number; | ||
maxRecursiveEmit?: number; | ||
} | ||
@@ -74,4 +70,4 @@ /** | ||
declare type Subscription<T> = ISubscription<T>; | ||
declare const Subscription: (<T = void>(options?: ISubscriptionOptions) => [T] extends [void] ? IVoidSubscription : ISubscription<T>) & { | ||
create: <T_1>(options?: ISubscriptionOptions) => ISubscription<T_1>; | ||
declare const Subscription: { | ||
create: <T>(options?: ISubscriptionOptions) => ISubscription<T>; | ||
createVoid: (options?: ISubscriptionOptions) => IVoidSubscription; | ||
@@ -82,6 +78,8 @@ }; | ||
MaxSubscriptionCountReached: erreur.ErreurDeclaration<null, []>; | ||
MaxRecursiveCallReached: erreur.ErreurDeclaration<null, []>; | ||
maxRecursiveEmitReached: erreur.ErreurDeclaration<{ | ||
limit: number; | ||
}, [limit: number]>; | ||
InvalidCallback: erreur.ErreurDeclaration<null, []>; | ||
}; | ||
export { ISubscription, ISubscriptionOptions, IVoidSubscription, IsSubscribedMethod, OnUnsubscribed, SubscribeMethod, Subscription, SubscriptionCallback, SubscriptionOptions, SuubErreur, Unsubscribe, UnsubscribeAllMethod, UnsubscribeMethod, VoidIsSubscribedMethod, VoidSubscribeMethod, VoidSubscription, VoidSubscriptionCallback, VoidUnsubscribeMethod }; | ||
export { ISubscription, ISubscriptionOptions, IVoidSubscription, IsSubscribedByIdMethod, IsSubscribedMethod, OnUnsubscribed, SubscribeByIdMethod, SubscribeMethod, Subscription, SubscriptionCallback, SubscriptionOptions, SuubErreur, Unsubscribe, UnsubscribeAllMethod, UnsubscribeByIdMethod, UnsubscribeMethod, VoidIsSubscribedByIdMethod, VoidIsSubscribedMethod, VoidSubscribeByIdMethod, VoidSubscribeMethod, VoidSubscription, VoidSubscriptionCallback, VoidUnsubscribeByIdMethod, VoidUnsubscribeMethod, VoidWatchByIdMethod, VoidWatchMethod }; |
@@ -28,6 +28,3 @@ var __defProp = Object.defineProperty; | ||
var Subscription = (() => { | ||
return Object.assign(legacyCreate, { create, createVoid }); | ||
function legacyCreate(options = {}) { | ||
return create(options); | ||
} | ||
return { create, createVoid }; | ||
function createVoid(options = {}) { | ||
@@ -37,9 +34,3 @@ return create(options); | ||
function create(options = {}) { | ||
const { | ||
onFirstSubscription, | ||
onLastUnsubscribe, | ||
onDestroy, | ||
maxRecursiveCall = 1e3, | ||
maxSubscriptionCount = 1e4 | ||
} = options; | ||
const { onFirstSubscription, onLastUnsubscribe, onDestroy, maxRecursiveEmit = 1e3, maxSubscriptionCount = 1e4 } = options; | ||
const subscriptions = []; | ||
@@ -52,5 +43,8 @@ let nextSubscriptions = []; | ||
subscribe, | ||
subscribeById, | ||
unsubscribe, | ||
unsubscribeById, | ||
isSubscribed, | ||
isSubscribedById, | ||
unsubscribeAll, | ||
isSubscribed, | ||
emit, | ||
@@ -84,3 +78,3 @@ size, | ||
isEmitting = true; | ||
let emitQueueSafe = maxRecursiveCall + 1; | ||
let emitQueueSafe = maxRecursiveEmit + 1; | ||
while (emitQueueSafe > 0 && emitQueue.length > 0) { | ||
@@ -103,13 +97,16 @@ emitQueueSafe--; | ||
isEmitting = false; | ||
throw SuubErreur.MaxRecursiveCallReached.create(); | ||
throw SuubErreur.maxRecursiveEmitReached.create(maxRecursiveEmit); | ||
} | ||
isEmitting = false; | ||
} | ||
function subscribe(arg1, arg2, arg3) { | ||
function subscribe(callback, onUnsubscribe) { | ||
return subscribeInternal(callback, null, onUnsubscribe); | ||
} | ||
function subscribeById(subId, callback, onUnsubscribe) { | ||
return subscribeInternal(callback, subId, onUnsubscribe); | ||
} | ||
function subscribeInternal(callback, subId, onUnsubscribe) { | ||
if (destroyed) { | ||
throw SuubErreur.SubscriptionDestroyed.create(); | ||
} | ||
const subId = typeof arg1 === "string" ? arg1 : null; | ||
const callback = typeof arg1 === "string" ? arg2 : arg1; | ||
const onUnsubscribe = typeof arg1 === "string" ? arg3 : arg2; | ||
if (typeof callback !== "function") { | ||
@@ -173,4 +170,9 @@ throw SuubErreur.InvalidCallback.create(); | ||
} | ||
function unsubscribe(arg1) { | ||
const [subId, callback] = typeof arg1 === "string" ? [arg1, void 0] : [null, arg1]; | ||
function unsubscribe(callback) { | ||
unsubscribeInternal(null, callback); | ||
} | ||
function unsubscribeById(subId) { | ||
unsubscribeInternal(subId); | ||
} | ||
function unsubscribeInternal(subId, callback) { | ||
const subscription = findSubscription(subId, callback); | ||
@@ -181,4 +183,9 @@ if (subscription) { | ||
} | ||
function isSubscribed(arg1) { | ||
const [subId, callback] = typeof arg1 === "string" ? [arg1, void 0] : [null, arg1]; | ||
function isSubscribed(callback) { | ||
return isSubscribedInternal(null, callback); | ||
} | ||
function isSubscribedById(subId) { | ||
return isSubscribedInternal(subId); | ||
} | ||
function isSubscribedInternal(subId, callback) { | ||
const subscription = findSubscription(subId, callback); | ||
@@ -195,3 +202,3 @@ return subscription !== void 0; | ||
MaxSubscriptionCountReached: import_erreur.Erreur.declare("MaxSubscriptionCountReached", () => `The maxSubscriptionCount has been reached. If this is expected you can use the maxSubscriptionCount option to raise the limit`).withTransform(() => null), | ||
MaxRecursiveCallReached: import_erreur.Erreur.declare("MaxRecursiveCallReached", () => `The maxRecursiveCall has been reached, did you emit() in a callback ? If this is expected you can use the maxRecursiveCall option to raise the limit`).withTransform(() => null), | ||
maxRecursiveEmitReached: import_erreur.Erreur.declare("maxRecursiveEmitReached", ({ limit }) => `The maxRecursiveEmit limit (${limit}) has been reached, did you emit() in a callback ? If this is expected you can use the maxRecursiveEmit option to raise the limit`).withTransform((limit) => ({ limit })), | ||
InvalidCallback: import_erreur.Erreur.declare("InvalidCallback", () => `The callback is not a function`).withTransform(() => null) | ||
@@ -198,0 +205,0 @@ }; |
{ | ||
"name": "suub", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "A simple pub/sub written in Typescript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -72,10 +72,10 @@ <p align="center"> | ||
```ts | ||
subscription.subscribe('mySubId', () => { | ||
subscription.subscribeById('mySubId', () => { | ||
/*...*/ | ||
}); | ||
// later | ||
subscription.unsubscribe('mySubId'); | ||
subscription.unsubscribeById('mySubId'); | ||
``` | ||
In both case the `subscribe` return a function that will unsubscribe: | ||
In both case the `subscribe[ById]` return a function that will unsubscribe: | ||
@@ -94,3 +94,3 @@ ```ts | ||
subscription.emit(42); | ||
// you can also emit with no value | ||
// for void subscription you don't need to pass any value | ||
subscription.emit(); | ||
@@ -101,3 +101,3 @@ ``` | ||
The `subscribe` method accept a optional function after the callback, this function will be called when this callback you are subscribing is unsubscribed. | ||
The `subscribe[ById]` methods accept a optional function after the callback, this function will be called when this callback you are subscribing is unsubscribed. | ||
@@ -115,3 +115,3 @@ ```ts | ||
// or with a subId | ||
subscription.subscribe( | ||
subscription.subscribeById( | ||
'mySub', | ||
@@ -127,3 +127,3 @@ () => { | ||
### Unsubscribing all callback | ||
### Unsubscribing all subscriptions | ||
@@ -138,3 +138,3 @@ You can call `unsubscribeAll` method on a subscription to remove all callback. This will also trigger the `onUnsubscribe` if any. | ||
The `Subscription.create` (or `Subscription.createVoid`) function accept an option object as parameter (all properties are optional): | ||
The `Subscription.create` (or `Subscription.createVoid`) functions accept an option object as parameter (all properties are optional): | ||
@@ -147,3 +147,3 @@ ```ts | ||
maxSubscriptionCount: 10000, | ||
maxRecursiveCall: 1000, | ||
maxRecursiveEmit: 1000, | ||
}); | ||
@@ -168,3 +168,3 @@ ``` | ||
#### `maxRecursiveCall` | ||
#### `maxRecursiveEmit` | ||
@@ -175,7 +175,7 @@ > A number to limit the maximum recursive call of `emit` (defaumt is `1000`). This limit exist to detect infinite loop where you `emit` in a `callback`. | ||
The `isSubscribed` let you test whether or not a callback / subId is currently subscribed | ||
The `isSubscribed[ById]` methods let you test whether or not a callback / subId is currently subscribed | ||
```ts | ||
subscription.isSubscribed(myCallback); // <- boolean | ||
subscription.isSubscribed('my-sub-id'); // <- boolean | ||
subscription.isSubscribedById('my-sub-id'); // <- boolean | ||
``` | ||
@@ -199,3 +199,3 @@ | ||
Once destroyed, calling `emit` or `subscribe` will throw an error. You can still call the other methods but they will have no effect. | ||
Once destroyed, calling `emit` or `subscribe[ById]` will throw an error. You can still call the other methods but they will have no effect. | ||
@@ -212,5 +212,6 @@ You can check if a subscription is destroyed by calling the `isDestroyed` method. | ||
#### If you re-subscribe the same callback it will not re-do a subscription but instead move the subscription to the end. | ||
#### If you re-subscribe the same callback or id it will not re-do a subscription but instead move the subscription to the end. | ||
In other words, calling `subscribe` on an already subscribed callback or subId will not make the callback called twice. But it will move the callback at the end of the subscription list. | ||
In the case of a subId, the callback will be replaced by the new one. | ||
@@ -311,4 +312,4 @@ #### If you call `unsubscribe` in a callback it will have effect immediatly. | ||
maxSubscriptionCount?: number; | ||
maxRecursiveCall?: number; | ||
maxRecursiveEmit?: number; | ||
} | ||
``` |
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
28335
458
304