Load the definition of the state machine from database #1517
-
I am trying to store the definition of the state machine in the couchdb-pouchdb database in an application with the svelte-sapper-webpack framework. But it throws an error when I try to instantiate it: But it throws me the following error: TypeError: Cannot use 'in' operator to search for 'parallel' in {
id: 'lamps',
type: 'parallel',
context: {
message: "",
svg: [],
},
states: {
lamp1: {
initial: 'off',
states: {
off: {
on: {
LAMP1: {
target: "on",
actions: "lampara1on",
}
},
},
switched on: {
on: {
LAMP1: {
target: "off",
actions: "lampara1off",
}
},
},
},
},
lamp2: {
initial: 'off',
states: {
off: {
on: {
LAMP2: {
target: "on",
actions: "lampara2on",
}
},
},
switched on: {
on: {
LAMP2: {
target: "off",
actions: "lampara2off",
}
},
},
},
},
},
},
{
actions: {
lamp1on: assign ({
message: (context, event) => event.data.control.message,
svg: [
{tag: "display", elm: "lampara1off", val: "none"},
{tag: "display", elm: "lampara1on", val: "inline"}],
}),
lamp1off: assign ({
message: (context, event) => event.data.control.message,
svg: [
{tag: "display", elm: "lampara1on", val: "none"},
{tag: "display", elm: "lampara1off", val: "inline"}],
}),
lamp2on: assign ({
message: (context, event) => event.data.control.message,
svg: [
{tag: "display", elm: "lampara2off", val: "none"},
{tag: "display", elm: "lampara2on", val: "inline"}],
}),
lamp2off: assign ({
message: (context, event) => event.data.control.message,
svg: [
{tag: "display", elm: "lampara2on", val: "none"},
{tag: "display", elm: "lampara2off", val: "inline"}],
})
}
}
How do I correct the error? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 5 replies
-
Can you please reformat your code? Use fenced code blocks: ```js // like this ``` |
Beta Was this translation helpful? Give feedback.
-
The definition of the State Machine is stored as an attachment in the database. |
Beta Was this translation helpful? Give feedback.
-
A parallel state cannot be the root state node of the machine. Try refactoring it so that it's nested in a compound state node instead: {
states: {
something: {
type: 'parallel',
states: { ... }
}
}
} |
Beta Was this translation helpful? Give feedback.
-
This is the test and it didn't work: {
id: 'lamps',
context: {
message: "",
svg: [],
},
states: {
lamps: {
type: 'parallel',
states: {
lamp1: {
initial: 'off',
states: {
off: {
on: {
LAMP1: {
target: "on",
actions: "lampara1on",
}
},
},
switched on: {
on: {
LAMP1: {
target: "off",
actions: "lampara1off",
}
},
},
},
},
lamp2: {
initial: 'off',
states: {
off: {
on: {
LAMP2: {
target: "on",
actions: "lampara2on",
}
},
},
switched on: {
on: {
LAMP2: {
target: "off",
actions: "lampara2off",
}
},
},
},
},
},
},
},
} Cannot retrieve initial state from simple state 'lamps'. I take the structure of the example of the guide on Parallel State Nodes and it is exactly the same as the one I showed initially, this is not a compound state node. |
Beta Was this translation helpful? Give feedback.
-
Answer here: #1517 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
When I import the definition of the state machine (import {lampsMachine} from "../machines/lamparasMachine"), it works without problems, but when I extract it from the database it throws me errors. example: {
id: 'lamps',
context: {
message: "",
svg: []
},
initial: "lamps",
states: {
lamps: {
type: "parallel",
states: {
lamp1: {
initial: 'off',
states: {
off: {
on: {
LAMP1: {
target: "on",
actions: "lampara1on",
}
},
},
switched on: {
on: {
LAMP1: {
target: "off",
actions: "lampara1off",
}
},
},
},
},
lamp2: {
initial: 'off',
states: {
off: {
on: {
LAMP2: {
target: "on",
actions: "lampara2on",
}
},
},
switched on: {
on: {
LAMP2: {
target: "off",
actions: "lampara2off",
}
},
},
},
},
},
},
},
}
code in svelte / sapper: async function getStateData (xId, xMaq) { I need help so that the state machine is available and can be modified from the database and used dynamically. |
Beta Was this translation helpful? Give feedback.
-
Has anyone managed to load the definition of the state machine from database? I have tried with the examples in the docs and none works. The definition can be loaded from a database or just by importing. This project has state machines as the means of programming IoT. Authorized clients have access to the creation of new state machines or to modify the existing one and incorporate them, for the control of devices, sensors and actuators. example: { id: 'toggle', initial: 'inactive', states: { inactive: { on: { TOGGLE: 'active' } }, active: { on: { TOGGLE: 'inactive' } } } }
|
Beta Was this translation helpful? Give feedback.
-
If you are right David, this is not a problem with XState, it was my fault, JSON.parse (someStringMachineConfig) was not needed, because the database already provides you with the object when you do result = await db.rel.find (" machine ", id). You just do createMachine (result.machineConfig) and voila. {
actions: {
lampara1on: assign({
mensaje: (context, event) => event.data.control.mensaje,
svg: [
{ tag: "display", elm: "lampara1off", val: "none" },
{ tag: "display", elm: "lampara1on", val: "inline" }],
}),
lampara1off: assign({
mensaje: (context, event) => event.data.control.mensaje,
svg: [
{ tag: "display", elm: "lampara1on", val: "none" },
{ tag: "display", elm: "lampara1off", val: "inline" }],
}),
lampara2on: assign({
mensaje: (context, event) => event.data.control.mensaje,
svg: [
{ tag: "display", elm: "lampara2off", val: "none" },
{ tag: "display", elm: "lampara2on", val: "inline" }],
}),
lampara2off: assign({
mensaje: (context, event) => event.data.control.mensaje,
svg: [
{ tag: "display", elm: "lampara2on", val: "none" },
{ tag: "display", elm: "lampara2off", val: "inline" }],
})
}
} Thanking you in advance for your great support. |
Beta Was this translation helpful? Give feedback.
-
How to properly 'should instead be part of the code' with user access to add, modify or delete defining/loading of state machines? Svelte shifts that work into a compile step that happens when you build your app. If the code is not in the resulting bundle, it does not exist. Someone could give me the key to get out of this crossroads. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
-
Okay. To avoid evils, const xMachine = createMachine(machine, sanitize(validate(configure(config)))); Thanks for your support David. |
Beta Was this translation helpful? Give feedback.
-
Hi Daab, Regards, |
Beta Was this translation helpful? Give feedback.
Answer here: #1517 (reply in thread)