Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add types definition
  • Loading branch information
magic-akari committed Nov 15, 2020
commit 2b75b353611111f30cb311a2519b93a6839b116c
67 changes: 67 additions & 0 deletions src/Lifecycle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { PageState } from "./PageState";
import StateChangeEvent from "./StateChangeEvent";

interface StateChangeEventListener {
(evt: StateChangeEvent): void;
}

interface StateChangeEventListenerObject {
handleEvent(evt: StateChangeEvent): void;
}

declare type StateChangeEventListenerOrStateChangeEventListenerObject =
| StateChangeEventListener
| StateChangeEventListenerObject;

/**
* Class definition for the exported, singleton lifecycle instance.
*/
export default class Lifecycle extends EventTarget {
/**
* Initializes state, state history, and adds event listeners to monitor
* state changes.
*/
/**
* @return {string}
*/
get state(): PageState;
/**
* Returns the value of document.wasDiscarded. This is arguably unnecessary
* but I think there's value in having the entire API in one place and
* consistent across browsers.
* @return {boolean}
*/
get pageWasDiscarded(): boolean;

addEventListener(
type: "statechange",
listener: (ev: StateChangeEvent) => void,
options?: boolean | AddEventListenerOptions
): void;
addEventListener(
type: "statechange",
listener: StateChangeEventListenerOrStateChangeEventListenerObject,
options?: boolean | AddEventListenerOptions
): void;
removeEventListener(
type: "statechange",
listener: (ev: StateChangeEvent) => void,
options?: boolean | EventListenerOptions
): void;
removeEventListener(
type: "statechange",
listener: StateChangeEventListenerOrStateChangeEventListenerObject,
options?: boolean | EventListenerOptions
): void;

/**
* @param {Symbol|Object} id A unique symbol or object identifying the
*. pending state. This ID is required when removing the state later.
*/
addUnsavedChanges(id: Symbol | Object): void;
/**
* @param {Symbol|Object} id A unique symbol or object identifying the
*. pending state. This ID is required when removing the state later.
*/
removeUnsavedChanges(id: Symbol | Object): void;
}
6 changes: 6 additions & 0 deletions src/PageState.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare type PageState =
| "active"
| "passive"
| "hidden"
| "frozen"
| "terminated";
16 changes: 16 additions & 0 deletions src/StateChangeEvent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PageState } from "./PageState";

/**
* implements {IStateChangeEvent}
*/
interface StateChangeEvent extends Event {
newState: PageState;
oldState: PageState;
originalEvent: Event;
}

declare var StateChangeEvent: {
prototype: StateChangeEvent;
};

export default StateChangeEvent;
3 changes: 3 additions & 0 deletions src/export.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Lifecycle from "./Lifecycle";
declare const _default: Lifecycle;
export default _default;