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

Added refsFn to computed #257

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/component/setup/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,24 @@ export default (component, computeds) => {
Log.warn(`${computed} is not a function`)
}
component[symbols.computedKeys].push(computed)

// get internal variables referenced in computed function
const refs = [...computeds[computed].toString().matchAll(/this[\.\[][\w\.\'\"\]\$]+/g)]
let refsFn
// when 1 or less internal references, there is no dependency between references
if (refs.length > 1) {
// create a function that simply lists all references,
// to ensure that reactivity is being set up, even if the value
// of 1 variable affects calling another one (inside the user defined computed function)
refsFn = new Function(refs.map((ref) => 'void ' + ref[0] + '\n'))
}

Object.defineProperty(component, computed, {
get() {
// execute the refsFn if it exists
if (refsFn !== undefined) {
refsFn.apply(this)
}
return computeds[computed].apply(this)
},
})
Expand Down