diff --git a/src/application/utils/apply-expression.js b/src/application/utils/apply-expression.js index 7b6f3d37a..b3cb08566 100644 --- a/src/application/utils/apply-expression.js +++ b/src/application/utils/apply-expression.js @@ -1,3 +1,4 @@ +import get from "lodash.get"; import store from "../worker/store"; export function applyExpression({ value, inputId }) { @@ -5,12 +6,15 @@ export function applyExpression({ value, inputId }) { inputId ); + const input = store.state.inputs.inputs[inputId]; + let dataOut = value; if (expressionAssignment) { const scope = { value: dataOut, - time: Date.now() + time: Date.now(), + inputValue: get(store.state, input.getLocation) }; dataOut = expressionAssignment.func.evaluate(scope); diff --git a/src/application/worker/index.worker.js b/src/application/worker/index.worker.js index 96bcd6c36..1db695556 100644 --- a/src/application/worker/index.worker.js +++ b/src/application/worker/index.worker.js @@ -344,6 +344,7 @@ async function start() { store.commit("groups/SWAP"); store.commit("modules/SWAP"); store.commit("inputs/SWAP"); + store.commit("expressions/SWAP"); return; } diff --git a/src/application/worker/store/modules/common/swap.js b/src/application/worker/store/modules/common/swap.js index 667aa3674..9eed32aa5 100644 --- a/src/application/worker/store/modules/common/swap.js +++ b/src/application/worker/store/modules/common/swap.js @@ -7,7 +7,7 @@ import Vue from "vue"; * 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) { +export function SWAP(swap, getDefault, sharedPropertyRestrictions) { return function(state) { const stateKeys = Object.keys(state); @@ -82,8 +82,6 @@ export default function SWAP(swap, getDefault, sharedPropertyRestrictions) { } } }); - } else { - Object.assign(swap, getDefault()); } Object.assign(swap, getDefault()); diff --git a/src/application/worker/store/modules/expressions.js b/src/application/worker/store/modules/expressions.js index 6bfc2fbe3..cfca79b47 100644 --- a/src/application/worker/store/modules/expressions.js +++ b/src/application/worker/store/modules/expressions.js @@ -1,9 +1,16 @@ +import get from "lodash.get"; import { v4 as uuidv4 } from "uuid"; +import { SWAP } from "./common/swap"; const math = require("mathjs"); -const state = { - assignments: {} -}; +function getDefaultState() { + return { + assignments: {} + }; +} + +const state = getDefaultState(); +const swap = getDefaultState(); // getters const getters = { @@ -14,8 +21,8 @@ const getters = { } }; -function compileExpression(expression) { - const scope = { value: 0, time: 0 }; +function compileExpression(expression, scopeItems = {}) { + const scope = { value: 0, time: 0, ...scopeItems }; let newFunction; try { @@ -32,7 +39,10 @@ function compileExpression(expression) { // actions const actions = { - create({ commit }, { expression = "value", id, inputId }) { + create( + { rootState, commit }, + { expression = "value", id, inputId, writeToSwap } + ) { if (!inputId) { throw new Error("Input ID required"); } @@ -43,7 +53,15 @@ const actions = { const expressionId = id || uuidv4(); - const func = compileExpression(expression); + const input = rootState.inputs.inputs[inputId]; + + const func = compileExpression(expression, { + // We currrently have no way of interacting with swap state. + // This would be something to fix in the future, maybe use an entire store + // for swap, or write a more specific mechanism to look up values in swap + // state. + inputValue: writeToSwap ? 0 : get(rootState, input.getLocation) + }); if (!func) { throw new Error("Unable to compile Expression"); @@ -56,12 +74,12 @@ const actions = { expression }; - commit("ADD_EXPRESSION", { assignment }); + commit("ADD_EXPRESSION", { assignment, writeToSwap }); return expressionId; }, - update({ commit }, { id, expression = "value" }) { + update({ rootState, commit }, { id, expression = "value", writeToSwap }) { if (!id) { throw new Error("Expression ID required"); } @@ -77,7 +95,11 @@ const actions = { return null; } - const func = compileExpression(expression); + const input = rootState.inputs.inputs[existingExpression.inputId]; + + const func = compileExpression(expression, { + inputValue: get(rootState, input.getLocation) + }); if (!func) { throw new Error("Unable to compile Expression"); @@ -86,7 +108,7 @@ const actions = { existingExpression.func = func; existingExpression.expression = expression; - commit("ADD_EXPRESSION", { assignment: existingExpression }); + commit("ADD_EXPRESSION", { assignment: existingExpression, writeToSwap }); return existingExpression.id; }, @@ -103,20 +125,23 @@ const actions = { for (let i = 0, len = assignments.length; i < len; i++) { const assignment = assignments[i]; - await dispatch("create", assignment); + await dispatch("create", { ...assignment, writeToSwap: true }); } } }; // mutations const mutations = { - ADD_EXPRESSION(state, { assignment }) { - state.assignments[assignment.id] = assignment; + ADD_EXPRESSION(state, { assignment, writeToSwap = false }) { + const writeTo = writeToSwap ? swap : state; + writeTo.assignments[assignment.id] = assignment; }, REMOVE_EXPRESSION(state, { id }) { delete state.assignments[id]; - } + }, + + SWAP: SWAP(swap, getDefaultState) }; export default { diff --git a/src/application/worker/store/modules/groups.js b/src/application/worker/store/modules/groups.js index db7698a62..d480ecb29 100644 --- a/src/application/worker/store/modules/groups.js +++ b/src/application/worker/store/modules/groups.js @@ -1,4 +1,4 @@ -import SWAP from "./common/swap"; +import { SWAP } from "./common/swap"; import store from "../"; import constants, { GROUP_DISABLED } from "../../../constants"; import { v4 as uuidv4 } from "uuid"; diff --git a/src/application/worker/store/modules/inputs.js b/src/application/worker/store/modules/inputs.js index 62ebf43e8..bc4dd8fed 100644 --- a/src/application/worker/store/modules/inputs.js +++ b/src/application/worker/store/modules/inputs.js @@ -1,6 +1,6 @@ import Vue from "vue"; import { v4 as uuidv4 } from "uuid"; -import SWAP from "./common/swap"; +import { SWAP } from "./common/swap"; /** * InputLinkType enum string values. @@ -99,8 +99,11 @@ const actions = { commit("SET_FOCUSED_INPUT", { id: null, title: null }); }, - addInput({ commit }, { type, location, data, id = uuidv4(), writeToSwap }) { - const input = { type, location, data, id }; + addInput( + { commit }, + { type, getLocation, location, data, id = uuidv4(), writeToSwap } + ) { + const input = { type, getLocation, location, data, id }; commit("ADD_INPUT", { input, writeToSwap }); return input; }, diff --git a/src/application/worker/store/modules/modules.js b/src/application/worker/store/modules/modules.js index a5c30a66e..580ed463d 100644 --- a/src/application/worker/store/modules/modules.js +++ b/src/application/worker/store/modules/modules.js @@ -1,5 +1,5 @@ import Vue from "vue"; -import SWAP from "./common/swap"; +import { SWAP } from "./common/swap"; import getNextName from "../../../utils/get-next-name"; import getPropDefault from "../../../utils/get-prop-default"; import store from ".."; @@ -80,6 +80,7 @@ async function initialiseModuleProperties( ) { const inputBind = await store.dispatch("inputs/addInput", { type: "action", + getLocation: `modules.active["${module.$id}"].props["${propKey}"]`, location: "modules/updateProp", data: { moduleId: module.$id, prop: propKey }, writeToSwap @@ -96,6 +97,7 @@ async function initialiseModuleProperties( const key = dataTypeInputsKeys[i]; await store.dispatch("inputs/addInput", { type: "action", + getLocation: `modules.active["${module.$id}"].props["${propKey}"]["${key}"]`, location: "modules/updateProp", data: { moduleId: module.$id, @@ -288,6 +290,7 @@ const actions = { if (!moduleMeta.isGallery) { const alphaInputBind = await store.dispatch("inputs/addInput", { type: "action", + getLocation: `modules.active["${module.$id}"].meta.alpha`, location: "modules/updateMeta", data: { id: module.$id, metaKey: "alpha" } }); @@ -296,6 +299,7 @@ const actions = { const enabledInputBind = await store.dispatch("inputs/addInput", { type: "action", + getLocation: `modules.active["${module.$id}"].meta.enabled`, location: "modules/updateMeta", data: { id: module.$id, metaKey: "enabled" } }); @@ -304,6 +308,7 @@ const actions = { const coInputBind = await store.dispatch("inputs/addInput", { type: "action", + getLocation: `modules.active["${module.$id}"].meta.compositeOperation`, location: "modules/updateMeta", data: { moduleId: module.$id, metaKey: "compositeOperation" } });