-
Notifications
You must be signed in to change notification settings - Fork 49.4k
interaction-tracking package #13234
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
interaction-tracking package #13234
Changes from all commits
523b27d
6f996da
8df2067
974d25e
14dffc7
ac6887d
4418e72
68bc9dd
f277c09
112f2ec
5380b5f
ae7e4fa
d98e713
ccc4f31
7c88a28
2892738
332d87d
d47164a
75d9c9a
6b467ee
d50a03c
8c0cefb
d2148f5
d8e4665
1dc899a
dc98726
e2ca33e
1fd5420
56dba65
dd7d78a
cea2995
a511be1
83ac9fd
b8287cf
012a9e6
0509a9f
d5ebc31
4150607
21248f8
43a97f1
d88cbb9
69fd285
cc45d8e
d41bd61
6e31ec3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
export * from './src/InteractionTracking'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/interaction-tracking.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/interaction-tracking.development.js'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "interaction-tracking", | ||
"description": "utility for tracking interaction events", | ||
"version": "0.0.1", | ||
"license": "MIT", | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"index.js", | ||
"cjs/", | ||
"umd/" | ||
], | ||
"keywords": [ | ||
"interaction", | ||
"tracking", | ||
"react" | ||
], | ||
"repository": "https://github.com/facebook/react", | ||
"bugs": { | ||
"url": "https://github.com/facebook/react/issues" | ||
}, | ||
"homepage": "https://reactjs.org/" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
/** | ||
* 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 { | ||
enableInteractionTracking, | ||
enableInteractionTrackingObserver, | ||
} from 'shared/ReactFeatureFlags'; | ||
|
||
export type Interaction = {| | ||
__count: number, | ||
id: number, | ||
name: string, | ||
timestamp: number, | ||
|}; | ||
|
||
export type Subscriber = { | ||
// A new interaction has been created via the track() method. | ||
onInteractionTracked: (interaction: Interaction) => void, | ||
|
||
// All scheduled async work for an interaction has finished. | ||
onInteractionScheduledWorkCompleted: (interaction: Interaction) => void, | ||
|
||
// New async work has been scheduled for a set of interactions. | ||
// When this work is later run, onWorkStarted/onWorkStopped will be called. | ||
// A batch of async/yieldy work may be scheduled multiple times before completing. | ||
// In that case, onWorkScheduled may be called more than once before onWorkStopped. | ||
// Work is scheduled by a "thread" which is identified by a unique ID. | ||
onWorkScheduled: (interactions: Set<Interaction>, threadID: number) => void, | ||
|
||
// A batch of scheduled work has been canceled. | ||
// Work is done by a "thread" which is identified by a unique ID. | ||
onWorkCanceled: (interactions: Set<Interaction>, threadID: number) => void, | ||
|
||
// A batch of work has started for a set of interactions. | ||
// When this work is complete, onWorkStopped will be called. | ||
// Work is not always completed synchronously; yielding may occur in between. | ||
// A batch of async/yieldy work may also be re-started before completing. | ||
// In that case, onWorkStarted may be called more than once before onWorkStopped. | ||
// Work is done by a "thread" which is identified by a unique ID. | ||
onWorkStarted: (interactions: Set<Interaction>, threadID: number) => void, | ||
|
||
// A batch of work has completed for a set of interactions. | ||
// Work is done by a "thread" which is identified by a unique ID. | ||
onWorkStopped: (interactions: Set<Interaction>, threadID: number) => void, | ||
}; | ||
|
||
export type InteractionsRef = { | ||
current: Set<Interaction>, | ||
}; | ||
|
||
export type SubscriberRef = { | ||
current: Subscriber | null, | ||
}; | ||
|
||
const DEFAULT_THREAD_ID = 0; | ||
|
||
// Counters used to generate unique IDs. | ||
let interactionIDCounter: number = 0; | ||
let threadIDCounter: number = 0; | ||
|
||
// Set of currently tracked interactions. | ||
// Interactions "stack"– | ||
// Meaning that newly tracked interactions are appended to the previously active set. | ||
// When an interaction goes out of scope, the previous set (if any) is restored. | ||
let interactionsRef: InteractionsRef = (null: any); | ||
|
||
// Listener(s) to notify when interactions begin and end. | ||
// Note that subscribers are only supported when enableInteractionTrackingObserver is enabled. | ||
let subscriberRef: SubscriberRef = (null: any); | ||
|
||
if (enableInteractionTracking) { | ||
interactionsRef = { | ||
current: new Set(), | ||
}; | ||
if (enableInteractionTrackingObserver) { | ||
subscriberRef = { | ||
current: null, | ||
}; | ||
} | ||
} | ||
|
||
// These values are exported for libraries with advanced use cases (i.e. React). | ||
// They should not typically be accessed directly. | ||
export {interactionsRef as __interactionsRef, subscriberRef as __subscriberRef}; | ||
|
||
export function clear(callback: Function): any { | ||
if (!enableInteractionTracking) { | ||
return callback(); | ||
} | ||
|
||
const prevInteractions = interactionsRef.current; | ||
interactionsRef.current = new Set(); | ||
|
||
try { | ||
return callback(); | ||
} finally { | ||
interactionsRef.current = prevInteractions; | ||
} | ||
} | ||
|
||
export function getCurrent(): Set<Interaction> | null { | ||
if (!enableInteractionTracking) { | ||
return null; | ||
} else { | ||
return interactionsRef.current; | ||
} | ||
} | ||
|
||
export function getThreadID(): number { | ||
return ++threadIDCounter; | ||
} | ||
|
||
export function track( | ||
name: string, | ||
timestamp: number, | ||
callback: Function, | ||
threadID: number = DEFAULT_THREAD_ID, | ||
): any { | ||
if (!enableInteractionTracking) { | ||
return callback(); | ||
} | ||
|
||
const interaction: Interaction = { | ||
__count: 0, | ||
id: interactionIDCounter++, | ||
name, | ||
timestamp, | ||
}; | ||
|
||
const prevInteractions = interactionsRef.current; | ||
|
||
// Tracked interactions should stack/accumulate. | ||
// To do that, clone the current interactions. | ||
// The previous set will be restored upon completion. | ||
const interactions = new Set(prevInteractions); | ||
interactions.add(interaction); | ||
interactionsRef.current = interactions; | ||
|
||
if (enableInteractionTrackingObserver) { | ||
// Update before calling callback in case it schedules follow-up work. | ||
interaction.__count = 1; | ||
|
||
let returnValue; | ||
const subscriber = subscriberRef.current; | ||
|
||
try { | ||
if (subscriber !== null) { | ||
subscriber.onInteractionTracked(interaction); | ||
} | ||
} finally { | ||
try { | ||
if (subscriber !== null) { | ||
subscriber.onWorkStarted(interactions, threadID); | ||
} | ||
} finally { | ||
try { | ||
returnValue = callback(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually just return here. The finally will still execute before returning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning in finally masks thrown errors. |
||
} finally { | ||
interactionsRef.current = prevInteractions; | ||
|
||
try { | ||
if (subscriber !== null) { | ||
subscriber.onWorkStopped(interactions, threadID); | ||
} | ||
} finally { | ||
interaction.__count--; | ||
|
||
// If no async work was scheduled for this interaction, | ||
// Notify subscribers that it's completed. | ||
if (subscriber !== null && interaction.__count === 0) { | ||
subscriber.onInteractionScheduledWorkCompleted(interaction); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return returnValue; | ||
} else { | ||
try { | ||
return callback(); | ||
} finally { | ||
interactionsRef.current = prevInteractions; | ||
} | ||
} | ||
} | ||
|
||
export function wrap( | ||
callback: Function, | ||
threadID: number = DEFAULT_THREAD_ID, | ||
): Function { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I understand why function wrap(callback) {
const continuedInteractions = interactions.current;
retain();
return (...args) => {
track(continuedInteractions, () => callback(...args));
release();
}
} where most of the code is reused from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose avoiding the extra function call is worthwhile, but I believe semantically they should be identical. See other comment: #13234 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think wrap is that complicated. This approach avoids an extra function call and additional wrapper function. Also the behavior of wrap/track are different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But... why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that's kind of fundamental to how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn’t modify the current set of interactions because it will reset them back to the previous set after it exits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I meant that it would modify the original set. So if I wrapped [foo,bar] I would expect my async work to be attributed to [foo,bar]– not [foo,bar,...whatever-else-was-also-active] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @acdlite's misunderstanding is a good opportunity to rename this function to something that makes it clearer. I could see how if I see In reality the purpose of wrap isn't to wrap a function. The primary purpose is to continue where you left off.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there's precedence for calling it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the brevity of "track" and "wrap" but I'm not bullish on them if others feel there are more meaningful names. |
||
if (!enableInteractionTracking) { | ||
return callback; | ||
} | ||
|
||
const wrappedInteractions = interactionsRef.current; | ||
|
||
if (enableInteractionTrackingObserver) { | ||
const subscriber = subscriberRef.current; | ||
if (subscriber !== null) { | ||
subscriber.onWorkScheduled(wrappedInteractions, threadID); | ||
} | ||
|
||
// Update the pending async work count for the current interactions. | ||
// Update after calling subscribers in case of error. | ||
wrappedInteractions.forEach(interaction => { | ||
interaction.__count++; | ||
}); | ||
} | ||
|
||
const wrapped = () => { | ||
const prevInteractions = interactionsRef.current; | ||
interactionsRef.current = wrappedInteractions; | ||
|
||
if (enableInteractionTrackingObserver) { | ||
const subscriber = subscriberRef.current; | ||
|
||
try { | ||
let returnValue; | ||
|
||
try { | ||
if (subscriber !== null) { | ||
subscriber.onWorkStarted(wrappedInteractions, threadID); | ||
} | ||
} finally { | ||
try { | ||
returnValue = callback.apply(undefined, arguments); | ||
} finally { | ||
interactionsRef.current = prevInteractions; | ||
|
||
if (subscriber !== null) { | ||
subscriber.onWorkStopped(wrappedInteractions, threadID); | ||
} | ||
} | ||
} | ||
|
||
return returnValue; | ||
} finally { | ||
// Update pending async counts for all wrapped interactions. | ||
// If this was the last scheduled async work for any of them, | ||
// Mark them as completed. | ||
wrappedInteractions.forEach(interaction => { | ||
interaction.__count--; | ||
|
||
if (subscriber !== null && interaction.__count === 0) { | ||
subscriber.onInteractionScheduledWorkCompleted(interaction); | ||
} | ||
}); | ||
} | ||
} else { | ||
try { | ||
return callback.apply(undefined, arguments); | ||
} finally { | ||
interactionsRef.current = prevInteractions; | ||
} | ||
} | ||
}; | ||
|
||
if (enableInteractionTrackingObserver) { | ||
wrapped.cancel = () => { | ||
const subscriber = subscriberRef.current; | ||
|
||
try { | ||
if (subscriber !== null) { | ||
subscriber.onWorkCanceled(wrappedInteractions, threadID); | ||
} | ||
} finally { | ||
// Update pending async counts for all wrapped interactions. | ||
// If this was the last scheduled async work for any of them, | ||
// Mark them as completed. | ||
wrappedInteractions.forEach(interaction => { | ||
interaction.__count--; | ||
|
||
if (subscriber && interaction.__count === 0) { | ||
subscriber.onInteractionScheduledWorkCompleted(interaction); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
return wrapped; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will provide a hint to the VM that we only want 3 inline fields but they you add an expando later.
We should always provide the four fields in initialization. If you want to exclude on when the feature flag is not used you can initialize the object in a conditional.
In the three field case you can cast it to any and then back to Interaction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good call. Thanks!