From adc76cccba3d813786ce59ecee9d56bbbfe5f4ec Mon Sep 17 00:00:00 2001 From: Marin Karamihalev <marin.karamihalev@iopsys.eu> Date: Thu, 11 Feb 2021 15:11:41 +0100 Subject: [PATCH] main: updated connect type info --- package.json | 2 +- .../_types_.subscriptionoptions.html | 4 ++-- .../modules/_commands_recipes_subscribe_.html | 4 ++-- src/index.ts | 22 +++++++++---------- src/types.ts | 21 ++++++++++-------- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index f9af8e8..ad70827 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "usp-js", - "version": "0.1.3", + "version": "0.1.4", "description": "Helper library for easy usp communication using mqtt over tcp or ws.", "main": "build/src/index.js", "scripts": { diff --git a/public/interfaces/_types_.subscriptionoptions.html b/public/interfaces/_types_.subscriptionoptions.html index 48e0c6d..ee4efa6 100644 --- a/public/interfaces/_types_.subscriptionoptions.html +++ b/public/interfaces/_types_.subscriptionoptions.html @@ -96,8 +96,8 @@ <h2>Properties</h2> <section class="tsd-panel tsd-member tsd-kind-property tsd-parent-kind-interface"> <a name="id" class="tsd-anchor"></a> - <h3>id</h3> - <div class="tsd-signature tsd-kind-icon">id<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div> + <h3><span class="tsd-flag ts-flagOptional">Optional</span> id</h3> + <div class="tsd-signature tsd-kind-icon">id<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">string</span></div> <aside class="tsd-sources"> <ul> <li>Defined in types.ts:309</li> diff --git a/public/modules/_commands_recipes_subscribe_.html b/public/modules/_commands_recipes_subscribe_.html index f6e7754..f2d10fc 100644 --- a/public/modules/_commands_recipes_subscribe_.html +++ b/public/modules/_commands_recipes_subscribe_.html @@ -93,7 +93,7 @@ <div class="tsd-signature tsd-kind-icon">subscription<wbr>Path<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">"Device.LocalAgent.Subscription."</span><span class="tsd-signature-symbol"> = "Device.LocalAgent.Subscription."</span></div> <aside class="tsd-sources"> <ul> - <li>Defined in commands/recipes/subscribe.ts:3</li> + <li>Defined in commands/recipes/subscribe.ts:4</li> </ul> </aside> </section> @@ -110,7 +110,7 @@ <li class="tsd-description"> <aside class="tsd-sources"> <ul> - <li>Defined in commands/recipes/subscribe.ts:5</li> + <li>Defined in commands/recipes/subscribe.ts:6</li> </ul> </aside> <h4 class="tsd-parameters-title">Parameters</h4> diff --git a/src/index.ts b/src/index.ts index 43903c0..a6c4149 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,11 +2,10 @@ import mqttAsync from "async-mqtt"; import { decode, makeEncode, makeRecipes, readMsg } from "./commands"; import { ConnectionOptions, - USP, CallFn, - ConnectionEvents, URLConnectionOptions, OnFn, + Connect, } from "./types"; import { makeCallbackRouter, makeRouter } from "./util"; const defaultPublishEndpoint = "/usp/endpoint"; @@ -32,10 +31,7 @@ const _connect = (opts: ConnectionOptions) => { * @param events - Optional event handlers * @returns A set of functions for interacting with the device */ -export default async function connect( - options: ConnectionOptions, - events?: ConnectionEvents -): Promise<USP> { +const connect: Connect = async (options, events) => { const subscribeEndpoint = options.subscribeEndpoint || defaultSubscribeEndpoint; const publishEndpoint = options.publishEndpoint || defaultPublishEndpoint; @@ -60,16 +56,16 @@ export default async function connect( if (err) call.reject(err); else call.resolve(message); } - + const cbs = callbackRouter.get(id); - cbs.forEach((cb) => { - if (message) cb(message, parsedMsg) - else if(err) cb(err, parsedMsg) - }) + cbs.forEach((cb) => { + if (message) cb(message, parsedMsg); + else if (err) cb(err, parsedMsg); + }); }); client.on("error", (err) => { - callbackRouter.get("error").forEach(cb => cb(err)) + callbackRouter.get("error").forEach((cb) => cb(err)); handleError(JSON.stringify(err, null, 2)); }); @@ -103,3 +99,5 @@ export default async function connect( disconnect: () => client.end(), }; } + +export default connect; \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 04ed5ae..ae019a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -297,6 +297,11 @@ export interface USP { disconnect: () => Promise<void>; } +export type Connect = ( + options: ConnectionOptions, + events?: ConnectionEvents +) => Promise<USP>; + type NotifType = | "Event" | "ValueChange" @@ -311,7 +316,10 @@ export interface SubscriptionOptions { reference: string | string[]; } -export type SubscriptionCallback = (msg: Response, fullMsg?: Record<string, any>) => void; +export type SubscriptionCallback = ( + msg: Response, + fullMsg?: Record<string, any> +) => void; export interface OperateOptions { ID?: string; @@ -363,10 +371,8 @@ export interface OtherConnectionOptions { export type ConnectionOptions = URLConnectionOptions | HostConnectionOptions; export type Response = string | Record<string, any>; -export type DecodeFn = ( - msg: Record<string, any> -) => DecodeResponse | [any]; -export type DecodeResponse = [any, ResponseID | null, null | Response] | [any] +export type DecodeFn = (msg: Record<string, any>) => DecodeResponse | [any]; +export type DecodeResponse = [any, ResponseID | null, null | Response] | [any]; export type EncodeArgs = { rootMsg: protobuf.Root; rootRecord: protobuf.Root; @@ -380,10 +386,7 @@ export type OnIdent = string | RegExp; export type EncodeFn = (args: Record<string, any>) => PbRequestMessage; export type CallArgs = Record<string, any>; export type ClearFn = () => void; -export type OnFn = ( - ident: OnIdent, - callback: SubscriptionCallback -) => ClearFn; +export type OnFn = (ident: OnIdent, callback: SubscriptionCallback) => ClearFn; export type MakeFn = (call: CallFn, on: OnFn) => Command; export type MakeRecipeFn = (call: CallFn) => Recipe; export type CommandTrigger = { -- GitLab