UNPKG

react-native-onyx

Version:

State management for React Native

48 lines (47 loc) 1.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const PerformanceProxy_1 = __importDefault(require("./dependencies/PerformanceProxy")); /** * Capture a measurement between the start mark and now */ function measureMarkToNow(startMark, detail) { PerformanceProxy_1.default.measure(`${startMark.name} [${startMark.detail.args.toString()}]`, { start: startMark.startTime, end: PerformanceProxy_1.default.now(), detail: Object.assign(Object.assign({}, startMark.detail), detail), }); } function isPromiseLike(value) { return value != null && typeof value === 'object' && 'then' in value; } /** * Wraps a function with metrics capturing logic */ function decorateWithMetrics(func, alias = func.name) { function decorated(...args) { const mark = PerformanceProxy_1.default.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; } exports.default = decorateWithMetrics;