Skip to content

Commit

Permalink
Restore module restored as property of the module
Browse files Browse the repository at this point in the history
  • Loading branch information
robbevp committed Jul 11, 2021
1 parent 6a288fd commit 0c16e98
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/store/albums.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
compareAlbumsByReleaseFirst,
compareAlbumsByTitleFirst,
} from "../comparators";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -108,7 +107,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.albumsRestored;
await this.restored;
await fetchAll(commit, generator, "setAlbums");
return true;
} catch (error) {
Expand All @@ -129,7 +128,7 @@ export default {
async read({ commit, rootState }, id) {
try {
const result = await read(rootState.auth, id);
await store.albumsRestored;
await this.restored;
commit("setAlbum", { id, album: result });
return result.id;
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/artists.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "../api/artists";
import { fetchAll } from "./actions";
import { compareStrings } from "../comparators";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -58,7 +57,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.artistsRestored;
await this.restored;
await fetchAll(commit, generator, "setArtists");
return true;
} catch (error) {
Expand All @@ -79,7 +78,7 @@ export default {
async read({ commit, rootState }, id) {
try {
const result = await read(rootState.auth, id);
await store.artistsRestored;
await this.restored;
commit("setArtist", { id, artist: result });
return result.id;
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/genres.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "../api/genres";
import { fetchAll } from "./actions";
import { compareStrings } from "../comparators";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -58,7 +57,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.genresRestored;
await this.restored;
await fetchAll(commit, generator, "setGenres");
return true;
} catch (error) {
Expand All @@ -79,7 +78,7 @@ export default {
async read({ commit, rootState }, id) {
try {
const result = await read(rootState.auth, id);
await store.genresRestored;
await this.restored;
commit("setGenre", { id, genre: result });
return result.id;
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "../api/labels";
import { fetchAll } from "./actions";
import { compareStrings } from "../comparators";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -58,7 +57,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.labelsRestored;
await this.restored;
await fetchAll(commit, generator, "setLabels");
return true;
} catch (error) {
Expand All @@ -79,7 +78,7 @@ export default {
async read({ commit, rootState }, id) {
try {
const result = await read(rootState.auth, id);
await store.labelsRestored;
await this.restored;
commit("setLabel", { id, label: result });
return result.id;
} catch (error) {
Expand Down
25 changes: 11 additions & 14 deletions src/store/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ class VuexPersistentModule extends VuexPersistence {

// Overwrite plugin to use replaceState and set a custom restored prop for each module
this.plugin = (store) => {
store[`${module}Restored`] = this.replaceState(
this.key,
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)
);
}
store._modules.root._children[module]._rawModule.restored =
this.replaceState(this.key, 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;
});
this.subscribed = true;
});
};
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/store/plays.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { index, create } from "../api/plays";
import { fetchAll } from "./actions";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -45,7 +44,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.playsRestored;
await this.restored;
await fetchAll(commit, generator, "setPlays");
return true;
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Vue from "vue";
import { index, create, destroy, update, read, merge } from "../api/tracks";
import { fetchAll } from "./actions";
import { compareTracks } from "../comparators";
import store from "./store";

export default {
namespaced: true,
Expand Down Expand Up @@ -101,7 +100,7 @@ export default {
async index({ commit, rootState }) {
const generator = index(rootState.auth);
try {
await store.tracksRestored;
await this.restored;
await fetchAll(commit, generator, "setTracks");
return true;
} catch (error) {
Expand All @@ -122,7 +121,7 @@ export default {
async read({ commit, rootState }, id) {
try {
const track = await read(rootState.auth, id);
await store.tracksRestored;
await this.restored;
commit("setTrack", { id, track });
return true;
} catch (error) {
Expand Down

0 comments on commit 0c16e98

Please sign in to comment.