🤖 Merge PR #43922 [pubsub-js] Add some more public function to export and reformat code by @morpheus-87

This commit is contained in:
Matthias Lindinger 2020-05-01 09:58:11 +02:00 committed by GitHub
parent 56dee133e9
commit eac92dfb05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 28 deletions

View File

@ -1,39 +1,52 @@
// Type definitions for PubSubJS 1.5.2
// Type definitions for PubSubJS 1.8.0
// Project: https://github.com/mroderick/PubSubJS
// Definitions by: Boris Yankov <https://github.com/borisyankov>
// Matthias Lindinger <https://github.com/morpheus-87>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace PubSubJS {
interface Base extends Publish, Subscribe, Unsubscribe, ClearAllSubscriptions {
version: string;
interface Base
extends CountSubscriptions,
ClearAllSubscriptions,
GetSubscriptions,
Publish,
Subscribe,
Unsubscribe {
name: string;
version: string;
}
interface Publish{
publish(message: any, data: any): boolean;
publish(message:any, data:any, sync:boolean, immediateExceptions:Function): boolean;
publishSync(message: any, data: any): boolean;
interface CountSubscriptions {
countSubscriptions(token: any): number;
}
interface Subscribe{
subscribe(message: any, func: Function): any;
interface ClearAllSubscriptions {
clearAllSubscriptions(token?: any): void;
}
interface GetSubscriptions {
getSubscriptions(token: any): any[];
}
interface Unsubscribe{
interface Publish {
publish(message: string, data: any): boolean;
publishSync(message: string, data: any): boolean;
}
interface Subscribe {
subscribe(message: string, func: Function): string;
subscribeOnce(message: string, func: Function): any;
}
interface Unsubscribe {
unsubscribe(tokenOrFunction: any): any;
}
interface ClearAllSubscriptions{
clearAllSubscriptions(): any;
}
}
declare var PubSub: PubSubJS.Base;
declare module "pubsub-js" {
export = PubSub;
declare module 'pubsub-js' {
export = PubSub;
}

View File

@ -1,9 +1,9 @@
/// Tests are taken from the PubSubJs Basic Examples https://github.com/mroderick/PubSubJS
function test_Subscribe() {
// create a function to receive messages
var mySubscriber = (msg: string, data: any) => { console.log(msg, data); }
var mySubscriber = (msg: string, data: any) => {
console.log(msg, data);
};
// add the function to the list of subscribers for a particular message
// we're keeping the returned token, in order to be able to unsubscribe
@ -22,7 +22,9 @@ function test_Subscribe() {
function test_unsubscribe_by_token() {
// create a function to receive messages
var mySubscriber = (msg: string, data: any) => { console.log(msg, data); }
var mySubscriber = (msg: string, data: any) => {
console.log(msg, data);
};
// add the function to the list of subscribers to a particular message
// we're keeping the returned token, in order to be able to unsubscribe
@ -35,7 +37,9 @@ function test_unsubscribe_by_token() {
function test_unsubcribe_by_function() {
// create a function to receive the message
var mySubscriber = (msg: string, data: any) => { console.log(msg, data); }
var mySubscriber = (msg: string, data: any) => {
console.log(msg, data);
};
// add the function to the list of subscribers to a particular message
// we're keeping the returned token, in order to be able to unsubscribe
@ -48,13 +52,17 @@ function test_unsubcribe_by_function() {
function test_Hierarchical_addressing() {
// create a subscriber to receive all messages from a hierarchy of topics
var myToplevelSubscriber = (msg: string, data: any) => { console.log('top level: ', msg, data); }
var myToplevelSubscriber = (msg: string, data: any) => {
console.log('top level: ', msg, data);
};
// subscribe to all topics in the 'car' hierarchy
PubSub.subscribe('car', myToplevelSubscriber);
// create a subscriber to receive only leaf message from hierarchy op topics
var mySpecificSubscriber = (msg: string, data: any) => { console.log('specific: ', msg, data); }
var mySpecificSubscriber = (msg: string, data: any) => {
console.log('specific: ', msg, data);
};
// subscribe only to 'car.drive' topics
PubSub.subscribe('car.drive', mySpecificSubscriber);
@ -72,7 +80,9 @@ function test_Hierarchical_addressing() {
function ClearAllSubscriptions() {
// create a function to receive messages
var mySubscriber = (msg: string, data: any) => { console.log(msg, data); }
var mySubscriber = (msg: string, data: any) => {
console.log(msg, data);
};
// create two subscriptions
PubSub.subscribe('topic1', mySubscriber);
@ -80,4 +90,4 @@ function ClearAllSubscriptions() {
// unsubscribe from all subscrpitions
PubSub.clearAllSubscriptions();
}
}