@@ -10,7 +10,6 @@ export function MqttVariableMessenger() {
10
10
const { status, publish, subscribe, uniqueId } = useMqtt ( ) ;
11
11
const publishedVariableValues = useRef < Map < string , any | undefined > > ( new Map ( ) ) ;
12
12
const knownVariables = useRef < Record < string , KnownVariable > > ( { } ) ; // <id, name>
13
- const lastReceveivedVariables = useRef < Map < string , number > > ( new Map ( ) ) ;
14
13
15
14
async function publishVariables ( variables ?: Variable [ ] ) {
16
15
const newVariables =
@@ -52,7 +51,7 @@ export function MqttVariableMessenger() {
52
51
useEffect ( ( ) => {
53
52
if ( status !== 'connected' ) return ;
54
53
55
- subscribe ( `microflow/v1/${ uniqueId } /+/variables/request` , topic => {
54
+ const req = subscribe ( `microflow/v1/${ uniqueId } /+/variables/request` , topic => {
56
55
const app = topic . split ( '/' ) [ 3 ] ;
57
56
publish (
58
57
`microflow/v1/${ uniqueId } /${ app } /variables/response` ,
@@ -61,18 +60,29 @@ export function MqttVariableMessenger() {
61
60
publishedVariableValues . current . forEach ( ( value , id ) => {
62
61
publish ( `microflow/v1/${ uniqueId } /${ app } /variable/${ id } ` , value ) ;
63
62
} ) ;
64
- } ) ;
63
+ } ) . catch ( console . error ) ;
65
64
66
- subscribe ( `microflow/v1/${ uniqueId } /+/variable/+/set` , async ( topic , message ) => {
65
+ const set = subscribe ( `microflow/v1/${ uniqueId } /+/variable/+/set` , async ( topic , message ) => {
67
66
const [ , , , app , , variableId ] = topic . split ( '/' ) ;
68
- const value = JSON . parse ( message . toString ( ) ) ;
67
+
68
+ let value = null ;
69
+ try {
70
+ value = JSON . parse ( message . toString ( ) ) ;
71
+ } catch ( e ) {
72
+ value = message . toString ( ) ;
73
+ }
69
74
70
75
console . debug ( '[SET] <<<' , value ) ;
71
76
72
77
// Make sure we don't send the same value back to the app
73
78
publishedVariableValues . current . set ( variableId , JSON . stringify ( value ) ) ;
74
79
sendMessageToFigma ( SetLocalValiable ( variableId , value as VariableValue ) ) ;
75
- } ) ;
80
+ } ) . catch ( console . error ) ;
81
+
82
+ return ( ) => {
83
+ req . then ( unsub => unsub ?.( ) ) . catch ( console . error ) ;
84
+ set . then ( unsub => unsub ?.( ) ) . catch ( console . error ) ;
85
+ } ;
76
86
} , [ status , subscribe , publish , uniqueId ] ) ;
77
87
78
88
useMessageListener < Variable [ ] | undefined > ( MESSAGE_TYPE . GET_LOCAL_VARIABLES , publishVariables , {
0 commit comments