From ca576af89da2c128e5412e29f02f2b15af88ed78 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 3 Oct 2019 22:05:27 +0200 Subject: [PATCH] Session Running and Stopping from menu and at task server --- FloatingSpace/CircularMenuItem.js | 11 +++++++- FloatingSpace/StrategyPart.js | 28 +++++++++++++++---- FloatingSpace/StrategyPartConstructor.js | 2 +- .../FunctionLibraries/SessionFunctions.js | 6 ++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/FloatingSpace/CircularMenuItem.js b/FloatingSpace/CircularMenuItem.js index 9fb252e8b..38bd97bd6 100644 --- a/FloatingSpace/CircularMenuItem.js +++ b/FloatingSpace/CircularMenuItem.js @@ -259,7 +259,16 @@ function newCircularMenuItem () { return } - function onPrimaryCallBack (err) { + function onPrimaryCallBack (err, event) { + /* While the primary action is being executed some event might have happen. Following we process the ones we can */ + + if (event !== undefined) { + if (event.type === 'Secondary Action Already Executed') { + setStatus(thisObject.secondaryWorkDoneLabel, UI_COLOR.PATINATED_TURQUOISE, 250, STATUS_SECONDARY_WORK_DONE) + return + } + } + /* If there is a secondary action we will act different that if there is not */ if (thisObject.secondaryAction === undefined) { // This means there are no more possible actions. if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { diff --git a/FloatingSpace/StrategyPart.js b/FloatingSpace/StrategyPart.js index ebe1c6bc0..3b6bebdaf 100644 --- a/FloatingSpace/StrategyPart.js +++ b/FloatingSpace/StrategyPart.js @@ -595,23 +595,39 @@ function newStrategyPart () { /* We will wait to the event that the execution was terminated in order to call back the menu item */ let key = thisObject.payload.node.name + '-' + thisObject.payload.node.type + '-' + thisObject.payload.node.id - systemEventHandler.listenToEvent(key, 'Running', undefined, key, undefined, onExecutionTerminated) + systemEventHandler.listenToEvent(key, 'Running', undefined, key, undefined, onRunning) - function onExecutionTerminated () { + function onRunning () { if (callBackFunction !== undefined) { callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE) } } + + /* + While it is running, it can happen that it naturally stops or is stopped not from the UI but from other means. + In those cases, the stop function would never be called (from the UI). So what we will do is to call it from + here with and event, and passing our own callBackFunction. In case there is an external source stopping this, + this will produce an execution of the callback with our event, which will produce that the menu item is restored + to its default stage. + + If on the other side, it is executed from the UI, then we will be processing the Stopped event twice, which in + both cases will reset the menu item to its default state. + */ + + let event = { + type: 'Secondary Action Already Executed' + } + stop(callBackFunction, event) } - function stop (callBackFunction) { + function stop (callBackFunction, event) { /* We will wait to the event that the execution was terminated in order to call back the menu item */ let key = thisObject.payload.node.name + '-' + thisObject.payload.node.type + '-' + thisObject.payload.node.id - let eventSubscriptionId = systemEventHandler.listenToEvent(key, 'Stopped', undefined, key, undefined, onExecutionTerminated) + systemEventHandler.listenToEvent(key, 'Stopped', undefined, key, undefined, onStopped) - function onExecutionTerminated () { + function onStopped () { if (callBackFunction !== undefined) { - callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE) + callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, event) } if (thisObject.circularProgressBar !== undefined) { diff --git a/FloatingSpace/StrategyPartConstructor.js b/FloatingSpace/StrategyPartConstructor.js index 620d19777..722c3c8ba 100644 --- a/FloatingSpace/StrategyPartConstructor.js +++ b/FloatingSpace/StrategyPartConstructor.js @@ -890,7 +890,7 @@ function newStrategyPartConstructor () { action: 'Run Session', actionFunction: payload.onMenuItemClick, label: 'Run', - workingLabel: 'Stop', + workingLabel: 'Runing...', workDoneLabel: 'Session Running', workFailedLabel: 'Session Cannot be Run', secondaryAction: 'Stop Session', diff --git a/StrategySpace/Workspace/FunctionLibraries/SessionFunctions.js b/StrategySpace/Workspace/FunctionLibraries/SessionFunctions.js index 598f8ebe0..90a380e9f 100644 --- a/StrategySpace/Workspace/FunctionLibraries/SessionFunctions.js +++ b/StrategySpace/Workspace/FunctionLibraries/SessionFunctions.js @@ -9,12 +9,14 @@ function newSessionFunctions () { function runSession (node, functionLibraryProtocolNode, callBackFunction) { node.payload.uiObject.run(callBackFunction) + let key = node.name + '-' + node.type + '-' + node.id + + /* Raise event to run the session */ let event = { session: JSON.stringify(functionLibraryProtocolNode.getProtocolNode(node, false, false, true)), definition: getDefinition() } - let key = 'Session' + '-' + node.id + '-' + 'Events' systemEventHandler.raiseEvent(key, 'Run Session', event) function getDefinition () { @@ -25,7 +27,7 @@ function newSessionFunctions () { } function stopSession (node, functionLibraryProtocolNode, callBackFunction) { - let key = 'Session' + '-' + node.id + '-' + 'Events' + let key = node.name + '-' + node.type + '-' + node.id systemEventHandler.raiseEvent(key, 'Stop Session') node.payload.uiObject.stop(callBackFunction)