@oxog/reactscope 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +230 -0
- package/dist/chunk-4TZAECHG.cjs +160 -0
- package/dist/chunk-4TZAECHG.cjs.map +1 -0
- package/dist/chunk-6L3WUS6V.cjs +160 -0
- package/dist/chunk-6L3WUS6V.cjs.map +1 -0
- package/dist/chunk-6LHJL6MV.js +1176 -0
- package/dist/chunk-6LHJL6MV.js.map +1 -0
- package/dist/chunk-ALGKN7FA.cjs +192 -0
- package/dist/chunk-ALGKN7FA.cjs.map +1 -0
- package/dist/chunk-DVRJ7PFB.cjs +185 -0
- package/dist/chunk-DVRJ7PFB.cjs.map +1 -0
- package/dist/chunk-E4MB5KJQ.js +98 -0
- package/dist/chunk-E4MB5KJQ.js.map +1 -0
- package/dist/chunk-H4W4UBLZ.cjs +149 -0
- package/dist/chunk-H4W4UBLZ.cjs.map +1 -0
- package/dist/chunk-I427KZYN.cjs +29 -0
- package/dist/chunk-I427KZYN.cjs.map +1 -0
- package/dist/chunk-IFCS6ABF.cjs +1180 -0
- package/dist/chunk-IFCS6ABF.cjs.map +1 -0
- package/dist/chunk-JESQX3XO.js +157 -0
- package/dist/chunk-JESQX3XO.js.map +1 -0
- package/dist/chunk-KCUWALXE.cjs +252 -0
- package/dist/chunk-KCUWALXE.cjs.map +1 -0
- package/dist/chunk-LZO35OCP.js +26 -0
- package/dist/chunk-LZO35OCP.js.map +1 -0
- package/dist/chunk-M65L55OT.js +146 -0
- package/dist/chunk-M65L55OT.js.map +1 -0
- package/dist/chunk-MPOQNCGP.js +157 -0
- package/dist/chunk-MPOQNCGP.js.map +1 -0
- package/dist/chunk-MTCOZBRI.js +115 -0
- package/dist/chunk-MTCOZBRI.js.map +1 -0
- package/dist/chunk-MVK2UT4Z.js +182 -0
- package/dist/chunk-MVK2UT4Z.js.map +1 -0
- package/dist/chunk-OJGWFS55.cjs +173 -0
- package/dist/chunk-OJGWFS55.cjs.map +1 -0
- package/dist/chunk-QYMRRTSN.cjs +134 -0
- package/dist/chunk-QYMRRTSN.cjs.map +1 -0
- package/dist/chunk-QZ5T7HPO.cjs +118 -0
- package/dist/chunk-QZ5T7HPO.cjs.map +1 -0
- package/dist/chunk-RYAIO3BO.js +131 -0
- package/dist/chunk-RYAIO3BO.js.map +1 -0
- package/dist/chunk-U2OPUU5R.js +170 -0
- package/dist/chunk-U2OPUU5R.js.map +1 -0
- package/dist/chunk-WQRBHD23.cjs +105 -0
- package/dist/chunk-WQRBHD23.cjs.map +1 -0
- package/dist/chunk-XNNGOQH2.js +189 -0
- package/dist/chunk-XNNGOQH2.js.map +1 -0
- package/dist/chunk-ZTO5ZZVE.js +248 -0
- package/dist/chunk-ZTO5ZZVE.js.map +1 -0
- package/dist/index.cjs +1521 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +198 -0
- package/dist/index.d.ts +198 -0
- package/dist/index.js +1381 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/console-reporter/index.cjs +17 -0
- package/dist/plugins/console-reporter/index.cjs.map +1 -0
- package/dist/plugins/console-reporter/index.d.cts +54 -0
- package/dist/plugins/console-reporter/index.d.ts +54 -0
- package/dist/plugins/console-reporter/index.js +4 -0
- package/dist/plugins/console-reporter/index.js.map +1 -0
- package/dist/plugins/dashboard-ui/index.cjs +17 -0
- package/dist/plugins/dashboard-ui/index.cjs.map +1 -0
- package/dist/plugins/dashboard-ui/index.d.cts +68 -0
- package/dist/plugins/dashboard-ui/index.d.ts +68 -0
- package/dist/plugins/dashboard-ui/index.js +4 -0
- package/dist/plugins/dashboard-ui/index.js.map +1 -0
- package/dist/plugins/json-exporter/index.cjs +16 -0
- package/dist/plugins/json-exporter/index.cjs.map +1 -0
- package/dist/plugins/json-exporter/index.d.cts +34 -0
- package/dist/plugins/json-exporter/index.d.ts +34 -0
- package/dist/plugins/json-exporter/index.js +3 -0
- package/dist/plugins/json-exporter/index.js.map +1 -0
- package/dist/plugins/lifecycle-tracker/index.cjs +16 -0
- package/dist/plugins/lifecycle-tracker/index.cjs.map +1 -0
- package/dist/plugins/lifecycle-tracker/index.d.cts +42 -0
- package/dist/plugins/lifecycle-tracker/index.d.ts +42 -0
- package/dist/plugins/lifecycle-tracker/index.js +3 -0
- package/dist/plugins/lifecycle-tracker/index.js.map +1 -0
- package/dist/plugins/memory-tracker/index.cjs +16 -0
- package/dist/plugins/memory-tracker/index.cjs.map +1 -0
- package/dist/plugins/memory-tracker/index.d.cts +56 -0
- package/dist/plugins/memory-tracker/index.d.ts +56 -0
- package/dist/plugins/memory-tracker/index.js +3 -0
- package/dist/plugins/memory-tracker/index.js.map +1 -0
- package/dist/plugins/network-correlator/index.cjs +17 -0
- package/dist/plugins/network-correlator/index.cjs.map +1 -0
- package/dist/plugins/network-correlator/index.d.cts +48 -0
- package/dist/plugins/network-correlator/index.d.ts +48 -0
- package/dist/plugins/network-correlator/index.js +4 -0
- package/dist/plugins/network-correlator/index.js.map +1 -0
- package/dist/plugins/props-differ/index.cjs +16 -0
- package/dist/plugins/props-differ/index.cjs.map +1 -0
- package/dist/plugins/props-differ/index.d.cts +41 -0
- package/dist/plugins/props-differ/index.d.ts +41 -0
- package/dist/plugins/props-differ/index.js +3 -0
- package/dist/plugins/props-differ/index.js.map +1 -0
- package/dist/plugins/render-tracker/index.cjs +16 -0
- package/dist/plugins/render-tracker/index.cjs.map +1 -0
- package/dist/plugins/render-tracker/index.d.cts +36 -0
- package/dist/plugins/render-tracker/index.d.ts +36 -0
- package/dist/plugins/render-tracker/index.js +3 -0
- package/dist/plugins/render-tracker/index.js.map +1 -0
- package/dist/plugins/timeline-recorder/index.cjs +17 -0
- package/dist/plugins/timeline-recorder/index.cjs.map +1 -0
- package/dist/plugins/timeline-recorder/index.d.cts +52 -0
- package/dist/plugins/timeline-recorder/index.d.ts +52 -0
- package/dist/plugins/timeline-recorder/index.js +4 -0
- package/dist/plugins/timeline-recorder/index.js.map +1 -0
- package/dist/plugins/wasted-render-detector/index.cjs +16 -0
- package/dist/plugins/wasted-render-detector/index.cjs.map +1 -0
- package/dist/plugins/wasted-render-detector/index.d.cts +44 -0
- package/dist/plugins/wasted-render-detector/index.d.ts +44 -0
- package/dist/plugins/wasted-render-detector/index.js +3 -0
- package/dist/plugins/wasted-render-detector/index.js.map +1 -0
- package/dist/types-DKPG_emi.d.cts +396 -0
- package/dist/types-DKPG_emi.d.ts +396 -0
- package/package.json +173 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ersinkoc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# ReactScope
|
|
2
|
+
|
|
3
|
+
**Zero-dependency React performance profiler with micro-kernel plugin architecture.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@oxog/reactscope)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
Track renders, detect wasted updates, monitor memory, and optimize your React applications with a powerful micro-kernel plugin architecture.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Zero Dependencies** - No runtime dependencies, everything implemented from scratch
|
|
14
|
+
- **Render Tracking** - Track every render with precise timing and phase detection
|
|
15
|
+
- **Wasted Render Detection** - Automatically detect unnecessary re-renders
|
|
16
|
+
- **Props Diffing** - Deep comparison of props between renders
|
|
17
|
+
- **Memory Monitoring** - Track heap usage and detect potential memory leaks
|
|
18
|
+
- **Network Correlation** - Correlate network requests with component renders
|
|
19
|
+
- **Visual Dashboard** - Real-time monitoring with Shadow DOM isolation
|
|
20
|
+
- **Plugin Architecture** - Extensible micro-kernel design
|
|
21
|
+
- **TypeScript First** - Full type safety with strict mode
|
|
22
|
+
- **Tree Shakeable** - Only include what you use
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @oxog/reactscope
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
yarn add @oxog/reactscope
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm add @oxog/reactscope
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. Wrap your app with the Provider
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { ReactScopeProvider } from '@oxog/reactscope'
|
|
44
|
+
|
|
45
|
+
function App() {
|
|
46
|
+
return (
|
|
47
|
+
<ReactScopeProvider>
|
|
48
|
+
<YourApp />
|
|
49
|
+
</ReactScopeProvider>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. Use the useScope hook to track components
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useScope } from '@oxog/reactscope'
|
|
58
|
+
|
|
59
|
+
function MyComponent({ data }) {
|
|
60
|
+
const scope = useScope('MyComponent')
|
|
61
|
+
|
|
62
|
+
// Access metrics
|
|
63
|
+
console.log('Renders:', scope.renderCount)
|
|
64
|
+
console.log('Avg time:', scope.averageRenderTime)
|
|
65
|
+
|
|
66
|
+
return <div>{data}</div>
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 3. Or wrap components with the Scope component
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import { Scope } from '@oxog/reactscope'
|
|
74
|
+
|
|
75
|
+
function App() {
|
|
76
|
+
return (
|
|
77
|
+
<Scope name="ExpensiveList">
|
|
78
|
+
<ExpensiveList items={items} />
|
|
79
|
+
</Scope>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 4. Enable the Dashboard
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import { ReactScopeProvider, createDashboardUIPlugin } from '@oxog/reactscope'
|
|
88
|
+
|
|
89
|
+
function App() {
|
|
90
|
+
return (
|
|
91
|
+
<ReactScopeProvider
|
|
92
|
+
plugins={[createDashboardUIPlugin()]}
|
|
93
|
+
onReady={(kernel) => {
|
|
94
|
+
// Open with Ctrl+Shift+R or programmatically
|
|
95
|
+
kernel.getPlugin('dashboard-ui')?.api.show()
|
|
96
|
+
}}
|
|
97
|
+
>
|
|
98
|
+
<YourApp />
|
|
99
|
+
</ReactScopeProvider>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## API Reference
|
|
105
|
+
|
|
106
|
+
### Provider
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
<ReactScopeProvider
|
|
110
|
+
enabled={true} // Enable/disable profiling
|
|
111
|
+
plugins={[customPlugin]} // Additional plugins
|
|
112
|
+
onReady={(kernel) => {}} // Callback when ready
|
|
113
|
+
options={{
|
|
114
|
+
trackAllComponents: false, // Auto-track all components
|
|
115
|
+
sampleRate: 1, // 0-1, for performance
|
|
116
|
+
maxHistorySize: 1000, // Limit stored events
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
{children}
|
|
120
|
+
</ReactScopeProvider>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Hooks
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
// Main profiling hook
|
|
127
|
+
const scope = useScope('ComponentName', options)
|
|
128
|
+
scope.renderCount // number
|
|
129
|
+
scope.lastRenderTime // number (ms)
|
|
130
|
+
scope.averageRenderTime // number (ms)
|
|
131
|
+
scope.wastedRenders // number
|
|
132
|
+
|
|
133
|
+
// Read-only metrics hook
|
|
134
|
+
const metrics = useScopeMetrics('ComponentId')
|
|
135
|
+
|
|
136
|
+
// Access kernel directly
|
|
137
|
+
const kernel = useScopeContext()
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### HOC
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
import { withScope } from '@oxog/reactscope'
|
|
144
|
+
|
|
145
|
+
const ProfiledComponent = withScope(MyComponent, {
|
|
146
|
+
name: 'MyComponent',
|
|
147
|
+
trackProps: true,
|
|
148
|
+
onRender: (metrics) => console.log(metrics),
|
|
149
|
+
})
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Plugins
|
|
153
|
+
|
|
154
|
+
ReactScope comes with 10 built-in plugins:
|
|
155
|
+
|
|
156
|
+
| Plugin | Description |
|
|
157
|
+
|--------|-------------|
|
|
158
|
+
| `render-tracker` | Tracks render count and duration |
|
|
159
|
+
| `props-differ` | Analyzes props changes between renders |
|
|
160
|
+
| `wasted-render-detector` | Detects unnecessary re-renders |
|
|
161
|
+
| `lifecycle-tracker` | Tracks mount/unmount events |
|
|
162
|
+
| `console-reporter` | Logs profiling data to console |
|
|
163
|
+
| `json-exporter` | Exports metrics as JSON |
|
|
164
|
+
| `timeline-recorder` | Records events for playback |
|
|
165
|
+
| `memory-tracker` | Monitors memory usage |
|
|
166
|
+
| `network-correlator` | Correlates renders with network requests |
|
|
167
|
+
| `dashboard-ui` | Visual overlay panel |
|
|
168
|
+
|
|
169
|
+
### Creating Custom Plugins
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
import { createPlugin } from '@oxog/reactscope'
|
|
173
|
+
|
|
174
|
+
const myPlugin = createPlugin({
|
|
175
|
+
name: 'my-plugin',
|
|
176
|
+
version: '1.0.0',
|
|
177
|
+
hooks: {
|
|
178
|
+
onRender: (event) => {
|
|
179
|
+
console.log(`${event.componentName} rendered in ${event.duration}ms`)
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
api: {
|
|
183
|
+
customMethod: () => 'custom data',
|
|
184
|
+
},
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Programmatic API
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
import { getKernel, getMetrics, exportMetrics, clearMetrics } from '@oxog/reactscope'
|
|
192
|
+
|
|
193
|
+
// Access global kernel
|
|
194
|
+
const kernel = getKernel()
|
|
195
|
+
|
|
196
|
+
// Get metrics store
|
|
197
|
+
const metrics = getMetrics()
|
|
198
|
+
|
|
199
|
+
// Export as JSON
|
|
200
|
+
const json = exportMetrics()
|
|
201
|
+
|
|
202
|
+
// Clear all data
|
|
203
|
+
clearMetrics()
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Browser Support
|
|
207
|
+
|
|
208
|
+
- Chrome 80+
|
|
209
|
+
- Firefox 75+
|
|
210
|
+
- Safari 14+
|
|
211
|
+
- Edge 80+
|
|
212
|
+
|
|
213
|
+
## Performance
|
|
214
|
+
|
|
215
|
+
ReactScope is designed to have minimal impact on your application:
|
|
216
|
+
|
|
217
|
+
- Uses `requestIdleCallback` for non-critical updates
|
|
218
|
+
- Batches metric updates
|
|
219
|
+
- Throttles dashboard renders
|
|
220
|
+
- Supports sampling rate for high-frequency renders
|
|
221
|
+
- Lazy initializes plugins
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
MIT
|
|
226
|
+
|
|
227
|
+
## Links
|
|
228
|
+
|
|
229
|
+
- [Documentation](https://reactscope.oxog.dev)
|
|
230
|
+
- [GitHub Repository](https://github.com/ersinkoc/reactscope)
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/plugins/wasted-render-detector/wasted-render-detector.ts
|
|
4
|
+
var WastedRenderDetectorPlugin = class {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.name = "wasted-render-detector";
|
|
7
|
+
this.version = "1.0.0";
|
|
8
|
+
this.kernel = null;
|
|
9
|
+
this.wastedRenders = /* @__PURE__ */ new Map();
|
|
10
|
+
this.lastPropsChange = /* @__PURE__ */ new Map();
|
|
11
|
+
this.maxHistorySize = 100;
|
|
12
|
+
this.api = {
|
|
13
|
+
getWastedRenders: (componentId) => {
|
|
14
|
+
return this.wastedRenders.get(componentId) ?? [];
|
|
15
|
+
},
|
|
16
|
+
getWastedRenderCount: (componentId) => {
|
|
17
|
+
return this.wastedRenders.get(componentId)?.length ?? 0;
|
|
18
|
+
},
|
|
19
|
+
getWastedRenderPercentage: (componentId) => {
|
|
20
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
21
|
+
if (!metrics || metrics.renderCount === 0) {
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
24
|
+
const wastedCount = this.wastedRenders.get(componentId)?.length ?? 0;
|
|
25
|
+
return wastedCount / metrics.renderCount * 100;
|
|
26
|
+
},
|
|
27
|
+
getAllWastedRenders: () => {
|
|
28
|
+
return new Map(this.wastedRenders);
|
|
29
|
+
},
|
|
30
|
+
getMostWastefulComponents: (limit = 10) => {
|
|
31
|
+
const stats = [];
|
|
32
|
+
for (const [componentId, wasted] of this.wastedRenders) {
|
|
33
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
34
|
+
if (!metrics) continue;
|
|
35
|
+
stats.push({
|
|
36
|
+
componentId,
|
|
37
|
+
componentName: metrics.componentName,
|
|
38
|
+
wastedCount: wasted.length,
|
|
39
|
+
wastedPercentage: metrics.renderCount > 0 ? wasted.length / metrics.renderCount * 100 : 0
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return stats.sort((a, b) => b.wastedCount - a.wastedCount).slice(0, limit);
|
|
43
|
+
},
|
|
44
|
+
clearWastedRenders: (componentId) => {
|
|
45
|
+
if (componentId) {
|
|
46
|
+
this.wastedRenders.delete(componentId);
|
|
47
|
+
} else {
|
|
48
|
+
this.wastedRenders.clear();
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
markAsWasted: (componentId, reason, details) => {
|
|
52
|
+
this.recordWastedRender(componentId, reason, details ?? "");
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
this.hooks = {
|
|
56
|
+
onRender: (event) => {
|
|
57
|
+
this.checkForWastedRender(event);
|
|
58
|
+
},
|
|
59
|
+
onPropsChange: (event) => {
|
|
60
|
+
this.lastPropsChange.set(event.componentId, event.timestamp);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
install(kernel) {
|
|
65
|
+
this.kernel = kernel;
|
|
66
|
+
this.maxHistorySize = kernel.getOptions().maxHistorySize ?? 100;
|
|
67
|
+
}
|
|
68
|
+
uninstall() {
|
|
69
|
+
this.kernel = null;
|
|
70
|
+
this.wastedRenders.clear();
|
|
71
|
+
this.lastPropsChange.clear();
|
|
72
|
+
}
|
|
73
|
+
checkForWastedRender(event) {
|
|
74
|
+
if (!this.kernel) return;
|
|
75
|
+
if (event.phase === "mount") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const lastChange = this.lastPropsChange.get(event.componentId);
|
|
79
|
+
const timeWindow = 5;
|
|
80
|
+
if (!lastChange || event.timestamp - lastChange > timeWindow) ;
|
|
81
|
+
}
|
|
82
|
+
recordWastedRender(componentId, reason, details) {
|
|
83
|
+
if (!this.kernel) return;
|
|
84
|
+
const metrics = this.kernel.getMetrics().get(componentId);
|
|
85
|
+
const wastedRender = {
|
|
86
|
+
timestamp: Date.now(),
|
|
87
|
+
reason,
|
|
88
|
+
details,
|
|
89
|
+
componentId,
|
|
90
|
+
componentName: metrics?.componentName ?? "Unknown"
|
|
91
|
+
};
|
|
92
|
+
let wasted = this.wastedRenders.get(componentId);
|
|
93
|
+
if (!wasted) {
|
|
94
|
+
wasted = [];
|
|
95
|
+
this.wastedRenders.set(componentId, wasted);
|
|
96
|
+
}
|
|
97
|
+
wasted.push(wastedRender);
|
|
98
|
+
if (wasted.length > this.maxHistorySize) {
|
|
99
|
+
this.wastedRenders.set(componentId, wasted.slice(-this.maxHistorySize));
|
|
100
|
+
}
|
|
101
|
+
this.kernel.updateMetrics(componentId, {
|
|
102
|
+
wastedRenderCount: (metrics?.wastedRenderCount ?? 0) + 1
|
|
103
|
+
});
|
|
104
|
+
if (metrics && metrics.renderHistory.length > 0) {
|
|
105
|
+
const lastRecord = metrics.renderHistory[metrics.renderHistory.length - 1];
|
|
106
|
+
if (lastRecord) {
|
|
107
|
+
lastRecord.wasWasted = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Check if the last render was wasted based on props comparison.
|
|
113
|
+
*/
|
|
114
|
+
checkAndMarkWasted(componentId, prevProps, nextProps, prevState, nextState) {
|
|
115
|
+
if (prevProps === nextProps || this.shallowEqual(prevProps, nextProps)) {
|
|
116
|
+
if (prevState === void 0 && nextState === void 0) {
|
|
117
|
+
this.recordWastedRender(componentId, "props-unchanged", "Props are shallow equal");
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
if (prevState === nextState || this.shallowEqual(prevState, nextState)) {
|
|
121
|
+
this.recordWastedRender(
|
|
122
|
+
componentId,
|
|
123
|
+
"props-unchanged",
|
|
124
|
+
"Both props and state are shallow equal"
|
|
125
|
+
);
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
shallowEqual(a, b) {
|
|
132
|
+
if (Object.is(a, b)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const keysA = Object.keys(a);
|
|
139
|
+
const keysB = Object.keys(b);
|
|
140
|
+
if (keysA.length !== keysB.length) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
const objA = a;
|
|
144
|
+
const objB = b;
|
|
145
|
+
for (const key of keysA) {
|
|
146
|
+
if (!Object.prototype.hasOwnProperty.call(objB, key) || !Object.is(objA[key], objB[key])) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
function createWastedRenderDetectorPlugin() {
|
|
154
|
+
return new WastedRenderDetectorPlugin();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
exports.WastedRenderDetectorPlugin = WastedRenderDetectorPlugin;
|
|
158
|
+
exports.createWastedRenderDetectorPlugin = createWastedRenderDetectorPlugin;
|
|
159
|
+
//# sourceMappingURL=chunk-4TZAECHG.cjs.map
|
|
160
|
+
//# sourceMappingURL=chunk-4TZAECHG.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugins/wasted-render-detector/wasted-render-detector.ts"],"names":[],"mappings":";;;AAyBO,IAAM,6BAAN,MAAmD;AAAA,EAAnD,WAAA,GAA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cesContent":["import type {\n Kernel,\n Plugin,\n RenderEvent,\n PropsChangeEvent,\n WastedRender,\n WastedRenderStats,\n WastedRenderReason,\n} from '../../types'\n\nexport interface WastedRenderDetectorAPI {\n [key: string]: unknown\n getWastedRenders(componentId: string): WastedRender[]\n getWastedRenderCount(componentId: string): number\n getWastedRenderPercentage(componentId: string): number\n getAllWastedRenders(): Map<string, WastedRender[]>\n getMostWastefulComponents(limit?: number): WastedRenderStats[]\n clearWastedRenders(componentId?: string): void\n markAsWasted(componentId: string, reason: WastedRenderReason, details?: string): void\n}\n\n/**\n * Wasted Render Detector Plugin\n * Detects renders where props/state didn't actually change.\n */\nexport class WastedRenderDetectorPlugin implements Plugin {\n readonly name = 'wasted-render-detector'\n readonly version = '1.0.0'\n\n private kernel: Kernel | null = null\n private wastedRenders = new Map<string, WastedRender[]>()\n private lastPropsChange = new Map<string, number>()\n private maxHistorySize = 100\n\n readonly api: WastedRenderDetectorAPI = {\n getWastedRenders: (componentId: string) => {\n return this.wastedRenders.get(componentId) ?? []\n },\n\n getWastedRenderCount: (componentId: string) => {\n return this.wastedRenders.get(componentId)?.length ?? 0\n },\n\n getWastedRenderPercentage: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n if (!metrics || metrics.renderCount === 0) {\n return 0\n }\n const wastedCount = this.wastedRenders.get(componentId)?.length ?? 0\n return (wastedCount / metrics.renderCount) * 100\n },\n\n getAllWastedRenders: () => {\n return new Map(this.wastedRenders)\n },\n\n getMostWastefulComponents: (limit = 10) => {\n const stats: WastedRenderStats[] = []\n\n for (const [componentId, wasted] of this.wastedRenders) {\n const metrics = this.kernel?.getMetrics().get(componentId)\n if (!metrics) continue\n\n stats.push({\n componentId,\n componentName: metrics.componentName,\n wastedCount: wasted.length,\n wastedPercentage: metrics.renderCount > 0 ? (wasted.length / metrics.renderCount) * 100 : 0,\n })\n }\n\n return stats.sort((a, b) => b.wastedCount - a.wastedCount).slice(0, limit)\n },\n\n clearWastedRenders: (componentId?: string) => {\n if (componentId) {\n this.wastedRenders.delete(componentId)\n } else {\n this.wastedRenders.clear()\n }\n },\n\n markAsWasted: (componentId: string, reason: WastedRenderReason, details?: string) => {\n this.recordWastedRender(componentId, reason, details ?? '')\n },\n }\n\n readonly hooks = {\n onRender: (event: RenderEvent) => {\n this.checkForWastedRender(event)\n },\n\n onPropsChange: (event: PropsChangeEvent) => {\n this.lastPropsChange.set(event.componentId, event.timestamp)\n },\n }\n\n install(kernel: Kernel): void {\n this.kernel = kernel\n this.maxHistorySize = kernel.getOptions().maxHistorySize ?? 100\n }\n\n uninstall(): void {\n this.kernel = null\n this.wastedRenders.clear()\n this.lastPropsChange.clear()\n }\n\n private checkForWastedRender(event: RenderEvent): void {\n if (!this.kernel) return\n\n // Skip mount phase - first render is never wasted\n if (event.phase === 'mount') {\n return\n }\n\n const lastChange = this.lastPropsChange.get(event.componentId)\n\n // If there's been no props change since the last render, this might be wasted\n // We use a small time window to account for timing differences\n const timeWindow = 5 // ms\n\n if (!lastChange || event.timestamp - lastChange > timeWindow) {\n // No recent props change - this render might be wasted\n // However, state changes or context changes could also cause renders\n // For now, we'll be conservative and only mark as wasted if explicitly told\n // or if we can verify props were unchanged\n\n // We'll mark this as potentially wasted based on no recent props changes\n // The actual determination should be done at the component level\n }\n }\n\n private recordWastedRender(\n componentId: string,\n reason: WastedRenderReason,\n details: string\n ): void {\n if (!this.kernel) return\n\n const metrics = this.kernel.getMetrics().get(componentId)\n\n const wastedRender: WastedRender = {\n timestamp: Date.now(),\n reason,\n details,\n componentId,\n componentName: metrics?.componentName ?? 'Unknown',\n }\n\n let wasted = this.wastedRenders.get(componentId)\n if (!wasted) {\n wasted = []\n this.wastedRenders.set(componentId, wasted)\n }\n\n wasted.push(wastedRender)\n\n // Limit history size\n if (wasted.length > this.maxHistorySize) {\n this.wastedRenders.set(componentId, wasted.slice(-this.maxHistorySize))\n }\n\n // Update metrics\n this.kernel.updateMetrics(componentId, {\n wastedRenderCount: (metrics?.wastedRenderCount ?? 0) + 1,\n })\n\n // Update render history to mark as wasted\n if (metrics && metrics.renderHistory.length > 0) {\n const lastRecord = metrics.renderHistory[metrics.renderHistory.length - 1]\n if (lastRecord) {\n lastRecord.wasWasted = true\n }\n }\n }\n\n /**\n * Check if the last render was wasted based on props comparison.\n */\n checkAndMarkWasted(\n componentId: string,\n prevProps: unknown,\n nextProps: unknown,\n prevState?: unknown,\n nextState?: unknown\n ): boolean {\n // Check props\n if (prevProps === nextProps || this.shallowEqual(prevProps, nextProps)) {\n // Check state if provided\n if (prevState === undefined && nextState === undefined) {\n this.recordWastedRender(componentId, 'props-unchanged', 'Props are shallow equal')\n return true\n }\n\n if (prevState === nextState || this.shallowEqual(prevState, nextState)) {\n this.recordWastedRender(\n componentId,\n 'props-unchanged',\n 'Both props and state are shallow equal'\n )\n return true\n }\n }\n\n return false\n }\n\n private shallowEqual(a: unknown, b: unknown): boolean {\n if (Object.is(a, b)) {\n return true\n }\n\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {\n return false\n }\n\n const keysA = Object.keys(a as object)\n const keysB = Object.keys(b as object)\n\n if (keysA.length !== keysB.length) {\n return false\n }\n\n const objA = a as Record<string, unknown>\n const objB = b as Record<string, unknown>\n\n for (const key of keysA) {\n if (!Object.prototype.hasOwnProperty.call(objB, key) || !Object.is(objA[key], objB[key])) {\n return false\n }\n }\n\n return true\n }\n}\n\n/**\n * Create a new wasted render detector plugin instance.\n */\nexport function createWastedRenderDetectorPlugin(): WastedRenderDetectorPlugin {\n return new WastedRenderDetectorPlugin()\n}\n"]}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/plugins/lifecycle-tracker/lifecycle-tracker.ts
|
|
4
|
+
var LifecycleTrackerPlugin = class {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.name = "lifecycle-tracker";
|
|
7
|
+
this.version = "1.0.0";
|
|
8
|
+
this.kernel = null;
|
|
9
|
+
this.lifecycleHistory = /* @__PURE__ */ new Map();
|
|
10
|
+
this.maxHistorySize = 100;
|
|
11
|
+
this.api = {
|
|
12
|
+
getMountTime: (componentId) => {
|
|
13
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
14
|
+
return metrics?.mountTime ?? null;
|
|
15
|
+
},
|
|
16
|
+
getUnmountTime: (componentId) => {
|
|
17
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
18
|
+
return metrics?.unmountTime ?? null;
|
|
19
|
+
},
|
|
20
|
+
getLifetime: (componentId) => {
|
|
21
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
22
|
+
if (!metrics?.mountTime) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (metrics.unmountTime) {
|
|
26
|
+
return metrics.unmountTime - metrics.mountTime;
|
|
27
|
+
}
|
|
28
|
+
if (metrics.isCurrentlyMounted) {
|
|
29
|
+
return Date.now() - metrics.mountTime;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
},
|
|
33
|
+
getMountCount: (componentId) => {
|
|
34
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
35
|
+
return metrics?.mountCount ?? 0;
|
|
36
|
+
},
|
|
37
|
+
isCurrentlyMounted: (componentId) => {
|
|
38
|
+
const metrics = this.kernel?.getMetrics().get(componentId);
|
|
39
|
+
return metrics?.isCurrentlyMounted ?? false;
|
|
40
|
+
},
|
|
41
|
+
getLifecycleHistory: (componentId) => {
|
|
42
|
+
return this.lifecycleHistory.get(componentId)?.events ?? [];
|
|
43
|
+
},
|
|
44
|
+
getMountedComponents: () => {
|
|
45
|
+
const mounted = [];
|
|
46
|
+
const allMetrics = this.kernel?.getMetrics().getAll();
|
|
47
|
+
if (allMetrics) {
|
|
48
|
+
for (const [id, metrics] of allMetrics) {
|
|
49
|
+
if (metrics.isCurrentlyMounted) {
|
|
50
|
+
mounted.push(id);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return mounted;
|
|
55
|
+
},
|
|
56
|
+
getAverageLifetime: (componentId) => {
|
|
57
|
+
const history = this.lifecycleHistory.get(componentId);
|
|
58
|
+
if (!history || history.lifetimes.length === 0) {
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
const sum = history.lifetimes.reduce((a, b) => a + b, 0);
|
|
62
|
+
return sum / history.lifetimes.length;
|
|
63
|
+
},
|
|
64
|
+
getAllMountCounts: () => {
|
|
65
|
+
const counts = /* @__PURE__ */ new Map();
|
|
66
|
+
const allMetrics = this.kernel?.getMetrics().getAll();
|
|
67
|
+
if (allMetrics) {
|
|
68
|
+
for (const [id, metrics] of allMetrics) {
|
|
69
|
+
counts.set(id, metrics.mountCount);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return counts;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
this.hooks = {
|
|
76
|
+
onMount: (event) => {
|
|
77
|
+
this.trackMount(event);
|
|
78
|
+
},
|
|
79
|
+
onUnmount: (event) => {
|
|
80
|
+
this.trackUnmount(event);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
install(kernel) {
|
|
85
|
+
this.kernel = kernel;
|
|
86
|
+
this.maxHistorySize = kernel.getOptions().maxHistorySize ?? 100;
|
|
87
|
+
}
|
|
88
|
+
uninstall() {
|
|
89
|
+
this.kernel = null;
|
|
90
|
+
this.lifecycleHistory.clear();
|
|
91
|
+
}
|
|
92
|
+
trackMount(event) {
|
|
93
|
+
if (!this.kernel) return;
|
|
94
|
+
const metrics = this.kernel.getMetrics().get(event.componentId);
|
|
95
|
+
this.kernel.updateMetrics(event.componentId, {
|
|
96
|
+
componentName: event.componentName,
|
|
97
|
+
mountTime: event.timestamp,
|
|
98
|
+
unmountTime: null,
|
|
99
|
+
isCurrentlyMounted: true,
|
|
100
|
+
mountCount: (metrics?.mountCount ?? 0) + 1,
|
|
101
|
+
parentId: event.parentId
|
|
102
|
+
});
|
|
103
|
+
this.kernel.getMetrics().updateTree(event.componentId, event.componentName, event.parentId);
|
|
104
|
+
this.recordLifecycleEvent(event.componentId, {
|
|
105
|
+
type: "mount",
|
|
106
|
+
timestamp: event.timestamp,
|
|
107
|
+
componentId: event.componentId,
|
|
108
|
+
componentName: event.componentName
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
trackUnmount(event) {
|
|
112
|
+
if (!this.kernel) return;
|
|
113
|
+
const metrics = this.kernel.getMetrics().get(event.componentId);
|
|
114
|
+
if (metrics?.mountTime) {
|
|
115
|
+
const lifetime = event.timestamp - metrics.mountTime;
|
|
116
|
+
this.recordLifetime(event.componentId, lifetime);
|
|
117
|
+
}
|
|
118
|
+
this.kernel.updateMetrics(event.componentId, {
|
|
119
|
+
unmountTime: event.timestamp,
|
|
120
|
+
isCurrentlyMounted: false
|
|
121
|
+
});
|
|
122
|
+
this.kernel.getMetrics().removeFromTree(event.componentId);
|
|
123
|
+
this.recordLifecycleEvent(event.componentId, {
|
|
124
|
+
type: "unmount",
|
|
125
|
+
timestamp: event.timestamp,
|
|
126
|
+
componentId: event.componentId,
|
|
127
|
+
componentName: event.componentName
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
recordLifecycleEvent(componentId, event) {
|
|
131
|
+
let history = this.lifecycleHistory.get(componentId);
|
|
132
|
+
if (!history) {
|
|
133
|
+
history = { events: [], lifetimes: [] };
|
|
134
|
+
this.lifecycleHistory.set(componentId, history);
|
|
135
|
+
}
|
|
136
|
+
history.events.push(event);
|
|
137
|
+
if (history.events.length > this.maxHistorySize) {
|
|
138
|
+
history.events = history.events.slice(-this.maxHistorySize);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
recordLifetime(componentId, lifetime) {
|
|
142
|
+
let history = this.lifecycleHistory.get(componentId);
|
|
143
|
+
if (!history) {
|
|
144
|
+
history = { events: [], lifetimes: [] };
|
|
145
|
+
this.lifecycleHistory.set(componentId, history);
|
|
146
|
+
}
|
|
147
|
+
history.lifetimes.push(lifetime);
|
|
148
|
+
if (history.lifetimes.length > this.maxHistorySize) {
|
|
149
|
+
history.lifetimes = history.lifetimes.slice(-this.maxHistorySize);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
function createLifecycleTrackerPlugin() {
|
|
154
|
+
return new LifecycleTrackerPlugin();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
exports.LifecycleTrackerPlugin = LifecycleTrackerPlugin;
|
|
158
|
+
exports.createLifecycleTrackerPlugin = createLifecycleTrackerPlugin;
|
|
159
|
+
//# sourceMappingURL=chunk-6L3WUS6V.cjs.map
|
|
160
|
+
//# sourceMappingURL=chunk-6L3WUS6V.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugins/lifecycle-tracker/lifecycle-tracker.ts"],"names":[],"mappings":";;;AA8BO,IAAM,yBAAN,MAA+C;AAAA,EAA/C,WAAA,GAAA;AACL,IAAA,IAAA,CAAS,IAAA,GAAO,mBAAA;AAChB,IAAA,IAAA,CAAS,OAAA,GAAU,OAAA;AAEnB,IAAA,IAAA,CAAQ,MAAA,GAAwB,IAAA;AAChC,IAAA,IAAA,CAAQ,gBAAA,uBAAuB,GAAA,EAA8B;AAC7D,IAAA,IAAA,CAAQ,cAAA,GAAiB,GAAA;AAEzB,IAAA,IAAA,CAAS,GAAA,GAA2B;AAAA,MAClC,YAAA,EAAc,CAAC,WAAA,KAAwB;AACrC,QAAA,MAAM,UAAU,IAAA,CAAK,MAAA,EAAQ,UAAA,EAAW,CAAE,IAAI,WAAW,CAAA;AACzD,QAAA,OAAO,SAAS,SAAA,IAAa,IAAA;AAAA,MAC/B,CAAA;AAAA,MAEA,cAAA,EAAgB,CAAC,WAAA,KAAwB;AACvC,QAAA,MAAM,UAAU,IAAA,CAAK,MAAA,EAAQ,UAAA,EAAW,CAAE,IAAI,WAAW,CAAA;AACzD,QAAA,OAAO,SAAS,WAAA,IAAe,IA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AAS,IAAA,CAAK,cAAA,EAAgB;AAC/C,MAAA,OAAA,CAAQ,SAAS,OAAA,CAAQ,MAAA,CAAO,KAAA,CAAM,CAAC,KAAK,cAAc,CAAA;AAAA,IAC5D;AAAA,EACF;AAAA,EAEQ,cAAA,CAAe,aAAqB,QAAA,EAAwB;AAClE,IAAA,IAAI,OAAA,GAAU,IAAA,CAAK,gBAAA,CAAiB,GAAA,CAAI,WAAW,CAAA;AACnD,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA,OAAA,GAAU,EAAE,MAAA,EAAQ,EAAC,EAAG,SAAA,EAAW,EAAC,EAAE;AACtC,MAAA,IAAA,CAAK,gBAAA,CAAiB,GAAA,CAAI,WAAA,EAAa,OAAO,CAAA;AAAA,IAChD;AAEA,IAAA,OAAA,CAAQ,SAAA,CAAU,KAAK,QAAQ,CAAA;AAG/B,IAAA,IAAI,OAAA,CAAQ,SAAA,CAAU,MAAA,GAAS,IAAA,CAAK,cAAA,EAAgB;AAClD,MAAA,OAAA,CAAQ,YAAY,OAAA,CAAQ,SAAA,CAAU,KAAA,CAAM,CAAC,KAAK,cAAc,CAAA;AAAA,IAClE;AAAA,EACF;AACF;AAKO,SAAS,4BAAA,GAAuD;AACrE,EAAA,OAAO,IAAI,sBAAA,EAAuB;AACpC","file":"chunk-6L3WUS6V.cjs","sourcesContent":["import type {\n Kernel,\n Plugin,\n MountEvent,\n UnmountEvent,\n LifecycleEvent,\n} from '../../types'\n\nexport interface LifecycleTrackerAPI {\n [key: string]: unknown\n getMountTime(componentId: string): number | null\n getUnmountTime(componentId: string): number | null\n getLifetime(componentId: string): number | null\n getMountCount(componentId: string): number\n isCurrentlyMounted(componentId: string): boolean\n getLifecycleHistory(componentId: string): LifecycleEvent[]\n getMountedComponents(): string[]\n getAverageLifetime(componentId: string): number\n getAllMountCounts(): Map<string, number>\n}\n\ninterface LifecycleHistory {\n events: LifecycleEvent[]\n lifetimes: number[]\n}\n\n/**\n * Lifecycle Tracker Plugin\n * Tracks mount/unmount events and component lifecycle.\n */\nexport class LifecycleTrackerPlugin implements Plugin {\n readonly name = 'lifecycle-tracker'\n readonly version = '1.0.0'\n\n private kernel: Kernel | null = null\n private lifecycleHistory = new Map<string, LifecycleHistory>()\n private maxHistorySize = 100\n\n readonly api: LifecycleTrackerAPI = {\n getMountTime: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n return metrics?.mountTime ?? null\n },\n\n getUnmountTime: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n return metrics?.unmountTime ?? null\n },\n\n getLifetime: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n if (!metrics?.mountTime) {\n return null\n }\n\n if (metrics.unmountTime) {\n return metrics.unmountTime - metrics.mountTime\n }\n\n // Still mounted\n if (metrics.isCurrentlyMounted) {\n return Date.now() - metrics.mountTime\n }\n\n return null\n },\n\n getMountCount: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n return metrics?.mountCount ?? 0\n },\n\n isCurrentlyMounted: (componentId: string) => {\n const metrics = this.kernel?.getMetrics().get(componentId)\n return metrics?.isCurrentlyMounted ?? false\n },\n\n getLifecycleHistory: (componentId: string) => {\n return this.lifecycleHistory.get(componentId)?.events ?? []\n },\n\n getMountedComponents: () => {\n const mounted: string[] = []\n const allMetrics = this.kernel?.getMetrics().getAll()\n\n if (allMetrics) {\n for (const [id, metrics] of allMetrics) {\n if (metrics.isCurrentlyMounted) {\n mounted.push(id)\n }\n }\n }\n\n return mounted\n },\n\n getAverageLifetime: (componentId: string) => {\n const history = this.lifecycleHistory.get(componentId)\n if (!history || history.lifetimes.length === 0) {\n return 0\n }\n\n const sum = history.lifetimes.reduce((a, b) => a + b, 0)\n return sum / history.lifetimes.length\n },\n\n getAllMountCounts: () => {\n const counts = new Map<string, number>()\n const allMetrics = this.kernel?.getMetrics().getAll()\n\n if (allMetrics) {\n for (const [id, metrics] of allMetrics) {\n counts.set(id, metrics.mountCount)\n }\n }\n\n return counts\n },\n }\n\n readonly hooks = {\n onMount: (event: MountEvent) => {\n this.trackMount(event)\n },\n\n onUnmount: (event: UnmountEvent) => {\n this.trackUnmount(event)\n },\n }\n\n install(kernel: Kernel): void {\n this.kernel = kernel\n this.maxHistorySize = kernel.getOptions().maxHistorySize ?? 100\n }\n\n uninstall(): void {\n this.kernel = null\n this.lifecycleHistory.clear()\n }\n\n private trackMount(event: MountEvent): void {\n if (!this.kernel) return\n\n const metrics = this.kernel.getMetrics().get(event.componentId)\n\n // Update metrics\n this.kernel.updateMetrics(event.componentId, {\n componentName: event.componentName,\n mountTime: event.timestamp,\n unmountTime: null,\n isCurrentlyMounted: true,\n mountCount: (metrics?.mountCount ?? 0) + 1,\n parentId: event.parentId,\n })\n\n // Update tree\n this.kernel.getMetrics().updateTree(event.componentId, event.componentName, event.parentId)\n\n // Record lifecycle event\n this.recordLifecycleEvent(event.componentId, {\n type: 'mount',\n timestamp: event.timestamp,\n componentId: event.componentId,\n componentName: event.componentName,\n })\n }\n\n private trackUnmount(event: UnmountEvent): void {\n if (!this.kernel) return\n\n const metrics = this.kernel.getMetrics().get(event.componentId)\n\n // Calculate lifetime if mount time is known\n if (metrics?.mountTime) {\n const lifetime = event.timestamp - metrics.mountTime\n this.recordLifetime(event.componentId, lifetime)\n }\n\n // Update metrics\n this.kernel.updateMetrics(event.componentId, {\n unmountTime: event.timestamp,\n isCurrentlyMounted: false,\n })\n\n // Remove from tree\n this.kernel.getMetrics().removeFromTree(event.componentId)\n\n // Record lifecycle event\n this.recordLifecycleEvent(event.componentId, {\n type: 'unmount',\n timestamp: event.timestamp,\n componentId: event.componentId,\n componentName: event.componentName,\n })\n }\n\n private recordLifecycleEvent(componentId: string, event: LifecycleEvent): void {\n let history = this.lifecycleHistory.get(componentId)\n if (!history) {\n history = { events: [], lifetimes: [] }\n this.lifecycleHistory.set(componentId, history)\n }\n\n history.events.push(event)\n\n // Limit history size\n if (history.events.length > this.maxHistorySize) {\n history.events = history.events.slice(-this.maxHistorySize)\n }\n }\n\n private recordLifetime(componentId: string, lifetime: number): void {\n let history = this.lifecycleHistory.get(componentId)\n if (!history) {\n history = { events: [], lifetimes: [] }\n this.lifecycleHistory.set(componentId, history)\n }\n\n history.lifetimes.push(lifetime)\n\n // Limit history size\n if (history.lifetimes.length > this.maxHistorySize) {\n history.lifetimes = history.lifetimes.slice(-this.maxHistorySize)\n }\n }\n}\n\n/**\n * Create a new lifecycle tracker plugin instance.\n */\nexport function createLifecycleTrackerPlugin(): LifecycleTrackerPlugin {\n return new LifecycleTrackerPlugin()\n}\n"]}
|