Skip to content

Commit

Permalink
Merge pull request DefinitelyTyped#33053 from hagl/master
Browse files Browse the repository at this point in the history
Add CometD 4.0.0 types for  AckExtension and TimeSyncExtension
  • Loading branch information
minestarks authored Feb 14, 2019
2 parents 4b9a901 + 6f8e852 commit e593217
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
12 changes: 12 additions & 0 deletions types/cometd/AckExtension/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as m from '..';

declare class AckExtension implements m.Extension {
constructor();

incoming: m.Listener;
outgoing: m.Listener;
registered: (name: string, cometd: m.CometD) => void;
unregistered: () => void;
}

export default AckExtension;
40 changes: 40 additions & 0 deletions types/cometd/TimeSyncExtension/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as m from '..';

declare class TimeSyncExtension implements m.Extension {
constructor();

incoming: m.Listener;
outgoing: m.Listener;
registered: (name: string, cometd: m.CometD) => void;
unregistered: () => void;

/**
* Get the estimated offset in ms from the clients clock to the
* servers clock. The server time is the client time plus the offset.
*/
getTimeOffset: () => number;

/**
* Get an array of multiple offset samples used to calculate
* the offset.
*/
getTimeOffsetSamples: () => [number];

/**
* Get the estimated network lag in ms from the client to the server.
*/
getNetworkLag: () => number;

/**
* Get the estimated server time in ms since the epoch.
*/
getServerTime: () => number;

/**
*
* Get the estimated server time as a Date object
*/
getServerDate: () => Date;
}

export default TimeSyncExtension;
20 changes: 19 additions & 1 deletion types/cometd/cometd-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CometD, Listener, Message, SubscriptionHandle } from "cometd";
import TimeSyncExtension from 'cometd/TimeSyncExtension';
import AckExtension from 'cometd/AckExtension';

const cometd = new CometD();

Expand All @@ -11,7 +13,23 @@ cometd.configure({
url: "http://localhost:8080/cometd"
});

cometd.registerExtension("ack", { incoming: () => {}, outgoing: () => {} });
cometd.registerExtension("ack", new AckExtension());

const timesync = new TimeSyncExtension();
cometd.registerExtension("timesync", timesync);

const timeSyncSubscription = cometd.addListener("/foo/bar", () => {
if (timesync.getNetworkLag() > 1000) {
cometd.publish("/mychannel", { timesyncStats: {
lag: timesync.getNetworkLag(),
serverTime: timesync.getServerTime(),
serverDate: timesync.getServerDate(),
timeOffset: timesync.getTimeOffset(),
timeOffsetSamples: timesync.getTimeOffsetSamples()
}
});
}
});

cometd.unregisterTransport("websocket");

Expand Down
7 changes: 6 additions & 1 deletion types/cometd/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Type definitions for CometD 4.0
// Project: https://cometd.org
// Definitions by: Derek Cicerone <https://github.com/derekcicerone>, Daniel Perez Alvarez <https://github.com/unindented>, Alex Henry <https://github.com/alxHenry>
// Definitions by: Derek Cicerone <https://github.com/derekcicerone>
// Daniel Perez Alvarez <https://github.com/unindented>
// Alex Henry <https://github.com/alxHenry>
// Harald Gliebe <https://github.com/hagl>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

Expand Down Expand Up @@ -88,6 +91,8 @@ export interface SubscriptionHandle {
export interface Extension {
incoming?: Listener;
outgoing?: Listener;
registered?: (name: string, cometd: CometD) => void;
unregistered?: () => void;
}

export class CometD {
Expand Down
6 changes: 4 additions & 2 deletions types/cometd/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
},
"files": [
"index.d.ts",
"cometd-tests.ts"
"cometd-tests.ts",
"AckExtension/index.d.ts",
"TimeSyncExtension/index.d.ts"
]
}
}

0 comments on commit e593217

Please sign in to comment.