-
Notifications
You must be signed in to change notification settings - Fork 75
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
chore: re-enable performance metrics #598
Changes from all commits
92817c4
3f21827
65c1571
2578654
0494c31
6e5275f
42055a0
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,32 @@ | ||
/** | ||
* Stores settings from Onyx.init globally so they can be made accessible by other parts of the library. | ||
*/ | ||
|
||
const globalSettings = { | ||
enablePerformanceMetrics: false, | ||
}; | ||
|
||
type GlobalSettings = typeof globalSettings; | ||
|
||
const listeners = new Set<(settings: GlobalSettings) => unknown>(); | ||
function addGlobalSettingsChangeListener(listener: (settings: GlobalSettings) => unknown) { | ||
listeners.add(listener); | ||
return () => { | ||
listeners.delete(listener); | ||
}; | ||
} | ||
|
||
function notifyListeners() { | ||
listeners.forEach((listener) => listener(globalSettings)); | ||
} | ||
|
||
function setPerformanceMetricsEnabled(enabled: boolean) { | ||
globalSettings.enablePerformanceMetrics = enabled; | ||
notifyListeners(); | ||
} | ||
|
||
function isPerformanceMetricsEnabled() { | ||
return globalSettings.enablePerformanceMetrics; | ||
} | ||
|
||
export {setPerformanceMetricsEnabled, isPerformanceMetricsEnabled, addGlobalSettingsChangeListener}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type ImportType = ReturnType<typeof require>; | ||
|
||
/** | ||
* Create a lazily-imported module proxy. | ||
* This is useful for lazily requiring optional dependencies. | ||
*/ | ||
const createModuleProxy = <TModule>(getModule: () => ImportType): TModule => { | ||
const holder: {module: TModule | undefined} = {module: undefined}; | ||
|
||
const proxy = new Proxy(holder, { | ||
get: (target, property) => { | ||
if (property === '$$typeof') { | ||
// If inlineRequires is enabled, Metro will look up all imports | ||
// with the $$typeof operator. In this case, this will throw the | ||
// `OptionalDependencyNotInstalledError` error because we try to access the module | ||
// even though we are not using it (Metro does it), so instead we return undefined | ||
// to bail out of inlineRequires here. | ||
return undefined; | ||
} | ||
|
||
if (target.module == null) { | ||
// lazy initialize module via require() | ||
// caller needs to make sure the require() call is wrapped in a try/catch | ||
// eslint-disable-next-line no-param-reassign | ||
target.module = getModule() as TModule; | ||
} | ||
return target.module[property as keyof typeof holder.module]; | ||
}, | ||
}); | ||
return proxy as unknown as TModule; | ||
}; | ||
|
||
class OptionalDependencyNotInstalledError extends Error { | ||
constructor(name: string) { | ||
super(`${name} is not installed!`); | ||
} | ||
} | ||
|
||
export {createModuleProxy, OptionalDependencyNotInstalledError}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type performance from 'react-native-performance'; | ||
import {createModuleProxy, OptionalDependencyNotInstalledError} from '../ModuleProxy'; | ||
|
||
const PerformanceProxy = createModuleProxy<typeof performance>(() => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('react-native-performance').default; | ||
} catch { | ||
throw new OptionalDependencyNotInstalledError('react-native-performance'); | ||
} | ||
}); | ||
|
||
export default PerformanceProxy; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Use the existing performance API on web | ||
export default performance; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import PerformanceProxy from './dependencies/PerformanceProxy'; | ||
|
||
const decoratedAliases = new Set(); | ||
|
||
/** | ||
* Capture a measurement between the start mark and now | ||
*/ | ||
function measureMarkToNow(startMark: PerformanceMark, detail: Record<string, unknown>) { | ||
PerformanceProxy.measure(`${startMark.name} [${startMark.detail.args.toString()}]`, { | ||
start: startMark.startTime, | ||
end: PerformanceProxy.now(), | ||
detail: {...startMark.detail, ...detail}, | ||
}); | ||
} | ||
|
||
function isPromiseLike(value: unknown): value is Promise<unknown> { | ||
return value != null && typeof value === 'object' && 'then' in value; | ||
} | ||
|
||
/** | ||
* Wraps a function with metrics capturing logic | ||
*/ | ||
function decorateWithMetrics<Args extends unknown[], ReturnType>(func: (...args: Args) => ReturnType, alias = func.name) { | ||
if (decoratedAliases.has(alias)) { | ||
throw new Error(`"${alias}" is already decorated`); | ||
} | ||
|
||
decoratedAliases.add(alias); | ||
function decorated(...args: Args) { | ||
const mark = PerformanceProxy.mark(alias, {detail: {args, alias}}); | ||
|
||
const originalReturnValue = func(...args); | ||
|
||
if (isPromiseLike(originalReturnValue)) { | ||
/* | ||
* The handlers added here are not affecting the original promise | ||
* They create a separate chain that's not exposed (returned) to the original caller | ||
*/ | ||
originalReturnValue | ||
.then((result) => { | ||
measureMarkToNow(mark, {result}); | ||
}) | ||
.catch((error) => { | ||
measureMarkToNow(mark, {error}); | ||
}); | ||
|
||
return originalReturnValue; | ||
} | ||
|
||
measureMarkToNow(mark, {result: originalReturnValue}); | ||
return originalReturnValue; | ||
} | ||
decorated.name = `${alias}_DECORATED`; | ||
|
||
return decorated; | ||
} | ||
|
||
export default decorateWithMetrics; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,7 @@ | |
"README.md", | ||
"LICENSE.md" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"main": "lib/index.ts", | ||
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. 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. Oh I am sorry, this was meant to be handled in a separate PR. For react native we could however consume the typescript code directly. I think this is what i did here: in the light of the better debugging tools i think for RN it would be nicer to be able to debug the TS code in NewDot than having to debug the transpiled JS code. 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. Actually these changes are also breaking the code on E/App, not just the development process described in the README, so I created a PR to revert it here. I will take a look at your PR and continue review there, thanks! |
||
"scripts": { | ||
"lint": "eslint .", | ||
"typecheck": "tsc --noEmit", | ||
|
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.
❤️ love this!!
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.
yeah i only hate the
ts-expect-error
suppressions 🤔