Skip to content

Interaction Tracking -- Observer context switching lifecycle. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 71 additions & 0 deletions packages/interaction-tracking/src/InteractionEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Interaction} from './InteractionTracking';

type Interactions = Array<Interaction>;

export type InteractionObserver = {
onInteractionsScheduled: (
interactions: Interactions,
executionID: number,
) => void,
onInteractionsStarting: (
interactions: Interactions,
executionID: number,
) => void,
onInteractionsEnded: (
interactions: Interactions,
executionID: number,
) => void,
};

const observers: Array<InteractionObserver> = [];

export function registerInteractionObserver(
observer: InteractionObserver,
): void {
observers.push(observer);
}

export function __onInteractionsScheduled(
interactions: Interactions,
executionID: number,
): void {
if (!observers.length) {
return;
}
observers.forEach(observer => {
observer.onInteractionsScheduled(interactions, executionID);
});
}

export function __onInteractionsStarting(
interactions: Interactions,
executionID: number,
) {
if (!observers.length) {
return;
}
observers.forEach(observer => {
observer.onInteractionsStarting(interactions, executionID);
});
}

export function __onInteractionsEnded(
interactions: Interactions,
executionID: number,
) {
if (!observers.length) {
return;
}
observers.forEach(observer => {
observer.onInteractionsEnded(interactions, executionID);
});
}
174 changes: 150 additions & 24 deletions packages/interaction-tracking/src/InteractionTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,126 @@
* @flow
*/

import {getCurrentContext, trackContext} from './InteractionZone';
import invariant from 'shared/invariant';
import {
__onInteractionsScheduled,
__onInteractionsStarting,
__onInteractionsEnded,
} from './InteractionEmitter';

export {
getCurrentContext as getCurrentEvents,
restoreContext as startContinuation,
completeContext as stopContinuation,
wrapForCurrentContext as wrap,
} from './InteractionZone';

// TODO This package will likely want to override browser APIs (e.g. setTimeout, fetch)
// So that async callbacks are automatically wrapped with the current tracked event info.
// For the initial iteration, async callbacks must be explicitely wrapped with wrap().
export {registerInteractionObserver} from './InteractionEmitter';

type Interactions = Array<Interaction>;

export type Interaction = {|
id: number,
name: string,
timestamp: number,
|};

export type Continuation = {
__hasBeenRun: boolean,
__id: number,
__interactions: Interactions,
__prevInteractions: Interactions | null,
};

// Normally we would use the current renderer HostConfig's "now" method,
// But since interaction-tracking will be a separate package,
// I instead just copied the approach used by ReactScheduler.
let now;
if (typeof performance === 'object' && typeof performance.now === 'function') {
const localPerformance = performance;
now = function() {
return localPerformance.now();
};
now = () => localPerformance.now();
} else {
const localDate = Date;
now = function() {
return localDate.now();
};
now = () => localDate.now();
}

let currentContinuation: Continuation | null = null;
let globalExecutionID: number = 0;
let globalInteractionID: number = 0;
let interactions: Interactions | null = null;

export function getCurrent(): Interactions | null {
if (!__PROFILE__) {
return null;
} else {
return interactions;
}
}

export function reserveContinuation(): Continuation | null {
if (!__PROFILE__) {
return null;
}

if (interactions !== null) {
const executionID = globalExecutionID++;

__onInteractionsScheduled(interactions, executionID);

return {
__hasBeenRun: false,
__id: executionID,
__interactions: interactions,
__prevInteractions: null,
};
} else {
return null;
}
}

export function startContinuation(continuation: Continuation | null): void {
if (!__PROFILE__) {
return;
}

invariant(
currentContinuation === null,
'Cannot start a continuation when one is already active.',
);

if (continuation === null) {
return;
}

invariant(
!continuation.__hasBeenRun,
'A continuation can only be started once',
);

continuation.__hasBeenRun = true;
currentContinuation = continuation;

// Continuations should mask (rather than extend) any current interactions.
// Upon completion of a continuation, previous interactions will be restored.
continuation.__prevInteractions = interactions;
interactions = continuation.__interactions;

__onInteractionsStarting(interactions, continuation.__id);
}

export function stopContinuation(continuation: Continuation): void {
if (!__PROFILE__) {
return;
}

invariant(
currentContinuation === continuation,
'Cannot stop a continuation that is not active.',
);

if (continuation === null) {
return;
}

__onInteractionsEnded(continuation.__interactions, continuation.__id);

currentContinuation = null;

// Restore previous interactions.
interactions = continuation.__prevInteractions;
}

export function track(name: string, callback: Function): void {
Expand All @@ -50,18 +136,58 @@ export function track(name: string, callback: Function): void {
}

const interaction: Interaction = {
id: globalInteractionID++,
name,
timestamp: now(),
};

// Tracked interactions should stack.
// To do that, create a new zone with a concatenated (cloned) array.
let interactions: Interactions | null = getCurrentContext();
const executionID = globalExecutionID++;
const prevInteractions = interactions;

// Tracked interactions should stack/accumulate.
// To do that, clone the current interactions array.
// The previous interactions array will be restored upon completion.
interactions =
interactions === null ? [interaction] : interactions.concat(interaction);

try {
__onInteractionsScheduled(interactions, executionID);
__onInteractionsStarting(interactions, executionID);

callback();
} finally {
__onInteractionsEnded(interactions, executionID);

interactions = prevInteractions;
}
}

export function wrap(callback: Function): Function {
if (!__PROFILE__) {
return callback;
}

if (interactions === null) {
interactions = [interaction];
} else {
interactions = interactions.concat(interaction);
return callback;
}

trackContext(interactions, callback);
const executionID = globalExecutionID++;
const wrappedInteractions = interactions;

__onInteractionsScheduled(wrappedInteractions, executionID);

return (...args) => {
const prevInteractions = interactions;
interactions = wrappedInteractions;

try {
__onInteractionsStarting(interactions, executionID);

callback(...args);
} finally {
__onInteractionsEnded(interactions, executionID);

interactions = prevInteractions;
}
};
}
60 changes: 0 additions & 60 deletions packages/interaction-tracking/src/InteractionZone.js

This file was deleted.

Loading