suub
Version:
A simple pub/sub written in Typescript
90 lines (87 loc) • 4.55 kB
TypeScript
import * as erreur from 'erreur';
type Unsubscribe = () => void;
type OnUnsubscribed = () => void;
type SubscriptionCallback<T> = (value: T) => void;
type VoidSubscriptionCallback = () => void;
type UnsubscribeAllMethod = () => void;
type SubscribeMethod<T> = (callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed) => Unsubscribe;
type SubscribeByIdMethod<T> = (subId: string, callback: SubscriptionCallback<T>, onUnsubscribe?: OnUnsubscribed) => Unsubscribe;
type VoidSubscribeMethod = (callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe;
type VoidSubscribeByIdMethod = (subId: string, callback: VoidSubscriptionCallback, onUnsubscribe?: OnUnsubscribed) => Unsubscribe;
type IsSubscribedMethod<T> = (callback: SubscriptionCallback<T>) => boolean;
type IsSubscribedByIdMethod = (subId: string) => boolean;
type UnsubscribeMethod<T> = (callback: SubscriptionCallback<T>) => void;
type UnsubscribeByIdMethod = (subId: string) => void;
type VoidIsSubscribedMethod = (callback: VoidSubscriptionCallback) => boolean;
type VoidIsSubscribedByIdMethod = (subId: string) => boolean;
type VoidUnsubscribeMethod = (callback: VoidSubscriptionCallback) => void;
type VoidUnsubscribeByIdMethod = (subId: string) => void;
type ChannelMethod<Data, Channel> = <D extends Data>(channel: Channel) => ISubscription<D, Channel>;
type VoidChannelMethod<Channel> = (channel: Channel) => IVoidSubscription<Channel>;
type DeferredMethod = <Result>(callback: () => Result) => Result;
interface ISubscription<Data, Channel = any> {
subscribe: SubscribeMethod<Data>;
subscribeById: SubscribeByIdMethod<Data>;
unsubscribe: UnsubscribeMethod<Data>;
unsubscribeById: UnsubscribeByIdMethod;
isSubscribed: IsSubscribedMethod<Data>;
isSubscribedById: IsSubscribedByIdMethod;
unsubscribeAll: UnsubscribeAllMethod;
size: () => number;
emit: (newValue: Data) => void;
deferred: DeferredMethod;
destroy: () => void;
isDestroyed: () => boolean;
channel: ChannelMethod<Data, Channel>;
}
interface IVoidSubscription<Channel = any> {
subscribe: VoidSubscribeMethod;
subscribeById: VoidSubscribeByIdMethod;
unsubscribe: VoidUnsubscribeMethod;
unsubscribeById: VoidUnsubscribeByIdMethod;
isSubscribed: VoidIsSubscribedMethod;
isSubscribedById: VoidIsSubscribedByIdMethod;
unsubscribeAll: UnsubscribeAllMethod;
size: () => number;
emit: () => void;
deferred: DeferredMethod;
destroy: () => void;
isDestroyed: () => boolean;
channel: VoidChannelMethod<Channel>;
}
type MultiCreateChannelMethod = <Data>() => ISubscription<Data, never>;
type MultiCreateVoidChannelMethod = () => IVoidSubscription<never>;
interface IMultiSubscription {
unsubscribeAll: UnsubscribeAllMethod;
size: () => number;
deferred: DeferredMethod;
destroy: () => void;
isDestroyed: () => boolean;
createChannel: MultiCreateChannelMethod;
createVoidChannel: MultiCreateVoidChannelMethod;
}
interface ISubscriptionOptions {
onFirstSubscription?: () => void;
onLastUnsubscribe?: () => void;
onDestroy?: () => void;
maxSubscriptionCount?: number;
maxRecursiveEmit?: number;
maxUnsubscribeAllLoop?: number;
}
declare const Suub: {
createSubscription: <Data, Channel = any>(options?: ISubscriptionOptions) => ISubscription<Data, Channel>;
createVoidSubscription: <Channel_1 = any>(options?: ISubscriptionOptions) => IVoidSubscription<Channel_1>;
createMultiSubscription: () => IMultiSubscription;
};
declare const SuubErreur: {
SubscriptionDestroyed: erreur.ErreurDeclaration<null, []>;
MaxSubscriptionCountReached: erreur.ErreurDeclaration<null, []>;
MaxRecursiveEmitReached: erreur.ErreurDeclaration<{
limit: number;
}, [limit: number]>;
MaxUnsubscribeAllLoopReached: erreur.ErreurDeclaration<{
limit: number;
}, [limit: number]>;
InvalidCallback: erreur.ErreurDeclaration<null, []>;
};
export { ChannelMethod, DeferredMethod, IMultiSubscription, ISubscription, ISubscriptionOptions, IVoidSubscription, IsSubscribedByIdMethod, IsSubscribedMethod, MultiCreateChannelMethod, MultiCreateVoidChannelMethod, OnUnsubscribed, SubscribeByIdMethod, SubscribeMethod, SubscriptionCallback, Suub, SuubErreur, Unsubscribe, UnsubscribeAllMethod, UnsubscribeByIdMethod, UnsubscribeMethod, VoidChannelMethod, VoidIsSubscribedByIdMethod, VoidIsSubscribedMethod, VoidSubscribeByIdMethod, VoidSubscribeMethod, VoidSubscriptionCallback, VoidUnsubscribeByIdMethod, VoidUnsubscribeMethod };