@@ -38,6 +38,54 @@ export function forEachQuery (callback: (query: Query<RemoteCommand<any, any, an
3838 queryCache . forEach ( callback )
3939}
4040
41+ interface DirtyQueryEvent {
42+ commandName : string
43+ propertyName : string | undefined
44+ value : string | number | undefined
45+ }
46+
47+ let dirtyQueryChannel : BroadcastChannel | null = null
48+
49+ if ( typeof BroadcastChannel !== 'undefined' ) {
50+ dirtyQueryChannel = new BroadcastChannel ( 'dirty-query' )
51+
52+ dirtyQueryChannel . addEventListener ( 'message' , ( event : MessageEvent < DirtyQueryEvent > ) => {
53+ const { commandName, propertyName, value } = event . data
54+
55+ dirtyQuery ( commandName , propertyName , value , { skipBroadcast : true } )
56+ } )
57+ }
58+
59+ export function dirtyQuery < CommandT extends RemoteCommand < any , any , any > > (
60+ commandClass : RemoteCommandConstructor < CommandT > | string ,
61+ propertyName : string | undefined = undefined ,
62+ propertyValue : string | number | undefined = undefined ,
63+ options : { skipBroadcast : boolean } = { skipBroadcast : false } ) {
64+ if ( typeof commandClass !== 'string' ) {
65+ commandClass = commandClass . fullCommandName
66+ }
67+
68+ forEachQuery ( ( query ) => {
69+ if ( query . CommandClass . fullCommandName === commandClass ) {
70+ if ( query . inputs == null || Object . keys ( query . inputs ) . length === 0 ) {
71+ query . setDirty ( )
72+ } else {
73+ if ( propertyName != null ) {
74+ if ( query . inputs [ propertyName ] !== propertyValue ) {
75+ return
76+ }
77+ }
78+
79+ query . setDirty ( )
80+ }
81+ }
82+ } )
83+
84+ if ( dirtyQueryChannel != null && ! options . skipBroadcast ) {
85+ dirtyQueryChannel . postMessage ( { commandName : commandClass , propertyName, value : propertyValue } )
86+ }
87+ }
88+
4189function toKey < CommandT extends RemoteCommand < any , any , any > > (
4290 CommandClass : RemoteCommandConstructor < CommandT > ,
4391 inputs : InputsOf < CommandT > | undefined
0 commit comments