@mediv0/v-bucket
Advanced tools
Comparing version 1.1.1 to 1.1.2
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
var vue = require("vue"); | ||
var vue = require('vue'); | ||
var bucketKey = "bucket"; | ||
function useBucket(key) { | ||
if (key === void 0) key = null; | ||
if ( key === void 0 ) key = null; | ||
@@ -17,3 +17,3 @@ return vue.inject(key !== null ? key : bucketKey); | ||
var NoOptionException = /*@__PURE__*/ (function(Error) { | ||
var NoOptionException = /*@__PURE__*/(function (Error) { | ||
function NoOptionException(message) { | ||
@@ -24,10 +24,10 @@ Error.call(this, message); | ||
if (Error) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create( Error && Error.prototype ); | ||
NoOptionException.prototype.constructor = NoOptionException; | ||
return NoOptionException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidModulePath = /*@__PURE__*/ (function(Error) { | ||
var InvalidModulePath = /*@__PURE__*/(function (Error) { | ||
function InvalidModulePath(message) { | ||
@@ -38,10 +38,10 @@ Error.call(this, message); | ||
if (Error) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create( Error && Error.prototype ); | ||
InvalidModulePath.prototype.constructor = InvalidModulePath; | ||
return InvalidModulePath; | ||
})(Error); | ||
}(Error)); | ||
var ModuleNotFound = /*@__PURE__*/ (function(Error) { | ||
var ModuleNotFound = /*@__PURE__*/(function (Error) { | ||
function ModuleNotFound(message) { | ||
@@ -52,10 +52,10 @@ Error.call(this, message); | ||
if (Error) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create( Error && Error.prototype ); | ||
ModuleNotFound.prototype.constructor = ModuleNotFound; | ||
return ModuleNotFound; | ||
})(Error); | ||
}(Error)); | ||
var InvalidCommitException = /*@__PURE__*/ (function(Error) { | ||
var InvalidCommitException = /*@__PURE__*/(function (Error) { | ||
function InvalidCommitException(message) { | ||
@@ -66,10 +66,10 @@ Error.call(this, message); | ||
if (Error) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidCommitException.prototype.constructor = InvalidCommitException; | ||
return InvalidCommitException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidDispatchException = /*@__PURE__*/ (function(Error) { | ||
var InvalidDispatchException = /*@__PURE__*/(function (Error) { | ||
function InvalidDispatchException(message) { | ||
@@ -80,12 +80,10 @@ Error.call(this, message); | ||
if (Error) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidDispatchException.prototype.constructor = InvalidDispatchException; | ||
return InvalidDispatchException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidGetterException = /*@__PURE__*/ (function(Error) { | ||
var InvalidGetterException = /*@__PURE__*/(function (Error) { | ||
function InvalidGetterException(message, namespace) { | ||
@@ -97,4 +95,4 @@ Error.call(this, message); | ||
if (Error) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidGetterException.prototype.constructor = InvalidGetterException; | ||
@@ -104,16 +102,16 @@ | ||
staticAccessors.namespace.get = function() { | ||
staticAccessors.namespace.get = function () { | ||
return this._namespace; | ||
}; | ||
staticAccessors.namespace.set = function(v) { | ||
staticAccessors.namespace.set = function (v) { | ||
this._namespace = v; | ||
}; | ||
Object.defineProperties(InvalidGetterException, staticAccessors); | ||
Object.defineProperties( InvalidGetterException, staticAccessors ); | ||
return InvalidGetterException; | ||
})(Error); | ||
}(Error)); | ||
var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) { | ||
var InstallPluginsOnModulesException = /*@__PURE__*/(function (Error) { | ||
function InstallPluginsOnModulesException(message) { | ||
@@ -124,10 +122,8 @@ Error.call(this, message); | ||
if (Error) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( Error && Error.prototype ); | ||
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException; | ||
return InstallPluginsOnModulesException; | ||
})(Error); | ||
}(Error)); | ||
@@ -149,6 +145,3 @@ function isDefined(v) { | ||
if (splitted[i].length === 0) { | ||
throw new InvalidModulePath( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
path | ||
); | ||
throw new InvalidModulePath(("\n Invalid Path. we couldn't find any module in the " + path)); | ||
} | ||
@@ -167,11 +160,8 @@ } | ||
} | ||
return arr.every(function(v) { | ||
return typeof v === "string"; | ||
}); | ||
return arr.every(function (v) { return typeof v === "string"; }); | ||
} | ||
function arrayToPath() { | ||
var path = [], | ||
len = arguments.length; | ||
while (len--) path[len] = arguments[len]; | ||
var path = [], len = arguments.length; | ||
while ( len-- ) path[ len ] = arguments[ len ]; | ||
@@ -195,5 +185,3 @@ return path.join("/"); | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
var _everyFn = arr.every(function(el) { | ||
return typeof el === "function"; | ||
}); | ||
var _everyFn = arr.every(function (el) { return typeof el === "function"; }); | ||
return _everyFn; | ||
@@ -225,6 +213,3 @@ } | ||
throw new ModuleNotFound( | ||
"We couldn't find your requested module. path: " + | ||
path + | ||
" # module: " + | ||
_lastModuleName | ||
("We couldn't find your requested module. path: " + path + " # module: " + _lastModuleName) | ||
); | ||
@@ -250,9 +235,8 @@ } | ||
if (_root._modulesDictionary.size) { | ||
_root._modulesDictionary.forEach(function(val, key) { | ||
_root._modulesDictionary.forEach(function (val, key) { | ||
_root._states[key] = val._states; | ||
}); | ||
} | ||
} else { | ||
return; | ||
} | ||
return; | ||
} | ||
@@ -263,5 +247,3 @@ | ||
throw new NoOptionException( | ||
"\n you are passing " + | ||
opts + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
("\n you are passing " + opts + " as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ") | ||
); | ||
@@ -281,15 +263,12 @@ } | ||
var prototypeAccessors = { | ||
state: { configurable: true }, | ||
getters: { configurable: true } | ||
}; | ||
var prototypeAccessors = { state: { configurable: true },getters: { configurable: true } }; | ||
Bucket.prototype.initializeSettings = function initializeSettings(ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
Bucket.prototype.initializeSettings = function initializeSettings (ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
@@ -300,3 +279,6 @@ // internal variables | ||
this._mutations = mutations || Object.create(null); | ||
this._getters = this.interceptGetters(this, getters || Object.create(null)); | ||
this._getters = this.interceptGetters( | ||
this, | ||
getters || Object.create(null) | ||
); | ||
this._actions = actions || Object.create(null); | ||
@@ -311,17 +293,15 @@ this._modules = modules || Object.create(null); | ||
Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) { | ||
Bucket.prototype.triggerCommit = function triggerCommit (_name, _payload) { | ||
// check if path is valid | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._mutations[actionName]; | ||
if (!_fn) { | ||
throw new InvalidCommitException( | ||
"\n commit " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidCommitException(("\n commit " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -333,3 +313,3 @@ _fn(module._data, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -339,20 +319,14 @@ }); | ||
Bucket.prototype.triggerDispatch = function triggerDispatch( | ||
_self, | ||
_name, | ||
_payload | ||
) { | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
Bucket.prototype.triggerDispatch = function triggerDispatch (_self, _name, _payload) { | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._actions[actionName]; | ||
if (!_fn) { | ||
throw new InvalidDispatchException( | ||
"\n dispatch " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidDispatchException(("\n dispatch " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -364,3 +338,3 @@ var _asyncDispatch = _fn(_self, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -375,10 +349,8 @@ }); | ||
prototypeAccessors.state.get = function() { | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
); | ||
prototypeAccessors.state.get = function () { | ||
console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "); | ||
return this._states; | ||
}; | ||
prototypeAccessors.getters.get = function() { | ||
prototypeAccessors.getters.get = function () { | ||
return this._getters; | ||
@@ -388,3 +360,3 @@ }; | ||
// refactor: create multiple functions for if-else | ||
Bucket.prototype.interceptGetters = function interceptGetters(_self, _target) { | ||
Bucket.prototype.interceptGetters = function interceptGetters (_self, _target) { | ||
// finding correct getter recursively from root to bottom | ||
@@ -396,5 +368,5 @@ return new Proxy(_target, { | ||
var ref = searchNestedModules(_path, _self); | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
// saving a reference to the current module name, in case we don't find any getters. | ||
@@ -409,13 +381,6 @@ InvalidGetterException.namespace = nextModuleName; | ||
if (!target[prop]) { | ||
throw new InvalidGetterException( | ||
"\n getter " + | ||
prop + | ||
" in " + | ||
(InvalidGetterException.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
throw new InvalidGetterException(("\n getter " + prop + " in " + (InvalidGetterException.namespace || | ||
"root") + " module is not found. please check your getter.\n ")); | ||
} | ||
return vue.computed(function() { | ||
return target[prop](_self._data); | ||
}).value; | ||
return vue.computed(function () { return target[prop](_self._data); }).value; | ||
} | ||
@@ -426,8 +391,8 @@ } | ||
Bucket.prototype.installModules = function installModules(_instance) { | ||
if (_instance === void 0) _instance = this; | ||
Bucket.prototype.installModules = function installModules (_instance) { | ||
if ( _instance === void 0 ) _instance = this; | ||
var entries = Object.entries(_instance._modules); | ||
if (entries.length) { | ||
entries.forEach(function(entry) { | ||
entries.forEach(function (entry) { | ||
/* | ||
@@ -437,9 +402,4 @@ installModules will be called on new Bucket instance. | ||
*/ | ||
var bucket = new Bucket( | ||
Object.assign( | ||
{}, | ||
{ name: entry[0] }, // set name for each module | ||
entry[1] | ||
) | ||
); | ||
var bucket = new Bucket(Object.assign({}, {name: entry[0]}, // set name for each module | ||
entry[1])); | ||
_instance._modulesDictionary.set(entry[0], bucket); | ||
@@ -452,9 +412,7 @@ }); | ||
Bucket.prototype.installPlugins = function installPlugins(_plugins) { | ||
var this$1 = this; | ||
Bucket.prototype.installPlugins = function installPlugins (_plugins) { | ||
var this$1$1 = this; | ||
if (this._name !== "root" && hasPlugin(_plugins)) { | ||
throw new InstallPluginsOnModulesException( | ||
"\n You can only register plugins in the root module." | ||
); | ||
throw new InstallPluginsOnModulesException("\n You can only register plugins in the root module."); | ||
} | ||
@@ -474,4 +432,4 @@ | ||
// call the plugins | ||
Object.values(_plugins).forEach(function(plugin) { | ||
plugin(this$1); | ||
Object.values(_plugins).forEach(function (plugin) { | ||
plugin(this$1$1); | ||
}); | ||
@@ -482,3 +440,3 @@ | ||
Bucket.prototype.notifyCommits = function notifyCommits(_data) { | ||
Bucket.prototype.notifyCommits = function notifyCommits (_data) { | ||
var _cbs = this._onMutationSubscribers; | ||
@@ -488,3 +446,3 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyActions = function notifyActions(_data) { | ||
Bucket.prototype.notifyActions = function notifyActions (_data) { | ||
var _cbs = this._onActionSubscribers; | ||
@@ -494,4 +452,5 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyPlugins = function notifyPlugins(_data, _cbs) { | ||
[].concat(_cbs.values()).forEach(function(cb) { | ||
Bucket.prototype.notifyPlugins = function notifyPlugins (_data, _cbs) { | ||
[].concat( _cbs.values() ).forEach(function (cb) { | ||
if (typeof cb !== "function") { return; } | ||
cb(_data); | ||
@@ -501,3 +460,3 @@ }); | ||
Bucket.prototype.install = function install(app, injectKey) { | ||
Bucket.prototype.install = function install (app, injectKey) { | ||
app.provide(injectKey || bucketKey, this); | ||
@@ -507,3 +466,3 @@ app.config.globalProperties.$bucket = this; | ||
Object.defineProperties(Bucket.prototype, prototypeAccessors); | ||
Object.defineProperties( Bucket.prototype, prototypeAccessors ); | ||
@@ -510,0 +469,0 @@ function createBucket(options) { |
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
import { inject, reactive, computed } from "vue"; | ||
import { inject, reactive, computed } from 'vue'; | ||
var bucketKey = "bucket"; | ||
function useBucket(key) { | ||
if (key === void 0) key = null; | ||
if ( key === void 0 ) key = null; | ||
@@ -15,3 +15,3 @@ return inject(key !== null ? key : bucketKey); | ||
var NoOptionException = /*@__PURE__*/ (function(Error) { | ||
var NoOptionException = /*@__PURE__*/(function (Error) { | ||
function NoOptionException(message) { | ||
@@ -22,10 +22,10 @@ Error.call(this, message); | ||
if (Error) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create( Error && Error.prototype ); | ||
NoOptionException.prototype.constructor = NoOptionException; | ||
return NoOptionException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidModulePath = /*@__PURE__*/ (function(Error) { | ||
var InvalidModulePath = /*@__PURE__*/(function (Error) { | ||
function InvalidModulePath(message) { | ||
@@ -36,10 +36,10 @@ Error.call(this, message); | ||
if (Error) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create( Error && Error.prototype ); | ||
InvalidModulePath.prototype.constructor = InvalidModulePath; | ||
return InvalidModulePath; | ||
})(Error); | ||
}(Error)); | ||
var ModuleNotFound = /*@__PURE__*/ (function(Error) { | ||
var ModuleNotFound = /*@__PURE__*/(function (Error) { | ||
function ModuleNotFound(message) { | ||
@@ -50,10 +50,10 @@ Error.call(this, message); | ||
if (Error) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create( Error && Error.prototype ); | ||
ModuleNotFound.prototype.constructor = ModuleNotFound; | ||
return ModuleNotFound; | ||
})(Error); | ||
}(Error)); | ||
var InvalidCommitException = /*@__PURE__*/ (function(Error) { | ||
var InvalidCommitException = /*@__PURE__*/(function (Error) { | ||
function InvalidCommitException(message) { | ||
@@ -64,10 +64,10 @@ Error.call(this, message); | ||
if (Error) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidCommitException.prototype.constructor = InvalidCommitException; | ||
return InvalidCommitException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidDispatchException = /*@__PURE__*/ (function(Error) { | ||
var InvalidDispatchException = /*@__PURE__*/(function (Error) { | ||
function InvalidDispatchException(message) { | ||
@@ -78,12 +78,10 @@ Error.call(this, message); | ||
if (Error) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidDispatchException.prototype.constructor = InvalidDispatchException; | ||
return InvalidDispatchException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidGetterException = /*@__PURE__*/ (function(Error) { | ||
var InvalidGetterException = /*@__PURE__*/(function (Error) { | ||
function InvalidGetterException(message, namespace) { | ||
@@ -95,4 +93,4 @@ Error.call(this, message); | ||
if (Error) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidGetterException.prototype.constructor = InvalidGetterException; | ||
@@ -102,16 +100,16 @@ | ||
staticAccessors.namespace.get = function() { | ||
staticAccessors.namespace.get = function () { | ||
return this._namespace; | ||
}; | ||
staticAccessors.namespace.set = function(v) { | ||
staticAccessors.namespace.set = function (v) { | ||
this._namespace = v; | ||
}; | ||
Object.defineProperties(InvalidGetterException, staticAccessors); | ||
Object.defineProperties( InvalidGetterException, staticAccessors ); | ||
return InvalidGetterException; | ||
})(Error); | ||
}(Error)); | ||
var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) { | ||
var InstallPluginsOnModulesException = /*@__PURE__*/(function (Error) { | ||
function InstallPluginsOnModulesException(message) { | ||
@@ -122,10 +120,8 @@ Error.call(this, message); | ||
if (Error) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( Error && Error.prototype ); | ||
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException; | ||
return InstallPluginsOnModulesException; | ||
})(Error); | ||
}(Error)); | ||
@@ -147,6 +143,3 @@ function isDefined(v) { | ||
if (splitted[i].length === 0) { | ||
throw new InvalidModulePath( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
path | ||
); | ||
throw new InvalidModulePath(("\n Invalid Path. we couldn't find any module in the " + path)); | ||
} | ||
@@ -165,11 +158,8 @@ } | ||
} | ||
return arr.every(function(v) { | ||
return typeof v === "string"; | ||
}); | ||
return arr.every(function (v) { return typeof v === "string"; }); | ||
} | ||
function arrayToPath() { | ||
var path = [], | ||
len = arguments.length; | ||
while (len--) path[len] = arguments[len]; | ||
var path = [], len = arguments.length; | ||
while ( len-- ) path[ len ] = arguments[ len ]; | ||
@@ -193,5 +183,3 @@ return path.join("/"); | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
var _everyFn = arr.every(function(el) { | ||
return typeof el === "function"; | ||
}); | ||
var _everyFn = arr.every(function (el) { return typeof el === "function"; }); | ||
return _everyFn; | ||
@@ -223,6 +211,3 @@ } | ||
throw new ModuleNotFound( | ||
"We couldn't find your requested module. path: " + | ||
path + | ||
" # module: " + | ||
_lastModuleName | ||
("We couldn't find your requested module. path: " + path + " # module: " + _lastModuleName) | ||
); | ||
@@ -248,9 +233,8 @@ } | ||
if (_root._modulesDictionary.size) { | ||
_root._modulesDictionary.forEach(function(val, key) { | ||
_root._modulesDictionary.forEach(function (val, key) { | ||
_root._states[key] = val._states; | ||
}); | ||
} | ||
} else { | ||
return; | ||
} | ||
return; | ||
} | ||
@@ -261,5 +245,3 @@ | ||
throw new NoOptionException( | ||
"\n you are passing " + | ||
opts + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
("\n you are passing " + opts + " as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ") | ||
); | ||
@@ -279,15 +261,12 @@ } | ||
var prototypeAccessors = { | ||
state: { configurable: true }, | ||
getters: { configurable: true } | ||
}; | ||
var prototypeAccessors = { state: { configurable: true },getters: { configurable: true } }; | ||
Bucket.prototype.initializeSettings = function initializeSettings(ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
Bucket.prototype.initializeSettings = function initializeSettings (ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
@@ -298,3 +277,6 @@ // internal variables | ||
this._mutations = mutations || Object.create(null); | ||
this._getters = this.interceptGetters(this, getters || Object.create(null)); | ||
this._getters = this.interceptGetters( | ||
this, | ||
getters || Object.create(null) | ||
); | ||
this._actions = actions || Object.create(null); | ||
@@ -309,17 +291,15 @@ this._modules = modules || Object.create(null); | ||
Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) { | ||
Bucket.prototype.triggerCommit = function triggerCommit (_name, _payload) { | ||
// check if path is valid | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._mutations[actionName]; | ||
if (!_fn) { | ||
throw new InvalidCommitException( | ||
"\n commit " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidCommitException(("\n commit " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -331,3 +311,3 @@ _fn(module._data, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -337,20 +317,14 @@ }); | ||
Bucket.prototype.triggerDispatch = function triggerDispatch( | ||
_self, | ||
_name, | ||
_payload | ||
) { | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
Bucket.prototype.triggerDispatch = function triggerDispatch (_self, _name, _payload) { | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._actions[actionName]; | ||
if (!_fn) { | ||
throw new InvalidDispatchException( | ||
"\n dispatch " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidDispatchException(("\n dispatch " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -362,3 +336,3 @@ var _asyncDispatch = _fn(_self, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -373,10 +347,8 @@ }); | ||
prototypeAccessors.state.get = function() { | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
); | ||
prototypeAccessors.state.get = function () { | ||
console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "); | ||
return this._states; | ||
}; | ||
prototypeAccessors.getters.get = function() { | ||
prototypeAccessors.getters.get = function () { | ||
return this._getters; | ||
@@ -386,3 +358,3 @@ }; | ||
// refactor: create multiple functions for if-else | ||
Bucket.prototype.interceptGetters = function interceptGetters(_self, _target) { | ||
Bucket.prototype.interceptGetters = function interceptGetters (_self, _target) { | ||
// finding correct getter recursively from root to bottom | ||
@@ -394,5 +366,5 @@ return new Proxy(_target, { | ||
var ref = searchNestedModules(_path, _self); | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
// saving a reference to the current module name, in case we don't find any getters. | ||
@@ -407,13 +379,6 @@ InvalidGetterException.namespace = nextModuleName; | ||
if (!target[prop]) { | ||
throw new InvalidGetterException( | ||
"\n getter " + | ||
prop + | ||
" in " + | ||
(InvalidGetterException.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
throw new InvalidGetterException(("\n getter " + prop + " in " + (InvalidGetterException.namespace || | ||
"root") + " module is not found. please check your getter.\n ")); | ||
} | ||
return computed(function() { | ||
return target[prop](_self._data); | ||
}).value; | ||
return computed(function () { return target[prop](_self._data); }).value; | ||
} | ||
@@ -424,8 +389,8 @@ } | ||
Bucket.prototype.installModules = function installModules(_instance) { | ||
if (_instance === void 0) _instance = this; | ||
Bucket.prototype.installModules = function installModules (_instance) { | ||
if ( _instance === void 0 ) _instance = this; | ||
var entries = Object.entries(_instance._modules); | ||
if (entries.length) { | ||
entries.forEach(function(entry) { | ||
entries.forEach(function (entry) { | ||
/* | ||
@@ -435,9 +400,4 @@ installModules will be called on new Bucket instance. | ||
*/ | ||
var bucket = new Bucket( | ||
Object.assign( | ||
{}, | ||
{ name: entry[0] }, // set name for each module | ||
entry[1] | ||
) | ||
); | ||
var bucket = new Bucket(Object.assign({}, {name: entry[0]}, // set name for each module | ||
entry[1])); | ||
_instance._modulesDictionary.set(entry[0], bucket); | ||
@@ -450,9 +410,7 @@ }); | ||
Bucket.prototype.installPlugins = function installPlugins(_plugins) { | ||
var this$1 = this; | ||
Bucket.prototype.installPlugins = function installPlugins (_plugins) { | ||
var this$1$1 = this; | ||
if (this._name !== "root" && hasPlugin(_plugins)) { | ||
throw new InstallPluginsOnModulesException( | ||
"\n You can only register plugins in the root module." | ||
); | ||
throw new InstallPluginsOnModulesException("\n You can only register plugins in the root module."); | ||
} | ||
@@ -472,4 +430,4 @@ | ||
// call the plugins | ||
Object.values(_plugins).forEach(function(plugin) { | ||
plugin(this$1); | ||
Object.values(_plugins).forEach(function (plugin) { | ||
plugin(this$1$1); | ||
}); | ||
@@ -480,3 +438,3 @@ | ||
Bucket.prototype.notifyCommits = function notifyCommits(_data) { | ||
Bucket.prototype.notifyCommits = function notifyCommits (_data) { | ||
var _cbs = this._onMutationSubscribers; | ||
@@ -486,3 +444,3 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyActions = function notifyActions(_data) { | ||
Bucket.prototype.notifyActions = function notifyActions (_data) { | ||
var _cbs = this._onActionSubscribers; | ||
@@ -492,4 +450,5 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyPlugins = function notifyPlugins(_data, _cbs) { | ||
[].concat(_cbs.values()).forEach(function(cb) { | ||
Bucket.prototype.notifyPlugins = function notifyPlugins (_data, _cbs) { | ||
[].concat( _cbs.values() ).forEach(function (cb) { | ||
if (typeof cb !== "function") { return; } | ||
cb(_data); | ||
@@ -499,3 +458,3 @@ }); | ||
Bucket.prototype.install = function install(app, injectKey) { | ||
Bucket.prototype.install = function install (app, injectKey) { | ||
app.provide(injectKey || bucketKey, this); | ||
@@ -505,3 +464,3 @@ app.config.globalProperties.$bucket = this; | ||
Object.defineProperties(Bucket.prototype, prototypeAccessors); | ||
Object.defineProperties( Bucket.prototype, prototypeAccessors ); | ||
@@ -518,3 +477,2 @@ function createBucket(options) { | ||
export default index; | ||
export { Bucket, createBucket, useBucket }; | ||
export { Bucket, createBucket, index as default, useBucket }; |
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
import { inject as t, reactive as e, computed as n } from "vue"; | ||
function o(e) { | ||
return void 0 === e && (e = null), t(null !== e ? e : "bucket"); | ||
} | ||
var r = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "NoOptionException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
i = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidModulePath"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
a = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "ModuleNotFound"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
s = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidCommitException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
c = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidDispatchException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
u = (function(t) { | ||
function e(e, n) { | ||
t.call(this, e), | ||
(this.name = "InvalidGetterException"), | ||
(this._namespace = n); | ||
} | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e); | ||
var n = { namespace: { configurable: !0 } }; | ||
return ( | ||
(n.namespace.get = function() { | ||
return this._namespace; | ||
}), | ||
(n.namespace.set = function(t) { | ||
this._namespace = t; | ||
}), | ||
Object.defineProperties(e, n), | ||
e | ||
); | ||
})(Error), | ||
l = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InstallPluginsOnModulesException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error); | ||
function p(t) { | ||
for (var e = t.split("/"), n = 0; n < e.length; n++) | ||
if (0 === e[n].length) | ||
throw new i( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
t | ||
); | ||
return e; | ||
} | ||
function h(t) { | ||
return ( | ||
Array.isArray(t) && | ||
(function(t) { | ||
if (!(t instanceof Array)) return !1; | ||
return t.every(function(t) { | ||
return "string" == typeof t; | ||
}); | ||
})(t) | ||
); | ||
} | ||
function f() { | ||
for (var t = [], e = arguments.length; e--; ) t[e] = arguments[e]; | ||
return t.join("/"); | ||
} | ||
function m(t) { | ||
return 0 === Object.keys(t).length; | ||
} | ||
function d(t, e) { | ||
var n = (h(t) && t) || p(t), | ||
o = "root"; | ||
if (1 === n.length && n.toString().trim().length > 0) | ||
return { module: e, actionName: t }; | ||
for (var r = e, i = 0; n.length > 1; i++) | ||
if (((o = n.shift()), !(r = r._modulesDictionary.get(o)))) | ||
throw new a( | ||
"We couldn't find your requested module. path: " + | ||
t + | ||
" # module: " + | ||
o | ||
); | ||
return { | ||
module: r, | ||
actionName: n.slice(-1).toString(), | ||
nextModuleName: o, | ||
nextPath: f.apply(void 0, n) | ||
}; | ||
} | ||
var y = function(t) { | ||
if (!t || !(t instanceof Object) || m(t)) | ||
throw new r( | ||
"\n you are passing " + | ||
t + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
); | ||
var e = this; | ||
this.initializeSettings(t), | ||
(this.commit = function(t, n) { | ||
return e.triggerCommit(t, n); | ||
}), | ||
(this.dispatch = function(t, n) { | ||
return e.triggerDispatch(e, t, n); | ||
}), | ||
this.installModules(), | ||
(function(t) { | ||
t._data && | ||
((t._states = t._data), | ||
t._modulesDictionary.size && | ||
t._modulesDictionary.forEach(function(e, n) { | ||
t._states[n] = e._states; | ||
})); | ||
})(e); | ||
}, | ||
_ = { state: { configurable: !0 }, getters: { configurable: !0 } }; | ||
function g(t) { | ||
return new y(t); | ||
} | ||
(y.prototype.initializeSettings = function(t) { | ||
var n = t.name, | ||
o = t.states, | ||
r = t.mutations, | ||
i = t.actions, | ||
a = t.getters, | ||
s = t.modules, | ||
c = t.plugins; | ||
(this._name = n || "root"), | ||
(this._data = e(o)), | ||
(this._mutations = r || Object.create(null)), | ||
(this._getters = this.interceptGetters(this, a || Object.create(null))), | ||
(this._actions = i || Object.create(null)), | ||
(this._modules = s || Object.create(null)), | ||
(this._states = Object.create(null)), | ||
(this._modulesDictionary = new Map()), | ||
(this._onMutationSubscribers = new Set()), | ||
(this._onActionSubscribers = new Set()), | ||
(this._pluginSubscribers = this.installPlugins(c)); | ||
}), | ||
(y.prototype.triggerCommit = function(t, e) { | ||
var n = d(t, this), | ||
o = n.module, | ||
r = n.actionName, | ||
i = n.nextModuleName, | ||
a = o._mutations[r]; | ||
if (!a) | ||
throw new s( | ||
"\n commit " + | ||
r + | ||
" in " + | ||
(i || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
a(o._data, e), | ||
this.notifyCommits({ | ||
name: r, | ||
module: this._name, | ||
fullPath: "root/" + t, | ||
payload: e | ||
}); | ||
}), | ||
(y.prototype.triggerDispatch = function(t, e, n) { | ||
var o = d(e, this), | ||
r = o.module, | ||
i = o.actionName, | ||
a = o.nextModuleName, | ||
s = r._actions[i]; | ||
if (!s) | ||
throw new c( | ||
"\n dispatch " + | ||
i + | ||
" in " + | ||
(a || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
var u, | ||
l = s(t, n); | ||
if ( | ||
(this.notifyActions({ | ||
name: i, | ||
module: this._name, | ||
fullPath: "root/" + e, | ||
payload: n | ||
}), | ||
null != (u = l) && | ||
"function" == typeof u.then && | ||
"function" == typeof u.catch) | ||
) | ||
return l; | ||
}), | ||
(_.state.get = function() { | ||
return ( | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
), | ||
this._states | ||
); | ||
}), | ||
(_.getters.get = function() { | ||
return this._getters; | ||
}), | ||
(y.prototype.interceptGetters = function(t, e) { | ||
return new Proxy(e, { | ||
get: function(e, o) { | ||
var r = p(o); | ||
if (r.length > 1) { | ||
var i = d(r, t), | ||
a = i.module, | ||
s = i.nextModuleName, | ||
c = i.nextPath; | ||
return (u.namespace = s), a.getters[c]; | ||
} | ||
if (!e[o]) | ||
throw new u( | ||
"\n getter " + | ||
o + | ||
" in " + | ||
(u.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
return n(function() { | ||
return e[o](t._data); | ||
}).value; | ||
} | ||
}); | ||
}), | ||
(y.prototype.installModules = function(t) { | ||
void 0 === t && (t = this); | ||
var e = Object.entries(t._modules); | ||
e.length && | ||
e.forEach(function(e) { | ||
var n = new y(Object.assign({}, { name: e[0] }, e[1])); | ||
t._modulesDictionary.set(e[0], n); | ||
}); | ||
}), | ||
(y.prototype.installPlugins = function(t) { | ||
var e, | ||
n = this; | ||
if ( | ||
"root" !== this._name && | ||
((e = t), | ||
Array.isArray(e) && | ||
e.length > 0 && | ||
e.every(function(t) { | ||
return "function" == typeof t; | ||
})) | ||
) | ||
throw new l( | ||
"\n You can only register plugins in the root module." | ||
); | ||
return void 0 === t || m(t) | ||
? Object.create(null) | ||
: ((this.onMutation = function(t) { | ||
this._onMutationSubscribers.add(t); | ||
}), | ||
(this.onAction = function(t) { | ||
this._onActionSubscribers.add(t); | ||
}), | ||
Object.values(t).forEach(function(t) { | ||
t(n); | ||
}), | ||
t); | ||
}), | ||
(y.prototype.notifyCommits = function(t) { | ||
var e = this._onMutationSubscribers; | ||
this.notifyPlugins(t, e); | ||
}), | ||
(y.prototype.notifyActions = function(t) { | ||
var e = this._onActionSubscribers; | ||
this.notifyPlugins(t, e); | ||
}), | ||
(y.prototype.notifyPlugins = function(t, e) { | ||
[].concat(e.values()).forEach(function(e) { | ||
e(t); | ||
}); | ||
}), | ||
(y.prototype.install = function(t, e) { | ||
t.provide(e || "bucket", this), | ||
(t.config.globalProperties.$bucket = this); | ||
}), | ||
Object.defineProperties(y.prototype, _); | ||
var b = { createBucket: g, Bucket: y, useBucket: o }; | ||
export default b; | ||
export { y as Bucket, g as createBucket, o as useBucket }; | ||
import{inject as t,reactive as e,computed as n}from"vue";var o="bucket";function r(e){return void 0===e&&(e=null),t(null!==e?e:o)}var i=function(t){function e(e){t.call(this,e),this.name="NoOptionException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),a=function(t){function e(e){t.call(this,e),this.name="InvalidModulePath"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),s=function(t){function e(e){t.call(this,e),this.name="ModuleNotFound"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),c=function(t){function e(e){t.call(this,e),this.name="InvalidCommitException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),u=function(t){function e(e){t.call(this,e),this.name="InvalidDispatchException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),l=function(t){function e(e,n){t.call(this,e),this.name="InvalidGetterException",this._namespace=n}t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e;var n={namespace:{configurable:!0}};return n.namespace.get=function(){return this._namespace},n.namespace.set=function(t){this._namespace=t},Object.defineProperties(e,n),e}(Error),p=function(t){function e(e){t.call(this,e),this.name="InstallPluginsOnModulesException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error);function f(t){for(var e=t.split("/"),n=0;n<e.length;n++)if(0===e[n].length)throw new a("\n Invalid Path. we couldn't find any module in the "+t);return e}function h(t){return Array.isArray(t)&&function(t){if(!(t instanceof Array))return!1;return t.every((function(t){return"string"==typeof t}))}(t)}function m(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return t.join("/")}function d(t){return 0===Object.keys(t).length}function y(t,e){var n=h(t)&&t||f(t),o="root";if(1===n.length&&n.toString().trim().length>0)return{module:e,actionName:t};for(var r=e,i=0;n.length>1;i++)if(o=n.shift(),!(r=r._modulesDictionary.get(o)))throw new s("We couldn't find your requested module. path: "+t+" # module: "+o);return{module:r,actionName:n.slice(-1).toString(),nextModuleName:o,nextPath:m.apply(void 0,n)}}var _=function(t){if(!t||!(t instanceof Object)||d(t))throw new i("\n you are passing "+t+" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ");var e=this;this.initializeSettings(t),this.commit=function(t,n){return e.triggerCommit(t,n)},this.dispatch=function(t,n){return e.triggerDispatch(e,t,n)},this.installModules(),function(t){t._data&&(t._states=t._data,t._modulesDictionary.size&&t._modulesDictionary.forEach((function(e,n){t._states[n]=e._states})))}(e)},g={state:{configurable:!0},getters:{configurable:!0}};function b(t){return new _(t)}_.prototype.initializeSettings=function(t){var n=t.name,o=t.states,r=t.mutations,i=t.actions,a=t.getters,s=t.modules,c=t.plugins;this._name=n||"root",this._data=e(o),this._mutations=r||Object.create(null),this._getters=this.interceptGetters(this,a||Object.create(null)),this._actions=i||Object.create(null),this._modules=s||Object.create(null),this._states=Object.create(null),this._modulesDictionary=new Map,this._onMutationSubscribers=new Set,this._onActionSubscribers=new Set,this._pluginSubscribers=this.installPlugins(c)},_.prototype.triggerCommit=function(t,e){var n=y(t,this),o=n.module,r=n.actionName,i=n.nextModuleName,a=o._mutations[r];if(!a)throw new c("\n commit "+r+" in "+(i||"root")+" module is invalid. please check your commit name.\n ");a(o._data,e),this.notifyCommits({name:r,module:this._name,fullPath:"root/"+t,payload:e})},_.prototype.triggerDispatch=function(t,e,n){var o=y(e,this),r=o.module,i=o.actionName,a=o.nextModuleName,s=r._actions[i];if(!s)throw new u("\n dispatch "+i+" in "+(a||"root")+" module is invalid. please check your commit name.\n ");var c,l=s(t,n);if(this.notifyActions({name:i,module:this._name,fullPath:"root/"+e,payload:n}),null!=(c=l)&&"function"==typeof c.then&&"function"==typeof c.catch)return l},g.state.get=function(){return console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "),this._states},g.getters.get=function(){return this._getters},_.prototype.interceptGetters=function(t,e){return new Proxy(e,{get:function(e,o){var r=f(o);if(r.length>1){var i=y(r,t),a=i.module,s=i.nextModuleName,c=i.nextPath;return l.namespace=s,a.getters[c]}if(!e[o])throw new l("\n getter "+o+" in "+(l.namespace||"root")+" module is not found. please check your getter.\n ");return n((function(){return e[o](t._data)})).value}})},_.prototype.installModules=function(t){void 0===t&&(t=this);var e=Object.entries(t._modules);e.length&&e.forEach((function(e){var n=new _(Object.assign({},{name:e[0]},e[1]));t._modulesDictionary.set(e[0],n)}))},_.prototype.installPlugins=function(t){var e,n=this;if("root"!==this._name&&(e=t,Array.isArray(e)&&e.length>0&&e.every((function(t){return"function"==typeof t}))))throw new p("\n You can only register plugins in the root module.");return void 0===t||d(t)?Object.create(null):(this.onMutation=function(t){this._onMutationSubscribers.add(t)},this.onAction=function(t){this._onActionSubscribers.add(t)},Object.values(t).forEach((function(t){t(n)})),t)},_.prototype.notifyCommits=function(t){var e=this._onMutationSubscribers;this.notifyPlugins(t,e)},_.prototype.notifyActions=function(t){var e=this._onActionSubscribers;this.notifyPlugins(t,e)},_.prototype.notifyPlugins=function(t,e){[].concat(e.values()).forEach((function(e){"function"==typeof e&&e(t)}))},_.prototype.install=function(t,e){t.provide(e||o,this),t.config.globalProperties.$bucket=this},Object.defineProperties(_.prototype,g);var v={createBucket:b,Bucket:_,useBucket:r};export{_ as Bucket,b as createBucket,v as default,r as useBucket}; |
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
import { inject, reactive, computed } from "vue"; | ||
import { inject, reactive, computed } from 'vue'; | ||
var bucketKey = "bucket"; | ||
function useBucket(key) { | ||
if (key === void 0) key = null; | ||
if ( key === void 0 ) key = null; | ||
@@ -15,3 +15,3 @@ return inject(key !== null ? key : bucketKey); | ||
var NoOptionException = /*@__PURE__*/ (function(Error) { | ||
var NoOptionException = /*@__PURE__*/(function (Error) { | ||
function NoOptionException(message) { | ||
@@ -22,10 +22,10 @@ Error.call(this, message); | ||
if (Error) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create( Error && Error.prototype ); | ||
NoOptionException.prototype.constructor = NoOptionException; | ||
return NoOptionException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidModulePath = /*@__PURE__*/ (function(Error) { | ||
var InvalidModulePath = /*@__PURE__*/(function (Error) { | ||
function InvalidModulePath(message) { | ||
@@ -36,10 +36,10 @@ Error.call(this, message); | ||
if (Error) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create( Error && Error.prototype ); | ||
InvalidModulePath.prototype.constructor = InvalidModulePath; | ||
return InvalidModulePath; | ||
})(Error); | ||
}(Error)); | ||
var ModuleNotFound = /*@__PURE__*/ (function(Error) { | ||
var ModuleNotFound = /*@__PURE__*/(function (Error) { | ||
function ModuleNotFound(message) { | ||
@@ -50,10 +50,10 @@ Error.call(this, message); | ||
if (Error) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create( Error && Error.prototype ); | ||
ModuleNotFound.prototype.constructor = ModuleNotFound; | ||
return ModuleNotFound; | ||
})(Error); | ||
}(Error)); | ||
var InvalidCommitException = /*@__PURE__*/ (function(Error) { | ||
var InvalidCommitException = /*@__PURE__*/(function (Error) { | ||
function InvalidCommitException(message) { | ||
@@ -64,10 +64,10 @@ Error.call(this, message); | ||
if (Error) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidCommitException.prototype.constructor = InvalidCommitException; | ||
return InvalidCommitException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidDispatchException = /*@__PURE__*/ (function(Error) { | ||
var InvalidDispatchException = /*@__PURE__*/(function (Error) { | ||
function InvalidDispatchException(message) { | ||
@@ -78,12 +78,10 @@ Error.call(this, message); | ||
if (Error) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidDispatchException.prototype.constructor = InvalidDispatchException; | ||
return InvalidDispatchException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidGetterException = /*@__PURE__*/ (function(Error) { | ||
var InvalidGetterException = /*@__PURE__*/(function (Error) { | ||
function InvalidGetterException(message, namespace) { | ||
@@ -95,4 +93,4 @@ Error.call(this, message); | ||
if (Error) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidGetterException.prototype.constructor = InvalidGetterException; | ||
@@ -102,16 +100,16 @@ | ||
staticAccessors.namespace.get = function() { | ||
staticAccessors.namespace.get = function () { | ||
return this._namespace; | ||
}; | ||
staticAccessors.namespace.set = function(v) { | ||
staticAccessors.namespace.set = function (v) { | ||
this._namespace = v; | ||
}; | ||
Object.defineProperties(InvalidGetterException, staticAccessors); | ||
Object.defineProperties( InvalidGetterException, staticAccessors ); | ||
return InvalidGetterException; | ||
})(Error); | ||
}(Error)); | ||
var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) { | ||
var InstallPluginsOnModulesException = /*@__PURE__*/(function (Error) { | ||
function InstallPluginsOnModulesException(message) { | ||
@@ -122,10 +120,8 @@ Error.call(this, message); | ||
if (Error) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( Error && Error.prototype ); | ||
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException; | ||
return InstallPluginsOnModulesException; | ||
})(Error); | ||
}(Error)); | ||
@@ -147,6 +143,3 @@ function isDefined(v) { | ||
if (splitted[i].length === 0) { | ||
throw new InvalidModulePath( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
path | ||
); | ||
throw new InvalidModulePath(("\n Invalid Path. we couldn't find any module in the " + path)); | ||
} | ||
@@ -165,11 +158,8 @@ } | ||
} | ||
return arr.every(function(v) { | ||
return typeof v === "string"; | ||
}); | ||
return arr.every(function (v) { return typeof v === "string"; }); | ||
} | ||
function arrayToPath() { | ||
var path = [], | ||
len = arguments.length; | ||
while (len--) path[len] = arguments[len]; | ||
var path = [], len = arguments.length; | ||
while ( len-- ) path[ len ] = arguments[ len ]; | ||
@@ -193,5 +183,3 @@ return path.join("/"); | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
var _everyFn = arr.every(function(el) { | ||
return typeof el === "function"; | ||
}); | ||
var _everyFn = arr.every(function (el) { return typeof el === "function"; }); | ||
return _everyFn; | ||
@@ -223,6 +211,3 @@ } | ||
throw new ModuleNotFound( | ||
"We couldn't find your requested module. path: " + | ||
path + | ||
" # module: " + | ||
_lastModuleName | ||
("We couldn't find your requested module. path: " + path + " # module: " + _lastModuleName) | ||
); | ||
@@ -248,9 +233,8 @@ } | ||
if (_root._modulesDictionary.size) { | ||
_root._modulesDictionary.forEach(function(val, key) { | ||
_root._modulesDictionary.forEach(function (val, key) { | ||
_root._states[key] = val._states; | ||
}); | ||
} | ||
} else { | ||
return; | ||
} | ||
return; | ||
} | ||
@@ -261,5 +245,3 @@ | ||
throw new NoOptionException( | ||
"\n you are passing " + | ||
opts + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
("\n you are passing " + opts + " as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ") | ||
); | ||
@@ -279,15 +261,12 @@ } | ||
var prototypeAccessors = { | ||
state: { configurable: true }, | ||
getters: { configurable: true } | ||
}; | ||
var prototypeAccessors = { state: { configurable: true },getters: { configurable: true } }; | ||
Bucket.prototype.initializeSettings = function initializeSettings(ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
Bucket.prototype.initializeSettings = function initializeSettings (ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
@@ -298,3 +277,6 @@ // internal variables | ||
this._mutations = mutations || Object.create(null); | ||
this._getters = this.interceptGetters(this, getters || Object.create(null)); | ||
this._getters = this.interceptGetters( | ||
this, | ||
getters || Object.create(null) | ||
); | ||
this._actions = actions || Object.create(null); | ||
@@ -309,17 +291,15 @@ this._modules = modules || Object.create(null); | ||
Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) { | ||
Bucket.prototype.triggerCommit = function triggerCommit (_name, _payload) { | ||
// check if path is valid | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._mutations[actionName]; | ||
if (!_fn) { | ||
throw new InvalidCommitException( | ||
"\n commit " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidCommitException(("\n commit " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -331,3 +311,3 @@ _fn(module._data, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -337,20 +317,14 @@ }); | ||
Bucket.prototype.triggerDispatch = function triggerDispatch( | ||
_self, | ||
_name, | ||
_payload | ||
) { | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
Bucket.prototype.triggerDispatch = function triggerDispatch (_self, _name, _payload) { | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._actions[actionName]; | ||
if (!_fn) { | ||
throw new InvalidDispatchException( | ||
"\n dispatch " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidDispatchException(("\n dispatch " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -362,3 +336,3 @@ var _asyncDispatch = _fn(_self, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -373,10 +347,8 @@ }); | ||
prototypeAccessors.state.get = function() { | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
); | ||
prototypeAccessors.state.get = function () { | ||
console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "); | ||
return this._states; | ||
}; | ||
prototypeAccessors.getters.get = function() { | ||
prototypeAccessors.getters.get = function () { | ||
return this._getters; | ||
@@ -386,3 +358,3 @@ }; | ||
// refactor: create multiple functions for if-else | ||
Bucket.prototype.interceptGetters = function interceptGetters(_self, _target) { | ||
Bucket.prototype.interceptGetters = function interceptGetters (_self, _target) { | ||
// finding correct getter recursively from root to bottom | ||
@@ -394,5 +366,5 @@ return new Proxy(_target, { | ||
var ref = searchNestedModules(_path, _self); | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
// saving a reference to the current module name, in case we don't find any getters. | ||
@@ -407,13 +379,6 @@ InvalidGetterException.namespace = nextModuleName; | ||
if (!target[prop]) { | ||
throw new InvalidGetterException( | ||
"\n getter " + | ||
prop + | ||
" in " + | ||
(InvalidGetterException.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
throw new InvalidGetterException(("\n getter " + prop + " in " + (InvalidGetterException.namespace || | ||
"root") + " module is not found. please check your getter.\n ")); | ||
} | ||
return computed(function() { | ||
return target[prop](_self._data); | ||
}).value; | ||
return computed(function () { return target[prop](_self._data); }).value; | ||
} | ||
@@ -424,8 +389,8 @@ } | ||
Bucket.prototype.installModules = function installModules(_instance) { | ||
if (_instance === void 0) _instance = this; | ||
Bucket.prototype.installModules = function installModules (_instance) { | ||
if ( _instance === void 0 ) _instance = this; | ||
var entries = Object.entries(_instance._modules); | ||
if (entries.length) { | ||
entries.forEach(function(entry) { | ||
entries.forEach(function (entry) { | ||
/* | ||
@@ -435,9 +400,4 @@ installModules will be called on new Bucket instance. | ||
*/ | ||
var bucket = new Bucket( | ||
Object.assign( | ||
{}, | ||
{ name: entry[0] }, // set name for each module | ||
entry[1] | ||
) | ||
); | ||
var bucket = new Bucket(Object.assign({}, {name: entry[0]}, // set name for each module | ||
entry[1])); | ||
_instance._modulesDictionary.set(entry[0], bucket); | ||
@@ -450,9 +410,7 @@ }); | ||
Bucket.prototype.installPlugins = function installPlugins(_plugins) { | ||
var this$1 = this; | ||
Bucket.prototype.installPlugins = function installPlugins (_plugins) { | ||
var this$1$1 = this; | ||
if (this._name !== "root" && hasPlugin(_plugins)) { | ||
throw new InstallPluginsOnModulesException( | ||
"\n You can only register plugins in the root module." | ||
); | ||
throw new InstallPluginsOnModulesException("\n You can only register plugins in the root module."); | ||
} | ||
@@ -472,4 +430,4 @@ | ||
// call the plugins | ||
Object.values(_plugins).forEach(function(plugin) { | ||
plugin(this$1); | ||
Object.values(_plugins).forEach(function (plugin) { | ||
plugin(this$1$1); | ||
}); | ||
@@ -480,3 +438,3 @@ | ||
Bucket.prototype.notifyCommits = function notifyCommits(_data) { | ||
Bucket.prototype.notifyCommits = function notifyCommits (_data) { | ||
var _cbs = this._onMutationSubscribers; | ||
@@ -486,3 +444,3 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyActions = function notifyActions(_data) { | ||
Bucket.prototype.notifyActions = function notifyActions (_data) { | ||
var _cbs = this._onActionSubscribers; | ||
@@ -492,4 +450,5 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyPlugins = function notifyPlugins(_data, _cbs) { | ||
[].concat(_cbs.values()).forEach(function(cb) { | ||
Bucket.prototype.notifyPlugins = function notifyPlugins (_data, _cbs) { | ||
[].concat( _cbs.values() ).forEach(function (cb) { | ||
if (typeof cb !== "function") { return; } | ||
cb(_data); | ||
@@ -499,3 +458,3 @@ }); | ||
Bucket.prototype.install = function install(app, injectKey) { | ||
Bucket.prototype.install = function install (app, injectKey) { | ||
app.provide(injectKey || bucketKey, this); | ||
@@ -505,3 +464,3 @@ app.config.globalProperties.$bucket = this; | ||
Object.defineProperties(Bucket.prototype, prototypeAccessors); | ||
Object.defineProperties( Bucket.prototype, prototypeAccessors ); | ||
@@ -518,3 +477,2 @@ function createBucket(options) { | ||
export default index; | ||
export { Bucket, createBucket, useBucket }; | ||
export { Bucket, createBucket, index as default, useBucket }; |
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
var vbucket = (function(vue) { | ||
"use strict"; | ||
var vbucket = (function (vue) { | ||
'use strict'; | ||
var bucketKey = "bucket"; | ||
function useBucket(key) { | ||
if (key === void 0) key = null; | ||
if ( key === void 0 ) key = null; | ||
@@ -16,3 +16,3 @@ return vue.inject(key !== null ? key : bucketKey); | ||
var NoOptionException = /*@__PURE__*/ (function(Error) { | ||
var NoOptionException = /*@__PURE__*/(function (Error) { | ||
function NoOptionException(message) { | ||
@@ -23,10 +23,10 @@ Error.call(this, message); | ||
if (Error) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) NoOptionException.__proto__ = Error; | ||
NoOptionException.prototype = Object.create( Error && Error.prototype ); | ||
NoOptionException.prototype.constructor = NoOptionException; | ||
return NoOptionException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidModulePath = /*@__PURE__*/ (function(Error) { | ||
var InvalidModulePath = /*@__PURE__*/(function (Error) { | ||
function InvalidModulePath(message) { | ||
@@ -37,10 +37,10 @@ Error.call(this, message); | ||
if (Error) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) InvalidModulePath.__proto__ = Error; | ||
InvalidModulePath.prototype = Object.create( Error && Error.prototype ); | ||
InvalidModulePath.prototype.constructor = InvalidModulePath; | ||
return InvalidModulePath; | ||
})(Error); | ||
}(Error)); | ||
var ModuleNotFound = /*@__PURE__*/ (function(Error) { | ||
var ModuleNotFound = /*@__PURE__*/(function (Error) { | ||
function ModuleNotFound(message) { | ||
@@ -51,10 +51,10 @@ Error.call(this, message); | ||
if (Error) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create(Error && Error.prototype); | ||
if ( Error ) ModuleNotFound.__proto__ = Error; | ||
ModuleNotFound.prototype = Object.create( Error && Error.prototype ); | ||
ModuleNotFound.prototype.constructor = ModuleNotFound; | ||
return ModuleNotFound; | ||
})(Error); | ||
}(Error)); | ||
var InvalidCommitException = /*@__PURE__*/ (function(Error) { | ||
var InvalidCommitException = /*@__PURE__*/(function (Error) { | ||
function InvalidCommitException(message) { | ||
@@ -65,12 +65,10 @@ Error.call(this, message); | ||
if (Error) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidCommitException.__proto__ = Error; | ||
InvalidCommitException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidCommitException.prototype.constructor = InvalidCommitException; | ||
return InvalidCommitException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidDispatchException = /*@__PURE__*/ (function(Error) { | ||
var InvalidDispatchException = /*@__PURE__*/(function (Error) { | ||
function InvalidDispatchException(message) { | ||
@@ -81,12 +79,10 @@ Error.call(this, message); | ||
if (Error) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidDispatchException.__proto__ = Error; | ||
InvalidDispatchException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidDispatchException.prototype.constructor = InvalidDispatchException; | ||
return InvalidDispatchException; | ||
})(Error); | ||
}(Error)); | ||
var InvalidGetterException = /*@__PURE__*/ (function(Error) { | ||
var InvalidGetterException = /*@__PURE__*/(function (Error) { | ||
function InvalidGetterException(message, namespace) { | ||
@@ -98,6 +94,4 @@ Error.call(this, message); | ||
if (Error) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InvalidGetterException.__proto__ = Error; | ||
InvalidGetterException.prototype = Object.create( Error && Error.prototype ); | ||
InvalidGetterException.prototype.constructor = InvalidGetterException; | ||
@@ -107,16 +101,16 @@ | ||
staticAccessors.namespace.get = function() { | ||
staticAccessors.namespace.get = function () { | ||
return this._namespace; | ||
}; | ||
staticAccessors.namespace.set = function(v) { | ||
staticAccessors.namespace.set = function (v) { | ||
this._namespace = v; | ||
}; | ||
Object.defineProperties(InvalidGetterException, staticAccessors); | ||
Object.defineProperties( InvalidGetterException, staticAccessors ); | ||
return InvalidGetterException; | ||
})(Error); | ||
}(Error)); | ||
var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) { | ||
var InstallPluginsOnModulesException = /*@__PURE__*/(function (Error) { | ||
function InstallPluginsOnModulesException(message) { | ||
@@ -127,10 +121,8 @@ Error.call(this, message); | ||
if (Error) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( | ||
Error && Error.prototype | ||
); | ||
if ( Error ) InstallPluginsOnModulesException.__proto__ = Error; | ||
InstallPluginsOnModulesException.prototype = Object.create( Error && Error.prototype ); | ||
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException; | ||
return InstallPluginsOnModulesException; | ||
})(Error); | ||
}(Error)); | ||
@@ -152,6 +144,3 @@ function isDefined(v) { | ||
if (splitted[i].length === 0) { | ||
throw new InvalidModulePath( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
path | ||
); | ||
throw new InvalidModulePath(("\n Invalid Path. we couldn't find any module in the " + path)); | ||
} | ||
@@ -170,11 +159,8 @@ } | ||
} | ||
return arr.every(function(v) { | ||
return typeof v === "string"; | ||
}); | ||
return arr.every(function (v) { return typeof v === "string"; }); | ||
} | ||
function arrayToPath() { | ||
var path = [], | ||
len = arguments.length; | ||
while (len--) path[len] = arguments[len]; | ||
var path = [], len = arguments.length; | ||
while ( len-- ) path[ len ] = arguments[ len ]; | ||
@@ -198,5 +184,3 @@ return path.join("/"); | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
var _everyFn = arr.every(function(el) { | ||
return typeof el === "function"; | ||
}); | ||
var _everyFn = arr.every(function (el) { return typeof el === "function"; }); | ||
return _everyFn; | ||
@@ -225,11 +209,6 @@ } | ||
// get nested module if there is any | ||
_currentModule = _currentModule._modulesDictionary.get( | ||
_lastModuleName | ||
); | ||
_currentModule = _currentModule._modulesDictionary.get(_lastModuleName); | ||
if (!_currentModule) { | ||
throw new ModuleNotFound( | ||
"We couldn't find your requested module. path: " + | ||
path + | ||
" # module: " + | ||
_lastModuleName | ||
("We couldn't find your requested module. path: " + path + " # module: " + _lastModuleName) | ||
); | ||
@@ -255,9 +234,8 @@ } | ||
if (_root._modulesDictionary.size) { | ||
_root._modulesDictionary.forEach(function(val, key) { | ||
_root._modulesDictionary.forEach(function (val, key) { | ||
_root._states[key] = val._states; | ||
}); | ||
} | ||
} else { | ||
return; | ||
} | ||
return; | ||
} | ||
@@ -268,5 +246,3 @@ | ||
throw new NoOptionException( | ||
"\n you are passing " + | ||
opts + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
("\n you are passing " + opts + " as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ") | ||
); | ||
@@ -286,15 +262,12 @@ } | ||
var prototypeAccessors = { | ||
state: { configurable: true }, | ||
getters: { configurable: true } | ||
}; | ||
var prototypeAccessors = { state: { configurable: true },getters: { configurable: true } }; | ||
Bucket.prototype.initializeSettings = function initializeSettings(ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
Bucket.prototype.initializeSettings = function initializeSettings (ref) { | ||
var name = ref.name; | ||
var states = ref.states; | ||
var mutations = ref.mutations; | ||
var actions = ref.actions; | ||
var getters = ref.getters; | ||
var modules = ref.modules; | ||
var plugins = ref.plugins; | ||
@@ -318,17 +291,15 @@ // internal variables | ||
Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) { | ||
Bucket.prototype.triggerCommit = function triggerCommit (_name, _payload) { | ||
// check if path is valid | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._mutations[actionName]; | ||
if (!_fn) { | ||
throw new InvalidCommitException( | ||
"\n commit " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidCommitException(("\n commit " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -340,3 +311,3 @@ _fn(module._data, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -346,20 +317,14 @@ }); | ||
Bucket.prototype.triggerDispatch = function triggerDispatch( | ||
_self, | ||
_name, | ||
_payload | ||
) { | ||
var ref = searchNestedModules(_name, this); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
Bucket.prototype.triggerDispatch = function triggerDispatch (_self, _name, _payload) { | ||
var ref = searchNestedModules( | ||
_name, | ||
this | ||
); | ||
var module = ref.module; | ||
var actionName = ref.actionName; | ||
var nextModuleName = ref.nextModuleName; | ||
var _fn = module._actions[actionName]; | ||
if (!_fn) { | ||
throw new InvalidDispatchException( | ||
"\n dispatch " + | ||
actionName + | ||
" in " + | ||
(nextModuleName || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
throw new InvalidDispatchException(("\n dispatch " + actionName + " in " + (nextModuleName || | ||
"root") + " module is invalid. please check your commit name.\n ")); | ||
} | ||
@@ -371,3 +336,3 @@ var _asyncDispatch = _fn(_self, _payload); | ||
module: this._name, | ||
fullPath: "root/" + _name, | ||
fullPath: ("root/" + _name), | ||
payload: _payload | ||
@@ -382,10 +347,8 @@ }); | ||
prototypeAccessors.state.get = function() { | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
); | ||
prototypeAccessors.state.get = function () { | ||
console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "); | ||
return this._states; | ||
}; | ||
prototypeAccessors.getters.get = function() { | ||
prototypeAccessors.getters.get = function () { | ||
return this._getters; | ||
@@ -395,6 +358,3 @@ }; | ||
// refactor: create multiple functions for if-else | ||
Bucket.prototype.interceptGetters = function interceptGetters( | ||
_self, | ||
_target | ||
) { | ||
Bucket.prototype.interceptGetters = function interceptGetters (_self, _target) { | ||
// finding correct getter recursively from root to bottom | ||
@@ -406,5 +366,5 @@ return new Proxy(_target, { | ||
var ref = searchNestedModules(_path, _self); | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
var module = ref.module; | ||
var nextModuleName = ref.nextModuleName; | ||
var nextPath = ref.nextPath; | ||
// saving a reference to the current module name, in case we don't find any getters. | ||
@@ -419,13 +379,6 @@ InvalidGetterException.namespace = nextModuleName; | ||
if (!target[prop]) { | ||
throw new InvalidGetterException( | ||
"\n getter " + | ||
prop + | ||
" in " + | ||
(InvalidGetterException.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
throw new InvalidGetterException(("\n getter " + prop + " in " + (InvalidGetterException.namespace || | ||
"root") + " module is not found. please check your getter.\n ")); | ||
} | ||
return vue.computed(function() { | ||
return target[prop](_self._data); | ||
}).value; | ||
return vue.computed(function () { return target[prop](_self._data); }).value; | ||
} | ||
@@ -436,8 +389,8 @@ } | ||
Bucket.prototype.installModules = function installModules(_instance) { | ||
if (_instance === void 0) _instance = this; | ||
Bucket.prototype.installModules = function installModules (_instance) { | ||
if ( _instance === void 0 ) _instance = this; | ||
var entries = Object.entries(_instance._modules); | ||
if (entries.length) { | ||
entries.forEach(function(entry) { | ||
entries.forEach(function (entry) { | ||
/* | ||
@@ -447,9 +400,4 @@ installModules will be called on new Bucket instance. | ||
*/ | ||
var bucket = new Bucket( | ||
Object.assign( | ||
{}, | ||
{ name: entry[0] }, // set name for each module | ||
entry[1] | ||
) | ||
); | ||
var bucket = new Bucket(Object.assign({}, {name: entry[0]}, // set name for each module | ||
entry[1])); | ||
_instance._modulesDictionary.set(entry[0], bucket); | ||
@@ -462,9 +410,7 @@ }); | ||
Bucket.prototype.installPlugins = function installPlugins(_plugins) { | ||
var this$1 = this; | ||
Bucket.prototype.installPlugins = function installPlugins (_plugins) { | ||
var this$1$1 = this; | ||
if (this._name !== "root" && hasPlugin(_plugins)) { | ||
throw new InstallPluginsOnModulesException( | ||
"\n You can only register plugins in the root module." | ||
); | ||
throw new InstallPluginsOnModulesException("\n You can only register plugins in the root module."); | ||
} | ||
@@ -484,4 +430,4 @@ | ||
// call the plugins | ||
Object.values(_plugins).forEach(function(plugin) { | ||
plugin(this$1); | ||
Object.values(_plugins).forEach(function (plugin) { | ||
plugin(this$1$1); | ||
}); | ||
@@ -492,3 +438,3 @@ | ||
Bucket.prototype.notifyCommits = function notifyCommits(_data) { | ||
Bucket.prototype.notifyCommits = function notifyCommits (_data) { | ||
var _cbs = this._onMutationSubscribers; | ||
@@ -498,3 +444,3 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyActions = function notifyActions(_data) { | ||
Bucket.prototype.notifyActions = function notifyActions (_data) { | ||
var _cbs = this._onActionSubscribers; | ||
@@ -504,4 +450,5 @@ this.notifyPlugins(_data, _cbs); | ||
Bucket.prototype.notifyPlugins = function notifyPlugins(_data, _cbs) { | ||
[].concat(_cbs.values()).forEach(function(cb) { | ||
Bucket.prototype.notifyPlugins = function notifyPlugins (_data, _cbs) { | ||
[].concat( _cbs.values() ).forEach(function (cb) { | ||
if (typeof cb !== "function") { return; } | ||
cb(_data); | ||
@@ -511,3 +458,3 @@ }); | ||
Bucket.prototype.install = function install(app, injectKey) { | ||
Bucket.prototype.install = function install (app, injectKey) { | ||
app.provide(injectKey || bucketKey, this); | ||
@@ -517,3 +464,3 @@ app.config.globalProperties.$bucket = this; | ||
Object.defineProperties(Bucket.prototype, prototypeAccessors); | ||
Object.defineProperties( Bucket.prototype, prototypeAccessors ); | ||
@@ -531,2 +478,3 @@ function createBucket(options) { | ||
return index_cjs; | ||
})(Vue); |
/*! | ||
* v-bucket v1.1.0 | ||
* (c) 2020 Mahdi Fakhr | ||
* v-bucket v1.1.2 | ||
* (c) 2024 Mahdi Fakhr | ||
* @license MIT | ||
*/ | ||
var vbucket = (function(t) { | ||
"use strict"; | ||
var e = "bucket"; | ||
var n = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "NoOptionException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
o = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidModulePath"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
r = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "ModuleNotFound"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
i = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidCommitException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
s = (function(t) { | ||
function e(e) { | ||
t.call(this, e), (this.name = "InvalidDispatchException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error), | ||
a = (function(t) { | ||
function e(e, n) { | ||
t.call(this, e), | ||
(this.name = "InvalidGetterException"), | ||
(this._namespace = n); | ||
} | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e); | ||
var n = { namespace: { configurable: !0 } }; | ||
return ( | ||
(n.namespace.get = function() { | ||
return this._namespace; | ||
}), | ||
(n.namespace.set = function(t) { | ||
this._namespace = t; | ||
}), | ||
Object.defineProperties(e, n), | ||
e | ||
); | ||
})(Error), | ||
c = (function(t) { | ||
function e(e) { | ||
t.call(this, e), | ||
(this.name = "InstallPluginsOnModulesException"); | ||
} | ||
return ( | ||
t && (e.__proto__ = t), | ||
(e.prototype = Object.create(t && t.prototype)), | ||
(e.prototype.constructor = e), | ||
e | ||
); | ||
})(Error); | ||
function u(t) { | ||
for (var e = t.split("/"), n = 0; n < e.length; n++) | ||
if (0 === e[n].length) | ||
throw new o( | ||
"\n Invalid Path. we couldn't find any module in the " + | ||
t | ||
); | ||
return e; | ||
} | ||
function l(t) { | ||
return ( | ||
Array.isArray(t) && | ||
(function(t) { | ||
if (!(t instanceof Array)) return !1; | ||
return t.every(function(t) { | ||
return "string" == typeof t; | ||
}); | ||
})(t) | ||
); | ||
} | ||
function p() { | ||
for (var t = [], e = arguments.length; e--; ) t[e] = arguments[e]; | ||
return t.join("/"); | ||
} | ||
function h(t) { | ||
return 0 === Object.keys(t).length; | ||
} | ||
function f(t, e) { | ||
var n = (l(t) && t) || u(t), | ||
o = "root"; | ||
if (1 === n.length && n.toString().trim().length > 0) | ||
return { module: e, actionName: t }; | ||
for (var i = e, s = 0; n.length > 1; s++) | ||
if (((o = n.shift()), !(i = i._modulesDictionary.get(o)))) | ||
throw new r( | ||
"We couldn't find your requested module. path: " + | ||
t + | ||
" # module: " + | ||
o | ||
); | ||
return { | ||
module: i, | ||
actionName: n.slice(-1).toString(), | ||
nextModuleName: o, | ||
nextPath: p.apply(void 0, n) | ||
}; | ||
} | ||
var m = function(t) { | ||
if (!t || !(t instanceof Object) || h(t)) | ||
throw new n( | ||
"\n you are passing " + | ||
t + | ||
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n " | ||
); | ||
var e = this; | ||
this.initializeSettings(t), | ||
(this.commit = function(t, n) { | ||
return e.triggerCommit(t, n); | ||
}), | ||
(this.dispatch = function(t, n) { | ||
return e.triggerDispatch(e, t, n); | ||
}), | ||
this.installModules(), | ||
(function(t) { | ||
t._data && | ||
((t._states = t._data), | ||
t._modulesDictionary.size && | ||
t._modulesDictionary.forEach(function(e, n) { | ||
t._states[n] = e._states; | ||
})); | ||
})(e); | ||
}, | ||
d = { state: { configurable: !0 }, getters: { configurable: !0 } }; | ||
return ( | ||
(m.prototype.initializeSettings = function(e) { | ||
var n = e.name, | ||
o = e.states, | ||
r = e.mutations, | ||
i = e.actions, | ||
s = e.getters, | ||
a = e.modules, | ||
c = e.plugins; | ||
(this._name = n || "root"), | ||
(this._data = t.reactive(o)), | ||
(this._mutations = r || Object.create(null)), | ||
(this._getters = this.interceptGetters( | ||
this, | ||
s || Object.create(null) | ||
)), | ||
(this._actions = i || Object.create(null)), | ||
(this._modules = a || Object.create(null)), | ||
(this._states = Object.create(null)), | ||
(this._modulesDictionary = new Map()), | ||
(this._onMutationSubscribers = new Set()), | ||
(this._onActionSubscribers = new Set()), | ||
(this._pluginSubscribers = this.installPlugins(c)); | ||
}), | ||
(m.prototype.triggerCommit = function(t, e) { | ||
var n = f(t, this), | ||
o = n.module, | ||
r = n.actionName, | ||
s = n.nextModuleName, | ||
a = o._mutations[r]; | ||
if (!a) | ||
throw new i( | ||
"\n commit " + | ||
r + | ||
" in " + | ||
(s || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
a(o._data, e), | ||
this.notifyCommits({ | ||
name: r, | ||
module: this._name, | ||
fullPath: "root/" + t, | ||
payload: e | ||
}); | ||
}), | ||
(m.prototype.triggerDispatch = function(t, e, n) { | ||
var o = f(e, this), | ||
r = o.module, | ||
i = o.actionName, | ||
a = o.nextModuleName, | ||
c = r._actions[i]; | ||
if (!c) | ||
throw new s( | ||
"\n dispatch " + | ||
i + | ||
" in " + | ||
(a || "root") + | ||
" module is invalid. please check your commit name.\n " | ||
); | ||
var u, | ||
l = c(t, n); | ||
if ( | ||
(this.notifyActions({ | ||
name: i, | ||
module: this._name, | ||
fullPath: "root/" + e, | ||
payload: n | ||
}), | ||
null != (u = l) && | ||
"function" == typeof u.then && | ||
"function" == typeof u.catch) | ||
) | ||
return l; | ||
}), | ||
(d.state.get = function() { | ||
return ( | ||
console.warn( | ||
"\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n " | ||
), | ||
this._states | ||
); | ||
}), | ||
(d.getters.get = function() { | ||
return this._getters; | ||
}), | ||
(m.prototype.interceptGetters = function(e, n) { | ||
return new Proxy(n, { | ||
get: function(n, o) { | ||
var r = u(o); | ||
if (r.length > 1) { | ||
var i = f(r, e), | ||
s = i.module, | ||
c = i.nextModuleName, | ||
l = i.nextPath; | ||
return (a.namespace = c), s.getters[l]; | ||
} | ||
if (!n[o]) | ||
throw new a( | ||
"\n getter " + | ||
o + | ||
" in " + | ||
(a.namespace || "root") + | ||
" module is not found. please check your getter.\n " | ||
); | ||
return t.computed(function() { | ||
return n[o](e._data); | ||
}).value; | ||
} | ||
}); | ||
}), | ||
(m.prototype.installModules = function(t) { | ||
void 0 === t && (t = this); | ||
var e = Object.entries(t._modules); | ||
e.length && | ||
e.forEach(function(e) { | ||
var n = new m(Object.assign({}, { name: e[0] }, e[1])); | ||
t._modulesDictionary.set(e[0], n); | ||
}); | ||
}), | ||
(m.prototype.installPlugins = function(t) { | ||
var e, | ||
n = this; | ||
if ( | ||
"root" !== this._name && | ||
((e = t), | ||
Array.isArray(e) && | ||
e.length > 0 && | ||
e.every(function(t) { | ||
return "function" == typeof t; | ||
})) | ||
) | ||
throw new c( | ||
"\n You can only register plugins in the root module." | ||
); | ||
return void 0 === t || h(t) | ||
? Object.create(null) | ||
: ((this.onMutation = function(t) { | ||
this._onMutationSubscribers.add(t); | ||
}), | ||
(this.onAction = function(t) { | ||
this._onActionSubscribers.add(t); | ||
}), | ||
Object.values(t).forEach(function(t) { | ||
t(n); | ||
}), | ||
t); | ||
}), | ||
(m.prototype.notifyCommits = function(t) { | ||
var e = this._onMutationSubscribers; | ||
this.notifyPlugins(t, e); | ||
}), | ||
(m.prototype.notifyActions = function(t) { | ||
var e = this._onActionSubscribers; | ||
this.notifyPlugins(t, e); | ||
}), | ||
(m.prototype.notifyPlugins = function(t, e) { | ||
[].concat(e.values()).forEach(function(e) { | ||
e(t); | ||
}); | ||
}), | ||
(m.prototype.install = function(t, n) { | ||
t.provide(n || e, this), (t.config.globalProperties.$bucket = this); | ||
}), | ||
Object.defineProperties(m.prototype, d), | ||
{ | ||
createBucket: function(t) { | ||
return new m(t); | ||
}, | ||
Bucket: m, | ||
useBucket: function(n) { | ||
return void 0 === n && (n = null), t.inject(null !== n ? n : e); | ||
} | ||
} | ||
); | ||
})(Vue); | ||
var vbucket=function(t){"use strict";var e="bucket";var n=function(t){function e(e){t.call(this,e),this.name="NoOptionException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),o=function(t){function e(e){t.call(this,e),this.name="InvalidModulePath"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),r=function(t){function e(e){t.call(this,e),this.name="ModuleNotFound"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),i=function(t){function e(e){t.call(this,e),this.name="InvalidCommitException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),s=function(t){function e(e){t.call(this,e),this.name="InvalidDispatchException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),a=function(t){function e(e,n){t.call(this,e),this.name="InvalidGetterException",this._namespace=n}t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e;var n={namespace:{configurable:!0}};return n.namespace.get=function(){return this._namespace},n.namespace.set=function(t){this._namespace=t},Object.defineProperties(e,n),e}(Error),c=function(t){function e(e){t.call(this,e),this.name="InstallPluginsOnModulesException"}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error);function u(t){for(var e=t.split("/"),n=0;n<e.length;n++)if(0===e[n].length)throw new o("\n Invalid Path. we couldn't find any module in the "+t);return e}function l(t){return Array.isArray(t)&&function(t){if(!(t instanceof Array))return!1;return t.every((function(t){return"string"==typeof t}))}(t)}function p(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return t.join("/")}function f(t){return 0===Object.keys(t).length}function h(t,e){var n=l(t)&&t||u(t),o="root";if(1===n.length&&n.toString().trim().length>0)return{module:e,actionName:t};for(var i=e,s=0;n.length>1;s++)if(o=n.shift(),!(i=i._modulesDictionary.get(o)))throw new r("We couldn't find your requested module. path: "+t+" # module: "+o);return{module:i,actionName:n.slice(-1).toString(),nextModuleName:o,nextPath:p.apply(void 0,n)}}var m=function(t){if(!t||!(t instanceof Object)||f(t))throw new n("\n you are passing "+t+" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n ");var e=this;this.initializeSettings(t),this.commit=function(t,n){return e.triggerCommit(t,n)},this.dispatch=function(t,n){return e.triggerDispatch(e,t,n)},this.installModules(),function(t){t._data&&(t._states=t._data,t._modulesDictionary.size&&t._modulesDictionary.forEach((function(e,n){t._states[n]=e._states})))}(e)},d={state:{configurable:!0},getters:{configurable:!0}};return m.prototype.initializeSettings=function(e){var n=e.name,o=e.states,r=e.mutations,i=e.actions,s=e.getters,a=e.modules,c=e.plugins;this._name=n||"root",this._data=t.reactive(o),this._mutations=r||Object.create(null),this._getters=this.interceptGetters(this,s||Object.create(null)),this._actions=i||Object.create(null),this._modules=a||Object.create(null),this._states=Object.create(null),this._modulesDictionary=new Map,this._onMutationSubscribers=new Set,this._onActionSubscribers=new Set,this._pluginSubscribers=this.installPlugins(c)},m.prototype.triggerCommit=function(t,e){var n=h(t,this),o=n.module,r=n.actionName,s=n.nextModuleName,a=o._mutations[r];if(!a)throw new i("\n commit "+r+" in "+(s||"root")+" module is invalid. please check your commit name.\n ");a(o._data,e),this.notifyCommits({name:r,module:this._name,fullPath:"root/"+t,payload:e})},m.prototype.triggerDispatch=function(t,e,n){var o=h(e,this),r=o.module,i=o.actionName,a=o.nextModuleName,c=r._actions[i];if(!c)throw new s("\n dispatch "+i+" in "+(a||"root")+" module is invalid. please check your commit name.\n ");var u,l=c(t,n);if(this.notifyActions({name:i,module:this._name,fullPath:"root/"+e,payload:n}),null!=(u=l)&&"function"==typeof u.then&&"function"==typeof u.catch)return l},d.state.get=function(){return console.warn("\n do not mutate state directly, use mutations for changing states value or getters to access the states.\n "),this._states},d.getters.get=function(){return this._getters},m.prototype.interceptGetters=function(e,n){return new Proxy(n,{get:function(n,o){var r=u(o);if(r.length>1){var i=h(r,e),s=i.module,c=i.nextModuleName,l=i.nextPath;return a.namespace=c,s.getters[l]}if(!n[o])throw new a("\n getter "+o+" in "+(a.namespace||"root")+" module is not found. please check your getter.\n ");return t.computed((function(){return n[o](e._data)})).value}})},m.prototype.installModules=function(t){void 0===t&&(t=this);var e=Object.entries(t._modules);e.length&&e.forEach((function(e){var n=new m(Object.assign({},{name:e[0]},e[1]));t._modulesDictionary.set(e[0],n)}))},m.prototype.installPlugins=function(t){var e,n=this;if("root"!==this._name&&(e=t,Array.isArray(e)&&e.length>0&&e.every((function(t){return"function"==typeof t}))))throw new c("\n You can only register plugins in the root module.");return void 0===t||f(t)?Object.create(null):(this.onMutation=function(t){this._onMutationSubscribers.add(t)},this.onAction=function(t){this._onActionSubscribers.add(t)},Object.values(t).forEach((function(t){t(n)})),t)},m.prototype.notifyCommits=function(t){var e=this._onMutationSubscribers;this.notifyPlugins(t,e)},m.prototype.notifyActions=function(t){var e=this._onActionSubscribers;this.notifyPlugins(t,e)},m.prototype.notifyPlugins=function(t,e){[].concat(e.values()).forEach((function(e){"function"==typeof e&&e(t)}))},m.prototype.install=function(t,n){t.provide(n||e,this),t.config.globalProperties.$bucket=this},Object.defineProperties(m.prototype,d),{createBucket:function(t){return new m(t)},Bucket:m,useBucket:function(n){return void 0===n&&(n=null),t.inject(null!==n?n:e)}}}(Vue); |
{ | ||
"name": "@mediv0/v-bucket", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"private": false, | ||
@@ -72,3 +72,2 @@ "description": "📦 Fast, Simple, and Lightweight State Management for Vue 3.0 built with composition API, inspired by Vuex.", | ||
"lint-staged": "^9.5.0", | ||
"node-sass": "^4.12.0", | ||
"prettier": "^1.19.1", | ||
@@ -91,3 +90,6 @@ "rollup": "^2.28.1", | ||
] | ||
}, | ||
"dependencies": { | ||
"sass": "^1.77.8" | ||
} | ||
} |
@@ -279,3 +279,3 @@ # v-bucket [](https://codecov.io/gh/mediv0/v-bucket) [](https://www.code-inspector.com/project/13923/status/svg) [](https://codeclimate.com/github/mediv0/v-bucket/maintainability) [](https://img.shields.io/github/license/mediv0/v-bucket) [](https://img.shields.io/github/release-date/mediv0/v-bucket) | ||
- state (used to access states object) | ||
- state (using to access states object) | ||
@@ -417,3 +417,3 @@ using it in your vue app: | ||
this.$bucket.getters["moduleA/get_id"]; // `moduleA`'s getter | ||
this.$bucket.getters["moduleB/get_name]; // `moduleB`'s name state | ||
this.$bucket.getters["moduleB/get_name"]; // `moduleB`'s name state | ||
@@ -446,3 +446,3 @@ // you can go deep as many levels as you want | ||
this.$bucket.commit("moduleA/set_id"); // we can access mutations by commit() | ||
this.$bucket.commit("moduleB/set_name); // we can access mutations by commit() | ||
this.$bucket.commit("moduleB/set_name"); // we can access mutations by commit() | ||
@@ -463,3 +463,3 @@ // ... | ||
this.$bucket.dispatch("moduleA/set_id"); // we can access mutations by dispatch() | ||
this.$bucket.dispatch("moduleB/set_name); // we can access mutations by dispatch() | ||
this.$bucket.dispatch("moduleB/set_name"); // we can access mutations by dispatch() | ||
@@ -466,0 +466,0 @@ // ... |
31
91505
2
1563
+ Addedsass@^1.77.8
+ Added@parcel/watcher@2.5.1(transitive)
+ Added@parcel/watcher-android-arm64@2.5.1(transitive)
+ Added@parcel/watcher-darwin-arm64@2.5.1(transitive)
+ Added@parcel/watcher-darwin-x64@2.5.1(transitive)
+ Added@parcel/watcher-freebsd-x64@2.5.1(transitive)
+ Added@parcel/watcher-linux-arm-glibc@2.5.1(transitive)
+ Added@parcel/watcher-linux-arm-musl@2.5.1(transitive)
+ Added@parcel/watcher-linux-arm64-glibc@2.5.1(transitive)
+ Added@parcel/watcher-linux-arm64-musl@2.5.1(transitive)
+ Added@parcel/watcher-linux-x64-glibc@2.5.1(transitive)
+ Added@parcel/watcher-linux-x64-musl@2.5.1(transitive)
+ Added@parcel/watcher-win32-arm64@2.5.1(transitive)
+ Added@parcel/watcher-win32-ia32@2.5.1(transitive)
+ Added@parcel/watcher-win32-x64@2.5.1(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchokidar@4.0.3(transitive)
+ Addeddetect-libc@1.0.3(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedimmutable@5.0.3(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addednode-addon-api@7.1.1(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedreaddirp@4.1.2(transitive)
+ Addedsass@1.85.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)