This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+530
View File
@@ -0,0 +1,530 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Takes the properties on object from parameter source and adds them to the object
* parameter target
* @param {object} target Object to have properties copied onto from y
* @param {object} source Object with properties to be copied to x
*/
function addPropertiesToObject(target, source) {
var _loop_1 = function (k) {
Object.defineProperty(target, k, {
get: function () { return source[k]; }
});
};
for (var _i = 0, _a = Object.keys(source || {}); _i < _a.length; _i++) {
var k = _a[_i];
_loop_1(k);
}
}
/**
* Returns a namespaced name of the module to be used as a store getter
* @param module
*/
function getModuleName(module) {
if (!module._vmdModuleName) {
throw new Error("ERR_GET_MODULE_NAME : Could not get module accessor.\n Make sure your module has name, we can't make accessors for unnamed modules\n i.e. @Module({ name: 'something' })");
}
return "vuexModuleDecorators/" + module._vmdModuleName;
}
var VuexModule = /** @class */ (function () {
function VuexModule(module) {
this.actions = module.actions;
this.mutations = module.mutations;
this.state = module.state;
this.getters = module.getters;
this.namespaced = module.namespaced;
this.modules = module.modules;
}
return VuexModule;
}());
function getModule(moduleClass, store) {
var moduleName = getModuleName(moduleClass);
if (store && store.getters[moduleName]) {
return store.getters[moduleName];
}
else if (moduleClass._statics) {
return moduleClass._statics;
}
var genStatic = moduleClass._genStatic;
if (!genStatic) {
throw new Error("ERR_GET_MODULE_NO_STATICS : Could not get module accessor.\n Make sure your module has name, we can't make accessors for unnamed modules\n i.e. @Module({ name: 'something' })");
}
var storeModule = genStatic(store);
if (store) {
store.getters[moduleName] = storeModule;
}
else {
moduleClass._statics = storeModule;
}
return storeModule;
}
var reservedKeys = ['actions', 'getters', 'mutations', 'modules', 'state', 'namespaced', 'commit'];
function stateFactory(module) {
var state = new module.prototype.constructor({});
var s = {};
Object.keys(state).forEach(function (key) {
if (reservedKeys.indexOf(key) !== -1) {
if (typeof state[key] !== 'undefined') {
throw new Error("ERR_RESERVED_STATE_KEY_USED: You cannot use the following\n ['actions', 'getters', 'mutations', 'modules', 'state', 'namespaced', 'commit']\n as fields in your module. These are reserved as they have special purpose in Vuex");
}
return;
}
if (state.hasOwnProperty(key)) {
if (typeof state[key] !== 'function') {
s[key] = state[key];
}
}
});
return s;
}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
function staticStateGenerator(module, modOpt, statics) {
var state = modOpt.stateFactory ? module.state() : module.state;
Object.keys(state).forEach(function (key) {
if (state.hasOwnProperty(key)) {
// If not undefined or function means it is a state value
if (['undefined', 'function'].indexOf(typeof state[key]) === -1) {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.state[modOpt.name][key];
}
});
}
}
});
}
function staticGetterGenerator(module, modOpt, statics) {
Object.keys(module.getters).forEach(function (key) {
if (module.namespaced) {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.getters[modOpt.name + "/" + key];
}
});
}
else {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.getters[key];
}
});
}
});
}
function staticMutationGenerator(module, modOpt, statics) {
Object.keys(module.mutations).forEach(function (key) {
if (module.namespaced) {
statics[key] = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
(_a = statics.store).commit.apply(_a, __spreadArrays([modOpt.name + "/" + key], args));
};
}
else {
statics[key] = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
(_a = statics.store).commit.apply(_a, __spreadArrays([key], args));
};
}
});
}
function staticActionGenerators(module, modOpt, statics) {
Object.keys(module.actions).forEach(function (key) {
if (module.namespaced) {
statics[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
return [2 /*return*/, (_a = statics.store).dispatch.apply(_a, __spreadArrays([modOpt.name + "/" + key], args))];
});
});
};
}
else {
statics[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
return [2 /*return*/, (_a = statics.store).dispatch.apply(_a, __spreadArrays([key], args))];
});
});
};
}
});
}
function registerDynamicModule(module, modOpt) {
if (!modOpt.name) {
throw new Error('Name of module not provided in decorator options');
}
if (!modOpt.store) {
throw new Error('Store not provided in decorator options when using dynamic option');
}
modOpt.store.registerModule(modOpt.name, // TODO: Handle nested modules too in future
module, { preserveState: modOpt.preserveState || false });
}
function moduleDecoratorFactory(moduleOptions) {
return function (constructor) {
var module = constructor;
var stateFactory$1 = function () { return stateFactory(module); };
if (!module.state) {
module.state = moduleOptions && moduleOptions.stateFactory ? stateFactory$1 : stateFactory$1();
}
if (!module.getters) {
module.getters = {};
}
if (!module.namespaced) {
module.namespaced = moduleOptions && moduleOptions.namespaced;
}
Object.getOwnPropertyNames(module.prototype).forEach(function (funcName) {
var descriptor = Object.getOwnPropertyDescriptor(module.prototype, funcName);
if (descriptor.get && module.getters) {
module.getters[funcName] = function (state, getters, rootState, rootGetters) {
var thisObj = { context: { state: state, getters: getters, rootState: rootState, rootGetters: rootGetters } };
addPropertiesToObject(thisObj, state);
addPropertiesToObject(thisObj, getters);
var got = descriptor.get.call(thisObj);
return got;
};
}
});
var modOpt = moduleOptions;
if (modOpt.name) {
Object.defineProperty(constructor, '_genStatic', {
value: function (store) {
var statics = { store: store || modOpt.store };
if (!statics.store) {
throw new Error("ERR_STORE_NOT_PROVIDED: To use getModule(), either the module\n should be decorated with store in decorator, i.e. @Module({store: store}) or\n store should be passed when calling getModule(), i.e. getModule(MyModule, this.$store)");
}
// =========== For statics ==============
// ------ state -------
staticStateGenerator(module, modOpt, statics);
// ------- getters -------
if (module.getters) {
staticGetterGenerator(module, modOpt, statics);
}
// -------- mutations --------
if (module.mutations) {
staticMutationGenerator(module, modOpt, statics);
}
// -------- actions ---------
if (module.actions) {
staticActionGenerators(module, modOpt, statics);
}
return statics;
}
});
Object.defineProperty(constructor, '_vmdModuleName', {
value: modOpt.name
});
}
if (modOpt.dynamic) {
registerDynamicModule(module, modOpt);
}
return constructor;
};
}
function Module(modOrOpt) {
if (typeof modOrOpt === 'function') {
/*
* @Module decorator called without options (directly on the class definition)
*/
moduleDecoratorFactory({})(modOrOpt);
}
else {
/*
* @Module({...}) decorator called with options
*/
return moduleDecoratorFactory(modOrOpt);
}
}
function actionDecoratorFactory(params) {
var _a = params || {}, _b = _a.commit, commit = _b === void 0 ? undefined : _b, _c = _a.rawError, rawError = _c === void 0 ? false : _c, _d = _a.root, root = _d === void 0 ? false : _d;
return function (target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('actions')) {
module.actions = Object.assign({}, module.actions);
}
var actionFunction = descriptor.value;
var action = function (context, payload) {
return __awaiter(this, void 0, void 0, function () {
var actionPayload, moduleName, moduleAccessor, thisObj, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
actionPayload = null;
if (!module._genStatic) return [3 /*break*/, 2];
moduleName = getModuleName(module);
moduleAccessor = context.rootGetters[moduleName]
? context.rootGetters[moduleName]
: getModule(module);
moduleAccessor.context = context;
return [4 /*yield*/, actionFunction.call(moduleAccessor, payload)];
case 1:
actionPayload = _a.sent();
return [3 /*break*/, 4];
case 2:
thisObj = { context: context };
addPropertiesToObject(thisObj, context.state);
addPropertiesToObject(thisObj, context.getters);
return [4 /*yield*/, actionFunction.call(thisObj, payload)];
case 3:
actionPayload = _a.sent();
_a.label = 4;
case 4:
if (commit) {
context.commit(commit, actionPayload);
}
return [2 /*return*/, actionPayload];
case 5:
e_1 = _a.sent();
throw rawError
? e_1
: new Error('ERR_ACTION_ACCESS_UNDEFINED: Are you trying to access ' +
'this.someMutation() or this.someGetter inside an @Action? \n' +
'That works only in dynamic modules. \n' +
'If not dynamic use this.context.commit("mutationName", payload) ' +
'and this.context.getters["getterName"]' +
'\n' +
new Error("Could not perform action " + key.toString()).stack +
'\n' +
e_1.stack);
case 6: return [2 /*return*/];
}
});
});
};
module.actions[key] = root ? { root: root, handler: action } : action;
};
}
/**
* The @Action decorator turns an async function into an Vuex action
*
* @param targetOrParams the module class
* @param key name of the action
* @param descriptor the action function descriptor
* @constructor
*/
function Action(targetOrParams, key, descriptor) {
if (!key && !descriptor) {
/*
* This is the case when `targetOrParams` is params.
* i.e. when used as -
* <pre>
@Action({commit: 'incrCount'})
async getCountDelta() {
return 5
}
* </pre>
*/
return actionDecoratorFactory(targetOrParams);
}
else {
/*
* This is the case when @Action is called on action function
* without any params
* <pre>
* @Action
* async doSomething() {
* ...
* }
* </pre>
*/
actionDecoratorFactory()(targetOrParams, key, descriptor);
}
}
function Mutation(target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('mutations')) {
module.mutations = Object.assign({}, module.mutations);
}
var mutationFunction = descriptor.value;
var mutation = function (state, payload) {
mutationFunction.call(state, payload);
};
module.mutations[key] = mutation;
}
function mutationActionDecoratorFactory(params) {
return function (target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('mutations')) {
module.mutations = Object.assign({}, module.mutations);
}
if (!module.hasOwnProperty('actions')) {
module.actions = Object.assign({}, module.actions);
}
var mutactFunction = descriptor.value;
var action = function (context, payload) {
return __awaiter(this, void 0, void 0, function () {
var actionPayload, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, mutactFunction.call(context, payload)];
case 1:
actionPayload = _a.sent();
context.commit(key, actionPayload);
return [3 /*break*/, 3];
case 2:
e_1 = _a.sent();
if (params.rawError) {
throw e_1;
}
else {
console.error('Could not perform action ' + key.toString());
console.error(e_1);
return [2 /*return*/, Promise.reject(e_1)];
}
case 3: return [2 /*return*/];
}
});
});
};
var mutation = function (state, payload) {
if (!params.mutate) {
params.mutate = Object.keys(payload);
}
for (var _i = 0, _a = params.mutate; _i < _a.length; _i++) {
var stateItem = _a[_i];
if (state.hasOwnProperty(stateItem) && payload.hasOwnProperty(stateItem)) {
state[stateItem] = payload[stateItem];
}
else {
throw new Error("ERR_MUTATE_PARAMS_NOT_IN_PAYLOAD\n In @MutationAction, mutate: ['a', 'b', ...] array keys must\n match with return type = {a: {}, b: {}, ...} and must\n also be in state.");
}
}
};
module.actions[key] = params.root ? { root: true, handler: action } : action;
module.mutations[key] = mutation;
};
}
/**
* The @MutationAction decorator turns this into an action that further calls a mutation
* Both the action and the mutation are generated for you
*
* @param paramsOrTarget the params or the target class
* @param key the name of the function
* @param descriptor the function body
* @constructor
*/
function MutationAction(paramsOrTarget, key, descriptor) {
if (!key && !descriptor) {
/*
* This is the case when `paramsOrTarget` is params.
* i.e. when used as -
* <pre>
@MutationAction({mutate: ['incrCount']})
async getCountDelta() {
return {incrCount: 5}
}
* </pre>
*/
return mutationActionDecoratorFactory(paramsOrTarget);
}
else {
/*
* This is the case when `paramsOrTarget` is target.
* i.e. when used as -
* <pre>
@MutationAction
async getCountDelta() {
return {incrCount: 5}
}
* </pre>
*/
mutationActionDecoratorFactory({})(paramsOrTarget, key, descriptor);
}
}
exports.Action = Action;
exports.Module = Module;
exports.Mutation = Mutation;
exports.MutationAction = MutationAction;
exports.VuexModule = VuexModule;
exports.getModule = getModule;
//# sourceMappingURL=index.js.map
File diff suppressed because one or more lines are too long
+521
View File
@@ -0,0 +1,521 @@
/**
* Takes the properties on object from parameter source and adds them to the object
* parameter target
* @param {object} target Object to have properties copied onto from y
* @param {object} source Object with properties to be copied to x
*/
function addPropertiesToObject(target, source) {
var _loop_1 = function (k) {
Object.defineProperty(target, k, {
get: function () { return source[k]; }
});
};
for (var _i = 0, _a = Object.keys(source || {}); _i < _a.length; _i++) {
var k = _a[_i];
_loop_1(k);
}
}
/**
* Returns a namespaced name of the module to be used as a store getter
* @param module
*/
function getModuleName(module) {
if (!module._vmdModuleName) {
throw new Error("ERR_GET_MODULE_NAME : Could not get module accessor.\n Make sure your module has name, we can't make accessors for unnamed modules\n i.e. @Module({ name: 'something' })");
}
return "vuexModuleDecorators/" + module._vmdModuleName;
}
var VuexModule = /** @class */ (function () {
function VuexModule(module) {
this.actions = module.actions;
this.mutations = module.mutations;
this.state = module.state;
this.getters = module.getters;
this.namespaced = module.namespaced;
this.modules = module.modules;
}
return VuexModule;
}());
function getModule(moduleClass, store) {
var moduleName = getModuleName(moduleClass);
if (store && store.getters[moduleName]) {
return store.getters[moduleName];
}
else if (moduleClass._statics) {
return moduleClass._statics;
}
var genStatic = moduleClass._genStatic;
if (!genStatic) {
throw new Error("ERR_GET_MODULE_NO_STATICS : Could not get module accessor.\n Make sure your module has name, we can't make accessors for unnamed modules\n i.e. @Module({ name: 'something' })");
}
var storeModule = genStatic(store);
if (store) {
store.getters[moduleName] = storeModule;
}
else {
moduleClass._statics = storeModule;
}
return storeModule;
}
var reservedKeys = ['actions', 'getters', 'mutations', 'modules', 'state', 'namespaced', 'commit'];
function stateFactory(module) {
var state = new module.prototype.constructor({});
var s = {};
Object.keys(state).forEach(function (key) {
if (reservedKeys.indexOf(key) !== -1) {
if (typeof state[key] !== 'undefined') {
throw new Error("ERR_RESERVED_STATE_KEY_USED: You cannot use the following\n ['actions', 'getters', 'mutations', 'modules', 'state', 'namespaced', 'commit']\n as fields in your module. These are reserved as they have special purpose in Vuex");
}
return;
}
if (state.hasOwnProperty(key)) {
if (typeof state[key] !== 'function') {
s[key] = state[key];
}
}
});
return s;
}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
function staticStateGenerator(module, modOpt, statics) {
var state = modOpt.stateFactory ? module.state() : module.state;
Object.keys(state).forEach(function (key) {
if (state.hasOwnProperty(key)) {
// If not undefined or function means it is a state value
if (['undefined', 'function'].indexOf(typeof state[key]) === -1) {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.state[modOpt.name][key];
}
});
}
}
});
}
function staticGetterGenerator(module, modOpt, statics) {
Object.keys(module.getters).forEach(function (key) {
if (module.namespaced) {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.getters[modOpt.name + "/" + key];
}
});
}
else {
Object.defineProperty(statics, key, {
get: function () {
return statics.store.getters[key];
}
});
}
});
}
function staticMutationGenerator(module, modOpt, statics) {
Object.keys(module.mutations).forEach(function (key) {
if (module.namespaced) {
statics[key] = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
(_a = statics.store).commit.apply(_a, __spreadArrays([modOpt.name + "/" + key], args));
};
}
else {
statics[key] = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
(_a = statics.store).commit.apply(_a, __spreadArrays([key], args));
};
}
});
}
function staticActionGenerators(module, modOpt, statics) {
Object.keys(module.actions).forEach(function (key) {
if (module.namespaced) {
statics[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
return [2 /*return*/, (_a = statics.store).dispatch.apply(_a, __spreadArrays([modOpt.name + "/" + key], args))];
});
});
};
}
else {
statics[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
return [2 /*return*/, (_a = statics.store).dispatch.apply(_a, __spreadArrays([key], args))];
});
});
};
}
});
}
function registerDynamicModule(module, modOpt) {
if (!modOpt.name) {
throw new Error('Name of module not provided in decorator options');
}
if (!modOpt.store) {
throw new Error('Store not provided in decorator options when using dynamic option');
}
modOpt.store.registerModule(modOpt.name, // TODO: Handle nested modules too in future
module, { preserveState: modOpt.preserveState || false });
}
function moduleDecoratorFactory(moduleOptions) {
return function (constructor) {
var module = constructor;
var stateFactory$1 = function () { return stateFactory(module); };
if (!module.state) {
module.state = moduleOptions && moduleOptions.stateFactory ? stateFactory$1 : stateFactory$1();
}
if (!module.getters) {
module.getters = {};
}
if (!module.namespaced) {
module.namespaced = moduleOptions && moduleOptions.namespaced;
}
Object.getOwnPropertyNames(module.prototype).forEach(function (funcName) {
var descriptor = Object.getOwnPropertyDescriptor(module.prototype, funcName);
if (descriptor.get && module.getters) {
module.getters[funcName] = function (state, getters, rootState, rootGetters) {
var thisObj = { context: { state: state, getters: getters, rootState: rootState, rootGetters: rootGetters } };
addPropertiesToObject(thisObj, state);
addPropertiesToObject(thisObj, getters);
var got = descriptor.get.call(thisObj);
return got;
};
}
});
var modOpt = moduleOptions;
if (modOpt.name) {
Object.defineProperty(constructor, '_genStatic', {
value: function (store) {
var statics = { store: store || modOpt.store };
if (!statics.store) {
throw new Error("ERR_STORE_NOT_PROVIDED: To use getModule(), either the module\n should be decorated with store in decorator, i.e. @Module({store: store}) or\n store should be passed when calling getModule(), i.e. getModule(MyModule, this.$store)");
}
// =========== For statics ==============
// ------ state -------
staticStateGenerator(module, modOpt, statics);
// ------- getters -------
if (module.getters) {
staticGetterGenerator(module, modOpt, statics);
}
// -------- mutations --------
if (module.mutations) {
staticMutationGenerator(module, modOpt, statics);
}
// -------- actions ---------
if (module.actions) {
staticActionGenerators(module, modOpt, statics);
}
return statics;
}
});
Object.defineProperty(constructor, '_vmdModuleName', {
value: modOpt.name
});
}
if (modOpt.dynamic) {
registerDynamicModule(module, modOpt);
}
return constructor;
};
}
function Module(modOrOpt) {
if (typeof modOrOpt === 'function') {
/*
* @Module decorator called without options (directly on the class definition)
*/
moduleDecoratorFactory({})(modOrOpt);
}
else {
/*
* @Module({...}) decorator called with options
*/
return moduleDecoratorFactory(modOrOpt);
}
}
function actionDecoratorFactory(params) {
var _a = params || {}, _b = _a.commit, commit = _b === void 0 ? undefined : _b, _c = _a.rawError, rawError = _c === void 0 ? false : _c, _d = _a.root, root = _d === void 0 ? false : _d;
return function (target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('actions')) {
module.actions = Object.assign({}, module.actions);
}
var actionFunction = descriptor.value;
var action = function (context, payload) {
return __awaiter(this, void 0, void 0, function () {
var actionPayload, moduleName, moduleAccessor, thisObj, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
actionPayload = null;
if (!module._genStatic) return [3 /*break*/, 2];
moduleName = getModuleName(module);
moduleAccessor = context.rootGetters[moduleName]
? context.rootGetters[moduleName]
: getModule(module);
moduleAccessor.context = context;
return [4 /*yield*/, actionFunction.call(moduleAccessor, payload)];
case 1:
actionPayload = _a.sent();
return [3 /*break*/, 4];
case 2:
thisObj = { context: context };
addPropertiesToObject(thisObj, context.state);
addPropertiesToObject(thisObj, context.getters);
return [4 /*yield*/, actionFunction.call(thisObj, payload)];
case 3:
actionPayload = _a.sent();
_a.label = 4;
case 4:
if (commit) {
context.commit(commit, actionPayload);
}
return [2 /*return*/, actionPayload];
case 5:
e_1 = _a.sent();
throw rawError
? e_1
: new Error('ERR_ACTION_ACCESS_UNDEFINED: Are you trying to access ' +
'this.someMutation() or this.someGetter inside an @Action? \n' +
'That works only in dynamic modules. \n' +
'If not dynamic use this.context.commit("mutationName", payload) ' +
'and this.context.getters["getterName"]' +
'\n' +
new Error("Could not perform action " + key.toString()).stack +
'\n' +
e_1.stack);
case 6: return [2 /*return*/];
}
});
});
};
module.actions[key] = root ? { root: root, handler: action } : action;
};
}
/**
* The @Action decorator turns an async function into an Vuex action
*
* @param targetOrParams the module class
* @param key name of the action
* @param descriptor the action function descriptor
* @constructor
*/
function Action(targetOrParams, key, descriptor) {
if (!key && !descriptor) {
/*
* This is the case when `targetOrParams` is params.
* i.e. when used as -
* <pre>
@Action({commit: 'incrCount'})
async getCountDelta() {
return 5
}
* </pre>
*/
return actionDecoratorFactory(targetOrParams);
}
else {
/*
* This is the case when @Action is called on action function
* without any params
* <pre>
* @Action
* async doSomething() {
* ...
* }
* </pre>
*/
actionDecoratorFactory()(targetOrParams, key, descriptor);
}
}
function Mutation(target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('mutations')) {
module.mutations = Object.assign({}, module.mutations);
}
var mutationFunction = descriptor.value;
var mutation = function (state, payload) {
mutationFunction.call(state, payload);
};
module.mutations[key] = mutation;
}
function mutationActionDecoratorFactory(params) {
return function (target, key, descriptor) {
var module = target.constructor;
if (!module.hasOwnProperty('mutations')) {
module.mutations = Object.assign({}, module.mutations);
}
if (!module.hasOwnProperty('actions')) {
module.actions = Object.assign({}, module.actions);
}
var mutactFunction = descriptor.value;
var action = function (context, payload) {
return __awaiter(this, void 0, void 0, function () {
var actionPayload, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, mutactFunction.call(context, payload)];
case 1:
actionPayload = _a.sent();
context.commit(key, actionPayload);
return [3 /*break*/, 3];
case 2:
e_1 = _a.sent();
if (params.rawError) {
throw e_1;
}
else {
console.error('Could not perform action ' + key.toString());
console.error(e_1);
return [2 /*return*/, Promise.reject(e_1)];
}
case 3: return [2 /*return*/];
}
});
});
};
var mutation = function (state, payload) {
if (!params.mutate) {
params.mutate = Object.keys(payload);
}
for (var _i = 0, _a = params.mutate; _i < _a.length; _i++) {
var stateItem = _a[_i];
if (state.hasOwnProperty(stateItem) && payload.hasOwnProperty(stateItem)) {
state[stateItem] = payload[stateItem];
}
else {
throw new Error("ERR_MUTATE_PARAMS_NOT_IN_PAYLOAD\n In @MutationAction, mutate: ['a', 'b', ...] array keys must\n match with return type = {a: {}, b: {}, ...} and must\n also be in state.");
}
}
};
module.actions[key] = params.root ? { root: true, handler: action } : action;
module.mutations[key] = mutation;
};
}
/**
* The @MutationAction decorator turns this into an action that further calls a mutation
* Both the action and the mutation are generated for you
*
* @param paramsOrTarget the params or the target class
* @param key the name of the function
* @param descriptor the function body
* @constructor
*/
function MutationAction(paramsOrTarget, key, descriptor) {
if (!key && !descriptor) {
/*
* This is the case when `paramsOrTarget` is params.
* i.e. when used as -
* <pre>
@MutationAction({mutate: ['incrCount']})
async getCountDelta() {
return {incrCount: 5}
}
* </pre>
*/
return mutationActionDecoratorFactory(paramsOrTarget);
}
else {
/*
* This is the case when `paramsOrTarget` is target.
* i.e. when used as -
* <pre>
@MutationAction
async getCountDelta() {
return {incrCount: 5}
}
* </pre>
*/
mutationActionDecoratorFactory({})(paramsOrTarget, key, descriptor);
}
}
export { Action, Module, Mutation, MutationAction, VuexModule, getModule };
//# sourceMappingURL=index.js.map
File diff suppressed because one or more lines are too long
+10
View File
@@ -0,0 +1,10 @@
/**
* Parameters that can be passed to the @Action decorator
*/
export interface ActionDecoratorParams {
commit?: string;
rawError?: boolean;
root?: boolean;
}
export declare function Action<T, R>(target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => R>): void;
export declare function Action<T>(params: ActionDecoratorParams): MethodDecorator;
+12
View File
@@ -0,0 +1,12 @@
/**
* Takes the properties on object from parameter source and adds them to the object
* parameter target
* @param {object} target Object to have properties copied onto from y
* @param {object} source Object with properties to be copied to x
*/
export declare function addPropertiesToObject(target: any, source: any): void;
/**
* Returns a namespaced name of the module to be used as a store getter
* @param module
*/
export declare function getModuleName(module: any): string;
+5
View File
@@ -0,0 +1,5 @@
export { VuexModule, getModule } from './vuexmodule';
export { Module } from './module';
export { Action } from './action';
export { Mutation } from './mutation';
export { MutationAction } from './mutationaction';
+4
View File
@@ -0,0 +1,4 @@
import { Module as Mod } from 'vuex';
import { ModuleOptions } from '../moduleoptions';
export declare function Module<S>(module: Function & Mod<S, any>): void;
export declare function Module<S>(options: ModuleOptions): ClassDecorator;
@@ -0,0 +1,2 @@
import { Module as Mod } from 'vuex';
export declare function stateFactory<S>(module: Function & Mod<S, any>): S;
@@ -0,0 +1,6 @@
import { Module as Mod } from 'vuex';
import { DynamicModuleOptions } from '../moduleoptions';
export declare function staticStateGenerator<S extends Object>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticGetterGenerator<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticMutationGenerator<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticActionGenerators<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
+45
View File
@@ -0,0 +1,45 @@
import { Store } from 'vuex';
/**
* Options to pass to the @Module decorator
*/
export interface StaticModuleOptions {
/**
* name of module, if being namespaced
*/
name?: string;
/**
* whether or not the module is namespaced
*/
namespaced?: boolean;
/**
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
}
export interface DynamicModuleOptions {
/**
* If this is a dynamic module (added to store after store is created)
*/
dynamic: true;
/**
* The store into which this module will be injected
*/
store: Store<any>;
/**
* name of module, compulsory for dynamic modules
*/
name: string;
/**
* If this is enabled it will preserve the state when loading the module
*/
preserveState?: boolean;
/**
* whether or not the module is namespaced
*/
namespaced?: boolean;
/**
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
}
export declare type ModuleOptions = StaticModuleOptions | DynamicModuleOptions;
+1
View File
@@ -0,0 +1 @@
export declare function Mutation<T extends Object, R>(target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => R>): void;
+9
View File
@@ -0,0 +1,9 @@
export interface MutationActionParams<M> {
mutate?: (keyof Partial<M>)[];
rawError?: boolean;
root?: boolean;
}
export declare function MutationAction<K, T extends K>(target: {
[k in keyof T]: T[k] | null;
}, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<K>>): void;
export declare function MutationAction<T>(params: MutationActionParams<T>): (target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<T>>) => void;
+22
View File
@@ -0,0 +1,22 @@
import { ActionTree, GetterTree, Module as Mod, ModuleTree, MutationTree, Store, ActionContext } from 'vuex';
export declare class VuexModule<S = ThisType<any>, R = any> implements Mod<S, R> {
static namespaced?: boolean;
static state?: any | (() => any);
static getters?: GetterTree<any, any>;
static actions?: ActionTree<any, any>;
static mutations?: MutationTree<any>;
static modules?: ModuleTree<any>;
modules?: ModuleTree<any>;
namespaced?: boolean;
getters?: GetterTree<S, R>;
state?: S | (() => S);
mutations?: MutationTree<S>;
actions?: ActionTree<S, R>;
context: ActionContext<S, R>;
constructor(module: Mod<S, any>);
}
declare type ConstructorOf<C> = {
new (...args: any[]): C;
};
export declare function getModule<M extends VuexModule>(moduleClass: ConstructorOf<M>, store?: Store<any>): M;
export {};