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
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 43 additions & 22 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ function applyMixin (Vue) {
}
}

var devtoolHook =
typeof window !== 'undefined' &&
window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
var target = typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: {};
var devtoolHook = target.__VUE_DEVTOOLS_GLOBAL_HOOK__;

function devtoolPlugin (store) {
if (!devtoolHook) { return }
Expand Down Expand Up @@ -89,6 +92,12 @@ function assert (condition, msg) {
if (!condition) { throw new Error(("[vuex] " + msg)) }
}

function partial (fn, arg) {
return function () {
return fn(arg)
}
}

// Base data struct for store's module, package with some attribute and method
var Module = function Module (rawModule, runtime) {
this.runtime = runtime;
Expand Down Expand Up @@ -550,7 +559,9 @@ function resetStoreVM (store, state, hot) {
var computed = {};
forEachValue(wrappedGetters, function (fn, key) {
// use computed to leverage its lazy-caching mechanism
computed[key] = function () { return fn(store); };
// direct inline function use will lead to closure preserving oldVm.
// using partial to return function with only arguments preserved in closure enviroment.
computed[key] = partial(fn, store);
Object.defineProperty(store.getters, key, {
get: function () { return store._vm[key]; },
enumerable: true // for local getters
Expand Down Expand Up @@ -687,27 +698,37 @@ function makeLocalContext (store, namespace, path) {
return local
}

var makeLocalGettersCache = {};
var cacheStore = {};

function makeLocalGetters (store, namespace) {
var gettersProxy = {};

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

// extract local getter type
var 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: function () { return store.getters[type]; },
enumerable: true
if (cacheStore !== store) {
makeLocalGettersCache = {};
cacheStore = store;
}

if (!makeLocalGettersCache[namespace]) {
var gettersProxy = {};
var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }

// extract local getter type
var 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: function () { return store.getters[type]; },
enumerable: true
});
});
});
makeLocalGettersCache[namespace] = gettersProxy;
}

return gettersProxy
return makeLocalGettersCache[namespace]
}

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