Skip to content

Commit

Permalink
add mw.now, mw.requestIdleCallback, mw.track, etc
Browse files Browse the repository at this point in the history
Not covered in https://doc.wikimedia.org/mediawiki-core/master/js/, so the doc comments are copied from source code.
  • Loading branch information
siddharthvp committed May 31, 2021
1 parent 9ee28bb commit 08b75f7
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions mw/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,98 @@ declare global {
*/
function format(formatString: string, ...parameters: unknown[]): string;

/**
* Get the current time, measured in milliseconds since January 1, 1970 (UTC).
*
* On browsers that implement the Navigation Timing API, this function will produce
* floating-point values with microsecond precision that are guaranteed to be monotonic.
* On all other browsers, it will fall back to using `Date`.
*
* @return {number} Current time
*/
function now(): number;

/**
* Schedule a deferred task to run in the background.
*
* This allows code to perform tasks in the main thread without impacting
* time-critical operations such as animations and response to input events.
*
* Basic logic is as follows:
*
* - User input event should be acknowledged within 100ms per [RAIL].
* - Idle work should be grouped in blocks of upto 50ms so that enough time
* remains for the event handler to execute and any rendering to take place.
* - Whenever a native event happens (e.g. user input), the deadline for any
* running idle callback drops to 0.
* - As long as the deadline is non-zero, other callbacks pending may be
* executed in the same idle period.
*
* See also:
*
* - <https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback>
* - <https://w3c.github.io/requestidlecallback/>
* - <https://developers.google.com/web/updates/2015/08/using-requestidlecallback>
* [RAIL]: https://developers.google.com/web/fundamentals/performance/rail
*
* @member mw
* @param {Function} callback
* @param {Object} [options]
* @param {number} [options.timeout] If set, the callback will be scheduled for
* immediate execution after this amount of time (in milliseconds) if it didn't run
* by that time.
*/
function requestIdleCallback(callback: ((...args: any[]) => any)): void;

/**
* Track an analytic event.
*
* This method provides a generic means for MediaWiki JavaScript code to capture state
* information for analysis. Each logged event specifies a string topic name that describes
* the kind of event that it is. Topic names consist of dot-separated path components,
* arranged from most general to most specific. Each path component should have a clear and
* well-defined purpose.
*
* Data handlers are registered via `mw.trackSubscribe`, and receive the full set of
* events that match their subscription, including those that fired before the handler was
* bound.
*
* @param {string} topic Topic name
* @param {Object|number|string} [data] Data describing the event.
*/
function track(topic: string, data?: object | number | string): void;

/**
* Track an early error event via mw.track and send it to the window console.
*
* @private
* @param {string} topic Topic name
* @param {Object} data Data describing the event, encoded as an object; see mw#logError
*/
function trackError(topic: string, data: object): void;

/**
* Register a handler for subset of analytic events, specified by topic.
*
* Handlers will be called once for each tracked event, including any events that fired before the
* handler was registered; 'this' is set to a plain object with a topic' property naming the event, and a
* 'data' property which is an object of event-specific data. The event topic and event data are
* also passed to the callback as the first and second arguments, respectively.
*
* @param {string} topic Handle events whose name starts with this string prefix
* @param {Function} callback Handler to call for each matching tracked event
* @param {string} callback.topic
* @param {Object} [callback.data]
*/
function trackSubscribe(topic: string, callback: ((topic: string, data: object) => any)): void;

/**
* Stop handling events for a particular handler
*
* @param {Function} callback
*/
function trackUnsubscribe(callback: Function): void;

// types for mw.widgets are out of scope!
const widgets: any;
}
Expand Down

0 comments on commit 08b75f7

Please sign in to comment.