{"name":"@paulirish/trace_engine","version":"0.0.27","keywords":[],"author":"","license":"BSD-3-Clause","_id":"@paulirish/trace_engine@0.0.27","dist":{"shasum":"0c0dcc3f63a1298e3ee30df93e4273c9a3c314e9","tarball":"http://registry-npm.ywwl.com/@paulirish/trace_engine/download/@paulirish/trace_engine-0.0.27.tgz","fileCount":422,"integrity":"sha512-P4xSh2JI7f3VTXAJcmo4uVttegj4bOvsTMto6bOKH8pWGkKbZCxaq5Js04IZ/dmb1S+t4aj4c+jvsp+R+nunFQ==","signatures":[{"sig":"MEQCIDzeycWmPeziHS+cVexBUOslTZRZbiMKJTudgt+ZEyALAiA/VO5/nmpUqsnY6ufknAYhn+DHCIQVyQmCxy3UjZ6B5Q==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":5367682,"size":3016181,"noattachment":false,"key":"/@paulirish/trace_engine/-/@paulirish/trace_engine-0.0.27.tgz"},"main":"models/trace/trace.js","type":"module","types":"./models/trace/trace.d.ts","scripts":{"test":"node test/test-trace-engine.mjs","prepublishOnly":"npm test"},"_npmUser":{"name":"hoten","email":"cjamcl@gmail.com"},"_npmVersion":"9.8.1","description":"This package contains the trace engine implementation used by the DevTools Performance Panel.","directories":{},"_nodeVersion":"18.18.2","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{},"_npmOperationalInternal":{"tmp":"tmp/trace_engine_0.0.27_1719605548707_0.2756587463352502","host":"s3://npm-registry-packages"},"readme":"# Trace Model (NOT FOR PUBLIC CONSUMPTION)\n\nThis package contains the trace engine implementation used by the DevTools Performance Panel.\n\n⚠️ The API is not stable and it's fairly likely that upgrades will break you (at some point).\nBut the breakages should be obvious exceptions or type failures.\n\n## API quickstart\n\n```js\nimport * as TraceModel from '@paulirish/trace_engine';\n\npolyfillDOMRect();\nconst engine = TraceModel.Processor.TraceProcessor.createWithAllHandlers();\n\nawait engine.parse(traceEvents);\nconsole.log(engine.data) // TraceParseData\n```\n\n**Note:** To run in Node, you'll need to polyfill `window.DOMRect`. 😜\n\nSee the included `analyze-trace.mjs` a runnable invocation.\n\n### Types\n\nYou'll probably use something like…\n\n    @type {import('@paulirish/trace_engine').Types.TraceEvents.TraceEventData[]\n    @type {import('@paulirish/trace_engine').Handlers.Types.TraceParseData\n\n## Maintainer cheatsheet\n\nSee also http://go/btlax\n\n```sh\n# Build devtools and prep a package, then invoke the copy script to prepare $HOME/code/trace_engine\nscripts/trace/prep-trace-engine-package.sh\n\n# switch to standalone\ncd $HOME/code/trace_engine\n\n# bump and publish\nnpm version v0.0.XXX   # Manually determine next version. `npm info @paulirish/trace_engine | grep latest` + 1\nnpm publish --access public --dry-run\nnpm publish --access public\n```\n\n## High level architecture\n\n```\n     ┌──────────────┐\n     │  Model#parse ├───┐\n     └──────────────┘   │\n                        │\n             ┌──────────▼──────────┐\n             │async processor#parse│\n             └──────────┬──────────┘\n                        │\n             ┌──────────▼────────────┐\n             │for handler of handlers│\n             └───┬────────────────┬──┘\n                 │                │\n┌────────────────▼────┐     ┌─────▼────────────────┐\n│NetworkRequestHandler│     │...many more handlers │\n│                     │     │                      │\n│     reset()         │     │                      │\n│                     │     │                      │\n│     handleEvent()   │     │                      │\n│                     │     │                      │\n│     finalize()      │     │                      │\n│                     │     │                      │\n│     data()          │  │  │                      │\n└─────────────────────┘  │  └──────────────────────┘\n                         │\n                         │\n      ┌──────────────────▼─────────────────┐\n      │const data = model.parsedTrace()│\n      └────────────────────────────────────┘\n```\n\n`Model#parse` is the entrypoint into the engine and is the public interface that consumers use to initiate tracing and to fetch data back.\n\nAll the processing is done by the `Processor`. The processor contains a series of *Handlers*, each of which is responsible for parsing events of a particular category.\n\nThe trace processor loops over every event in the trace and calls each handler in turn (done this way so we only loop over the trace file once, rather than doing it once-per-handler). A Handler is a file that exposes a set of methods, most importantly `handleEvent()` and `data()`. The `handleEvent` function will be called for each event in the trace, and it is up to an individual handler to do something with that event if it needs to. The `data` method should return the final data that has been parsed and generated by the handler.\n\nOnce processing is done (read on for more details on how to track this), you can use the `parsedTrace()` method to fetch the result of parsing a given trace.\n\n## Enabled handlers and creating a model\n\nWe use `Model.createWithAllHandlers()`, which initializes a model configured correctly with the right handlers.\n\nIf you want to strictly control the set of handlers that are run (for example, if you only want to run one particular handler), you can initialize the model yourself and pass in the set of handlers:\n\n```ts\nconst model = new Model({\n  NetworkRequestHandler: Handlers.ModelHandlers.NetworkRequestHandler,\n})\n```\n\n## Parsing a trace and getting data back\n\nOnce you have an instance of the model, you can call the `parse` method to take a set of raw events and parse them. Once parsed, you then have to call the `parsedTrace` method, providing an index of the trace you want to have the data for. This is because any model can store a number of traces. Each trace is given an index, which starts at 0 and increments by one as a new trace is parsed.\n\nIf you are managing multiple traces, you should store them in some form of indexed data structure so you can easily know which index to use to fetch any data from the model. You may delete a trace with `deleteTraceByIndex`, which will then update the indexes of all other traces too.\n\nIf you need to check how many traces you have, you can call `model.size()`. The latest trace's index is therefore always `model.size() - 1`.\n\n## Waiting for updates from the model\n\nWhen you call `parse` you have two options. You can `await` it, which will wait until the trace is fully parsed:\n\n```ts\nawait this.model.parse();\n```\n\nBut it's likely preferable to instead use events, to avoid blocking the UI whilst parsing is in progress. You can listen to the `ModelUpdateEvent` for updates:\n\n```ts\nthis.model.addEventListener(Model.ModelUpdateEvent.eventName, event => {\n  const {data} = event as Model.ModelUpdateEvent;\n\n  if (data.data === 'done') {\n    // trace is complete\n    const newestData = this.model.parsedTrace(this.model.size() - 1);\n  } else {\n    // data.data will be an object: { index: X, total: Y}, which represents how many events (X) have been processed out of a total (Y).\n    // This can be used to show a progress bar, for example.\n  }\n})\n```\n\n## The structure of the final data object\n\nThe object returned from `parsedTrace()` is an object of key-value pairs where each key is the name of a handler, and the value is the data that was parsed and returned from that handler.\n\n```ts\n{\n  NetworkRequestHandler: ReturnType<typeof NetworkRequestHandler['data']>,\n  LayoutShiftHandler: ReturnType<typeof LayoutShiftHandler['data']>,\n  // and so on for each enabled Handler\n}\n```\n","publish_time":1719605548977,"maintainers":[{"name":"paulirish","email":"npm@paul.irish"},{"name":"hoten","email":"cjamcl@gmail.com"}],"_cnpm_publish_time":1719605548977,"dist-tags":{"beta":"0.0.67-beta.2","latest":"0.0.68"}}