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: remove immutable from 'config' state slice #4960

Merged
merged 8 commits into from
Mar 11, 2021

Conversation

smashercosmo
Copy link
Contributor

Summary

Continue with #3853

Test plan

Run existing tests

@smashercosmo smashercosmo requested a review from a team February 15, 2021 17:32
@erezrokah erezrokah added the type: chore work needed to keep the product and development running smoothly label Feb 16, 2021
@erezrokah erezrokah force-pushed the remove-immutable-from-config2 branch from 80cb9d3 to 349e141 Compare March 2, 2021 11:09
@erezrokah erezrokah force-pushed the remove-immutable-from-config2 branch from 349e141 to 652bebf Compare March 10, 2021 15:24

const backend = new Backend(implementation, { config, backendName: config.backend.name });
const backend = new Backend(implementation, { config: {}, backendName: 'github' });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erezrokah why do you think it's better to not use real default state? When tests are converted to typescript we'll have to either put a lot of tsignore statements or "as SomeType" statements.

Copy link
Contributor

Choose a reason for hiding this comment

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

I worried this will make the tests brittle. Personally I like to have in the tests as little as information possible required for them to pass so I don't have to change them too often.

As for typing the tests - we'll have to see if that makes our lives easier or not? I would expect tests to fail when something breaks and not rely on types?

Copy link
Contributor Author

@smashercosmo smashercosmo Mar 10, 2021

Choose a reason for hiding this comment

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

I would expect tests to fail when something breaks and not rely on types

this is true. On the other hand you're basically passing incorrect values to functions being tested. So tests might pass because of that. And might fail if correct values are provided.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is true. On the other hand you're basically passing incorrect values to functions being tested. So tests might pass because of that. And might fail if correct values are provided.

I believe that's something to verify when writing the tests. The test should be scoped to assert a specific thing and not verify that backend.ts methods work with the default config?

If so, we should have separate unit tests to verify that backend.ts handles the configuration properly (and not assume it works because we're passing default values).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright

@@ -643,7 +643,7 @@ function evaluateFolder(
entryMap: EntryMap | undefined,
field: EntryField | undefined,
) {
let currentFolder = config[folderKey];
let currentFolder = config[folderKey]!;
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'm not sure this is a correct way to handle it. Because here we're lying to compiler.

Copy link
Contributor

@erezrokah erezrokah Mar 10, 2021

Choose a reason for hiding this comment

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

Under this function that value is always defined.
The only reason we allow not to set media_folder is if you have an external media library which in that case this code doesn't run.

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'm wondering if there is more correct way to express it with types, so we don't have to use these escape hatches.

for (const collection of collections) {
newState[collection.name] = collection;
}
const newState = Object.fromEntries(
Copy link
Contributor Author

@smashercosmo smashercosmo Mar 10, 2021

Choose a reason for hiding this comment

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

Can't agree that using map + fromEntries is more readable than simple loop :) functional style doesn't really shine here and looks way more confusing than the loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually both methods have a bug of not maintaining the order of the collections as appeared in the configuration.
See c0e43bc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, in future we can use js Map to maintain the order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I'm wondering, why is it happening in the first place. I thought modern js engines maintain property order in js objects. It wasn't the case in the past, but I was sure that it had been fixed.

Copy link
Contributor Author

@smashercosmo smashercosmo Mar 10, 2021

Choose a reason for hiding this comment

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

Yeah, I'm pretty sure this is the problem with Immutable. This works correctly. The order is being preserved.

let obj = {}; 
let arr = ['collection_1', 'collection_3', 'collection_2']
for (const item of arr) {
  obj[item] = item;
}

Though the order is not being preserved if property name is a number.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, JS keeps insertions order. It's the fromJS that doesn't know how to handle it.

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

🔥

Sorry for taking so long to do this. I feel like I had to grow another set of 👀 for it.

@erezrokah erezrokah merged commit 6623740 into decaporg:master Mar 11, 2021
@smashercosmo
Copy link
Contributor Author

thx @erezrokah. no need to apologize. I know it's a lot of changes to review. thank you for being a thorough reviewer. I'll try to not make that many changes in future PRs, though it's not that easy in this particular-getting-rid-of-immutable case :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants