Skip to content

Commit

Permalink
🚀 release 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mediv0 committed Sep 26, 2020
1 parent c3b3c78 commit d3bba8c
Show file tree
Hide file tree
Showing 6 changed files with 644 additions and 157 deletions.
96 changes: 93 additions & 3 deletions dist/v-bucket.cjs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* vbucket v1.0.0
* v-bucket v1.1.0
* (c) 2020 Mahdi Fakhr
* @license MIT
*/
Expand Down Expand Up @@ -107,6 +107,21 @@ var InvalidGetterException = /*@__PURE__*/ (function(Error) {
return InvalidGetterException;
})(Error);

var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) {
function InstallPluginsOnModulesException(message) {
Error.call(this, message);
this.name = "InstallPluginsOnModulesException";
}

if (Error) InstallPluginsOnModulesException.__proto__ = Error;
InstallPluginsOnModulesException.prototype = Object.create(
Error && Error.prototype
);
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException;

return InstallPluginsOnModulesException;
})(Error);

function isDefined(v) {
return v !== undefined && v !== null;
}
Expand Down Expand Up @@ -164,6 +179,16 @@ function isObjectEmpty(o) {
return Object.keys(o).length === 0;
}

function hasPlugin(arr) {
if (Array.isArray(arr) && arr.length > 0) {
var _everyFn = arr.every(function(el) {
return typeof el === "function";
});
return _everyFn;
}
return false;
}

function searchNestedModules(path, bucket) {
// if the path already calculated. there is no need to do it again
var _nodes = (isPathAlreadyCalculated(path) && path) || parsePath(path);
Expand Down Expand Up @@ -234,14 +259,17 @@ var Bucket = function Bucket(opts) {
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n "
);
}
var name = opts.name;
var states = opts.states;
var mutations = opts.mutations;
var actions = opts.actions;
var getters = opts.getters;
var modules = opts.modules;
var plugins = opts.plugins;
var _root = this;

// internal variables
this._name = name || "root";
this._data = vue.reactive(states);
this._mutations = mutations || Object.create(null);
this._getters = this.interceptGetters(
Expand All @@ -252,6 +280,9 @@ var Bucket = function Bucket(opts) {
this._modules = modules || Object.create(null);
this._states = Object.create(null);
this._modulesDictionary = new Map();
this._onMutationSubscribers = new Set();
this._onActionSubscribers = new Set();
this._pluginSubscribers = this.installPlugins(plugins);

this.commit = function boundCommit(_name, _payload) {
return _root.triggerCommit(_name, _payload);
Expand All @@ -260,8 +291,9 @@ var Bucket = function Bucket(opts) {
this.dispatch = function boundDispatch(_name, _payload) {
return _root.triggerDispatch(_root, _name, _payload);
};

this.installModules();
createStateTree(this);
createStateTree(_root);
};

var prototypeAccessors = {
Expand All @@ -286,6 +318,12 @@ Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) {
);
}
_fn(module._data, _payload);
this.notifyPlugins("mutation", {
name: actionName,
module: this._name,
fullPath: "root/" + _name,
payload: _payload
});
};

Bucket.prototype.triggerDispatch = function triggerDispatch(
Expand All @@ -312,6 +350,13 @@ Bucket.prototype.triggerDispatch = function triggerDispatch(
if (isPromise(_asyncDispatch)) {
return _asyncDispatch;
}

this.notifyPlugins("actions", {
name: actionName,
module: this._name,
fullPath: "root/" + _name,
payload: _payload
});
};

prototypeAccessors.state.get = function() {
Expand Down Expand Up @@ -370,14 +415,59 @@ Bucket.prototype.installModules = function installModules(_instance) {
installModules will be called on new Bucket instance.
so it will recursively register modules for current instance and return it to the root.
*/
var bucket = new Bucket(entry[1]);
var bucket = new Bucket(
Object.assign(
{},
{ name: entry[0] }, // set name for each module
entry[1]
)
);
_instance._modulesDictionary.set(entry[0], bucket);
});
} else {
return;
}
};

Bucket.prototype.installPlugins = function installPlugins(_plugins) {
var this$1 = this;

if (this._name !== "root" && hasPlugin(_plugins)) {
throw new InstallPluginsOnModulesException(
"\n You can only register plugins in the root module."
);
}

if (_plugins === undefined || isObjectEmpty(_plugins)) {
return Object.create(null);
}

this.onMutation = function boundNotify(_cb) {
this._onMutationSubscribers.add(_cb);
};
this.onAction = function boundNotify(_cb) {
this._onActionSubscribers.add(_cb);
};

// call the plugins
Object.values(_plugins).forEach(function(plugin) {
plugin(this$1);
});

return _plugins;
};

Bucket.prototype.notifyPlugins = function notifyPlugins(_type, _value) {
// maybe use ENUMS idk!
var _callbackFns =
_type === "mutation"
? this._onMutationSubscribers
: this._onActionSubscribers;
[].concat(_callbackFns.values()).forEach(function(cb) {
cb(_value);
});
};

