Skip to content
Snippets Groups Projects
Commit 1ae4a51a authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

#187 - Find pad id by user provided name, receive pad name updates, cache pads...

#187 - Find pad id by user provided name, receive pad name updates, cache pads by name in project model for fast access
parent 0485b2f7
No related branches found
No related tags found
No related merge requests found
...@@ -4,17 +4,19 @@ module.exports = function (self) { ...@@ -4,17 +4,19 @@ module.exports = function (self) {
name: 'Trigger Pad', name: 'Trigger Pad',
options: [ options: [
{ {
id: 'num', id: 'name',
type: 'number', type: 'textinput',
label: 'Test', label: 'Pad Name',
default: 5, default: '',
min: 0,
max: 100,
}, },
], ],
callback: async (event) => { callback: async (event) => {
const pad = self.currentProject.findPadByName(event.options.name);
if (!pad) {
self.log('warning', 'Cannot find a pad with name ' + event.options.name);
}
const payload = { const payload = {
'pad': '8b7ef494-d735-45f9-ab84-c95e299298a6' 'pad': pad.id
}; };
self.sendToWebSocket('cart-action', payload) self.sendToWebSocket('cart-action', payload)
}, },
......
...@@ -5,6 +5,8 @@ const UpdateFeedbacks = require('./feedbacks') ...@@ -5,6 +5,8 @@ const UpdateFeedbacks = require('./feedbacks')
const UpdateVariableDefinitions = require('./variables') const UpdateVariableDefinitions = require('./variables')
const WebSocket = require('ws') const WebSocket = require('ws')
const ProjectUpdate = require("./receive/project_update"); const ProjectUpdate = require("./receive/project_update");
const Project = require("./project");
const PadNameUpdate = require("./receive/pad_name_update");
class ModuleInstance extends InstanceBase { class ModuleInstance extends InstanceBase {
isInitialized = false isInitialized = false
...@@ -12,10 +14,11 @@ class ModuleInstance extends InstanceBase { ...@@ -12,10 +14,11 @@ class ModuleInstance extends InstanceBase {
wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$' wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
messageHandlers = { messageHandlers = {
'project-current': new ProjectUpdate() 'project-current': new ProjectUpdate(),
'pad-name-changed': new PadNameUpdate()
}; };
currentProject = {} currentProject = new Project({});
constructor(internal) { constructor(internal) {
super(internal) super(internal)
...@@ -152,8 +155,14 @@ class ModuleInstance extends InstanceBase { ...@@ -152,8 +155,14 @@ class ModuleInstance extends InstanceBase {
} else { } else {
this.log('debug', `Cannot handle incoming message of type ${message.type}`) this.log('debug', `Cannot handle incoming message of type ${message.type}`)
} }
} else if (message.updateType != null) {
if (this.messageHandlers[message.updateType] != null) {
this.messageHandlers[message.updateType].handleMessage(this, message)
} else { } else {
this.log('debug', `Cannot handle incoming message ${message}`) this.log('debug', `Cannot handle incoming message of updateType ${message.updateType}`)
}
} else {
this.log('debug', `Cannot handle incoming message ${data}`)
} }
} }
} }
......
module.exports = class Project {
constructor(data) {
this.data = data
this.padCache = {}
// Prefill with current project
if (data) {
console.log('Create pad cache')
for (const pageId in data.pages) {
const page = data.pages[pageId]
for (const padId in page.pads) {
const pad = page.pads[padId];
if (pad.name !== "") {
this.padCache[pad.name] = pad;
console.log(`Add pad "${pad.name}" to cache`)
}
}
}
}
}
/**
*
* @param {string} oldName
* @param {string} newName
*/
updatePadName(oldName, newName) {
this.padCache[newName] = this.padCache[oldName];
delete this.padCache[oldName];
}
findPadByName(name) {
return this.padCache[name];
}
}
\ No newline at end of file
module.exports = class MessageExecutable { module.exports = class MessageExecutable {
/**
*
* @param {ModuleInstance} plugin
* @param {Object} message
*/
handleMessage(plugin, message) { handleMessage(plugin, message) {
// Abstract method // Abstract method
} }
......
const MessageExecutable = require("./message_executable");
module.exports = class PadNameUpdate extends MessageExecutable {
handleMessage(plugin, message) {
plugin.currentProject.updatePadName(message.oldValue, message.newValue);
}
}
\ No newline at end of file
const MessageExecutable = require("./message_executable"); const MessageExecutable = require("./message_executable");
const Project = require("../project");
module.exports = class ProjectUpdate extends MessageExecutable { module.exports = class ProjectUpdate extends MessageExecutable {
handleMessage(plugin, message) { handleMessage(plugin, message) {
plugin.currentProject = message.payload plugin.currentProject = new Project(message)
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment