From 89f08736f0993af9797e3b3de02b8a35f4e711fe Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Mon, 8 May 2017 00:43:40 +0100 Subject: [PATCH] Fix Podium tests --- types/hapi/index.d.ts | 12 +-- types/podium/index.d.ts | 176 +++++++++++++++++------------------ types/podium/podium-tests.ts | 4 +- 3 files changed, 95 insertions(+), 97 deletions(-) diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index c2bf0ae3f5..6922f65bd8 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -40,7 +40,7 @@ import { import * as Catbox from 'catbox'; import {MimosOptions} from 'mimos'; -import * as Podium from 'podium'; +import Podium = require('podium'); import * as Shot from 'shot'; export interface Dictionary { @@ -54,7 +54,7 @@ export interface Dictionary { * [See docs](https://hapijs.com/api/16.1.1#server-properties) * [See docs](https://hapijs.com/api/16.1.1#server-events) */ -export class Server extends Podium.Podium { +export class Server extends Podium { /** * Creates a new Server object */ @@ -982,7 +982,7 @@ export type PayLoadOutputOption = 'data' | 'stream' | 'file'; * * a podium [See docs](https://github.com/hapijs/podium) emitter object. * For context [See docs](https://hapijs.com/api/16.1.1#servereventevents) > events parameter */ -export type ApplicationEvent = string | ApplicationEventOptionsObject | Podium.Podium; +export type ApplicationEvent = string | ApplicationEventOptionsObject | Podium; /** * an event options object @@ -1727,7 +1727,7 @@ export interface ServerCacheMethod { * [See docs](https://hapijs.com/api/16.1.1#request-object) * [See docs](https://hapijs.com/api/16.1.1#request-properties) */ -export class Request extends Podium.Podium { +export class Request extends Podium { /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name]. */ app: any; /** authentication information */ @@ -2228,11 +2228,11 @@ export interface ReplyNoContinue extends Base_Reply {} * Response object * [See docs](https://hapijs.com/api/16.1.1#response-object) * - * TODO, check extending from Podium.Podium is correct. Extending because of "The response object supports the following events" [See docs](https://hapijs.com/api/16.1.1#response-events) + * TODO, check extending from Podium is correct. Extending because of "The response object supports the following events" [See docs](https://hapijs.com/api/16.1.1#response-events) * * 'peek' - emitted for each chunk of data written back to the client connection. The event method signature is function(chunk, encoding). * * 'finish' - emitted when the response finished writing but before the client response connection is ended. The event method signature is function (). */ -export interface Response extends Podium.Podium { +export interface Response extends Podium { /** the HTTP response status code. Defaults to 200 (except for errors). */ statusCode: number; /** an object containing the response headers where each key is a header field name. Note that this is an incomplete list of headers to be included with the response. Additional headers will be added once the response is prepared for transmission. */ diff --git a/types/podium/index.d.ts b/types/podium/index.d.ts index 0d62740479..cf89d57f82 100644 --- a/types/podium/index.d.ts +++ b/types/podium/index.d.ts @@ -4,29 +4,27 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 - /** * Podium * Node (semi) compatible event emitter with extra features. * podium is an event emitter with support for tags, filters, channels, event update cloning, arguments spreading, and other features useful when building large scale applications. While node's native EventEmitter is strictly focused on maximum performance, it lacks many features that do not belong in the core implementation. podium is not restricted by node's performance requirement as it is designed for application layer needs where it's overhead is largely insignificant as implementing these features will have similar cost on top of the native emitter. * @see {@link https://github.com/hapijs/podium} */ -// declare class Podium { -export class Podium { +interface Podium { /** * Creates a new podium emitter * @param events if present, the value is passed to podium.registerEvent(). */ - constructor(events?: Events[]); - constructor(events?: Events); + new(events?: Podium.Events[]): Podium; + new(events?: Podium.Events): Podium; /** * podium.registerEvent(events) * Register the specified events and their optional configuration. Events must be registered before they can be emitted or subscribed to. This is done to detect event name mispelling and invalid event activities. * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} */ - registerEvent(events: Events[]): void; - registerEvent(events: Events): void; + registerEvent(events: Podium.Events[]): void; + registerEvent(events: Podium.Events): void; /** * podium.registerPodium(podiums) @@ -54,7 +52,7 @@ export class Podium { * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} */ - on(criteria: string | Criteria, listener: Listener): void; + on(criteria: string | Podium.Criteria, listener: Podium.Listener): void; /** * podium.addListener(criteria, listener) @@ -63,7 +61,7 @@ export class Podium { * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} */ - addListener(criteria: string | Criteria, listener: Listener): void; + addListener(criteria: string | Podium.Criteria, listener: Podium.Listener): void; /** * podium.once(criteria, listener) @@ -72,7 +70,7 @@ export class Podium { * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} */ - once(criteria: string | Criteria, listener: Listener): void; + once(criteria: string | Podium.Criteria, listener: Podium.Listener): void; /** * podium.removeListener(name, listener) @@ -82,7 +80,7 @@ export class Podium { * Returns a reference to the current emitter. * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumremovelistenername-listener} */ - removeListener(name: string, listener: Listener): Podium; + removeListener(name: string, listener: Podium.Listener): Podium; /** * podium.removeAllListeners(name) @@ -103,86 +101,86 @@ export class Podium { hasListeners(name: string): boolean; } -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} - */ -export type Events = string | EventOptionsObject | Podium; - -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} - */ -export interface EventOptionsObject { - /** the event name string (required). */ - name: string; - /** a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not). */ - channels?: string | string[]; - /** if true, the data object passed to podium.emit() is cloned before it is passed to the listeners (unless an override specified by each listener). Defaults to false (data is passed as-is). */ - clone?: boolean; - /** if true, the data object passed to podium.emit() must be an array and the listener method is called with each array element passed as a separate argument (unless an override specified by each listener). This should only be used when the emitted data structure is known and predictable. Defaults to false (data is emitted as a single argument regardless of its type). */ - spread?: boolean; - /** if true and the criteria object passed to podium.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended to the arguments list at the end (but before the callback argument if block is set). A configuration override can be set by each listener. Defaults to false. */ - tags?: boolean; - /** if true, the same event name can be registered multiple times where the second registration is ignored. Note that if the registration config is changed between registrations, only the first configuration is used. Defaults to false (a duplicate registration will throw an error). */ - shared?: boolean; -} - -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumaddlistenercriteria-listener} - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncecriteria-listener} - */ -export interface CriteriaObject { - /** the event name string (required). */ - name: string; - /** if true, the listener method receives an additional callback argument which must be called when the method completes. No other event will be emitted until the callback methods is called. The method signature is function(). If block is set to a positive integer, the value is used to set a timeout after which any pending events will be emitted, ignoring the eventual call to callback. Defaults to false (non blocking). */ - block?: boolean | number; - /** a string or array of strings specifying the event channels to subscribe to. If the event registration specified a list of allowed channels, the channels array must match the allowed channels. If channels are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter. */ - channels?: string | string[]; - /** if true, the data object passed to podium.emit() is cloned before it is passed to the listener method. Defaults to the event registration option (which defaults to false). */ - clone?: boolean; - /** a positive integer indicating the number of times the listener can be called after which the subscription is automatically removed. A count of 1 is the same as calling podium.once(). Defaults to no limit. */ - count?: number; +declare namespace Podium { /** - * the event tags (if present) to subscribe to which can be one of: - * * a tag string. - * * an array of tag strings. - * * an object with the following: + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} */ - filter?: string | string[] | CriteriaFilterOptionsObject; - /** if true, and the data object passed to podium.emit() is an array, the listener method is called with each array element passed as a separate argument. This should only be used when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults to false). */ - spread?: boolean; - /** if true and the criteria object passed to podium.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended to the arguments list at the end (but before the callback argument if block is set). Defaults to the event registration option (which defaults to false). */ - tags?: boolean; - /** the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. */ - listener?: Listener; + export type Events = string | EventOptionsObject | Podium; + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} + */ + export interface EventOptionsObject { + /** the event name string (required). */ + name: string; + /** a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not). */ + channels?: string | string[]; + /** if true, the data object passed to podium.emit() is cloned before it is passed to the listeners (unless an override specified by each listener). Defaults to false (data is passed as-is). */ + clone?: boolean; + /** if true, the data object passed to podium.emit() must be an array and the listener method is called with each array element passed as a separate argument (unless an override specified by each listener). This should only be used when the emitted data structure is known and predictable. Defaults to false (data is emitted as a single argument regardless of its type). */ + spread?: boolean; + /** if true and the criteria object passed to podium.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended to the arguments list at the end (but before the callback argument if block is set). A configuration override can be set by each listener. Defaults to false. */ + tags?: boolean; + /** if true, the same event name can be registered multiple times where the second registration is ignored. Note that if the registration config is changed between registrations, only the first configuration is used. Defaults to false (a duplicate registration will throw an error). */ + shared?: boolean; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumaddlistenercriteria-listener} + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncecriteria-listener} + */ + export interface CriteriaObject { + /** the event name string (required). */ + name: string; + /** if true, the listener method receives an additional callback argument which must be called when the method completes. No other event will be emitted until the callback methods is called. The method signature is function(). If block is set to a positive integer, the value is used to set a timeout after which any pending events will be emitted, ignoring the eventual call to callback. Defaults to false (non blocking). */ + block?: boolean | number; + /** a string or array of strings specifying the event channels to subscribe to. If the event registration specified a list of allowed channels, the channels array must match the allowed channels. If channels are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter. */ + channels?: string | string[]; + /** if true, the data object passed to podium.emit() is cloned before it is passed to the listener method. Defaults to the event registration option (which defaults to false). */ + clone?: boolean; + /** a positive integer indicating the number of times the listener can be called after which the subscription is automatically removed. A count of 1 is the same as calling podium.once(). Defaults to no limit. */ + count?: number; + /** + * the event tags (if present) to subscribe to which can be one of: + * * a tag string. + * * an array of tag strings. + * * an object with the following: + */ + filter?: string | string[] | CriteriaFilterOptionsObject; + /** if true, and the data object passed to podium.emit() is an array, the listener method is called with each array element passed as a separate argument. This should only be used when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults to false). */ + spread?: boolean; + /** if true and the criteria object passed to podium.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended to the arguments list at the end (but before the callback argument if block is set). Defaults to the event registration option (which defaults to false). */ + tags?: boolean; + /** the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. */ + listener?: Listener; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export interface CriteriaFilterOptionsObject { + /** a tag string or array of tag strings. */ + tags?: string | string[]; + /** if true, all tags must be present for the event update to match the subscription. Defaults to false (at least one matching tag). */ + all?: boolean; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export type Criteria = string | CriteriaObject; + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export interface Listener { + (data: any, tags?: Tags, callback?: () => void): void; + } + + export type Tags = {[tag: string]: boolean}; } -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} - */ -export interface CriteriaFilterOptionsObject { - /** a tag string or array of tag strings. */ - tags?: string | string[]; - /** if true, all tags must be present for the event update to match the subscription. Defaults to false (at least one matching tag). */ - all?: boolean; -} +declare var Podium: Podium; -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} - */ -export type Criteria = string | CriteriaObject; - -/** - * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} - */ -export interface Listener { -(data: any, tags?: Tags, callback?: () => void): void; -} - -export type Tags = {[tag: string]: boolean}; - -// export = Podium; - -// declare namespace Podium { -// type Tags = {[tag: string]: boolean}; -// } +export = Podium; diff --git a/types/podium/podium-tests.ts b/types/podium/podium-tests.ts index f5cee6bbec..1717d2af1a 100644 --- a/types/podium/podium-tests.ts +++ b/types/podium/podium-tests.ts @@ -1,4 +1,5 @@ -/* +/// + import Podium = require('podium'); const podiumObject = new Podium(); // new emitter @@ -30,4 +31,3 @@ podiumObject.addListener('event1',listener1); // podium.once(criteria, listener) Same as calling podium.on() with the count option set to 1. Whenever we call emit(), listener1 will get fired but also get removed, so that it won't get fired on call to emit(). podiumObject.once('event1',listener1); -*/