Type mqtt.js a bit
This commit is contained in:
parent
fda6a95359
commit
a677da4ed5
3 changed files with 67 additions and 8 deletions
|
|
@ -5,8 +5,12 @@ src/
|
|||
|
||||
[libs]
|
||||
types/types.js
|
||||
types/mqtt.js
|
||||
|
||||
[options]
|
||||
esproposal.export_star_as=enable
|
||||
module.system.node.resolve_dirname=node_modules
|
||||
module.system.node.resolve_dirname=src
|
||||
|
||||
[lints]
|
||||
all=warn
|
||||
|
|
|
|||
|
|
@ -5,15 +5,15 @@ import _ from "lodash";
|
|||
// TODO: type mqtt.js
|
||||
|
||||
export type MqttSettings = {
|
||||
onReconnect?: (mqtt: Object) => void,
|
||||
onDisconnect?: (mqtt: Object) => void,
|
||||
onMessage?: (topic: string, message: Object) => void,
|
||||
onMessageSent?: (topic: string, message: any) => void,
|
||||
onConnect?: (mqtt: Object) => void,
|
||||
onReconnect?: (mqtt: mqtt.Client) => void,
|
||||
onDisconnect?: (mqtt: mqtt.Client) => void,
|
||||
onMessage?: (topic: string, message: Buffer) => void,
|
||||
onMessageSent?: (topic: string, message: Buffer) => void,
|
||||
onConnect?: (mqtt: mqtt.Client) => void,
|
||||
subscribe?: Array<string>
|
||||
}
|
||||
|
||||
export type MessageCallback = (topic: string, message: any) => void;
|
||||
export type MessageCallback = (topic: string, message: Buffer) => void;
|
||||
|
||||
export default function connectMqtt(
|
||||
url: string,
|
||||
|
|
@ -28,7 +28,7 @@ export default function connectMqtt(
|
|||
settings.onConnect(client);
|
||||
}
|
||||
});
|
||||
client.on("message", (topic, message) => {
|
||||
client.on("message", (topic: string, message: Buffer) => {
|
||||
if (settings.onMessage != null) {
|
||||
settings.onMessage(topic, message);
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ export default function connectMqtt(
|
|||
settings.onReconnect(client);
|
||||
}
|
||||
});
|
||||
return (topic: string, message: any) => {
|
||||
return (topic: string, message: Buffer) => {
|
||||
client.publish(topic, message, null, (error) => {
|
||||
if (error == null && settings.onMessageSent != null) {
|
||||
settings.onMessageSent(topic, message);
|
||||
|
|
|
|||
55
types/mqtt.js
Normal file
55
types/mqtt.js
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue