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

Support Symbol named observable properties #1944

Merged
merged 1 commit into from
Jun 4, 2019

Conversation

loklaan
Copy link
Contributor

@loklaan loklaan commented Apr 3, 2019

Hey folks,

I was recently stuck trying to figure out why my observable properties weren't working with Symbols as keys. Then I found #1809 & #1925.

In this PR I hope to get things kicked off. Being new to the codebase there are probably thing's I've missed.

  • Please let me know if I've gone about it the wrong way
  • Or, there are still more tests and types to write

I've also tested this is a mid size production app.


PR checklist:

  • Added unit tests
  • Updated changelog
  • Updated docs (either in the description of this PR as markdown, or as separate PR on the gh-pages branch. Please refer to this PR). For new functionality, at least API.md should be updated
  • Added typescript typings
  • Verified that there is no significant performance drop (npm run perf)

@coveralls
Copy link

coveralls commented Apr 3, 2019

Coverage Status

Coverage increased (+0.03%) to 93.96% when pulling 421e793 on loklaan:feature/symbol-support into f05f952 on mobxjs:master.

Copy link
Member

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

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

Thanks! In generally looking good :). I think the implementation isn't entirely correct yet, see the comments.

export function getPlainObjectKeys(object) {
return unique(
([] as PropertyKey[]).concat.apply(
[],
Copy link
Member

Choose a reason for hiding this comment

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

I think this implementation is incorrect, the for ... in loop returns properties from the entire prototype chain (not just the direct prototype, as in the current one), but only the ones that are enumerable (unlike getOwnPropertySymbols), so this implementation needs to be refined a little further.

Copy link
Member

Choose a reason for hiding this comment

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

I think however that in none of the cases it is needed to grab keys from the prototype, so Reflect.keys, and filtering out the enumerable ones, should do the trick (with e.g. propertyIsEnumerable).

A more accurate name would then be getOwnEnumerablePropertyNames.

After a quick inspection of the usage sides, the only function that might run into trouble by not inspecting prototype chain might be toJS, it would be good to test that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool I'll run with these suggestions, thanks mate! I'll get back with changes as soon as I can.

I did observe in toJS tests that we are expecting enumerable properties up the prototype to be output, this initial implementation was to address those failing tests - I can split a test or two out to make this intention a little bit more obvious for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed up what I think is now the correct implementation, following your advice! Ty! 😄

const objKeys = getPlainObjectKeys(source)
for (let i in objKeys) {
const key = objKeys[i]
if (
Copy link
Member

Choose a reason for hiding this comment

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

I think these exception are no longer needed once non-enumerable symbol properties are filtered out (see below)

@mweststrate
Copy link
Member

mweststrate commented Apr 18, 2019 via email

@xuchaoqian
Copy link

Hi friends. What's the status of this patch? @loklaan @mweststrate

@mweststrate
Copy link
Member

I think @loklaan didn't push any new changes after the review?

* Add tests for using symbol keys and getting them from toJS
@loklaan loklaan force-pushed the feature/symbol-support branch from 94d812d to 421e793 Compare June 1, 2019 06:15
@loklaan
Copy link
Contributor Author

loklaan commented Jun 1, 2019

@xuchaobei Slow turn around on my end 😅 I hope the changes I just pushed up (rebased on latest master too) can move the PR along

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.

5 participants