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

fix: not unwrapping markRaw objects #386

Merged
merged 2 commits into from Jun 19, 2020
Merged

Conversation

pikax
Copy link
Member

@pikax pikax commented Jun 16, 2020

fix #384

@pikax pikax requested a review from antfu June 16, 2020 07:13
@vmihailenco
Copy link

Thanks for fixing this.

Is there any way to undo unwrapping / get the target/underlying object? Something like undo(props.foo) which returns the target object which was not marked with markRaw.

@pikax
Copy link
Member Author

pikax commented Jun 16, 2020

Because of the vue2 limitations, when you change the ref it will change the underlying value

  it('test', ()=>{
    const a = {a:1 };
    const r = ref(a);

    r.value.a = 2

    expect(a.a).toBe(2)
  })

EDIT: Not sure if I understand your question

@vmihailenco
Copy link

vmihailenco commented Jun 16, 2020

Let me re-phrase it. Automatic unwrapping practically changes object like {foo: Ref<numer>} into {foo: number} using a Proxy / get&set. So instead of obj.foo.value I should/can write obj.foo. Is there any way to get original {foo: Ref<numer>} back from auto unwrapped {foo: number}?

PS I am okay with Vue 3 only solution - just want to understand what options I have.

@pikax
Copy link
Member Author

pikax commented Jun 16, 2020

Not currently possible.

How do you do that in vue3?

@vmihailenco
Copy link

How do you do that in vue3?

I don't know - I thought you have an idea. Thanks for the help and sorry for the offtopic.

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

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

That's weird to have "raw ref" 😅

Anyway, LGTM👍

@@ -28,8 +29,12 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {

for (const k of Object.keys(value)) {
const r = value[k]
// don't unwrap or do anything to raw
if (isRaw(r)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I found that when r is null or undefined, it causes TypeError: Cannot convert undefined or null to object.

// don't unwrap or do anything to raw
if (isRaw(r)) {
// don't process on falsy or raw
if (!r || isRaw(r)) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it better to check falsy inside isRaw?

Copy link
Member Author

Choose a reason for hiding this comment

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

export function isRaw(obj: any): boolean {
  return !!obj && hasOwn(obj, RawIdentifierKey) && obj[RawIdentifierKey] === RawIdentifier;
}

Not sure if is will help, the idea is if is isRaw it shouldn't do anything to the object.

even if I add that condition on isRaw I still need to do unwrap, since we don't need to process falsy values

@antfu antfu merged commit 575d100 into vuejs:master Jun 19, 2020
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.

markRaw does not prevent automatic ref unwrapping
4 participants