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

chore: refactor log function to class #20382

Merged
merged 6 commits into from
Mar 9, 2022

Conversation

sainthkh
Copy link
Contributor

@sainthkh sainthkh commented Feb 28, 2022

User facing changelog

N/A. It's an internal refactoring

Additional details

  • Why was this change necessary? => Modernize the code.
  • What is affected by this change? => N/A
  • Any implementation details to explain? => Change the function/object to class.

Summary of changes.

As it's a refactoring, the code change is hard to read. So, I left this.

  • I organized the code into 3 parts: LogUtils, LogManager, Log.
    • LogUtils are functions used in other files. They were exported with export default. I could remove them.
    • LogManager: Inside create function, logs variable exists and it manages logs created. But it makes us create fireChangeEvent function inside create function and attach it to Log. So, I created LogManager class and Log class reference it.
    • Log: It was a function that returns an object. I changed it to a class.
  • I left some comments about other logic changes below.

How has the user experience changed?

N/A

PR Tasks

  • [na] Have tests been added/updated?
  • [na] Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • [na] Has a PR for user-facing changes been opened in cypress-documentation?
  • [na] Have API changes been updated in the type definitions?
  • [na] Have new configuration options been added to the cypress.schema.json?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 28, 2022

Thanks for taking the time to open a PR!


if (!(!_.isFunction(value) || !groupsOrTableRe.test(key))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this part to _.isFunction(value) && groupsOrTableRe.test(key), because it's a bit hard to read with double negation.


this.fireChangeEvent()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has been changed to this.logManager.fireChangeEvent(this).

@@ -613,25 +631,5 @@ function create (Cypress, cy, state, config) {
return log
}

logFn._logs = logs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this line because it doesn't do anything. And it might make typing hard later.

It was added at 2c8b9af for test. And changed to this style at 22f43ba.

But it is not used anywhere currently.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the clean up!

@sainthkh sainthkh marked this pull request as ready for review February 28, 2022 04:00
@sainthkh sainthkh requested a review from a team as a code owner February 28, 2022 04:00
@sainthkh sainthkh requested review from jennifer-shehane and removed request for a team February 28, 2022 04:00
// as fast as every 4ms
// @ts-ignore
log.fireChangeEvent = _.debounce(triggerStateChanged, 4)
const log = new Log(cy, state, config, logManager, options)
Copy link
Member

Choose a reason for hiding this comment

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

Are we able to pass logManager.fireChangeEvent instead of the entire class instance? Since the logManger stores all of the incoming logs, this feels heavy to each log instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


const triggerStateChanged = () => {
return trigger(log, 'command:log:changed')
export function create (Cypress, cy, state, config) {
Copy link
Member

Choose a reason for hiding this comment

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

any reason the logFn logic couldn't live in the LogManager ?

export function create (Cypress, cy, state, config) {
  const logManager = new LogManager()
  return logManager.createLog(cy, state, config, logManager, options)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left it there because it was there for years.

After your comment, I also thought about it and it's Manager's job to create logFn. So, I moved it.

@jennifer-shehane jennifer-shehane removed their request for review March 7, 2022 16:02
@tbiethman tbiethman requested a review from BlueWinds March 7, 2022 16:34
@jennifer-shehane jennifer-shehane merged commit 7d87da9 into cypress-io:develop Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants