From 0bb267a06f429e56c71a9cfd3bdc57f71ab4401b Mon Sep 17 00:00:00 2001 From: tkqubo Date: Sat, 19 Sep 2015 16:43:16 +0900 Subject: [PATCH] Provide pusher-js with typings --- pusher-js/pusher-js-tests.ts | 87 +++++++++++++++++++++++++++++++++++- pusher-js/pusher-js.d.ts | 31 +++++++++---- 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/pusher-js/pusher-js-tests.ts b/pusher-js/pusher-js-tests.ts index 1312e9d455..a1c0429b0a 100644 --- a/pusher-js/pusher-js-tests.ts +++ b/pusher-js/pusher-js-tests.ts @@ -1,5 +1,88 @@ /// -import js = require('pusher-js'); +import Pusher = require('pusher-js'); + +var API_KEY: string; +var pusher: Pusher.Pusher; + +// +// Configuration +// + +pusher = new Pusher(API_KEY, { + authEndpoint: "http://example.com/pusher/auth" +}); + +pusher = new Pusher(API_KEY, { + auth: { + params: { foo: "bar" }, + headers: { baz: "boo" } + } +}); + +pusher = new Pusher(API_KEY, { + auth: { + params: { foo: "bar" }, + headers: { "X-CSRF-Token": "SOME_CSRF_TOKEN" } + } +}); + +pusher = new Pusher(API_KEY, { cluster: "eu" }); + +pusher = new Pusher(API_KEY, { enabledTransports: ["ws"] }); + +pusher = new Pusher(API_KEY, { disabledTransports: ["sockjs"] }); + +// will only use WebSockets +pusher = new Pusher(API_KEY, { + enabledTransports: ["ws", "xhr_streaming"], + disabledTransports: ["xhr_streaming"] +}); + +// +// Connection +// + +var socket: Pusher.Pusher; +var my_channel: Pusher.Channel; +var channels: Pusher.Channel[]; + +socket = new Pusher(API_KEY); + +// +// Subscribing to channels +// + +my_channel = socket.subscribe('my-channel'); + +my_channel = socket.subscribe('private-my-channel'); + +channels = socket.allChannels(); +console.group('Pusher - subscribed to:'); +for (var i = 0; i < channels.length; i++) { + var channel = channels[i]; + console.log(channel.name); +} +console.groupEnd(); + +my_channel = socket.subscribe('my-channel'); +socket.bind('new-comment', + function(data: any) { + // add comment into page + } +); + +var channel: Pusher.Channel; + +var context = { title: 'Pusher' }; +var handler = function(){ + console.log('My name is ' + this.title); +}; +channel.bind('new-comment', handler, context); + +channel.unbind('new-comment', handler); // removes just `handler` for the `new-comment` event +channel.unbind('new-comment'); // removes all handlers for the `new-comment` event +channel.unbind(null, handler); // removes `handler` for all events +channel.unbind(null, null, context); // removes all handlers for `context` +channel.unbind(); // removes all handlers on `channel` -//TODO diff --git a/pusher-js/pusher-js.d.ts b/pusher-js/pusher-js.d.ts index f218a776f6..826df78d20 100644 --- a/pusher-js/pusher-js.d.ts +++ b/pusher-js/pusher-js.d.ts @@ -6,17 +6,21 @@ declare module "pusher-js" { namespace pusher { - interface PusherConstructor { - (apiKey: string, config?: Config): Pusher; - //TODO + interface PusherStatic { + new(apiKey: string, config?: Config): Pusher; } interface Pusher { - + subscribe(name: string): Channel; + subscribeAll(): void; + unsubscribe(name: string): void; + channel(name: string): Channel; + allChannels(): Channel[]; + bind(eventName: string, callback: Function): Pusher; + bind_all(callback: Function): Pusher; } interface Config { - /** * Forces the connection to use encrypted transports. */ @@ -95,12 +99,23 @@ declare module "pusher-js" { } interface AuthConfig { - params: { [key: string]: any }; - headers: { [key: string]: any }; + params?: { [key: string]: any }; + headers?: { [key: string]: any }; + } + + interface Channel { + bind(eventName: string, callback: Function, context?: any): Channel; + bind_all(callback: Function): Channel; + unbind(eventName?: string, callback?: Function, context?: any): Channel; + unbind_all(eventName?: string, callback?: Function): Channel; + trigger(eventName: string, data?: any): any; + pusher: Pusher; + name: string; + subscribed: boolean; } } - var pusher: pusher.PusherConstructor; + var pusher: pusher.PusherStatic; export = pusher; }