direct-vuex
Advanced tools
Comparing version 0.9.11 to 0.10.0
@@ -18,3 +18,5 @@ import Vuex from 'vuex'; | ||
rootActionContext: (originalContext) => getModuleActionContext(originalContext, options, options), | ||
moduleActionContext: (originalContext, moduleOptions) => getModuleActionContext(originalContext, moduleOptions, options) | ||
moduleActionContext: (originalContext, moduleOptions) => getModuleActionContext(originalContext, moduleOptions, options), | ||
rootGetterContext: (state, getters) => getModuleGetterContext(state, getters, state, getters, options, options), | ||
moduleGetterContext: (state, getters, rootState, rootGetters, moduleOptions) => getModuleGetterContext(state, getters, rootState, rootGetters, moduleOptions, options) | ||
}; | ||
@@ -207,4 +209,29 @@ } | ||
} | ||
// GetterContext | ||
const getterContextCache = new WeakMap(); | ||
function getModuleGetterContext(state, getters, rootState, rootGetters, options, rootOptions) { | ||
let context = actionContextCache.get(state); | ||
// console.log(">> to-getterContext", context ? "FROM_CACHE" : "CREATE", options); | ||
if (!context) { | ||
context = { | ||
get rootState() { | ||
return rootState; | ||
}, | ||
get rootGetters() { | ||
return toDirectGetters(rootOptions, rootGetters); | ||
}, | ||
get state() { | ||
return state; | ||
}, | ||
get getters() { | ||
return toDirectGetters(options, getters); | ||
} | ||
}; | ||
if (state) // Can be undefined in unit tests | ||
getterContextCache.set(state, context); | ||
} | ||
return context; | ||
} | ||
export default directVuex; | ||
export { createActions, createDirectStore, createGetters, createModule, createModules, createMutations, defineActions, defineGetters, defineModule, defineModules, defineMutations }; |
@@ -1,1 +0,1 @@ | ||
import Vuex from"vuex";function createDirectStore(t){const e=new Vuex.Store(t),o={get state(){return e.state},getters:toDirectGetters(t,e.getters),commit:toDirectCommit(t,e.commit),dispatch:toDirectDispatch(t,e.dispatch),original:e};return e.direct=o,{store:o,rootActionContext:e=>getModuleActionContext(e,t,t),moduleActionContext:(e,o)=>getModuleActionContext(e,o,t)}}function defineModule(t){return t}function defineModules(){return t=>t}function defineGetters(){return t=>t}function defineMutations(){return t=>t}function defineActions(t){return t}const createModule=obsolete(defineModule,"createModule","defineModule"),createModules=obsolete(defineModules,"createModules","defineModules"),createGetters=obsolete(defineGetters,"createGetters","defineGetters"),createMutations=obsolete(defineMutations,"createMutations","defineMutations"),createActions=obsolete(defineActions,"createActions","defineActions");function obsolete(t,e,o){return(...n)=>(console.warn(`Function '${e}' is obsolete, please use '${o}'.`),t(...n))}var directVuex={createDirectStore:createDirectStore,defineModule:defineModule,defineModules:defineModules,defineGetters:defineGetters,defineMutations:defineMutations,defineActions:defineActions,createModule:createModule,createModules:createModules,createGetters:createGetters,createMutations:createMutations,createActions:createActions};const gettersCache=new WeakMap;function toDirectGetters(t,e){let o=gettersCache.get(e);return o||(o=gettersFromOptions({},t,e),gettersCache.set(e,o)),o}function gettersFromOptions(t,e,o,n=[]){if(e.getters&&createDirectGetters(t,e.getters,o,n),e.modules)for(const r of Object.keys(e.modules)){const c=e.modules[r];c.namespaced?t[r]=gettersFromOptions({},c,o,[...n,r]):gettersFromOptions(t,c,o,n)}return t}function createDirectGetters(t,e,o,n){const r=n&&0!==n.length?`${n.join("/")}/`:"";for(const n of Object.keys(e))Object.defineProperties(t,{[n]:{get:()=>o[`${r}${n}`]}})}const commitCache=new WeakMap;function toDirectCommit(t,e){let o=commitCache.get(e);return o||(o=commitFromOptions({},t,e),commitCache.set(e,o)),o}const rootCommitCache=new WeakMap;function toDirectRootCommit(t,e){let o=rootCommitCache.get(e);if(!o){o=commitFromOptions({},t,(t,o)=>e(t,o,{root:!0})),rootCommitCache.set(e,o)}return o}function commitFromOptions(t,e,o,n=[]){if(e.mutations&&createDirectMutations(t,e.mutations,o,n),e.modules)for(const r of Object.keys(e.modules)){const c=e.modules[r];c.namespaced?t[r]=commitFromOptions({},c,o,[...n,r]):commitFromOptions(t,c,o,n)}return t}function createDirectMutations(t,e,o,n){const r=n&&0!==n.length?`${n.join("/")}/`:"";for(const n of Object.keys(e))t[n]=t=>o(`${r}${n}`,t)}const dispatchCache=new WeakMap;function toDirectDispatch(t,e){let o=dispatchCache.get(e);return o||(o=dispatchFromOptions({},t,e),dispatchCache.set(e,o)),o}const rootDispatchCache=new WeakMap;function toDirectRootDispatch(t,e){let o=rootDispatchCache.get(e);if(!o){o=dispatchFromOptions({},t,(t,o)=>e(t,o,{root:!0})),rootDispatchCache.set(e,o)}return o}function dispatchFromOptions(t,e,o,n=[]){if(e.actions&&createDirectActions(t,e.actions,o,n),e.modules)for(const r of Object.keys(e.modules)){const c=e.modules[r];c.namespaced?t[r]=dispatchFromOptions({},c,o,[...n,r]):dispatchFromOptions(t,c,o,n)}return t}function createDirectActions(t,e,o,n){const r=n&&0!==n.length?`${n.join("/")}/`:"";for(const n of Object.keys(e))t[n]=t=>o(`${r}${n}`,t)}const actionContextCache=new WeakMap;function getModuleActionContext(t,e,o){let n=actionContextCache.get(t.state);return n||(n={get rootState(){return t.rootState},get rootGetters(){return toDirectGetters(o,t.rootGetters)},get rootCommit(){return toDirectRootCommit(o,t.commit)},get rootDispatch(){return toDirectRootDispatch(o,t.dispatch)},get state(){return t.state},get getters(){return toDirectGetters(e,t.getters)},get commit(){return toDirectCommit(e,t.commit)},get dispatch(){return toDirectDispatch(e,t.dispatch)}},t.state&&actionContextCache.set(t.state,n)),n}export default directVuex;export{createActions,createDirectStore,createGetters,createModule,createModules,createMutations,defineActions,defineGetters,defineModule,defineModules,defineMutations}; | ||
import Vuex from"vuex";function createDirectStore(t){const e=new Vuex.Store(t),o={get state(){return e.state},getters:toDirectGetters(t,e.getters),commit:toDirectCommit(t,e.commit),dispatch:toDirectDispatch(t,e.dispatch),original:e};return e.direct=o,{store:o,rootActionContext:e=>getModuleActionContext(e,t,t),moduleActionContext:(e,o)=>getModuleActionContext(e,o,t),rootGetterContext:(e,o)=>getModuleGetterContext(e,o,e,o,t,t),moduleGetterContext:(e,o,r,n,c)=>getModuleGetterContext(e,o,r,n,c,t)}}function defineModule(t){return t}function defineModules(){return t=>t}function defineGetters(){return t=>t}function defineMutations(){return t=>t}function defineActions(t){return t}const createModule=obsolete(defineModule,"createModule","defineModule"),createModules=obsolete(defineModules,"createModules","defineModules"),createGetters=obsolete(defineGetters,"createGetters","defineGetters"),createMutations=obsolete(defineMutations,"createMutations","defineMutations"),createActions=obsolete(defineActions,"createActions","defineActions");function obsolete(t,e,o){return(...r)=>(console.warn(`Function '${e}' is obsolete, please use '${o}'.`),t(...r))}var directVuex={createDirectStore:createDirectStore,defineModule:defineModule,defineModules:defineModules,defineGetters:defineGetters,defineMutations:defineMutations,defineActions:defineActions,createModule:createModule,createModules:createModules,createGetters:createGetters,createMutations:createMutations,createActions:createActions};const gettersCache=new WeakMap;function toDirectGetters(t,e){let o=gettersCache.get(e);return o||(o=gettersFromOptions({},t,e),gettersCache.set(e,o)),o}function gettersFromOptions(t,e,o,r=[]){if(e.getters&&createDirectGetters(t,e.getters,o,r),e.modules)for(const n of Object.keys(e.modules)){const c=e.modules[n];c.namespaced?t[n]=gettersFromOptions({},c,o,[...r,n]):gettersFromOptions(t,c,o,r)}return t}function createDirectGetters(t,e,o,r){const n=r&&0!==r.length?`${r.join("/")}/`:"";for(const r of Object.keys(e))Object.defineProperties(t,{[r]:{get:()=>o[`${n}${r}`]}})}const commitCache=new WeakMap;function toDirectCommit(t,e){let o=commitCache.get(e);return o||(o=commitFromOptions({},t,e),commitCache.set(e,o)),o}const rootCommitCache=new WeakMap;function toDirectRootCommit(t,e){let o=rootCommitCache.get(e);if(!o){o=commitFromOptions({},t,(t,o)=>e(t,o,{root:!0})),rootCommitCache.set(e,o)}return o}function commitFromOptions(t,e,o,r=[]){if(e.mutations&&createDirectMutations(t,e.mutations,o,r),e.modules)for(const n of Object.keys(e.modules)){const c=e.modules[n];c.namespaced?t[n]=commitFromOptions({},c,o,[...r,n]):commitFromOptions(t,c,o,r)}return t}function createDirectMutations(t,e,o,r){const n=r&&0!==r.length?`${r.join("/")}/`:"";for(const r of Object.keys(e))t[r]=t=>o(`${n}${r}`,t)}const dispatchCache=new WeakMap;function toDirectDispatch(t,e){let o=dispatchCache.get(e);return o||(o=dispatchFromOptions({},t,e),dispatchCache.set(e,o)),o}const rootDispatchCache=new WeakMap;function toDirectRootDispatch(t,e){let o=rootDispatchCache.get(e);if(!o){o=dispatchFromOptions({},t,(t,o)=>e(t,o,{root:!0})),rootDispatchCache.set(e,o)}return o}function dispatchFromOptions(t,e,o,r=[]){if(e.actions&&createDirectActions(t,e.actions,o,r),e.modules)for(const n of Object.keys(e.modules)){const c=e.modules[n];c.namespaced?t[n]=dispatchFromOptions({},c,o,[...r,n]):dispatchFromOptions(t,c,o,r)}return t}function createDirectActions(t,e,o,r){const n=r&&0!==r.length?`${r.join("/")}/`:"";for(const r of Object.keys(e))t[r]=t=>o(`${n}${r}`,t)}const actionContextCache=new WeakMap;function getModuleActionContext(t,e,o){let r=actionContextCache.get(t.state);return r||(r={get rootState(){return t.rootState},get rootGetters(){return toDirectGetters(o,t.rootGetters)},get rootCommit(){return toDirectRootCommit(o,t.commit)},get rootDispatch(){return toDirectRootDispatch(o,t.dispatch)},get state(){return t.state},get getters(){return toDirectGetters(e,t.getters)},get commit(){return toDirectCommit(e,t.commit)},get dispatch(){return toDirectDispatch(e,t.dispatch)}},t.state&&actionContextCache.set(t.state,r)),r}const getterContextCache=new WeakMap;function getModuleGetterContext(t,e,o,r,n,c){let i=actionContextCache.get(t);return i||(i={get rootState(){return o},get rootGetters(){return toDirectGetters(c,r)},get state(){return t},get getters(){return toDirectGetters(n,e)}},t&&getterContextCache.set(t,i)),i}export default directVuex;export{createActions,createDirectStore,createGetters,createModule,createModules,createMutations,defineActions,defineGetters,defineModule,defineModules,defineMutations}; |
@@ -31,3 +31,7 @@ (function (global, factory) { | ||
rootActionContext: function (originalContext) { return getModuleActionContext(originalContext, options, options); }, | ||
moduleActionContext: function (originalContext, moduleOptions) { return getModuleActionContext(originalContext, moduleOptions, options); } | ||
moduleActionContext: function (originalContext, moduleOptions) { return getModuleActionContext(originalContext, moduleOptions, options); }, | ||
rootGetterContext: function (state, getters) { return getModuleGetterContext(state, getters, state, getters, options, options); }, | ||
moduleGetterContext: function (state, getters, rootState, rootGetters, moduleOptions) { | ||
return getModuleGetterContext(state, getters, rootState, rootGetters, moduleOptions, options); | ||
} | ||
}; | ||
@@ -245,2 +249,27 @@ } | ||
} | ||
// GetterContext | ||
var getterContextCache = new WeakMap(); | ||
function getModuleGetterContext(state, getters, rootState, rootGetters, options, rootOptions) { | ||
var context = actionContextCache.get(state); | ||
// console.log(">> to-getterContext", context ? "FROM_CACHE" : "CREATE", options); | ||
if (!context) { | ||
context = { | ||
get rootState() { | ||
return rootState; | ||
}, | ||
get rootGetters() { | ||
return toDirectGetters(rootOptions, rootGetters); | ||
}, | ||
get state() { | ||
return state; | ||
}, | ||
get getters() { | ||
return toDirectGetters(options, getters); | ||
} | ||
}; | ||
if (state) // Can be undefined in unit tests | ||
getterContextCache.set(state, context); | ||
} | ||
return context; | ||
} | ||
@@ -247,0 +276,0 @@ exports.createActions = createActions; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vuex")):"function"==typeof define&&define.amd?define(["exports","vuex"],t):t((e=e||self).DirectVuex={},e.Vuex)}(this,(function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var n=this&&this.__spreadArrays||function(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),o=0;for(t=0;t<n;t++)for(var u=arguments[t],i=0,a=u.length;i<a;i++,o++)r[o]=u[i];return r};function r(e){var n=new t.Store(e),r={get state(){return n.state},getters:M(e,n.getters),commit:y(e,n.commit),dispatch:A(e,n.dispatch),original:n};return n.direct=r,{store:r,rootActionContext:function(t){return G(t,e,e)},moduleActionContext:function(t,n){return G(t,n,e)}}}function o(e){return e}function u(){return function(e){return e}}function i(){return function(e){return e}}function a(){return function(e){return e}}function c(e){return e}var s=g(o,"createModule","defineModule"),f=g(u,"createModules","defineModules"),d=g(i,"createGetters","defineGetters"),l=g(a,"createMutations","defineMutations"),v=g(c,"createActions","defineActions");function g(e,t,n){return function(){for(var r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];return console.warn("Function '"+t+"' is obsolete, please use '"+n+"'."),e.apply(void 0,r)}}var m={createDirectStore:r,defineModule:o,defineModules:u,defineGetters:i,defineMutations:a,defineActions:c,createModule:s,createModules:f,createGetters:d,createMutations:l,createActions:v},p=new WeakMap;function M(e,t){var r=p.get(t);return r||(r=function e(t,r,o,u){void 0===u&&(u=[]);r.getters&&function(e,t,n,r){for(var o=r&&0!==r.length?r.join("/")+"/":"",u=function(t){var r;Object.defineProperties(e,((r={})[t]={get:function(){return n[""+o+t]}},r))},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(t,r.getters,o,u);if(r.modules)for(var i=0,a=Object.keys(r.modules);i<a.length;i++){var c=a[i],s=r.modules[c];s.namespaced?t[c]=e({},s,o,n(u,[c])):e(t,s,o,u)}return t}({},e,t),p.set(t,r)),r}var h=new WeakMap;function y(e,t){var n=h.get(t);return n||(n=k({},e,t),h.set(t,n)),n}var j=new WeakMap;function k(e,t,r,o){if(void 0===o&&(o=[]),t.mutations&&function(e,t,n,r){for(var o=r&&0!==r.length?r.join("/")+"/":"",u=function(t){e[t]=function(e){return n(""+o+t,e)}},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(e,t.mutations,r,o),t.modules)for(var u=0,i=Object.keys(t.modules);u<i.length;u++){var a=i[u],c=t.modules[a];c.namespaced?e[a]=k({},c,r,n(o,[a])):k(e,c,r,o)}return e}var b=new WeakMap;function A(e,t){var n=b.get(t);return n||(n=x({},e,t),b.set(t,n)),n}var w=new WeakMap;function x(e,t,r,o){if(void 0===o&&(o=[]),t.actions&&function(e,t,n,r){for(var o=r&&0!==r.length?r.join("/")+"/":"",u=function(t){e[t]=function(e){return n(""+o+t,e)}},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(e,t.actions,r,o),t.modules)for(var u=0,i=Object.keys(t.modules);u<i.length;u++){var a=i[u],c=t.modules[a];c.namespaced?e[a]=x({},c,r,n(o,[a])):x(e,c,r,o)}return e}var O=new WeakMap;function G(e,t,n){var r=O.get(e.state);return r||(r={get rootState(){return e.rootState},get rootGetters(){return M(n,e.rootGetters)},get rootCommit(){return function(e,t){var n=j.get(t);if(!n){n=k({},e,(function(e,n){return t(e,n,{root:!0})})),j.set(t,n)}return n}(n,e.commit)},get rootDispatch(){return function(e,t){var n=w.get(t);if(!n){n=x({},e,(function(e,n){return t(e,n,{root:!0})})),w.set(t,n)}return n}(n,e.dispatch)},get state(){return e.state},get getters(){return M(t,e.getters)},get commit(){return y(t,e.commit)},get dispatch(){return A(t,e.dispatch)}},e.state&&O.set(e.state,r)),r}e.createActions=v,e.createDirectStore=r,e.createGetters=d,e.createModule=s,e.createModules=f,e.createMutations=l,e.default=m,e.defineActions=c,e.defineGetters=i,e.defineModule=o,e.defineModules=u,e.defineMutations=a,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vuex")):"function"==typeof define&&define.amd?define(["exports","vuex"],t):t((e=e||self).DirectVuex={},e.Vuex)}(this,(function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var r=this&&this.__spreadArrays||function(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var u=arguments[t],i=0,a=u.length;i<a;i++,o++)n[o]=u[i];return n};function n(e){var r=new t.Store(e),n={get state(){return r.state},getters:M(e,r.getters),commit:y(e,r.commit),dispatch:G(e,r.dispatch),original:r};return r.direct=n,{store:n,rootActionContext:function(t){return O(t,e,e)},moduleActionContext:function(t,r){return O(t,r,e)},rootGetterContext:function(t,r){return S(t,r,t,r,e,e)},moduleGetterContext:function(t,r,n,o,u){return S(t,r,n,o,u,e)}}}function o(e){return e}function u(){return function(e){return e}}function i(){return function(e){return e}}function a(){return function(e){return e}}function c(e){return e}var s=v(o,"createModule","defineModule"),f=v(u,"createModules","defineModules"),d=v(i,"createGetters","defineGetters"),l=v(a,"createMutations","defineMutations"),g=v(c,"createActions","defineActions");function v(e,t,r){return function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return console.warn("Function '"+t+"' is obsolete, please use '"+r+"'."),e.apply(void 0,n)}}var m={createDirectStore:n,defineModule:o,defineModules:u,defineGetters:i,defineMutations:a,defineActions:c,createModule:s,createModules:f,createGetters:d,createMutations:l,createActions:g},p=new WeakMap;function M(e,t){var n=p.get(t);return n||(n=function e(t,n,o,u){void 0===u&&(u=[]);n.getters&&function(e,t,r,n){for(var o=n&&0!==n.length?n.join("/")+"/":"",u=function(t){var n;Object.defineProperties(e,((n={})[t]={get:function(){return r[""+o+t]}},n))},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(t,n.getters,o,u);if(n.modules)for(var i=0,a=Object.keys(n.modules);i<a.length;i++){var c=a[i],s=n.modules[c];s.namespaced?t[c]=e({},s,o,r(u,[c])):e(t,s,o,u)}return t}({},e,t),p.set(t,n)),n}var h=new WeakMap;function y(e,t){var r=h.get(t);return r||(r=j({},e,t),h.set(t,r)),r}var k=new WeakMap;function j(e,t,n,o){if(void 0===o&&(o=[]),t.mutations&&function(e,t,r,n){for(var o=n&&0!==n.length?n.join("/")+"/":"",u=function(t){e[t]=function(e){return r(""+o+t,e)}},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(e,t.mutations,n,o),t.modules)for(var u=0,i=Object.keys(t.modules);u<i.length;u++){var a=i[u],c=t.modules[a];c.namespaced?e[a]=j({},c,n,r(o,[a])):j(e,c,n,o)}return e}var x=new WeakMap;function G(e,t){var r=x.get(t);return r||(r=w({},e,t),x.set(t,r)),r}var b=new WeakMap;function w(e,t,n,o){if(void 0===o&&(o=[]),t.actions&&function(e,t,r,n){for(var o=n&&0!==n.length?n.join("/")+"/":"",u=function(t){e[t]=function(e){return r(""+o+t,e)}},i=0,a=Object.keys(t);i<a.length;i++){var c=a[i];u(c)}}(e,t.actions,n,o),t.modules)for(var u=0,i=Object.keys(t.modules);u<i.length;u++){var a=i[u],c=t.modules[a];c.namespaced?e[a]=w({},c,n,r(o,[a])):w(e,c,n,o)}return e}var A=new WeakMap;function O(e,t,r){var n=A.get(e.state);return n||(n={get rootState(){return e.rootState},get rootGetters(){return M(r,e.rootGetters)},get rootCommit(){return function(e,t){var r=k.get(t);if(!r){r=j({},e,(function(e,r){return t(e,r,{root:!0})})),k.set(t,r)}return r}(r,e.commit)},get rootDispatch(){return function(e,t){var r=b.get(t);if(!r){r=w({},e,(function(e,r){return t(e,r,{root:!0})})),b.set(t,r)}return r}(r,e.dispatch)},get state(){return e.state},get getters(){return M(t,e.getters)},get commit(){return y(t,e.commit)},get dispatch(){return G(t,e.dispatch)}},e.state&&A.set(e.state,n)),n}var W=new WeakMap;function S(e,t,r,n,o,u){var i=A.get(e);return i||(i={get rootState(){return r},get rootGetters(){return M(u,n)},get state(){return e},get getters(){return M(o,t)}},e&&W.set(e,i)),i}e.createActions=g,e.createDirectStore=n,e.createGetters=d,e.createModule=s,e.createModules=f,e.createMutations=l,e.default=m,e.defineActions=c,e.defineGetters=i,e.defineModule=o,e.defineModules=u,e.defineMutations=a,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "direct-vuex", | ||
"version": "0.9.11", | ||
"version": "0.10.0", | ||
"description": "Use and implement your Vuex store with TypeScript types. Compatible with the Vue 3 composition API.", | ||
@@ -5,0 +5,0 @@ "author": "Paleo", |
@@ -35,3 +35,9 @@ # direct-vuex | ||
const { store, rootActionContext, moduleActionContext } = createDirectStore({ | ||
const { | ||
store, | ||
rootActionContext, | ||
moduleActionContext, | ||
rootGetterContext, | ||
moduleGetterContext | ||
} = createDirectStore({ | ||
// … store implementation here … | ||
@@ -44,4 +50,9 @@ }) | ||
// The following exports will be used to enable types in the | ||
// implementation of actions. | ||
export { rootActionContext, moduleActionContext } | ||
// implementation of actions and getters. | ||
export { | ||
rootActionContext, | ||
moduleActionContext, | ||
rootGetterContext, | ||
moduleGetterContext | ||
} | ||
@@ -159,4 +170,6 @@ // The following lines enable types in the injected store '$store'. | ||
getters: { | ||
p1OrDefault(state) { | ||
// Here, the type of 'state' is 'Mod1State'. | ||
p1OrDefault(...args) { | ||
const { state, getters, rootState, rootGetters } = mod1GetterContext(...args) | ||
// Here, 'getters', 'state', 'rootGetters' and 'rootState' are typed. | ||
// Without 'mod1GetterContext' only 'state' would be typed. | ||
return state.p1 || "default" | ||
@@ -181,2 +194,3 @@ } | ||
const mod1ActionContext = (context: any) => moduleActionContext(context, mod1) | ||
const mod1GetterContext = (...args: any[]) => moduleGetterContext(...args, mod1) | ||
``` | ||
@@ -199,2 +213,16 @@ | ||
### Get the typed context of a Vuex Getter, but in the root store | ||
The generated function `rootGetterContext` converts the injected action context to the direct-vuex one, at the root level (not in a module). | ||
```ts | ||
getters: { | ||
getterInTheRootStore(...args) { | ||
const { state, getters, rootState, rootGetters } = rootGetterContext(...args) | ||
// Here, 'getters', 'state', 'rootGetters' and 'rootState' are typed. | ||
// Without 'rootGetterContext' only 'state' would be typed. | ||
} | ||
} | ||
``` | ||
### Use `defineGetters` | ||
@@ -210,3 +238,5 @@ | ||
getter1(state) { | ||
// Here, the type of 'state' is 'Mod1State'. | ||
const { state, getters, rootState, rootGetters } = mod1GetterContext(...args) | ||
// Here, 'getters', 'state', 'rootGetters' and 'rootState' are typed. | ||
// Without 'mod1GetterContext' only 'state' would be typed. | ||
}, | ||
@@ -251,3 +281,3 @@ }) | ||
When the helper `moduleActionContext` is used, linters may warn about an issue: _"Variable used before it's assigned"_. I couldn't avoid circular dependencies. Module action contexts need to be inferred at the store level, because they contain `rootState` etc. | ||
When the helper `moduleActionContext` and `moduleGetterContext` are used, linters may warn about an issue: _"Variable used before it's assigned"_. I couldn't avoid circular dependencies. Action contexts and getter contexts need to be inferred at the store level, because they contain `rootState` etc. | ||
@@ -260,2 +290,8 @@ Here is an example of a Vuex module implementation: | ||
const mod1 = { | ||
getters: { | ||
p1OrDefault(...args) { | ||
const { state, getters, rootState, rootGetters } = mod1GetterContext(...args(state, ...args) | ||
// … | ||
} | ||
}, | ||
actions: { | ||
@@ -271,5 +307,6 @@ loadP1(context, payload: { id: string }) { | ||
const mod1ActionContext = (context: any) => moduleActionContext(context, mod1) | ||
const mod1GetterContext = (...args: any[]) => moduleGetterContext(...args, mod1) | ||
``` | ||
It works because `mod1ActionContext` is not executed at the same time it is declared. It is executed when an action is executed, ie. after all the store and modules are already initialized. | ||
It works because `mod1ActionContext` (or `mod1GetterContext`) is not executed at the same time it is declared. It is executed when an action (or a getter) is executed, ie. after all the store and modules are already initialized. | ||
@@ -276,0 +313,0 @@ I suggest to disable the linter rule with a comment at the top of the source file. |
@@ -12,2 +12,9 @@ import { ActionContext, Store } from "vuex" | ||
): DirectActionContext<R, O> | ||
rootGetterContext: ( | ||
state: any, | ||
getters: any | ||
) => DirectGetterContext<R, R> | ||
moduleGetterContext: <O extends ModuleOptions>( | ||
state: any, getters: any, rootState: any, rootGetters: any, module: O | ||
) => DirectGetterContext<R, O> | ||
} | ||
@@ -112,2 +119,9 @@ | ||
export type DirectGetterContext<R, O> = ShowContent<{ | ||
rootState: DirectState<R> | ||
rootGetters: DirectGetters<R> | ||
state: DirectState<O> | ||
getters: DirectGetters<O> | ||
}> | ||
// Common helpers | ||
@@ -138,2 +152,2 @@ | ||
T extends infer O ? { [K in keyof O]: O[K] } : never | ||
: T | ||
: T |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57904
702
325