From 967db8dd4cfe66faa06f08a4ad6a396ccbf081ec Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 28 Mar 2016 16:55:11 +0300 Subject: [PATCH 1/3] Added definitions for Wampy object --- wampy/wampy-tests.d.ts | 0 wampy/wampy.d.ts | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 wampy/wampy-tests.d.ts create mode 100644 wampy/wampy.d.ts diff --git a/wampy/wampy-tests.d.ts b/wampy/wampy-tests.d.ts new file mode 100644 index 000000000..e69de29bb diff --git a/wampy/wampy.d.ts b/wampy/wampy.d.ts new file mode 100644 index 000000000..245c2f834 --- /dev/null +++ b/wampy/wampy.d.ts @@ -0,0 +1,87 @@ +// Type definitions for wampy.js v2.0.1 +// Project: https://github.com/KSDaemon/wampy.js +// Definitions by: Konstantin Burkalev +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface WampyOptions { + autoReconnect?: boolean; + reconnectInterval?: number; + maxRetries?: number; + transportEncoding?: string; + realm?: string; + helloCustomDetails?: any; + onConnect?: () => void; + onClose?: () => void; + onError?: () => void; + onReconnect?: () => void; + ws?: any; + msgpackCoder?: any; +} + +interface WampyOpStatus { + code: number; + description: string; + reqId?: number; +} + +interface SuccessErrorCallbacksHash { + onSuccess?: () => void; + onError?: (err: string) => void; +} + +interface SubscribeCallbacksHash extends SuccessErrorCallbacksHash { + onEvent: (data: any) => void; +} + +interface RegisterCallbacksHash extends SuccessErrorCallbacksHash { + rpc: (data: any) => any; +} + +interface AdvancedOptions { + exclude?: number | number[]; + eligible?: number | number[]; + exclude_me?: boolean; + disclose_me?: boolean; +} + +interface CallAdvancedOptions extends AdvancedOptions { + receive_progress?: boolean; +} + +interface CancelAdvancedOptions { + mode?: "skip" | "kill" | "killnowait"; +} + +interface Wampy { + options(opts?: WampyOptions): WampyOptions | Wampy; + getOpStatus(): WampyOpStatus; + getSessionId(): number; + connect(url?: string): Wampy; + disconnect(): Wampy; + abort(): Wampy; + subscribe(topicURI: string, callbacks: (() => void | SubscribeCallbacksHash)): Wampy; + unsubscribe(topicURI: string, callbacks: (() => void | SubscribeCallbacksHash)): Wampy; + publish(topicURI: string, + payload?: any, + callbacks?: SuccessErrorCallbacksHash, + advancedOptions?: AdvancedOptions): Wampy; + call(topicURI: string, + payload?: any, + callbacks?: (() => void | SuccessErrorCallbacksHash), + advancedOptions?: CallAdvancedOptions): Wampy; + cancel(reqId: number, + callbacks?: (() => void | SuccessErrorCallbacksHash), + advancedOptions?: CancelAdvancedOptions): Wampy; + register(topicURI: string, callbacks: ((data: any) => any | RegisterCallbacksHash)): Wampy; + unregister(topicURI: string, callbacks?: (() => void | SuccessErrorCallbacksHash)): Wampy; +} + +interface WampyInstance { + new(url?: string, options?: WampyOptions): Wampy; +} + +declare var wampy: WampyInstance; + +declare module "wampy" { + export = wampy; +} From c5344fa9139b10ec4809a1fabc5cab0b249c0a56 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 28 Mar 2016 17:58:22 +0300 Subject: [PATCH 2/3] Fixed and added tests --- wampy/wampy-tests.d.ts | 0 wampy/wampy-tests.ts | 119 ++++++++++++++++++++++++++++++ wampy/wampy.d.ts | 164 +++++++++++++++++++++-------------------- 3 files changed, 204 insertions(+), 79 deletions(-) delete mode 100644 wampy/wampy-tests.d.ts create mode 100644 wampy/wampy-tests.ts diff --git a/wampy/wampy-tests.d.ts b/wampy/wampy-tests.d.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/wampy/wampy-tests.ts b/wampy/wampy-tests.ts new file mode 100644 index 000000000..eeea0132d --- /dev/null +++ b/wampy/wampy-tests.ts @@ -0,0 +1,119 @@ +/// +/// + +import Wampy = require('wampy'); + +var ws = new Wampy('http://wamp.router.url', {realm: 'WAMPRealm'}); + +ws.options(); + +ws.options({ + reconnectInterval: 1000, + maxRetries: 999, + onConnect: function () { console.log('Yahoo! We are online!'); }, + onClose: function () { console.log('See you next time!'); }, + onError: function () { console.log('Breakdown happened'); }, + onReconnect: function () { console.log('Reconnecting...'); } +}); + +ws.connect(); +ws.connect('/my-socket-path'); +ws.connect('wss://socket.server.com:5000/ws'); +var id: number = ws.getSessionId(); +ws.disconnect(); +ws.abort(); + +ws.subscribe('system.monitor.update', function (data) { + console.log('Received system.monitor.update event!'); + }) + .subscribe('client.message', function (data) { + console.log('Received client.message event!'); + }); + +var f1 = function () { console.log('Subscribe processing!'); }; +ws.unsubscribe('subscribed.topic', f1); + +ws.unsubscribe('chat.message.received'); + +ws.call('get.server.time', null, { + onSuccess: function (stime) { + console.log('RPC successfully called'); + console.log('Server time is ' + stime); + }, + onError: function (err) { + console.log('RPC call failed with error ' + err); + } +}); + +ws.publish('system.monitor.update'); +ws.getOpStatus(); + +ws.publish('user.logged.in'); +ws.publish('chat.message.received', 'user message'); +ws.publish('chat.message.received', ['user message1', 'user message2']); +ws.publish('user.modified', { field1: 'field1', field2: true, field3: 123 }); +ws.publish('user.modified', { field1: 'field1', field2: true, field3: 123 }, { + onSuccess: function () { console.log('User successfully modified'); } +}); +ws.publish('user.modified', { field1: 'field1', field2: true, field3: 123 }, { + onSuccess: function () { console.log('User successfully modified'); }, + onError: function (err) { console.log('User modification failed', err); } +}); +ws.publish('chat.message.received', ['Private message'], null, { eligible: 123456789 }); + +ws.call('server.time', null, function (data) { console.log('Server time is ' + data[0]); }); + +ws.call('start.migration', null, { + onSuccess: function (data) { + console.log('RPC successfully called'); + }, + onError: function (err) { + console.log('RPC call failed!',err); + } +}); + +ws.call('restore.backup', { backupFile: 'backup.zip' }, { + onSuccess: function (data) { + console.log('Backup successfully restored'); + }, + onError: function (err) { + console.log('Restore failed!',err); + } +}); + +ws.call('start.migration', null, { + onSuccess: function (data) { + console.log('RPC successfully called'); + }, + onError: function (err) { + console.log('RPC call failed!',err); + } +}); +var status = ws.getOpStatus(); + +ws.cancel(status.reqId); + +var sqrt_f = function (x: number) { return x*x; }; + +ws.register('sqrt.value', sqrt_f); + +ws.register('sqrt.value', { + rpc: sqrt_f, + onSuccess: function (data) { + console.log('RPC successfully registered'); + }, + onError: function (err) { + console.log('RPC registration failed!',err); + } +}); + +ws.unregister('sqrt.value'); + +ws.unregister('sqrt.value', { + onSuccess: function (data) { + console.log('RPC successfully unregistered'); + }, + onError: function (err) { + console.log('RPC unregistration failed!',err); + } +}); diff --git a/wampy/wampy.d.ts b/wampy/wampy.d.ts index 245c2f834..4c94eb0dd 100644 --- a/wampy/wampy.d.ts +++ b/wampy/wampy.d.ts @@ -3,85 +3,91 @@ // Definitions by: Konstantin Burkalev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface WampyOptions { - autoReconnect?: boolean; - reconnectInterval?: number; - maxRetries?: number; - transportEncoding?: string; - realm?: string; - helloCustomDetails?: any; - onConnect?: () => void; - onClose?: () => void; - onError?: () => void; - onReconnect?: () => void; - ws?: any; - msgpackCoder?: any; -} - -interface WampyOpStatus { - code: number; - description: string; - reqId?: number; -} - -interface SuccessErrorCallbacksHash { - onSuccess?: () => void; - onError?: (err: string) => void; -} - -interface SubscribeCallbacksHash extends SuccessErrorCallbacksHash { - onEvent: (data: any) => void; -} - -interface RegisterCallbacksHash extends SuccessErrorCallbacksHash { - rpc: (data: any) => any; -} - -interface AdvancedOptions { - exclude?: number | number[]; - eligible?: number | number[]; - exclude_me?: boolean; - disclose_me?: boolean; -} - -interface CallAdvancedOptions extends AdvancedOptions { - receive_progress?: boolean; -} - -interface CancelAdvancedOptions { - mode?: "skip" | "kill" | "killnowait"; -} - -interface Wampy { - options(opts?: WampyOptions): WampyOptions | Wampy; - getOpStatus(): WampyOpStatus; - getSessionId(): number; - connect(url?: string): Wampy; - disconnect(): Wampy; - abort(): Wampy; - subscribe(topicURI: string, callbacks: (() => void | SubscribeCallbacksHash)): Wampy; - unsubscribe(topicURI: string, callbacks: (() => void | SubscribeCallbacksHash)): Wampy; - publish(topicURI: string, - payload?: any, - callbacks?: SuccessErrorCallbacksHash, - advancedOptions?: AdvancedOptions): Wampy; - call(topicURI: string, - payload?: any, - callbacks?: (() => void | SuccessErrorCallbacksHash), - advancedOptions?: CallAdvancedOptions): Wampy; - cancel(reqId: number, - callbacks?: (() => void | SuccessErrorCallbacksHash), - advancedOptions?: CancelAdvancedOptions): Wampy; - register(topicURI: string, callbacks: ((data: any) => any | RegisterCallbacksHash)): Wampy; - unregister(topicURI: string, callbacks?: (() => void | SuccessErrorCallbacksHash)): Wampy; -} - -interface WampyInstance { - new(url?: string, options?: WampyOptions): Wampy; -} - -declare var wampy: WampyInstance; - declare module "wampy" { + + interface WampyOptions { + autoReconnect?: boolean; + reconnectInterval?: number; + maxRetries?: number; + transportEncoding?: string; + realm?: string; + helloCustomDetails?: any; + onConnect?: () => void; + onClose?: () => void; + onError?: () => void; + onReconnect?: () => void; + ws?: any; + msgpackCoder?: any; + } + + interface WampyOpStatus { + code: number; + description: string; + reqId?: number; + } + + interface SuccessErrorCallbacksHash { + onSuccess?: (data: any) => void; + onError?: (err: string) => void; + } + + interface SubscribeCallbacksHash extends SuccessErrorCallbacksHash { + onEvent: (data: any) => void; + } + + interface RegisterCallbacksHash extends SuccessErrorCallbacksHash { + rpc: (data: any) => any; + } + + interface CallSuccessErrorCallbacksHash { + onSuccess: (data: any) => any; + onError?: (err: string) => void; + } + + interface AdvancedOptions { + exclude?: number | number[]; + eligible?: number | number[]; + exclude_me?: boolean; + disclose_me?: boolean; + } + + interface CallAdvancedOptions extends AdvancedOptions { + receive_progress?: boolean; + } + + interface CancelAdvancedOptions { + mode?: "skip" | "kill" | "killnowait"; + } + + interface Wampy { + options(opts?: WampyOptions): WampyOptions | Wampy; + getOpStatus(): WampyOpStatus; + getSessionId(): number; + connect(url?: string): Wampy; + disconnect(): Wampy; + abort(): Wampy; + subscribe(topicURI: string, callbacks: (((data: any) => void) | SubscribeCallbacksHash)): Wampy; + unsubscribe(topicURI: string, callbacks?: (((data: any) => void) | SubscribeCallbacksHash)): Wampy; + publish(topicURI: string, + payload?: any, + callbacks?: SuccessErrorCallbacksHash, + advancedOptions?: AdvancedOptions): Wampy; + call(topicURI: string, + payload?: any, + callbacks?: (((data: any) => void) | CallSuccessErrorCallbacksHash), + advancedOptions?: CallAdvancedOptions): Wampy; + cancel(reqId: number, + callbacks?: ((() => void) | SuccessErrorCallbacksHash), + advancedOptions?: CancelAdvancedOptions): Wampy; + register(topicURI: string, callbacks: (((data: any) => any) | RegisterCallbacksHash)): Wampy; + unregister(topicURI: string, callbacks?: ((() => void) | SuccessErrorCallbacksHash)): Wampy; + } + + interface WampyInstance { + new(url?: string, options?: WampyOptions): Wampy; + } + + var wampy: WampyInstance; + export = wampy; } From 7228f5ce05c3fc2dd7dd4a5b5cb1635bd226c4f1 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 28 Mar 2016 18:07:11 +0300 Subject: [PATCH 3/3] Fixes for es6 compilation --- wampy/wampy-tests.ts | 2 +- wampy/wampy.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wampy/wampy-tests.ts b/wampy/wampy-tests.ts index eeea0132d..f7dcf9fe5 100644 --- a/wampy/wampy-tests.ts +++ b/wampy/wampy-tests.ts @@ -1,7 +1,7 @@ /// /// -import Wampy = require('wampy'); +import Wampy from 'wampy'; var ws = new Wampy('http://wamp.router.url', {realm: 'WAMPRealm'}); diff --git a/wampy/wampy.d.ts b/wampy/wampy.d.ts index 4c94eb0dd..650a8a91a 100644 --- a/wampy/wampy.d.ts +++ b/wampy/wampy.d.ts @@ -89,5 +89,5 @@ declare module "wampy" { var wampy: WampyInstance; - export = wampy; + export default wampy; }