-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
179 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import VuexPersistence from "vuex-persist"; | ||
import localForage from "localforage"; | ||
import debounce from "lodash.debounce"; | ||
|
||
// A subclass for VuexPersistence to deal with single namespaced modules and large collections | ||
// Should be provided with a module and an async storage | ||
// IMPORTANT: This class assumes a lot about the structure of state and mutations available in each module | ||
// If this structure changes, we inevitably have to update this class | ||
class VuexPersistentModule extends VuexPersistence { | ||
constructor(module, storage) { | ||
const lowerCaseModule = module.toLowerCase(); | ||
// Write startLoading and the main collection (as an array) to storage | ||
async function saveModule(module, state, storage) { | ||
const modifiedState = { | ||
startLoading: state[module].startLoading, | ||
}; | ||
modifiedState[module] = Object.values(state[module][module]); | ||
await storage.setItem(module, modifiedState); | ||
} | ||
|
||
// Set all options in parent class | ||
super({ | ||
storage, | ||
asyncStorage: true, | ||
modules: [lowerCaseModule], | ||
key: lowerCaseModule, | ||
filter: (m) => | ||
m.type.substring(0, m.type.indexOf("/")) === lowerCaseModule, | ||
saveState: debounce(saveModule, 60000), | ||
}); | ||
|
||
this.capitilizedModule = module; | ||
|
||
// Get item from an async storage and commit them back to the store | ||
this.replaceState = async ( | ||
lowerCaseModule, | ||
capitilizedModule, | ||
storage, | ||
store | ||
) => { | ||
const savedState = await storage.getItem(lowerCaseModule); | ||
if (savedState) { | ||
if (savedState.startLoading) { | ||
store.commit( | ||
`${lowerCaseModule}/setStartLoading`, | ||
savedState.startLoading | ||
); | ||
} | ||
if (savedState[lowerCaseModule]) { | ||
store.commit( | ||
`${lowerCaseModule}/set${capitilizedModule}`, | ||
savedState[lowerCaseModule] | ||
); | ||
} | ||
} | ||
}; | ||
|
||
// Overwrite plugin to use replaceState and set a custom restored prop for each module | ||
this.plugin = (store) => { | ||
store[`${this.key}Restored`] = this.replaceState( | ||
this.key, | ||
this.capitilizedModule, | ||
this.storage, | ||
store | ||
).then(() => { | ||
// The subscriber is the same as in VuexPersistence, but we have to add it ourselves since we overwrite the plugin method | ||
this.subscriber(store)((mutation, state) => { | ||
if (this.filter(mutation)) { | ||
this._mutex.enqueue( | ||
this.saveState(this.key, this.reducer(state), this.storage) | ||
); | ||
} | ||
}); | ||
this.subscribed = true; | ||
}); | ||
}; | ||
} | ||
} | ||
|
||
const plugins = [ | ||
"Albums", | ||
"Artists", | ||
"Genres", | ||
"Labels", | ||
"Plays", | ||
"Tracks", | ||
].map((module) => { | ||
const plugin = new VuexPersistentModule(module, localForage); | ||
return plugin.plugin; | ||
}); | ||
|
||
const localStorageModules = [ | ||
"auth", | ||
"codecConversions", | ||
"codecs", | ||
"coverFilenames", | ||
"imageTypes", | ||
"locations", | ||
"rescan", | ||
"users", | ||
"userSettings", | ||
]; | ||
|
||
export const vuexLocalStorage = new VuexPersistence({ | ||
storage: window.localStorage, | ||
strictMode: process.env.NODE_ENV !== "production", | ||
modules: localStorageModules, | ||
filter: (m) => | ||
localStorageModules.includes(m.type.substring(0, m.type.indexOf("/"))), | ||
}); | ||
|
||
plugins.push(vuexLocalStorage.plugin); | ||
|
||
export default plugins; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.