Bucket.prototype.install = function install(app, injectKey) {
app.provide(injectKey || bucketKey, this);
app.config.globalProperties.$bucket = this;
Expand Down
96 changes: 93 additions & 3 deletions dist/v-bucket.esm-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* vbucket v1.0.0
* v-bucket v1.1.0
* (c) 2020 Mahdi Fakhr
* @license MIT
*/
Expand Down Expand Up @@ -105,6 +105,21 @@ var InvalidGetterException = /*@__PURE__*/ (function(Error) {
return InvalidGetterException;
})(Error);

var InstallPluginsOnModulesException = /*@__PURE__*/ (function(Error) {
function InstallPluginsOnModulesException(message) {
Error.call(this, message);
this.name = "InstallPluginsOnModulesException";
}

if (Error) InstallPluginsOnModulesException.__proto__ = Error;
InstallPluginsOnModulesException.prototype = Object.create(
Error && Error.prototype
);
InstallPluginsOnModulesException.prototype.constructor = InstallPluginsOnModulesException;

return InstallPluginsOnModulesException;
})(Error);

function isDefined(v) {
return v !== undefined && v !== null;
}
Expand Down Expand Up @@ -162,6 +177,16 @@ function isObjectEmpty(o) {
return Object.keys(o).length === 0;
}

function hasPlugin(arr) {
if (Array.isArray(arr) && arr.length > 0) {
var _everyFn = arr.every(function(el) {
return typeof el === "function";
});
return _everyFn;
}
return false;
}

function searchNestedModules(path, bucket) {
// if the path already calculated. there is no need to do it again
var _nodes = (isPathAlreadyCalculated(path) && path) || parsePath(path);
Expand Down Expand Up @@ -232,14 +257,17 @@ var Bucket = function Bucket(opts) {
" as your root module. please provide a valid object format\n your object should contain [states, mutations, actions, getters, modules]\n "
);
}
var name = opts.name;
var states = opts.states;
var mutations = opts.mutations;
var actions = opts.actions;
var getters = opts.getters;
var modules = opts.modules;
var plugins = opts.plugins;
var _root = this;

// internal variables
this._name = name || "root";
this._data = reactive(states);
this._mutations = mutations || Object.create(null);
this._getters = this.interceptGetters(
Expand All @@ -250,6 +278,9 @@ var Bucket = function Bucket(opts) {
this._modules = modules || Object.create(null);
this._states = Object.create(null);
this._modulesDictionary = new Map();
this._onMutationSubscribers = new Set();
this._onActionSubscribers = new Set();
this._pluginSubscribers = this.installPlugins(plugins);

this.commit = function boundCommit(_name, _payload) {
return _root.triggerCommit(_name, _payload);
Expand All @@ -258,8 +289,9 @@ var Bucket = function Bucket(opts) {
this.dispatch = function boundDispatch(_name, _payload) {
return _root.triggerDispatch(_root, _name, _payload);
};

this.installModules();
createStateTree(this);
createStateTree(_root);
};

var prototypeAccessors = {
Expand All @@ -284,6 +316,12 @@ Bucket.prototype.triggerCommit = function triggerCommit(_name, _payload) {
);
}
_fn(module._data, _payload);
this.notifyPlugins("mutation", {
name: actionName,
module: this._name,
fullPath: "root/" + _name,
payload: _payload
});
};

Bucket.prototype.triggerDispatch = function triggerDispatch(
Expand All @@ -310,6 +348,13 @@ Bucket.prototype.triggerDispatch = function triggerDispatch(
if (isPromise(_asyncDispatch)) {
return _asyncDispatch;
}

this.notifyPlugins("actions", {
name: actionName,
module: this._name,
fullPath: "root/" + _name,
payload: _payload
});
};

prototypeAccessors.state.get = function() {
Expand Down Expand Up @@ -368,14 +413,59 @@ Bucket.prototype.installModules = function installModules(_instance) {
installModules will be called on new Bucket instance.
so it will recursively register modules for current instance and return it to the root.
*/
var bucket = new Bucket(entry[1]);
var bucket = new Bucket(
Object.assign(
{},
{ name: entry[0] }, // set name for each module
entry[1]
)
);
_instance._modulesDictionary.set(entry[0], bucket);
});
} else {
return;
}
};

Bucket.prototype.installPlugins = function installPlugins(_plugins) {
var this$1 = this;

if (this._name !== "root" && hasPlugin(_plugins)) {
throw new InstallPluginsOnModulesException(
"\n You can only register plugins in the root module."
);
}

if (_plugins === undefined || isObjectEmpty(_plugins)) {
return Object.create(null);
}

this.onMutation = function boundNotify(_cb) {
this._onMutationSubscribers.add(_cb);
};
this.onAction = function boundNotify(_cb) {
this._onActionSubscribers.add(_cb);
};

// call the plugins
Object.values(_plugins).forEach(function(plugin) {
plugin(this$1);
});

return _plugins;
};

Bucket.prototype.notifyPlugins = function notifyPlugins(_type, _value) {
// maybe use ENUMS idk!
var _callbackFns =
_type === "mutation"
? this._onMutationSubscribers
: this._onActionSubscribers;
[].concat(_callbackFns.values()).forEach(function(cb) {
cb(_value);
});
};

Bucket.prototype.install = function install(app, injectKey) {
app.provide(injectKey || bucketKey, this);
app.config.globalProperties.$bucket = this;
Expand Down
Loading

0 comments on commit d3bba8c

Please sign in to comment.