-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
import { Observable, mergeMap } from "rxjs"; | ||
import { Observable, mergeMap, scan } from "rxjs"; | ||
import fromPerformanceObserver from "./fromPerformanceObserver"; | ||
|
||
// TODO: refactor to expose all shifts first, then convert to CLS | ||
export function cls() { | ||
return new Observable(subscriber => { | ||
let totalCls = 0; | ||
const entries = []; | ||
|
||
const obs = fromPerformanceObserver({ | ||
type: 'layout-shift', | ||
buffered: true, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries() | ||
) | ||
); | ||
|
||
obs.subscribe((entry) => { | ||
if (!entry.hadRecentInput) { | ||
entries.push(entry); | ||
totalCls += entry.value; | ||
subscriber.next({ | ||
score: totalCls, | ||
entries, | ||
}); | ||
} | ||
}); | ||
|
||
// TODO: test this | ||
return obs.unsubscribe; | ||
}); | ||
return fromPerformanceObserver({ | ||
type: 'layout-shift', | ||
// buffered: true, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries() | ||
.filter(entry => !entry.hadRecentInput) | ||
.map(entry => ({ | ||
score: entry.value, | ||
entries: [entry], | ||
})) | ||
), | ||
scan((acc, curr) => ({ | ||
score: acc.score + curr.score, | ||
entries: acc.entries.concat(curr.entries), | ||
}), { score: 0, entries: [] }) | ||
); | ||
} | ||
|
||
export default cls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,26 @@ | ||
import { Observable, filter, mergeAll, mergeMap } from "rxjs"; | ||
import { Observable, distinctUntilChanged, filter, groupBy, map, mergeAll, mergeMap, scan } from "rxjs"; | ||
import fromPerformanceObserver from "./fromPerformanceObserver"; | ||
import interactions from "./interactions"; | ||
|
||
export function inp() { | ||
return new Observable(subscriber => { | ||
const idToEventMap = {}; | ||
let maxInp = 0; | ||
let maxInp = 0; | ||
|
||
const obs = fromPerformanceObserver({ | ||
type: 'event', | ||
buffered: true, | ||
durationThreshold: 0, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries() | ||
), | ||
filter( | ||
et => !!et.interactionId | ||
) | ||
return interactions() | ||
.pipe( | ||
groupBy((value) => value.entries[0].interactionId), | ||
mergeMap((group$) => { | ||
return group$.pipe( | ||
scan((acc, curr) => ({ | ||
score: Math.max(acc.score, curr.score), | ||
entries: acc.entries.concat(curr.entries), | ||
}), { score: 0, entries: [] }) | ||
); | ||
}), | ||
distinctUntilChanged((prev, curr) => { | ||
// Return true if the current INP is "same" as existing | ||
return curr.score <= prev.score; | ||
}) | ||
); | ||
|
||
obs.subscribe((entry) => { | ||
const interactionEntries = (idToEventMap[entry.interactionId] ??= []); | ||
interactionEntries.push(entry); | ||
if (entry.duration > maxInp) { | ||
maxInp = entry.duration; | ||
subscriber.next({ | ||
score: entry.duration, | ||
entries: interactionEntries, | ||
}); | ||
} | ||
}); | ||
|
||
// TODO: test this | ||
return () => { | ||
console.log('here'); | ||
obs.unsubscribe(); | ||
}; | ||
}); | ||
} | ||
|
||
export default inp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Observable, filter, map, mergeAll, mergeMap } from "rxjs"; | ||
import fromPerformanceObserver from "./fromPerformanceObserver"; | ||
|
||
// TODO: Should this perhaps group interaction events together? | ||
export function interactions() { | ||
return fromPerformanceObserver({ | ||
type: 'event', | ||
// buffered: true, | ||
durationThreshold: 0, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries() | ||
.filter(entry => entry.interactionId != 0) | ||
.map(entry => ({ | ||
score: entry.duration, | ||
entries: [entry], | ||
})) | ||
) | ||
); | ||
} | ||
|
||
export default interactions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,18 @@ | ||
import { Observable, mergeMap } from "rxjs"; | ||
import { mergeMap } from "rxjs"; | ||
import fromPerformanceObserver from "./fromPerformanceObserver"; | ||
|
||
export function lcp() { | ||
return new Observable(subscriber => { | ||
const obs = fromPerformanceObserver({ | ||
type: 'largest-contentful-paint', | ||
buffered: true, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries() | ||
) | ||
); | ||
|
||
obs.subscribe((entry) => { | ||
subscriber.next({ | ||
return fromPerformanceObserver({ | ||
type: 'largest-contentful-paint', | ||
buffered: true, | ||
}).pipe( | ||
mergeMap( | ||
list => list.getEntries().map(entry => ({ | ||
score: entry.startTime, | ||
entries: [entry], | ||
}); | ||
}); | ||
|
||
// TODO: test this | ||
return obs.unsubscribe; | ||
}); | ||
})) | ||
) | ||
); | ||
} | ||
|
||
export default lcp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters