Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(registerObserver): preserve observer reference #48

Merged
merged 5 commits into from
Sep 13, 2018

Conversation

adnasa
Copy link
Collaborator

@adnasa adnasa commented Jul 18, 2018

  1. returns observer when calling registerObserver
  2. supercedes Prevent PerformanceObserver from getting destroyed over time. #26
  3. extends tests

Tech debt

  1. Missing possibility to mock logToConsole
  2. consider using takeRecords or another option over callback
    One problem I found with the current implementation of passing callback into window.PerformanceObserver is that we "leaking" or "imposing" external functions into the instantiation of window.PerformanceObserver, which makes it hard or rather unintuitive to mock and test. One option to consider over this is to return a promise resolving the entries of the observer, but this is anti-pattern to Observers in general. A more optimal solution is a follow up and continuation to returning the instance is having callback removed and allow consumers to registerOberserver to use takeRecords which fulfills two purpose:
    • empty the store
    • process data. Given this; we give raw records back and let consumers for themselves decide how they want to process this (optimal but breaking change)

@adnasa adnasa self-assigned this Jul 18, 2018
@adnasa adnasa requested a review from nitin42 July 18, 2018 00:23
const registerObserver = (params, callback) => {
params = params || {}

// TODO: Is there any way to polyfill this API ?
Copy link
Collaborator Author

@adnasa adnasa Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a devtool for chrome + firefox. no need to polyfill.
Writing add-ons/extensions for IE is a whole different problem

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #52, we'll polyfill for older browsers if we somewhen find a proper polyfill for the API :)

src/npm/hook.js Outdated
window.__REACT_PERF_DEVTOOL_GLOBAL_STORE__ = {
measures,
length: list.getEntries().length,
rawMeasures: list.getEntries()
}

// For logging to console
Copy link
Collaborator Author

@adnasa adnasa Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if-statement function name clearly indicates that we are logging to console. a comment on this is superfluous

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Yup, totally.

@adnasa adnasa force-pushed the adnasa-register-observer-preserve-reference branch from 78d8de8 to 2c5fc45 Compare July 18, 2018 00:38
@nitin42
Copy link
Owner

nitin42 commented Jul 18, 2018

I think if we go for observer.takeRecords() then users of this hook will receive the raw measures (not parsed and aggregated), and hence this goes against the usage of this hook. The main purpose of this hook is to provide users with options to get parsed measures so that they can send it over to their analytics server. If we undo this, then we may wanna expose utility functions too along with registerObserver to parse the measures. And I'm afraid that this will increase the API surface area. I just wanted to keep things minimal. Let me know what you think!

@adnasa
Copy link
Collaborator Author

adnasa commented Jul 18, 2018

The main purpose of this hook is to provide users with options to get parsed measures so that they can send it over to their analytics server.

This is exactly what I had in mind when I discussed takeRecords. There is nothing stopping us from returning the following shape upon instantiation:

return {
  // raw oberserver with it's API
  observer,
  // list of entries that were already processed with
  // `generateDataFromMeasures` and `getReactPerformanceData`
  // by react-perf-devtool
  compiledMeasurements,
}

which would serve the two main purposes with this change:

  1. a reference of observer is received
  2. a list of measurements are received

hmm.. I realised now that yes, you're right. You're right because the problem with my suggestion is that consumer can not receive new measurements as soon as they are updated.

let's leave this as is. please review :)

@nitin42
Copy link
Owner

nitin42 commented Jul 18, 2018

I'll review your PR soon (by the end of this week). I am just busy doing interviews for full-time gig. Although the code changes looks good to me but I'll still pull the changes manually on my machine for behavioural testing. Thank you for all the contribution you've made to this project. I really appreciate your help 😄

@adnasa
Copy link
Collaborator Author

adnasa commented Aug 10, 2018

... 24 days later ... 😆 🤣

src/npm/hook.js Outdated
})
observer.observe({ entryTypes: ['measure'] })
return observer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you should then also adapt the README as in #26 to make sure the user declares the observer in the global scope

Copy link
Collaborator

@flxwu flxwu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tested locally aswell!

@flxwu flxwu merged commit 1aac6c4 into master Sep 13, 2018
@flxwu flxwu deleted the adnasa-register-observer-preserve-reference branch September 13, 2018 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants