From 041d920447f8e5fc9dbfd17962b7118c312b766e Mon Sep 17 00:00:00 2001 From: spcfran Date: Mon, 11 Mar 2019 15:30:46 +0000 Subject: [PATCH] More Connection properties. Transport info & factories. Bump version --- types/autobahn/autobahn-tests.ts | 27 ++++++++++++++++++ types/autobahn/index.d.ts | 48 ++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/types/autobahn/autobahn-tests.ts b/types/autobahn/autobahn-tests.ts index 8f05c25141..420d95a3d1 100644 --- a/types/autobahn/autobahn-tests.ts +++ b/types/autobahn/autobahn-tests.ts @@ -46,4 +46,31 @@ function test_client() { if (!connection.isOpen && !connection.isRetrying && connection.session == null) { connection.open(); } + + var { isConnected, transport: { info: { protocol, type, url } }, defer } = connection; + console.log(isConnected, protocol, type, url, defer); +} + +function test_custom_transport_factory() { + autobahn.transports.register('custom', CustomTransportFactory) + + var CustomFactoryFactory = autobahn.transports.get(''); + var customFactory = new CustomFactoryFactory({}); + var customTransport = customFactory.create() + console.log(customTransport.info); +} + +class CustomTransportFactory { + create(): autobahn.ITransport { + return { + onopen() {}, + onmessage(message: any[]) {}, + onclose(details: autobahn.ICloseEventDetails) {}, + send(message: any[]) {}, + close(errorCode: number, reason?: string) {}, + info: {type: 'custom'} + }; + } + + type: autobahn.TransportType = 'custom' } diff --git a/types/autobahn/index.d.ts b/types/autobahn/index.d.ts index 9c1d66fb85..cfa07c3518 100644 --- a/types/autobahn/index.d.ts +++ b/types/autobahn/index.d.ts @@ -1,6 +1,6 @@ -// Type definitions for AutobahnJS 0.9 -// Project: http://autobahn.ws/js/, https://github.com/crossbario/autobahn-js -// Definitions by: Elad Zelingher , Andy Hawkins , Wladimir Totino , Mathias Teier +// Type definitions for AutobahnJS 18.10 +// Project: https://crossbar.io/autobahn/, https://github.com/crossbario/autobahn-js +// Definitions by: Elad Zelingher , Andy Hawkins , Wladimir Totino , Mathias Teier , Fran Rodriguez // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -190,9 +190,12 @@ declare namespace autobahn { export class Connection { constructor(options?: IConnectionOptions); - isOpen: boolean; - isRetrying: boolean; - session?: Session; + readonly isConnected: boolean; + readonly isOpen: boolean; + readonly isRetrying: boolean; + readonly transport: ITransport; + readonly session?: Session; + readonly defer?: DeferFactory; open(): void; @@ -205,7 +208,7 @@ declare namespace autobahn { interface ITransportDefinition { url?: string; protocols?: string[]; - type: string; + type: TransportType; } type DeferFactory = () => When.Promise; @@ -226,7 +229,7 @@ declare namespace autobahn { url?: string; protocols?: string[]; onchallenge?: OnChallengeHandler; - realm?: string; + realm: string; authmethods?: string[]; authid?: string; } @@ -237,6 +240,19 @@ declare namespace autobahn { code: number; } + type DefaultTransportType = 'websocket' | 'longpoll' | 'rawsocket'; + + // Workaround to get intellisense on type unions of 'literals' | string. + // See https://github.com/Microsoft/TypeScript/issues/29729 + type CustomTransportType = string & { zz_IGNORE_ME?: never }; + type TransportType = DefaultTransportType | CustomTransportType; + + interface ITransportInfo { + url?: string; + protocol?: string; + type: TransportType; + } + interface ITransport { onopen: () => void; onmessage: (message: any[]) => void; @@ -244,19 +260,23 @@ declare namespace autobahn { send(message: any[]): void; close(errorCode: number, reason?: string): void; + info: ITransportInfo; } interface ITransportFactory { - // constructor(options: any); - type: string; + type: TransportType; create(): ITransport; } + + interface ITransportFactoryFactory { + new (options: any): ITransportFactory; + } interface ITransports { - register(name: string, factory: any): void; - isRegistered(name: string): boolean; - get(name: string): any; - list(): string[]; + register(name: TransportType, factory: ITransportFactoryFactory): void; + isRegistered(name: TransportType): boolean; + get(name: TransportType): ITransportFactoryFactory; + list(): TransportType[]; } interface ILog {