@rosskevin/ifvisible
Version:
Cross-browser, lightweight way to check if user is looking at the page or interacting with it. (wrapper around HTML5 visibility api)
76 lines (72 loc) • 2.43 kB
TypeScript
type FireableEvent = 'idle' | 'statusChanged' | 'blur' | 'focus' | 'wakeup';
type Status = 'active' | 'idle' | 'hidden';
interface Data {
status: Status;
}
type FireableEventCallback = (data?: Data) => void;
declare class EventBus {
private store;
constructor();
attach(event: FireableEvent, callback: FireableEventCallback): void;
fire(event: FireableEvent, data?: Data): void;
remove(event: FireableEvent, callback?: FireableEventCallback): void;
}
interface IIdleInfo {
isIdle: boolean;
idleFor: number;
timeLeft: number;
timeLeftPer: number;
}
type TimerCallback = () => void;
declare class Timer {
private id;
private ifvInstance;
private seconds;
private callback;
constructor(ifvInstance: IfVisible, seconds: number, callback: TimerCallback);
stop(): void;
resume(): void;
pause(): void;
private start;
}
declare class IfVisible {
private status;
private timers;
private idleTime;
private idleStartedTime?;
private win;
private doc;
private eventBus;
private winListeners?;
private docListeners?;
private focusListener?;
private throttleDuration;
constructor(win: Window, doc: Document);
on(event: FireableEvent, callback: FireableEventCallback): IfVisible;
off(event: FireableEvent, callback?: FireableEventCallback): IfVisible;
setIdleDuration(seconds: number): IfVisible;
getIdleDuration(): number;
getIdleInfo(): IIdleInfo;
setThrottleDuration(milliseconds: number): IfVisible;
idle(callback?: FireableEventCallback): IfVisible;
blur(callback?: FireableEventCallback): IfVisible;
focus(callback?: FireableEventCallback): IfVisible;
wakeup(callback?: FireableEventCallback): IfVisible;
onEvery(seconds: number, callback: TimerCallback): Timer;
now(check?: Status): boolean;
getStatus(): Status;
/**
* Removes all listeners from the DOM, but not user added listeners so it may be reattached later.
*/
detach(): void;
/**
* Allows for:
* - control of DOM detach/reattach
* - recognition of changes to option and reattachment with those taken into account.
*/
reattach(): void;
private trackChange;
private startIdleTimer;
}
declare const ifvisible: IfVisible;
export { type Data, EventBus, type FireableEvent, type FireableEventCallback, type IIdleInfo, IfVisible, type Status, ifvisible };