From af69548da480880f254ee74209f6879ff6839b9c Mon Sep 17 00:00:00 2001 From: Daniel Rose Date: Fri, 12 Apr 2019 14:27:29 +0200 Subject: [PATCH] Callbacks are optional. --- types/sc-broker-cluster/index.d.ts | 14 +++---- .../sc-broker-cluster-tests.ts | 39 +++++++++++++++++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/types/sc-broker-cluster/index.d.ts b/types/sc-broker-cluster/index.d.ts index b63b0afbea..9796797005 100644 --- a/types/sc-broker-cluster/index.d.ts +++ b/types/sc-broker-cluster/index.d.ts @@ -69,9 +69,9 @@ export type mapperFunction = (keyChain: KeyChain, method: string, clientIds: num export class SCExchange extends AbstractDataClient { constructor(privateClientCluster: ClientCluster, publicClientCluster: ClientCluster, ioClusterClient: Client); - send(data: any, mapIndex: number | string | string[] | null, callback?: (err?: Error) => void): void; + send(data: any, mapIndex: number | string | string[] | null, callback?: AsyncResultArrayCallback): void; - publish(channelName: string, data: any, callback: (err?: Error) => void): void; + publish(channelName: string, data: any, callback?: (err?: Error) => void): void; subscribe(channelName: string): SCChannel; unsubscribe(channelName: string): void; @@ -146,12 +146,12 @@ export class Client extends EventEmitter { on(event: "ready", listener: () => void): this; on(event: "message", listener: (packet: MessagePacket) => void): this; - destroy(callback: AsyncResultArrayCallback): void; + destroy(callback?: AsyncResultArrayCallback): void; - subscribe(channel: string, callback: (err?: Error) => void): void; - unsubscribe(channel: string, callback: () => void): void; - unsubscribeAll(callback: () => void): void; - isSubscribed(channel: string, includePending: boolean): boolean; + subscribe(channel: string, callback?: (err?: Error) => void): void; + unsubscribe(channel: string, callback?: () => void): void; + unsubscribeAll(callback?: AsyncResultArrayCallback): void; + isSubscribed(channel: string, includePending?: boolean): boolean; subscribeSocket(socket: SCServerSocket, channel: string, callback?: (err?: Error) => void): void; unsubscribeSocket(socket: SCServerSocket, channel: string, callback?: () => void): void; diff --git a/types/sc-broker-cluster/sc-broker-cluster-tests.ts b/types/sc-broker-cluster/sc-broker-cluster-tests.ts index 0f5c274248..8abac30739 100644 --- a/types/sc-broker-cluster/sc-broker-cluster-tests.ts +++ b/types/sc-broker-cluster/sc-broker-cluster-tests.ts @@ -2,6 +2,8 @@ import { Client } from "sc-broker-cluster"; import { SCServer, SCServerSocket } from "socketcluster-server"; import WebSocket = require("ws"); +// Client tests + const client = new Client({ brokers: [], secretKey: "secretKey", @@ -12,18 +14,47 @@ const client = new Client({ client.on("error", err => {}); client.on("warning", err => {}); -const exchange = client.exchange(); - const scServer = new SCServer(); client.setSCServer(scServer); client.once("ready", () => {}); +client.subscribe("channel"); +client.subscribe("channel", () => {}); +client.subscribe("channel", err => {}); + +client.unsubscribe("channel"); +client.unsubscribe("channel", () => {}); + +client.unsubscribeAll(); +client.unsubscribeAll(() => {}); +client.unsubscribeAll(err => {}); +client.unsubscribeAll((err, results) => {}); + +client.isSubscribed("channel"); +client.isSubscribed("channel", true); + const wsSocket = new WebSocket("address"); const socket = new SCServerSocket("id", scServer, wsSocket); +client.subscribeSocket(socket, "channelName"); client.subscribeSocket(socket, "channelName", err => {}); client.unsubscribeSocket(socket, "channelName"); +client.unsubscribeSocket(socket, "channelName", () => {}); -const data: any = {}; -exchange.publish("channelName", data, err => {}); +client.destroy(); +client.destroy(() => {}); +client.destroy(err => {}); +client.destroy((err, results) => {}); + +// SCExchange tests + +const exchange = client.exchange(); + +exchange.send({}, null); +exchange.send("dummy", 1, () => {}); +exchange.send("dummy", ["1", "2"], err => {}); +exchange.send(123, "*", (err, results) => {}); + +exchange.publish("channelName", {}, err => {}); +exchange.publish("channelName", {});