@@ -337,14 +337,14 @@ impl SwitchNode {
337
337
let mut rules = Vec :: with_capacity ( raw_rules. len ( ) ) ;
338
338
for raw_rule in raw_rules. into_iter ( ) {
339
339
let ( vt, v) = match ( raw_rule. value_type , raw_rule. value ) {
340
+ ( Some ( SwitchPropertyType :: Prev ) , _) => ( SwitchPropertyType :: Prev , RedPropertyValue :: null ( ) ) ,
340
341
( None , Some ( raw_value) ) => {
341
342
if raw_value. is_number ( ) {
342
343
( SwitchPropertyType :: Num , RedPropertyValue :: Constant ( raw_value) )
343
344
} else {
344
345
( SwitchPropertyType :: Str , RedPropertyValue :: Constant ( raw_value) )
345
346
}
346
347
}
347
- ( Some ( SwitchPropertyType :: Prev ) , _) => ( SwitchPropertyType :: Prev , RedPropertyValue :: null ( ) ) ,
348
348
( Some ( raw_vt) , Some ( raw_value) ) => {
349
349
if raw_vt. is_constant ( ) {
350
350
let evaluated = RedPropertyValue :: evaluate_constant ( & raw_value, raw_vt. try_into ( ) ?) ?;
@@ -359,14 +359,14 @@ impl SwitchNode {
359
359
360
360
let ( v2t, v2) = if let Some ( raw_v2) = raw_rule. value2 {
361
361
match raw_rule. value2_type {
362
+ Some ( SwitchPropertyType :: Prev ) => ( Some ( SwitchPropertyType :: Prev ) , Some ( RedPropertyValue :: null ( ) ) ) ,
362
363
None => {
363
364
if raw_v2. is_number ( ) {
364
365
( Some ( SwitchPropertyType :: Num ) , Some ( RedPropertyValue :: Constant ( raw_v2) ) )
365
366
} else {
366
367
( Some ( SwitchPropertyType :: Str ) , Some ( RedPropertyValue :: Constant ( raw_v2) ) )
367
368
}
368
369
}
369
- Some ( SwitchPropertyType :: Prev ) => ( Some ( SwitchPropertyType :: Prev ) , None ) ,
370
370
Some ( raw_v2t) => {
371
371
if raw_v2t. is_constant ( ) {
372
372
let evaluated = RedPropertyValue :: evaluate_constant ( & raw_v2, raw_v2t. try_into ( ) ?) ?;
@@ -377,7 +377,7 @@ impl SwitchNode {
377
377
}
378
378
}
379
379
} else {
380
- ( raw_rule. value2_type , None )
380
+ ( raw_rule. value2_type , Some ( RedPropertyValue :: null ( ) ) )
381
381
} ;
382
382
383
383
let v = match v {
@@ -403,19 +403,40 @@ impl SwitchNode {
403
403
404
404
async fn dispatch_msg ( & self , orig_msg : & MsgHandle , cancel : CancellationToken ) -> crate :: Result < ( ) > {
405
405
let mut envelopes: SmallVec < [ Envelope ; 4 ] > = SmallVec :: new ( ) ;
406
- {
407
- let msg = orig_msg . read ( ) . await ;
408
- let from_value = self . eval_property_value ( & msg ) . await ? ;
409
- for ( port, rule) in self . config . rules . iter ( ) . enumerate ( ) {
410
- let v1 = self . get_v1 ( rule , & msg ) . await ? ;
411
- let v2 = if rule . value2 . is_some ( ) { self . get_v2 ( rule , & msg ) . await ? } else { Variant :: Null } ;
412
- if rule . operator . apply ( & from_value , & v1 , & v2 , rule . case , & [ ] ) ? {
406
+ let msg = orig_msg . read ( ) . await ;
407
+ let from_value = self . eval_property_value ( & msg ) . await ? ;
408
+ let last_property_value = from_value . clone ( ) ;
409
+ for ( port, rule) in self . config . rules . iter ( ) . enumerate ( ) {
410
+ if rule . value_type == SwitchPropertyType :: Prev {
411
+ let prev_guard = self . prev_value . read ( ) . await ;
412
+ if prev_guard . is_null ( ) {
413
413
envelopes. push ( Envelope { port, msg : orig_msg. clone ( ) } ) ;
414
414
if !self . config . check_all {
415
415
break ;
416
416
}
417
+ continue ;
418
+ }
419
+ }
420
+ if rule. value2_type == Some ( SwitchPropertyType :: Prev ) {
421
+ let prev_guard = self . prev_value . read ( ) . await ;
422
+ if prev_guard. is_null ( ) {
423
+ continue ;
417
424
}
418
425
}
426
+ let v1 = self . get_v1 ( rule, & msg) . await ?;
427
+ let v2 = self . get_v2 ( rule, & msg) . await ?;
428
+ if rule. operator . apply ( & from_value, & v1, & v2, rule. case , & [ ] ) ? {
429
+ envelopes. push ( Envelope { port, msg : orig_msg. clone ( ) } ) ;
430
+ }
431
+
432
+ if !self . config . check_all {
433
+ break ;
434
+ }
435
+ }
436
+ // Update prev_value
437
+ if !last_property_value. is_null ( ) {
438
+ let mut prev = self . prev_value . write ( ) . await ;
439
+ * prev = last_property_value;
419
440
}
420
441
if !envelopes. is_empty ( ) {
421
442
self . fan_out_many ( envelopes, cancel) . await ?;
@@ -463,6 +484,7 @@ impl SwitchNode {
463
484
)
464
485
. await
465
486
}
487
+ ( None , _) => Ok ( Variant :: Null ) ,
466
488
_ => Err ( EdgelinkError :: BadArgument ( "rule" ) . into ( ) ) ,
467
489
}
468
490
}
0 commit comments