Provide pusher-js with typings

This commit is contained in:
tkqubo
2015-09-19 16:43:16 +09:00
parent dbbafb3e72
commit 0bb267a06f
2 changed files with 108 additions and 10 deletions
+85 -2
View File
@@ -1,5 +1,88 @@
/// <reference path="pusher-js.d.ts" />
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
+23 -8
View File
@@ -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;
}