From 16c11e20ce2e5db508a2b4fe7483be6cd3e68058 Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Thu, 31 Jan 2019 11:03:43 +0100 Subject: [PATCH 01/10] Added ignoredPath option. --- dist/persisted-state.js | 2 +- src/persisted-state.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/dist/persisted-state.js b/dist/persisted-state.js index af4dc12..56f4507 100644 --- a/dist/persisted-state.js +++ b/dist/persisted-state.js @@ -1 +1 @@ -"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 { if (this.blacklist && this.blacklist(mutation)) return if (this.whitelist && !this.whitelist(mutation)) return + if (this.options.ignoredPaths) { + this.persistedStoreCopy = this.removeIgnoredPaths(state) + this.setState(this.persistedStoreCopy) + return + } this.setState(state) }) From e9779c86a350f02b9a31383c8c13362ef321edcb Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Thu, 31 Jan 2019 11:29:20 +0100 Subject: [PATCH 02/10] Added a few comments, and a unit test. --- src/persisted-state.js | 14 ++++++++------ tests/persisted-state.test.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/persisted-state.js b/src/persisted-state.js index 47b1167..8cc40f4 100644 --- a/src/persisted-state.js +++ b/src/persisted-state.js @@ -53,27 +53,29 @@ class PersistedState { // Removes ignored paths from the store object before persisting it removeIgnoredPaths(state) { try { - // Creates a copy of the store object, + // Creates a copy of the store object var stateCopy = JSON.parse(JSON.stringify(state)) for (let i = 0; i < this.options.ignoredPaths.length; i++) { const path = this.options.ignoredPaths[i] - this.setToValue(stateCopy, undefined, path) + 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.") + throw new Error( + "[Vuex Electron] An error occurred while removing ignored paths from state. Please use a string array of property paths." + ) } } // Sets a property on an object to a supplied value, based on a given property path - setToValue(obj, value, path) { + deleteValue(obj, path) { var i - path = path.split('.') + path = path.split(".") for (i = 0; i < path.length - 1; i++) { obj = obj[path[i]] } - obj[path[i]] = value + delete obj[path[i]] } checkStorage() { diff --git a/tests/persisted-state.test.js b/tests/persisted-state.test.js index 3aa467f..6a1d60a 100644 --- a/tests/persisted-state.test.js +++ b/tests/persisted-state.test.js @@ -149,4 +149,23 @@ describe("createPersistedState", () => { expect(store.state.count).toEqual(0) expect(storage.get("state").count).toEqual(0) }) + + it("filters using ignoredPaths", () => { + const storage = createStorage() + + const store = createStore({ + persistedState: { + ignoredPaths: ["count"], + storage + } + }) + + store.dispatch("increment") + expect(store.state.count).toEqual(1) + expect(storage.get("state").count).toBeUndefined() + + store.dispatch("decrement") + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toBeUndefined() + }) }) From 167d1de4a7ac913572b5ef86c1ba03572c638471 Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Thu, 31 Jan 2019 11:53:01 +0100 Subject: [PATCH 03/10] Updated dist files. --- dist/persisted-state.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/persisted-state.js b/dist/persisted-state.js index 56f4507..9671e42 100644 --- a/dist/persisted-state.js +++ b/dist/persisted-state.js @@ -1 +1 @@ -"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 Date: Sat, 9 Feb 2019 16:23:20 +0100 Subject: [PATCH 04/10] Added a launch.json to make working in VS Code a bit nicer. --- .vscode/launch.json | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7a167ca --- /dev/null +++ b/.vscode/launch.json @@ -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", + } + } + ] +} \ No newline at end of file From be128566057f6606b43b8dca1d85824c41de5e72 Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Sat, 9 Feb 2019 16:25:23 +0100 Subject: [PATCH 05/10] Removed whitelist and blacklist, and added ignoredCommits. --- src/persisted-state.js | 53 +++++++++++++++++++++++++++++++++++++----- tests/helpers.js | 19 ++++++++++++++- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/persisted-state.js b/src/persisted-state.js index 8cc40f4..dc123b6 100644 --- a/src/persisted-state.js +++ b/src/persisted-state.js @@ -16,8 +16,7 @@ class PersistedState { 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) } createStorage() { @@ -32,12 +31,17 @@ class PersistedState { this.options.storage.set(this.options.storageKey, state) } - loadFilter(filter, name) { + loadFilter(filter, name, invertIgnored) { if (!filter) { return null } else if (filter instanceof Array) { return this.filterInArray(filter) } else if (typeof filter === "function") { + if (invertIgnored) { + return (mutation) => { + return !filter(mutation) + } + } return filter } else { throw new Error(`[Vuex Electron] Filter "${name}" should be Array or Function. Please, read the docs.`) @@ -46,6 +50,9 @@ class PersistedState { filterInArray(list) { return (mutation) => { + if (this.options.invertIgnored) { + return !list.includes(mutation.type) + } return list.includes(mutation.type) } } @@ -53,6 +60,15 @@ class PersistedState { // Removes ignored paths from the store object before persisting it removeIgnoredPaths(state) { try { + if (this.options.invertIgnored) { + var newState = {} + for (let i = 0; i < this.options.ignoredPaths.length; i++) { + const 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 (let i = 0; i < this.options.ignoredPaths.length; i++) { @@ -67,7 +83,7 @@ class PersistedState { } } - // Sets a property on an object to a supplied value, based on a given property path + // Deletes, based on a given property path deleteValue(obj, path) { var i path = path.split(".") @@ -78,6 +94,32 @@ class PersistedState { delete obj[path[i]] } + // Curtesy of qiao on Stack Overflow. + 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 + } + + 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 + } + checkStorage() { try { this.options.storage.set(STORAGE_TEST_KEY, STORAGE_TEST_KEY) @@ -119,8 +161,7 @@ class PersistedState { subscribeOnChanges() { this.store.subscribe((mutation, state) => { - if (this.blacklist && this.blacklist(mutation)) return - if (this.whitelist && !this.whitelist(mutation)) return + if (this.ignoredCommits && this.ignoredCommits(mutation)) return if (this.options.ignoredPaths) { this.persistedStoreCopy = this.removeIgnoredPaths(state) this.setState(this.persistedStoreCopy) diff --git a/tests/helpers.js b/tests/helpers.js index 0549915..f6350e5 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -23,7 +23,8 @@ function createStore(options = {}) { return new Vuex.Store({ state: { - count: 0 + count: 0, + count2: 0 }, actions: { increment({ commit }) { @@ -32,6 +33,14 @@ function createStore(options = {}) { decrement({ commit }) { commit("decrement") + }, + + increment2({ commit }) { + commit("increment2") + }, + + decrement2({ commit }) { + commit("decrement2") } }, mutations: { @@ -41,6 +50,14 @@ function createStore(options = {}) { decrement(state) { state.count-- + }, + + increment2(state) { + state.count2++ + }, + + decrement2(state) { + state.count2-- } }, plugins From cc7a5a92330e8a4a1a5fdc9f08991274bada624f Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Sat, 9 Feb 2019 16:25:41 +0100 Subject: [PATCH 06/10] Added relevant tests for ignoredCommits. --- tests/persisted-state.test.js | 89 +++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/tests/persisted-state.test.js b/tests/persisted-state.test.js index 6a1d60a..bd9791c 100644 --- a/tests/persisted-state.test.js +++ b/tests/persisted-state.test.js @@ -48,124 +48,133 @@ describe("createPersistedState", () => { expect(store.state.count).toEqual(randomNumber) }) - it("filters whitelist (wrong type)", () => { + it("filters ignoredCommits (wrong type)", () => { const storage = createStorage() expect(() => { createStore({ persistedState: { - whitelist: {}, + ignoredCommits: {}, storage } }) }).toThrow() }) - it("filters whitelist (array)", () => { + it("filters ignoredCommits (array)", () => { const storage = createStorage() const store = createStore({ persistedState: { - whitelist: ["increment"], + ignoredCommits: ["increment"], storage } }) store.dispatch("increment") expect(store.state.count).toEqual(1) - expect(storage.get("state").count).toEqual(1) + expect(storage.get("state")).toBeUndefined() store.dispatch("decrement") expect(store.state.count).toEqual(0) - expect(storage.get("state").count).toEqual(1) + expect(storage.get("state").count).toEqual(0) }) - it("filters whitelist (function)", () => { + it("filters ignoredCommits (function)", () => { const storage = createStorage() const store = createStore({ persistedState: { - whitelist: (mutation) => ["increment"].includes(mutation.type), + ignoredCommits: (mutation) => ["increment"].includes(mutation.type), storage } }) store.dispatch("increment") expect(store.state.count).toEqual(1) - expect(storage.get("state").count).toEqual(1) + expect(storage.get("state")).toBeUndefined() store.dispatch("decrement") expect(store.state.count).toEqual(0) - expect(storage.get("state").count).toEqual(1) - }) - - it("filters blacklist (wrong type)", () => { - const storage = createStorage() - - expect(() => { - createStore({ - persistedState: { - blacklist: {}, - storage - } - }) - }).toThrow() + expect(storage.get("state").count).toEqual(0) }) - it("filters blacklist (array)", () => { + it("filters using ignoredPaths", () => { const storage = createStorage() const store = createStore({ persistedState: { - blacklist: ["increment"], + ignoredPaths: ["count"], storage } }) - store.dispatch("increment") + store.dispatch("increment") // Modifications will NOT be allowed to be persisted. expect(store.state.count).toEqual(1) - expect(storage.get("state")).toBeUndefined() + expect(storage.get("state").count).toBeUndefined() - store.dispatch("decrement") + store.dispatch("decrement") // Modifications will NOT be allowed to be persisted. expect(store.state.count).toEqual(0) - expect(storage.get("state").count).toEqual(0) + expect(storage.get("state").count).toBeUndefined() }) - it("filters blacklist (function)", () => { + it("filters using ignoredPaths and ignoredCommits", () => { const storage = createStorage() const store = createStore({ persistedState: { - blacklist: (mutation) => ["increment"].includes(mutation.type), + ignoredPaths: ["count"], + ignoredCommits: (mutation) => ["increment2"].includes(mutation.type), storage } }) - store.dispatch("increment") + store.dispatch("increment") // Modifications will NOT be allowed to be persisted, but WILL trigger a persistance. expect(store.state.count).toEqual(1) - expect(storage.get("state")).toBeUndefined() + expect(storage.get("state").count).toBeUndefined() - store.dispatch("decrement") + store.dispatch("decrement") // Modifications will NOT be allowed to be persisted, but WILL trigger a persistance. expect(store.state.count).toEqual(0) - expect(storage.get("state").count).toEqual(0) + expect(storage.get("state").count).toBeUndefined() + + store.dispatch("increment2") // Modifications WILL be allowed to be persisted, but wont trigger a persistance. + expect(store.state.count2).toEqual(1) + expect(storage.get("state").count2).toEqual(0) + + store.dispatch("decrement2") // Modifications WILL be allowed to be persisted, AND WILL trigger a persistance. + expect(store.state.count2).toEqual(0) + expect(storage.get("state").count2).toEqual(0) }) - it("filters using ignoredPaths", () => { + it("filters using ignoredPaths, ignoredCommits and invertIgnored", () => { const storage = createStorage() const store = createStore({ persistedState: { ignoredPaths: ["count"], + ignoredCommits: (mutation) => ["increment2"].includes(mutation.type), + invertIgnored: true, storage } }) - store.dispatch("increment") + store.dispatch("increment") // Modifications will be allowed to be persisted, but wont trigger a persistance. expect(store.state.count).toEqual(1) - expect(storage.get("state").count).toBeUndefined() + expect(storage.get("state")).toBeUndefined() - store.dispatch("decrement") + store.dispatch("decrement") // Modifications will be allowed to be persisted, but wont trigger a persistance. expect(store.state.count).toEqual(0) - expect(storage.get("state").count).toBeUndefined() + expect(storage.get("state")).toBeUndefined() + + store.dispatch("increment2") // Modifications will NOT be allowed to be persisted, but WILL trigger a persistance. + expect(store.state.count2).toEqual(1) + expect(storage.get("state").count2).toBeUndefined() + expect(storage.get("state").count).toEqual(0) + + store.dispatch("decrement2") // Modifications will NOT be allowed to be persisted, AND will NOT trigger a persistance. + expect(store.state.count2).toEqual(0) + expect(storage.get("state").count2).toBeUndefined() + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toEqual(0) }) }) From 863ed8448e5149c1d00997b2b3369001806f9bb9 Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Fri, 1 Mar 2019 23:03:59 +0100 Subject: [PATCH 07/10] Readded whitelist and blacklist code for compatibility, --- src/persisted-state.js | 6 ++ tests/persisted-state.test.js | 102 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/persisted-state.js b/src/persisted-state.js index dc123b6..0faf0e5 100644 --- a/src/persisted-state.js +++ b/src/persisted-state.js @@ -16,6 +16,9 @@ class PersistedState { 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) } @@ -161,6 +164,9 @@ class PersistedState { subscribeOnChanges() { this.store.subscribe((mutation, state) => { + if (this.blacklist && this.blacklist(mutation)) return + if (this.whitelist && !this.whitelist(mutation)) return + if (this.ignoredCommits && this.ignoredCommits(mutation)) return if (this.options.ignoredPaths) { this.persistedStoreCopy = this.removeIgnoredPaths(state) diff --git a/tests/persisted-state.test.js b/tests/persisted-state.test.js index bd9791c..0c0c23c 100644 --- a/tests/persisted-state.test.js +++ b/tests/persisted-state.test.js @@ -48,6 +48,108 @@ describe("createPersistedState", () => { expect(store.state.count).toEqual(randomNumber) }) + it("filters whitelist (wrong type)", () => { + const storage = createStorage() + + expect(() => { + createStore({ + persistedState: { + whitelist: {}, + storage + } + }) + }).toThrow() + }) + + it("filters whitelist (array)", () => { + const storage = createStorage() + + const store = createStore({ + persistedState: { + whitelist: ["increment"], + storage + } + }) + + store.dispatch("increment") + expect(store.state.count).toEqual(1) + expect(storage.get("state").count).toEqual(1) + + store.dispatch("decrement") + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toEqual(1) + }) + + it("filters whitelist (function)", () => { + const storage = createStorage() + + const store = createStore({ + persistedState: { + whitelist: (mutation) => ["increment"].includes(mutation.type), + storage + } + }) + + store.dispatch("increment") + expect(store.state.count).toEqual(1) + expect(storage.get("state").count).toEqual(1) + + store.dispatch("decrement") + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toEqual(1) + }) + + it("filters blacklist (wrong type)", () => { + const storage = createStorage() + + expect(() => { + createStore({ + persistedState: { + blacklist: {}, + storage + } + }) + }).toThrow() + }) + + it("filters blacklist (array)", () => { + const storage = createStorage() + + const store = createStore({ + persistedState: { + blacklist: ["increment"], + storage + } + }) + + store.dispatch("increment") + expect(store.state.count).toEqual(1) + expect(storage.get("state")).toBeUndefined() + + store.dispatch("decrement") + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toEqual(0) + }) + + it("filters blacklist (function)", () => { + const storage = createStorage() + + const store = createStore({ + persistedState: { + blacklist: (mutation) => ["increment"].includes(mutation.type), + storage + } + }) + + store.dispatch("increment") + expect(store.state.count).toEqual(1) + expect(storage.get("state")).toBeUndefined() + + store.dispatch("decrement") + expect(store.state.count).toEqual(0) + expect(storage.get("state").count).toEqual(0) + }) + it("filters ignoredCommits (wrong type)", () => { const storage = createStorage() From 63c467b65c2dd4f44593fb4a189526cc802f729f Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Fri, 1 Mar 2019 23:13:43 +0100 Subject: [PATCH 08/10] Added some documentation and comments. --- README.md | 31 +++++++++++++++---------------- src/persisted-state.js | 3 +++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 55ab273..8df572f 100644 --- a/README.md +++ b/README.md @@ -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: ["whitelistedAction", "anotherWhitelistedAction"], - - // or - - whitelist: (mutation) => { - return true - }, - - // or - - blacklist: ["ignoredAction", "anotherIgnoredAction"], - - // 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 } }) ``` diff --git a/src/persisted-state.js b/src/persisted-state.js index 0faf0e5..88a4e57 100644 --- a/src/persisted-state.js +++ b/src/persisted-state.js @@ -167,7 +167,10 @@ class PersistedState { if (this.blacklist && this.blacklist(mutation)) return if (this.whitelist && !this.whitelist(mutation)) return + // Returns if the current commit should not cause persistance. if (this.ignoredCommits && this.ignoredCommits(mutation)) return + + // Filters the state before persisting, if ignoredPaths is set. if (this.options.ignoredPaths) { this.persistedStoreCopy = this.removeIgnoredPaths(state) this.setState(this.persistedStoreCopy) From 25b83a5d3ecbd9d76f912a2300076736aab37106 Mon Sep 17 00:00:00 2001 From: Martin Finnerup Date: Sun, 3 Mar 2019 04:18:31 +0100 Subject: [PATCH 09/10] Simplified a small bit of code, and updated dist files. --- dist/index.js | 24 +++- dist/persisted-state.js | 257 ++++++++++++++++++++++++++++++++++++++- dist/shared-mutations.js | 166 ++++++++++++++++++++++++- src/persisted-state.js | 9 +- 4 files changed, 449 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index fb5c3ec..e4fe42c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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}} \ No newline at end of file +"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 }; } \ No newline at end of file diff --git a/dist/persisted-state.js b/dist/persisted-state.js index 9671e42..ecad33b 100644 --- a/dist/persisted-state.js +++ b/dist/persisted-state.js @@ -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 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"]; \ No newline at end of file diff --git a/dist/shared-mutations.js b/dist/shared-mutations.js index 40a787d..9262856 100644 --- a/dist/shared-mutations.js +++ b/dist/shared-mutations.js @@ -1 +1,165 @@ -"use strict";var _electron=require("electron");Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;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 0 && arguments[0] !== undefined ? arguments[0] : {}; + return function (store) { + var sharedMutations = new SharedMutations(options, store); + sharedMutations.loadOptions(); + sharedMutations.activatePlugin(); + }; +}; + +exports.default = _default; +module.exports = exports["default"]; \ No newline at end of file diff --git a/src/persisted-state.js b/src/persisted-state.js index 88a4e57..16c9030 100644 --- a/src/persisted-state.js +++ b/src/persisted-state.js @@ -37,18 +37,19 @@ class PersistedState { loadFilter(filter, name, invertIgnored) { if (!filter) { return null - } else if (filter instanceof Array) { + } + if (filter instanceof Array) { return this.filterInArray(filter) - } else if (typeof filter === "function") { + } + if (typeof filter === "function") { if (invertIgnored) { return (mutation) => { return !filter(mutation) } } return filter - } else { - throw new Error(`[Vuex Electron] Filter "${name}" should be Array or Function. Please, read the docs.`) } + throw new Error(`[Vuex Electron] Filter "${name}" should be Array or Function. Please, read the docs.`) } filterInArray(list) { From 79f37c20a86e570783420c713035037c9907bcc8 Mon Sep 17 00:00:00 2001 From: Tyler Storm Date: Thu, 28 Mar 2019 13:29:46 -0400 Subject: [PATCH 10/10] Minify The Dist files --- dist/index.js | 24 +--- dist/persisted-state.js | 257 +-------------------------------------- dist/shared-mutations.js | 166 +------------------------ 3 files changed, 3 insertions(+), 444 deletions(-) diff --git a/dist/index.js b/dist/index.js index e4fe42c..fb5c3ec 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,23 +1 @@ -"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 }; } \ No newline at end of file +"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}} \ No newline at end of file diff --git a/dist/persisted-state.js b/dist/persisted-state.js index ecad33b..c09c68c 100644 --- a/dist/persisted-state.js +++ b/dist/persisted-state.js @@ -1,256 +1 @@ -"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"]; \ No newline at end of file +"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 0 && arguments[0] !== undefined ? arguments[0] : {}; - return function (store) { - var sharedMutations = new SharedMutations(options, store); - sharedMutations.loadOptions(); - sharedMutations.activatePlugin(); - }; -}; - -exports.default = _default; -module.exports = exports["default"]; \ No newline at end of file +"use strict";var _electron=require("electron");Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;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