Skip to content

Commit

Permalink
Add cancel() function to public API (#66)
Browse files Browse the repository at this point in the history
* Export a public `cancel()` function

* Document new `cancel` method

Co-authored-by: Andrew Hyndman <ahyndman@dropbox.com>
  • Loading branch information
ajhyndman and Andrew Hyndman authored Sep 19, 2022
1 parent 13e9aeb commit 43136f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ The callback function may be called more than once if in-page navigation occurs.

`getTTVC` may be called more than once to register more than one subscriber.

#### `cancel()`

```typescript
type cancel = () => void;
```

Abort the current TTVC measurement.

This method is provided as an escape hatch. Consider using `cancel` to notify @dropbox/ttvc that a user interaction has occurred and continuing the measurement may produce an invalid result.

#### `incrementAjaxCount() & decrementAjaxCount()`

```typescript
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export const getTTVC = (callback: MetricSubscriber) => calculator?.getTTVC(callb
*/
export const start = () => calculator?.start(performance.now());

/**
* Abort the current TTVC measurement.
*
* This method is provided as an escape hatch. Consider using it to notify
* @dropbox/ttvc that a user interaction has occurred and continuing the
* measurement may produce an invalid result.
*/
export const cancel = () => calculator?.cancel();

/**
* Call this to notify ttvc that an AJAX request has just begun.
*
Expand Down
22 changes: 14 additions & 8 deletions src/visuallyCompleteCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class VisuallyCompleteCalculator {
private lastImageLoadTarget?: HTMLElement;
private subscribers = new Set<MetricSubscriber>();
private navigationCount = 0;
private activeMeasurementIndex?: number; // only one measurement should be active at a time

/**
* Determine whether the calculator should run in the current environment
Expand Down Expand Up @@ -75,14 +76,23 @@ class VisuallyCompleteCalculator {
});
}

/** abort the current TTVC measurement */
cancel() {
this.activeMeasurementIndex = undefined;
}

/** begin measuring a new navigation */
async start(start = 0) {
const navigationIndex = (this.navigationCount += 1);
this.activeMeasurementIndex = navigationIndex;
Logger.info('VisuallyCompleteCalculator.start()');

// setup
let shouldCancel = false;
const cancel = () => (shouldCancel = true);
const cancel = () => {
if (this.activeMeasurementIndex === navigationIndex) {
this.activeMeasurementIndex = undefined;
}
};

this.inViewportImageObserver.observe();
this.inViewportMutationObserver.observe(document.documentElement);
Expand All @@ -100,12 +110,8 @@ class VisuallyCompleteCalculator {
// - wait for simultaneous network and CPU idle
const didNetworkTimeOut = await new Promise<boolean>(requestAllIdleCallback);

// if this isn't the most recent navigation, abort
if (navigationIndex !== this.navigationCount) {
cancel();
}

if (!shouldCancel) {
// if this navigation's measurment hasn't been cancelled, record it.
if (navigationIndex === this.activeMeasurementIndex) {
// identify timestamp of last visible change
const end = Math.max(start, this.lastImageLoadTimestamp, this.lastMutation?.timestamp ?? 0);

Expand Down

0 comments on commit 43136f8

Please sign in to comment.