Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expressions): adds inputValue as scope item #859

Merged
merged 4 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/application/utils/apply-expression.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import get from "lodash.get";
import store from "../worker/store";

export function applyExpression({ value, inputId }) {
const expressionAssignment = store.getters["expressions/getByInputId"](
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);
Expand Down
1 change: 1 addition & 0 deletions src/application/worker/index.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ async function start() {
store.commit("groups/SWAP");
store.commit("modules/SWAP");
store.commit("inputs/SWAP");
store.commit("expressions/SWAP");

return;
}
Expand Down
4 changes: 1 addition & 3 deletions src/application/worker/store/modules/common/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -82,8 +82,6 @@ export default function SWAP(swap, getDefault, sharedPropertyRestrictions) {
}
}
});
} else {
Object.assign(swap, getDefault());
}

Object.assign(swap, getDefault());
Expand Down
55 changes: 40 additions & 15 deletions src/application/worker/store/modules/expressions.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 {
Expand All @@ -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");
}
Expand All @@ -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");
Expand All @@ -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");
}
Expand All @@ -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");
Expand All @@ -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;
},

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/application/worker/store/modules/groups.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
9 changes: 6 additions & 3 deletions src/application/worker/store/modules/inputs.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
},
Expand Down
7 changes: 6 additions & 1 deletion src/application/worker/store/modules/modules.js
Original file line number Diff line number Diff line change
@@ -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 "..";
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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" }
});
Expand All @@ -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" }
});
Expand All @@ -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" }
});
Expand Down