Skip to content

Commit

Permalink
fix(swap): fixes array handling with SWAP (#474)
Browse files Browse the repository at this point in the history
* fix(swap): fixes array handling with SWAP

Fixes array handling when using SWAP state. Removes temp entirely

* chore(swap): Removed debugger and added some bit of info about what is happening
  • Loading branch information
2xAA authored Dec 4, 2020
1 parent 8b9bd11 commit 58fee65
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 56 deletions.
68 changes: 33 additions & 35 deletions src/application/worker/store/modules/common/swap.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Vue from "vue";
// import cloneDeep from "lodash.clonedeep";

export default function SWAP(
swap,
temp,
getDefault,
sharedPropertyRestrictions
) {
/*
* When loading a preset we want to swap the data from the preset with the current state
* and make sure that only the "allowed" properties are moved, see sharedPropertyRestrictions.
*
* The idea is that this makes loading presets smooth and the end user will not see any
* glitches in the render loop.
*/
export default function SWAP(swap, getDefault, sharedPropertyRestrictions) {
return function(state) {
const stateKeys = Object.keys(state);

Expand All @@ -17,38 +18,32 @@ export default function SWAP(

if (sharedPropertyRestrictions) {
if (typeof sharedPropertyRestrictions[key] === "function") {
const restrictedKeys = sharedPropertyRestrictions[key](state[key]);
const stateChildKeys = Object.keys(state[key]);

// eslint-disable-next-line
if (isArray) {
Vue.set(temp, key, []);
state[key] = state[key].filter(
sharedPropertyRestrictions[key](state[key])
);
} else {
Vue.set(temp, key, {});
}
const restrictedKeys = sharedPropertyRestrictions[key](
state[key]
);

// eslint-disable-next-line
stateChildKeys.forEach(stateChildKey => {
if (restrictedKeys.indexOf(stateChildKey) < 0) {
if (isArray) {
temp[key].push(state[key][stateChildKey]);
state[key].splice(stateChildKey, 1);
} else {
Vue.set(temp[key], stateChildKey, state[key][stateChildKey]);
// eslint-disable-next-line
stateChildKeys.forEach(stateChildKey => {
if (restrictedKeys.indexOf(stateChildKey) < 0) {
delete state[key][stateChildKey];
}
}
});
});
}
} else if (sharedPropertyRestrictions[key]) {
Vue.set(temp, key, state[key]);
delete state[key];
}
} else {
Vue.set(temp, key, state[key]);
delete state[key];
}
});
} else {
Object.assign(temp, getDefault());
}

const swapKeys = Object.keys(swap);
Expand All @@ -60,19 +55,22 @@ export default function SWAP(

if (sharedPropertyRestrictions) {
if (typeof sharedPropertyRestrictions[key] === "function") {
const restrictedKeys = sharedPropertyRestrictions[key](swap[key]);
const swapChildKeys = Object.keys(swap[key]);

// eslint-disable-next-line
swapChildKeys.forEach(swapChildKey => {
if (restrictedKeys.indexOf(swapChildKey) < 0) {
if (isArray) {
state[key].push(swap[key][swapChildKey]);
} else {
if (isArray) {
state[key] = swap[key].filter(
sharedPropertyRestrictions[key](swap[key])
);
} else {
const restrictedKeys = sharedPropertyRestrictions[key](swap[key]);

// eslint-disable-next-line
swapChildKeys.forEach(swapChildKey => {
if (restrictedKeys.indexOf(swapChildKey) < 0) {
Vue.set(state[key], swapChildKey, swap[key][swapChildKey]);
}
}
});
});
}
} else if (sharedPropertyRestrictions[key]) {
if (isArray) {
Vue.set(state, key, [...swap[key]]);
Expand All @@ -92,6 +90,6 @@ export default function SWAP(
Object.assign(swap, getDefault());
}

Object.assign(swap, temp || getDefault());
Object.assign(swap, getDefault());
};
}
18 changes: 7 additions & 11 deletions src/application/worker/store/modules/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,17 @@ import uuidv4 from "uuid/v4";

const state = { groups: [] };
const swap = { groups: [] };
const temp = { groups: [] };

// Any keys marked false or arrays with keys given
// will not be moved from the base state when swapped
const sharedPropertyRestrictions = {
groups: (
// keeps gallery group in place
value
) => {
const index = value.findIndex(
group => group.name === constants.GALLERY_GROUP_NAME
);

return index > -1 ? [`${index}`] : [];
}
// As state.groups is an Array, we return a function which checks the item in the Array,
// returns a boolean. True to remove, false to keep.
// Objects return an Array of keys to remove.
// This keeps gallery group in place
group
) => () => group.name !== constants.GALLERY_GROUP_NAME
};

const actions = {
Expand Down Expand Up @@ -292,7 +288,7 @@ const mutations = {
}
},

SWAP: SWAP(swap, temp, () => ({ groups: [] }), sharedPropertyRestrictions)
SWAP: SWAP(swap, () => ({ groups: [] }), sharedPropertyRestrictions)
};

export default {
Expand Down
3 changes: 1 addition & 2 deletions src/application/worker/store/modules/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function getDefaultState() {

const state = getDefaultState();
const swap = getDefaultState();
const temp = getDefaultState();

const getters = {};

Expand Down Expand Up @@ -146,7 +145,7 @@ const mutations = {
Vue.delete(writeTo.inputLinks, inputId);
},

SWAP: SWAP(swap, temp, getDefaultState)
SWAP: SWAP(swap, getDefaultState)
};

export default {
Expand Down
9 changes: 1 addition & 8 deletions src/application/worker/store/modules/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ const swap = {
metaQueue: {}
};

const temp = {
registered: {},
active: {},
propQueue: {},
metaQueue: {}
};

// this function either creates module properties from an existing module
// (e.g. loading a preset) or initialises the default value
async function initialiseModuleProperties(
Expand Down Expand Up @@ -536,7 +529,7 @@ const mutations = {
Vue.set(writeTo.active[id].meta, metaKey, data);
},

SWAP: SWAP(swap, temp, () => ({}), sharedPropertyRestrictions)
SWAP: SWAP(swap, () => ({}), sharedPropertyRestrictions)
};

export default {
Expand Down

0 comments on commit 58fee65

Please sign in to comment.