"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.autoTrace = exports.delayTraceTillSignal = void 0;
const fs = __importStar(require("fs"));
const chrome_trace_event_1 = require("chrome-trace-event");
const store = global;
store._forgeTracer = store._forgeTracer || {
    tracer: new chrome_trace_event_1.Tracer(),
    traceIdCounter: 1,
};
const forgeTracer = store._forgeTracer;
if (process.env.ELECTRON_FORGE_TRACE_FILE) {
    store._forgeTracer.pipe(fs.createWriteStream(process.env.ELECTRON_FORGE_TRACE_FILE));
}
else {
    store._forgeTracer = null;
}
const nextRoot = () => `forge-auto-trace-root-${forgeTracer.traceIdCounter++}`;
function _autoTrace(tracer, autoTraceId, opts, fn) {
    return (async (...args) => {
        const traceArgs = {
            id: autoTraceId,
            name: opts.name,
            cat: [opts.category],
            args: opts.extraDetails,
            tid: autoTraceId.split('-')[autoTraceId.split('-').length - 1],
        };
        tracer?.begin(traceArgs);
        const childTrace = (opts, fn) => {
            return _autoTrace(tracer?.child(traceArgs) ?? null, opts.newRoot ? nextRoot() : autoTraceId, opts, fn);
        };
        childTrace._autoEnd = true;
        childTrace._end = () => tracer?.end(traceArgs);
        try {
            return await Promise.resolve(fn(childTrace, ...args));
        }
        finally {
            if (childTrace._autoEnd) {
                childTrace._end();
            }
        }
    });
}
function delayTraceTillSignal(trace, signaller, signal) {
    const original = signaller[signal];
    trace._autoEnd = false;
    signaller[signal] = function (...args) {
        const result = original.call(signaller, ...args);
        if (typeof result === 'object' && result.then && result.catch) {
            result.then(() => trace._end()).catch(() => trace._end());
        }
        else {
            trace._end();
        }
        return result;
    };
    return signaller;
}
exports.delayTraceTillSignal = delayTraceTillSignal;
function autoTrace(opts, fn) {
    return _autoTrace(forgeTracer.tracer, nextRoot(), opts, fn);
}
exports.autoTrace = autoTrace;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSx1Q0FBeUI7QUFFekIsMkRBQW9EO0FBRXBELE1BQU0sS0FBSyxHQUFHLE1BQWEsQ0FBQztBQUM1QixLQUFLLENBQUMsWUFBWSxHQUFHLEtBQUssQ0FBQyxZQUFZLElBQUk7SUFDekMsTUFBTSxFQUFFLElBQUksMkJBQU0sRUFBRTtJQUNwQixjQUFjLEVBQUUsQ0FBQztDQUNsQixDQUFDO0FBQ0YsTUFBTSxXQUFXLEdBR2IsS0FBSyxDQUFDLFlBQVksQ0FBQztBQUV2QixJQUFJLE9BQU8sQ0FBQyxHQUFHLENBQUMseUJBQXlCLEVBQUU7SUFDekMsS0FBSyxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsRUFBRSxDQUFDLGlCQUFpQixDQUFDLE9BQU8sQ0FBQyxHQUFHLENBQUMseUJBQXlCLENBQUMsQ0FBQyxDQUFDO0NBQ3RGO0tBQU07SUFDTCxLQUFLLENBQUMsWUFBWSxHQUFHLEl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