Skip to content

Added ignoredPaths option #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]
}
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,27 @@ Installation of the Vuex Electron easy as 1-2-3.

In renderer process to call actions you need to use `dispatch` or `mapActions`. Don't use `commit` because actions fired via `commit` will not be shared between processes.

### Breaking changes

**[Deprecated]** The whitelist and blacklist features do **not** work, and have been replaced by the ignoredPaths, ignoredCommits and invertIgnored options.

### Options

Available options for `createPersistedState()`

```javascript
createPersistedState({
whitelist: ["whitelistedMutation", "anotherWhitelistedMutation"],

// or

whitelist: (mutation) => {
return true
},

// or

blacklist: ["ignoredMutation", "anotherIgnoredMutation"],

// or

blacklist: (mutation) => {
return true
// Inverts the ignoredPaths and ignoredCommits settings
invertIgnored: true,

// Ignores specific paths on the state object, when persisting.
ignoredPaths: ["a.path.on.state", "another.path"],

// Specifies commits that will not trigger a persistance update.
ignoredCommits: ["increment"],
// or a variation using a function
ignoredCommits: (mutation) => {
return true // Return true for mutations that should not trigger an update
}
})
```
Expand Down
24 changes: 23 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"createPersistedState",{enumerable:!0,get:function a(){return _persistedState.default}}),Object.defineProperty(exports,"createSharedMutations",{enumerable:!0,get:function a(){return _sharedMutations.default}});var _persistedState=_interopRequireDefault(require("./persisted-state")),_sharedMutations=_interopRequireDefault(require("./shared-mutations"));function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "createPersistedState", {
enumerable: true,
get: function get() {
return _persistedState.default;
}
});
Object.defineProperty(exports, "createSharedMutations", {
enumerable: true,
get: function get() {
return _sharedMutations.default;
}
});

var _persistedState = _interopRequireDefault(require("./persisted-state"));

