|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow strict |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +export type Timestamp = number; |
| 14 | +export type PerformanceEntryType = 'event'; |
| 15 | + |
| 16 | +export class PerformanceEntry { |
| 17 | + name: string; |
| 18 | + entryType: PerformanceEntryType; |
| 19 | + startTime: Timestamp; |
| 20 | + duration: number; |
| 21 | + |
| 22 | + // For "event" entries only: |
| 23 | + processingStart: ?Timestamp; |
| 24 | + processingEnd: ?Timestamp; |
| 25 | + interactionId: ?number; |
| 26 | + |
| 27 | + // $FlowIgnore: Flow(unclear-type) |
| 28 | + toJSON(): Object { |
| 29 | + return { |
| 30 | + name: this.name, |
| 31 | + entryType: this.entryType, |
| 32 | + startTime: this.startTime, |
| 33 | + duration: this.duration, |
| 34 | + }; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export type PerformanceEntryList = $ReadOnlyArray<PerformanceEntry>; |
| 39 | + |
| 40 | +export class PerformanceObserverEntryList { |
| 41 | + _entries: PerformanceEntryList; |
| 42 | + |
| 43 | + constructor(entries: PerformanceEntryList) { |
| 44 | + this._entries = entries; |
| 45 | + } |
| 46 | + |
| 47 | + getEntries(): PerformanceEntryList { |
| 48 | + return this._entries; |
| 49 | + } |
| 50 | + |
| 51 | + getEntriesByType(type: PerformanceEntryType): PerformanceEntryList { |
| 52 | + return this._entries.filter(entry => entry.entryType === type); |
| 53 | + } |
| 54 | + |
| 55 | + getEntriesByName( |
| 56 | + name: string, |
| 57 | + type?: PerformanceEntryType, |
| 58 | + ): PerformanceEntryList { |
| 59 | + if (type === undefined) { |
| 60 | + return this._entries.filter(entry => entry.name === name); |
| 61 | + } else { |
| 62 | + return this._entries.filter( |
| 63 | + entry => entry.name === name && entry.entryType === type, |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export type PerformanceObserverCallback = ( |
| 70 | + list: PerformanceObserverEntryList, |
| 71 | + observer: PerformanceObserver, |
| 72 | +) => void; |
| 73 | + |
| 74 | +export type PerformanceObserverInit = |
| 75 | + | { |
| 76 | + entryTypes: PerformanceEntryType[], |
| 77 | + buffered?: boolean, |
| 78 | + } |
| 79 | + | { |
| 80 | + type: PerformanceEntryType, |
| 81 | + buffered?: boolean, |
| 82 | + }; |
| 83 | + |
| 84 | +/** |
| 85 | + * Implementation of the PerformanceObserver interface for RN, |
| 86 | + * corresponding to the standard in https://www.w3.org/TR/performance-timeline/ |
| 87 | + * |
| 88 | + * @example |
| 89 | + * const observer = new PerformanceObserver((list, _observer) => { |
| 90 | + * const entries = list.getEntries(); |
| 91 | + * entries.forEach(entry => { |
| 92 | + * reportEvent({ |
| 93 | + * eventName: entry.name, |
| 94 | + * startTime: entry.startTime, |
| 95 | + * endTime: entry.startTime + entry.duration, |
| 96 | + * processingStart: entry.processingStart, |
| 97 | + * processingEnd: entry.processingEnd, |
| 98 | + * interactionId: entry.interactionId, |
| 99 | + * }); |
| 100 | + * }); |
| 101 | + * }); |
| 102 | + * observer.observe({ type: "event" }); |
| 103 | + */ |
| 104 | +export default class PerformanceObserver { |
| 105 | + _callback: PerformanceObserverCallback; |
| 106 | + |
| 107 | + constructor(callback: PerformanceObserverCallback) { |
| 108 | + this._callback = callback; |
| 109 | + } |
| 110 | + |
| 111 | + observe(options: PerformanceObserverInit) { |
| 112 | + console.log('PerformanceObserver: started observing'); |
| 113 | + } |
| 114 | + |
| 115 | + disconnect(): void { |
| 116 | + console.log('PerformanceObserver: stopped observing'); |
| 117 | + } |
| 118 | + |
| 119 | + takeRecords(): PerformanceEntryList { |
| 120 | + return []; |
| 121 | + } |
| 122 | + |
| 123 | + static supportedEntryTypes: PerformanceEntryType[] = ['event']; |
| 124 | +} |
0 commit comments