Skip to content

Commit 5eac92f

Browse files
committed
Use a channel to make other tabs automatically reload their queries
1 parent 1be8474 commit 5eac92f

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.3.1] - 2026-02-28
2+
3+
- Use a channel to make other tabs automatically reload their queries
4+
15
## [1.3.0] - 2026-02-26
26

37
- Add auto-dirty queries feature

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-typescript-remote-command-generator (1.3.0)
4+
foobara-typescript-remote-command-generator (1.3.1)
55
foobara (>= 0.1.16, < 2.0.0)
66
foobara-files-generator (< 2.0.0)
77

88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
addressable (2.8.8)
11+
addressable (2.8.9)
1212
public_suffix (>= 2.0.2, < 8.0)
1313
ast (2.4.3)
1414
base64 (0.3.0)
@@ -106,7 +106,7 @@ GEM
106106
rspec-its (2.0.0)
107107
rspec-core (>= 3.13.0)
108108
rspec-expectations (>= 3.13.0)
109-
rspec-mocks (3.13.7)
109+
rspec-mocks (3.13.8)
110110
diff-lcs (>= 1.2.0, < 2.0)
111111
rspec-support (~> 3.13.0)
112112
rspec-support (3.13.7)
@@ -131,7 +131,7 @@ GEM
131131
rubocop-rspec (3.9.0)
132132
lint_roller (~> 1.1)
133133
rubocop (~> 1.81)
134-
ruby-prof (2.0.2)
134+
ruby-prof (2.0.3)
135135
base64
136136
ostruct
137137
ruby-progressbar (1.13.0)

templates/base/QueryCache.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4189
function toKey<CommandT extends RemoteCommand<any, any, any>> (
4290
CommandClass: RemoteCommandConstructor<CommandT>,
4391
inputs: InputsOf<CommandT> | undefined

templates/base/RemoteCommand.ts.erb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Outcome, SuccessfulOutcome, ErrorOutcome } from './Outcome'
22
import { type FoobaraError } from './Error'
33
<% if auto_dirty_queries? %>
4-
import * as QueryCache from './QueryCache'
4+
import { dirtyQuery } from './QueryCache'
55
<% end %>
66

77
export default abstract class RemoteCommand<Inputs, Result, CommandError extends FoobaraError> {
@@ -136,19 +136,13 @@ export default abstract class RemoteCommand<Inputs, Result, CommandError extends
136136
<% if auto_dirty_queries? %>
137137
dirtyQueries () {
138138
for (const [commandClass, inputs] of this.dirties()) {
139-
QueryCache.forEachQuery((query) => {
140-
// debugger
141-
if (query.CommandClass === commandClass) {
142-
if (inputs == null || Object.keys(inputs).length === 0) {
143-
query.setDirty()
144-
} else {
145-
for (const [property, value] of Object.entries(inputs)) {
146-
if (query.inputs[property] !== value) { return }
147-
}
148-
query.setDirty()
149-
}
139+
if (inputs != null) {
140+
for (const [propertyName, value] of Object.entries(inputs)) {
141+
dirtyQuery(commandClass, propertyName, value)
150142
}
151-
})
143+
} else {
144+
dirtyQuery(commandClass)
145+
}
152146
}
153147
}
154148
<% end %>

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Foobara
22
module TypescriptRemoteCommandGenerator
33
module Version
4-
VERSION = "1.3.0".freeze
4+
VERSION = "1.3.1".freeze
55
MINIMUM_RUBY_VERSION = ">= 3.4.0".freeze
66
end
77
end

0 commit comments

Comments
 (0)