Skip to content

Commit

Permalink
chore: remove console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Dec 4, 2019
1 parent f8ca9a1 commit 0369bae
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 176 deletions.
3 changes: 0 additions & 3 deletions src/src/main/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ define([
.then(doInitScript)
.then(
function () {
console.log('has changed');
ActionManager.viewHasChanged(view);
console.log(ActionManager.listActions())
console.log('finished changed');
ModuleFactory.getModules().forEach(function (module) {
if (
module.controller &&
Expand Down
346 changes: 173 additions & 173 deletions src/src/util/actionmanager.js
Original file line number Diff line number Diff line change
@@ -1,197 +1,197 @@
"use strict";
'use strict';

define(["./versioning", "./sandbox"], function(Versioning, Sandbox) {
let API;
let evaluatedScripts;
define(['./versioning', './sandbox'], function (Versioning, Sandbox) {
let API;
let evaluatedScripts;

function doScripts() {
const data = getActionScripts();
function doScripts() {
const data = getActionScripts();

if (!data || evaluatedScripts) {
return;
}
if (!data || evaluatedScripts) {
return;
}

const evalSandbox = new Sandbox();
evalSandbox.setContext({ API });
const evaled = {};
const evalSandbox = new Sandbox();
evalSandbox.setContext({ API });
const evaled = {};

for (let i = 0; i < data.length; i++) {
const action = data[i].groups.action[0];
evaled[action.name[0]] = evalSandbox.run(
`(function(value) { ${action.script[0]} })`
);
}

evaluatedScripts = evaled;
for (let i = 0; i < data.length; i++) {
const action = data[i].groups.action[0];
evaled[action.name[0]] = evalSandbox.run(
`(function(value) { ${action.script[0]} })`
);
}

function getActionScripts() {
return Versioning.getView().actionscripts || [];
}
evaluatedScripts = evaled;
}

function setActionScripts(form) {
evaluatedScripts = undefined;
Versioning.getView().actionscripts = form; // Keeps track of the scripts in the view
doScripts(form);
}
function getActionScripts() {
return Versioning.getView().actionscripts || [];
}

let actionsFiles;
function setActionScripts(form) {
evaluatedScripts = undefined;
Versioning.getView().actionscripts = form; // Keeps track of the scripts in the view
doScripts(form);
}

function setActionFiles(form) {
Versioning.getView().actionfiles = form;
doFiles();
}
let actionsFiles;

function doFiles() {
const files = Versioning.getView().actionfiles;
function setActionFiles(form) {
Versioning.getView().actionfiles = form;
doFiles();
}

actionsFiles = {};
function doFiles() {
const files = Versioning.getView().actionfiles;

if (!files) {
return;
}
actionsFiles = {};

for (let i = 0, l = files[0].groups.action[0].length; i < l; i++) {
actionsFiles[files[0].groups.action[0][i].name] =
actionsFiles[files[0].groups.action[0][i].name] || [];
actionsFiles[files[0].groups.action[0][i].name].push({
file: files[0].groups.action[0][i].file,
mode: files[0].groups.action[0][i].mode
});
}
if (!files) {
return;
}

function executeActionFile(file, value) {
switch (file.mode) {
case "amd": {
require([file.file], function(File) {
File(value);
});
break;
}
case "worker": {
const worker = new Worker(file.file);

worker.postMessage({ method: "actionValue", value: value });

worker.onmessage = function(event) {
if (!event.data.method) {
return;
}

switch (event.data.method) {
case "getVar": {
if (!Array.isArray(event.data.variables)) {
return;
}

const variables = {};
const varArray = event.data.variables;
for (let i = 0; i < varArray.length; i++) {
variables[varArray[i]] = API.getVar(
varArray[i]
);
}

worker.postMessage({
method: "getVar",
variables: variables
});
break;
}
case "setVar": {
if (!event.data.variables) {
return;
}

for (const i in event.data.variables) {
API.setVar(i, event.data.variables[i]);
}
break;
}
case "terminate": {
worker.terminate();
break;
}
case "sendAction": {
if (!event.data.actionName) {
return;
}

API.doAction(
event.data.actionName,
event.data.actionValue
);
break;
}
case "highlight": {
if (!event.data.highlightId) {
return;
}

API.highlightId(
event.data.highlightId,
event.data.highlightValue || false
);
break;
}
}
};

break;
}
}
for (let i = 0, l = files[0].groups.action[0].length; i < l; i++) {
actionsFiles[files[0].groups.action[0][i].name] =
actionsFiles[files[0].groups.action[0][i].name] || [];
actionsFiles[files[0].groups.action[0][i].name].push({
file: files[0].groups.action[0][i].file,
mode: files[0].groups.action[0][i].mode
});
}

return {
setScriptsFromForm(form) {
setActionScripts(form);
},

setFilesFromForm(form) {
setActionFiles(form);
},

viewHasChanged(view) {
setActionScripts(view.actionscripts);
doFiles();
},

getScriptsForm() {
return Versioning.getView().actionscripts || [];
},

getFilesForm() {
return Versioning.getView().actionfiles || [];
},

listActions() {
let actions = [];
if (actionsFiles) {
actions.push(...Object.keys(actionsFiles));
}

function executeActionFile(file, value) {
switch (file.mode) {
case 'amd': {
require([file.file], function (File) {
File(value);
});
break;
}
case 'worker': {
const worker = new Worker(file.file);

worker.postMessage({ method: 'actionValue', value: value });

worker.onmessage = function (event) {
if (!event.data.method) {
return;
}

switch (event.data.method) {
case 'getVar': {
if (!Array.isArray(event.data.variables)) {
return;
}

const variables = {};
const varArray = event.data.variables;
for (let i = 0; i < varArray.length; i++) {
variables[varArray[i]] = API.getVar(
varArray[i]
);
}

worker.postMessage({
method: 'getVar',
variables: variables
});
break;
}
if (evaluatedScripts) {
actions.push(...Object.keys(evaluatedScripts));
case 'setVar': {
if (!event.data.variables) {
return;
}

for (const i in event.data.variables) {
API.setVar(i, event.data.variables[i]);
}
break;
}
return actions;
},

execute(actionName, actionValue) {
if (evaluatedScripts && evaluatedScripts[actionName]) {
setImmediate(function() {
evaluatedScripts[actionName](actionValue);
});
case 'terminate': {
worker.terminate();
break;
}

if (actionsFiles && actionsFiles[actionName]) {
for (let i = 0; i < actionsFiles[actionName].length; i++) {
executeActionFile(actionsFiles[actionName][i], actionValue);
}
case 'sendAction': {
if (!event.data.actionName) {
return;
}

API.doAction(
event.data.actionName,
event.data.actionValue
);
break;
}
case 'highlight': {
if (!event.data.highlightId) {
return;
}

API.highlightId(
event.data.highlightId,
event.data.highlightValue || false
);
break;
}
},
}
};

setAPI(value) {
API = value;
break;
}
}
}

return {
setScriptsFromForm(form) {
setActionScripts(form);
},

setFilesFromForm(form) {
setActionFiles(form);
},

viewHasChanged(view) {
setActionScripts(view.actionscripts);
doFiles();
},

getScriptsForm() {
return Versioning.getView().actionscripts || [];
},

getFilesForm() {
return Versioning.getView().actionfiles || [];
},

listActions() {
let actions = [];
if (actionsFiles) {
actions.push(...Object.keys(actionsFiles));
}
if (evaluatedScripts) {
actions.push(...Object.keys(evaluatedScripts));
}
return actions;
},

execute(actionName, actionValue) {
if (evaluatedScripts && evaluatedScripts[actionName]) {
setImmediate(function () {
evaluatedScripts[actionName](actionValue);
});
}

if (actionsFiles && actionsFiles[actionName]) {
for (let i = 0; i < actionsFiles[actionName].length; i++) {
executeActionFile(actionsFiles[actionName][i], actionValue);
}
};
}
},

setAPI(value) {
API = value;
}
};
});

0 comments on commit 0369bae

Please sign in to comment.