var _sharedMutations = _interopRequireDefault(require("./shared-mutations"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
257 changes: 256 additions & 1 deletion dist/persisted-state.js
Original file line number Diff line number Diff line change
@@ -1 +1,256 @@
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _deepmerge=_interopRequireDefault(require("deepmerge")),_electronStore=_interopRequireDefault(require("electron-store"));function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var STORAGE_NAME="vuex",STORAGE_KEY="state",STORAGE_TEST_KEY="test",PersistedState=function(){function a(b,c){_classCallCheck(this,a),this.options=b,this.store=c}return _createClass(a,[{key:"loadOptions",value:function a(){this.options.storage||(this.options.storage=this.createStorage()),this.options.storageKey||(this.options.storageKey=STORAGE_KEY),this.whitelist=this.loadFilter(this.options.whitelist,"whitelist"),this.blacklist=this.loadFilter(this.options.blacklist,"blacklist")}},{key:"createStorage",value:function a(){return new _electronStore.default({name:this.options.storageName||STORAGE_NAME})}},{key:"getState",value:function a(){return this.options.storage.get(this.options.storageKey)}},{key:"setState",value:function b(a){this.options.storage.set(this.options.storageKey,a)}},{key:"loadFilter",value:function c(a,b){if(!a)return null;if(a instanceof Array)return this.filterInArray(a);if("function"==typeof a)return a;throw new Error("[Vuex Electron] Filter \"".concat(b,"\" should be Array or Function. Please, read the docs."))}},{key:"filterInArray",value:function b(a){return function(b){return a.includes(b.type)}}},{key:"checkStorage",value:function a(){try{this.options.storage.set(STORAGE_TEST_KEY,STORAGE_TEST_KEY),this.options.storage.get(STORAGE_TEST_KEY),this.options.storage.delete(STORAGE_TEST_KEY)}catch(a){throw new Error("[Vuex Electron] Storage is not valid. Please, read the docs.")}}},{key:"combineMerge",value:function e(a,b,c){var d=function(a){return Array.isArray(a)?[]:{}},f=function(a,b){return(0,_deepmerge.default)(d(a),a,b)},g=a.slice();return b.forEach(function(b,d){if("undefined"==typeof g[d]){var e=!1!==c.clone,h=e&&c.isMergeableObject(b);g[d]=h?f(b,c):b}else c.isMergeableObject(b)?g[d]=(0,_deepmerge.default)(a[d],b,c):-1===a.indexOf(b)&&g.push(b)}),g}},{key:"loadInitialState",value:function b(){var a=this.getState(this.options.storage,this.options.storageKey);if(a){var c=(0,_deepmerge.default)(this.store.state,a,{arrayMerge:this.combineMerge});this.store.replaceState(c)}}},{key:"subscribeOnChanges",value:function b(){var a=this;this.store.subscribe(function(b,c){a.blacklist&&a.blacklist(b)||a.whitelist&&!a.whitelist(b)||a.setState(c)})}}]),a}(),_default=function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return function(b){var c=new PersistedState(a,b);c.loadOptions(),c.checkStorage(),c.loadInitialState(),c.subscribeOnChanges()}};exports.default=_default,module.exports=exports["default"];
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _deepmerge = _interopRequireDefault(require("deepmerge"));

var _electronStore = _interopRequireDefault(require("electron-store"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var STORAGE_NAME = "vuex";
var STORAGE_KEY = "state";
var STORAGE_TEST_KEY = "test";

var PersistedState =
/*#__PURE__*/
function () {
function PersistedState(options, store) {
_classCallCheck(this, PersistedState);

this.options = options;
this.store = store;
this.persistedStoreCopy = {};
}

_createClass(PersistedState, [{
key: "loadOptions",
value: function loadOptions() {
if (!this.options.storage) this.options.storage = this.createStorage();
if (!this.options.storageKey) this.options.storageKey = STORAGE_KEY;
this.whitelist = this.loadFilter(this.options.whitelist, "whitelist");
this.blacklist = this.loadFilter(this.options.blacklist, "blacklist");
this.ignoredCommits = this.loadFilter(this.options.ignoredCommits, "ignoredCommits", this.options.invertIgnored);
}
}, {
key: "createStorage",
value: function createStorage() {
return new _electronStore.default({
name: this.options.storageName || STORAGE_NAME
});
}
}, {
key: "getState",
value: function getState() {
return this.options.storage.get(this.options.storageKey);
}
}, {
key: "setState",
value: function setState(state) {
this.options.storage.set(this.options.storageKey, state);
}
}, {
key: "loadFilter",
value: function loadFilter(filter, name, invertIgnored) {
if (!filter) {
return null;
}

if (filter instanceof Array) {
return this.filterInArray(filter);
}

if (typeof filter === "function") {
if (invertIgnored) {
return function (mutation) {
return !filter(mutation);
};
}

return filter;
}

throw new Error("[Vuex Electron] Filter \"".concat(name, "\" should be Array or Function. Please, read the docs."));
}
}, {
key: "filterInArray",
value: function filterInArray(list) {
var _this = this;

return function (mutation) {
if (_this.options.invertIgnored) {
return !list.includes(mutation.type);
}

return list.includes(mutation.type);
};
} // Removes ignored paths from the store object before persisting it

}, {
key: "removeIgnoredPaths",
value: function removeIgnoredPaths(state) {
try {
if (this.options.invertIgnored) {
var newState = {};

for (var i = 0; i < this.options.ignoredPaths.length; i++) {
var path = this.options.ignoredPaths[i];
this.setToValue(newState, this.deepFind(state, path), path);
}

return newState;
} // Creates a copy of the store object


var stateCopy = JSON.parse(JSON.stringify(state));

for (var _i = 0; _i < this.options.ignoredPaths.length; _i++) {
var _path = this.options.ignoredPaths[_i];
this.deleteValue(stateCopy, _path);
}

return stateCopy;
} catch (error) {
throw new Error("[Vuex Electron] An error occurred while removing ignored paths from state. Please use a string array of property paths.");
}
} // Deletes, based on a given property path

}, {
key: "deleteValue",
value: function deleteValue(obj, path) {
var i;
path = path.split(".");

for (i = 0; i < path.length - 1; i++) {
obj = obj[path[i]];
}

delete obj[path[i]];
} // Curtesy of qiao on Stack Overflow.

}, {
key: "deepFind",
value: function deepFind(obj, path) {
var paths = path.split("."),
current = obj,
i;

for (i = 0; i < paths.length; ++i) {
if (current[paths[i]] == undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}

return current;
}
}, {
key: "setToValue",
value: function setToValue(obj, value, path) {
var i;
path = path.split(".");

for (i = 0; i < path.length - 1; i++) {
obj = obj[path[i]];
}

obj[path[i]] = value;
}
}, {
key: "checkStorage",
value: function checkStorage() {
try {
this.options.storage.set(STORAGE_TEST_KEY, STORAGE_TEST_KEY);
this.options.storage.get(STORAGE_TEST_KEY);
this.options.storage.delete(STORAGE_TEST_KEY);
} catch (error) {
throw new Error("[Vuex Electron] Storage is not valid. Please, read the docs.");
}
}
}, {
key: "combineMerge",
value: function combineMerge(target, source, options) {
var emptyTarget = function emptyTarget(value) {
return Array.isArray(value) ? [] : {};
};

var clone = function clone(value, options) {
return (0, _deepmerge.default)(emptyTarget(value), value, options);
};

var destination = target.slice();
source.forEach(function (e, i) {
if (typeof destination[i] === "undefined") {
var cloneRequested = options.clone !== false;
var shouldClone = cloneRequested && options.isMergeableObject(e);
destination[i] = shouldClone ? clone(e, options) : e;
} else if (options.isMergeableObject(e)) {
destination[i] = (0, _deepmerge.default)(target[i], e, options);
} else if (target.indexOf(e) === -1) {
destination.push(e);
}
});
return destination;
}
}, {
key: "loadInitialState",
value: function loadInitialState() {
var state = this.getState(this.options.storage, this.options.storageKey);

if (state) {
var mergedState = (0, _deepmerge.default)(this.store.state, state, {
arrayMerge: this.combineMerge
});
this.store.replaceState(mergedState);
}
}
}, {
key: "subscribeOnChanges",
value: function subscribeOnChanges() {
var _this2 = this;

this.store.subscribe(function (mutation, state) {
if (_this2.blacklist && _this2.blacklist(mutation)) return;
if (_this2.whitelist && !_this2.whitelist(mutation)) return; // Returns if the current commit should not cause persistance.

if (_this2.ignoredCommits && _this2.ignoredCommits(mutation)) return; // Filters the state before persisting, if ignoredPaths is set.

if (_this2.options.ignoredPaths) {
_this2.persistedStoreCopy = _this2.removeIgnoredPaths(state);

_this2.setState(_this2.persistedStoreCopy);

return;
}

_this2.setState(state);
});
}
}]);

return PersistedState;
}();

var _default = function _default() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (store) {
var persistedState = new PersistedState(options, store);
persistedState.loadOptions();
persistedState.checkStorage();
persistedState.loadInitialState();
persistedState.subscribeOnChanges();
};
};

exports.default = _default;
module.exports = exports["default"];
Loading