Type mqtt.js a bit

This commit is contained in:
uwap 2018-01-20 14:15:16 +01:00
parent fda6a95359
commit a677da4ed5
3 changed files with 67 additions and 8 deletions

View file

@ -5,8 +5,12 @@ src/
[libs] [libs]
types/types.js types/types.js
types/mqtt.js
[options] [options]
esproposal.export_star_as=enable esproposal.export_star_as=enable
module.system.node.resolve_dirname=node_modules module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=src module.system.node.resolve_dirname=src
[lints]
all=warn

View file

@ -5,15 +5,15 @@ import _ from "lodash";
// TODO: type mqtt.js // TODO: type mqtt.js
export type MqttSettings = { export type MqttSettings = {
onReconnect?: (mqtt: Object) => void, onReconnect?: (mqtt: mqtt.Client) => void,
onDisconnect?: (mqtt: Object) => void, onDisconnect?: (mqtt: mqtt.Client) => void,
onMessage?: (topic: string, message: Object) => void, onMessage?: (topic: string, message: Buffer) => void,
onMessageSent?: (topic: string, message: any) => void, onMessageSent?: (topic: string, message: Buffer) => void,
onConnect?: (mqtt: Object) => void, onConnect?: (mqtt: mqtt.Client) => void,
subscribe?: Array<string> subscribe?: Array<string>
} }
export type MessageCallback = (topic: string, message: any) => void; export type MessageCallback = (topic: string, message: Buffer) => void;
export default function connectMqtt( export default function connectMqtt(
url: string, url: string,
@ -28,7 +28,7 @@ export default function connectMqtt(
settings.onConnect(client); settings.onConnect(client);
} }
}); });
client.on("message", (topic, message) => { client.on("message", (topic: string, message: Buffer) => {
if (settings.onMessage != null) { if (settings.onMessage != null) {
settings.onMessage(topic, message); settings.onMessage(topic, message);
} }
@ -48,7 +48,7 @@ export default function connectMqtt(
settings.onReconnect(client); settings.onReconnect(client);
} }
}); });
return (topic: string, message: any) => { return (topic: string, message: Buffer) => {
client.publish(topic, message, null, (error) => { client.publish(topic, message, null, (error) => {
if (error == null && settings.onMessageSent != null) { if (error == null && settings.onMessageSent != null) {
settings.onMessageSent(topic, message); settings.onMessageSent(topic, message);

55
types/mqtt.js Normal file
View file

@ -0,0 +1,55 @@
declare module 'mqtt' {
declare type QoS = 0 | 1 | 2;
declare type ClientOptions = {
wsOptions?: any, // TODO: websocket options
keepalive?: number,
reschedulePings?: boolean,
clientId?: string,
protocolId?: 'MQTT' | 'MQIsdp',
protocolVersion?: 3 | 4,
clean?: boolean,
reconnectPeriod?: number,
connectTimeout?: number,
username?: string,
password?: string | Buffer,
incomingStore?: any, // TODO: redux store
outgoingStore?: any, // TODO: store
queueQoSZero?: boolean,
will?: {
topic?: string,
payload?: Buffer,
qos?: QoS,
retain?: boolean
},
transformWsUrl?: any, // TODO: function
resubscribe?: boolean
};
declare type PublishOptions = {
qos?: QoS,
retain?: boolean,
dup?: boolean
};
declare type ClientSubscribeOptions = {
qos: QoS
};
declare class Client {
constructor(streamBuilder: any, options?: ClientOptions): Client; // TODO: StreamBuilder
publish(topic: string, message: Buffer | string, options: ?PublishOptions, callback: any): void; // TODO: message: Buffer|string, callback..., topic array and topic object
subscribe(topic: string | Array<string>, cb: (error: Error, granted: any) => void): void; // TODO: granted
subscribe(topic: string | Array<string>, opts: ClientSubscribeOptions, cb: (error: Error, granted: any) => void): void; // TODO: granted
on(event: "message", // TODO: packet
cb: (topic: string, payload: Buffer, packet: any) => void): void;
on(event: "offline", cb: () => void): void;
on(event: "reconnect", cb: () => void): void;
on(event: "close", cb: () => void): void;
}
declare function connect(url: string, options?: ClientOptions): Client;
}