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

Implementing a cache for the gettersProxy object creation #1546

Merged
merged 4 commits into from
Nov 9, 2019
Merged
Changes from 3 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
42 changes: 24 additions & 18 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ function resetStoreVM (store, state, hot) {

// bind store public getters
store.getters = {}
// reset local getters cache
makeLocalGettersCache = {}
frankcs marked this conversation as resolved.
Show resolved Hide resolved
const wrappedGetters = store._wrappedGetters
const computed = {}
forEachValue(wrappedGetters, (fn, key) => {
Expand Down Expand Up @@ -393,27 +395,31 @@ function makeLocalContext (store, namespace, path) {
return local
}

let makeLocalGettersCache = {}
frankcs marked this conversation as resolved.
Show resolved Hide resolved

function makeLocalGetters (store, namespace) {
const gettersProxy = {}

const splitPos = namespace.length
Object.keys(store.getters).forEach(type => {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) return

// extract local getter type
const localType = type.slice(splitPos)

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: () => store.getters[type],
enumerable: true
if (!makeLocalGettersCache[namespace]) {
const gettersProxy = {}
const splitPos = namespace.length
Object.keys(store.getters).forEach(type => {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) return

// extract local getter type
const localType = type.slice(splitPos)

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: () => store.getters[type],
enumerable: true
})
})
})
makeLocalGettersCache[namespace] = gettersProxy
}

return gettersProxy
return makeLocalGettersCache[namespace]
}

function registerMutation (store, type, handler, local) {
Expand Down