-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(reactivity): unwrap nested refs on the template (#361)
* fix: unwrap nested refs on the template * chore: add more nested to test * Update src/reactivity/unwrap.ts Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> * chore: if the object is not extensible stop unwrapping Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
3 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { isRef } from './ref' | ||
import { proxy, isFunction, isObject, isArray } from '../utils' | ||
import { isReactive } from './reactive' | ||
|
||
export function unwrapRefProxy(value: any) { | ||
if (isFunction(value)) { | ||
return value | ||
} | ||
|
||
if (isRef(value)) { | ||
return value | ||
} | ||
|
||
if (isArray(value)) { | ||
return value | ||
} | ||
|
||
if (isReactive(value)) { | ||
return value | ||
} | ||
|
||
if (!isObject(value)) { | ||
return value | ||
} | ||
|
||
if (!Object.isExtensible(value)) { | ||
return value | ||
} | ||
|
||
const obj: any = {} | ||
|
||
// copy symbols over | ||
Object.getOwnPropertySymbols(value).forEach( | ||
(s) => (obj[s] = (value as any)[s]) | ||
) | ||
|
||
for (const k of Object.keys(value)) { | ||
const r = value[k] | ||
// if is a ref, create a proxy to retrieve the value, | ||
if (isRef(r)) { | ||
const set = (v: any) => (r.value = v) | ||
const get = () => r.value | ||
|
||
proxy(obj, k, { get, set }) | ||
} else { | ||
obj[k] = unwrapRefProxy(r) | ||
} | ||
} | ||
|
||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters