Skip to content

Commit 921f9a1

Browse files
takaokoujiGemini
andcommitted
fix: update GraphQL operations for unified subscription and filtering
- Added groupId and domain to selection sets of mutations and subscription - Updated MeshV2Service to use a single subscription (onMessageInGroup) - Optimized event queue by removing unnecessary waitForCompletion (now guaranteed by single stream) Fixes: scratchfoundation#500 🤖 Generated with [Gemini Code](https://gemini.google.com/code) Co-Authored-By: Gemini <noreply@google.com>
1 parent cd28816 commit 921f9a1

File tree

3 files changed

+276
-77
lines changed

3 files changed

+276
-77
lines changed

src/extensions/scratch3_mesh_v2/gql-operations.js

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ const DISSOLVE_GROUP = gql`
6464
dissolveGroup(groupId: $groupId, domain: $domain, hostId: $hostId) {
6565
groupId
6666
domain
67-
message
67+
groupDissolve {
68+
groupId
69+
domain
70+
message
71+
}
6872
}
6973
}
7074
`;
@@ -95,77 +99,79 @@ const SEND_MEMBER_HEARTBEAT = gql`
9599
const REPORT_DATA = gql`
96100
mutation ReportDataByNode($groupId: ID!, $domain: String!, $nodeId: ID!, $data: [SensorDataInput!]!) {
97101
reportDataByNode(groupId: $groupId, domain: $domain, nodeId: $nodeId, data: $data) {
98-
nodeId
99102
groupId
100103
domain
101-
data {
102-
key
103-
value
104+
nodeStatus {
105+
nodeId
106+
groupId
107+
domain
108+
data {
109+
key
110+
value
111+
}
112+
timestamp
104113
}
105-
timestamp
106114
}
107115
}
108116
`;
109117

110118
const FIRE_EVENTS = gql`
111119
mutation FireEventsByNode($groupId: ID!, $domain: String!, $nodeId: ID!, $events: [EventInput!]!) {
112120
fireEventsByNode(groupId: $groupId, domain: $domain, nodeId: $nodeId, events: $events) {
113-
events {
114-
name
121+
groupId
122+
domain
123+
batchEvent {
124+
events {
125+
name
126+
firedByNodeId
127+
groupId
128+
domain
129+
payload
130+
timestamp
131+
}
115132
firedByNodeId
116133
groupId
117134
domain
118-
payload
119135
timestamp
120136
}
121-
firedByNodeId
122-
groupId
123-
domain
124-
timestamp
125137
}
126138
}
127139
`;
128140

129-
const ON_DATA_UPDATE = gql`
130-
subscription OnDataUpdateInGroup($groupId: ID!, $domain: String!) {
131-
onDataUpdateInGroup(groupId: $groupId, domain: $domain) {
132-
nodeId
141+
const ON_MESSAGE_IN_GROUP = gql`
142+
subscription OnMessageInGroup($groupId: ID!, $domain: String!) {
143+
onMessageInGroup(groupId: $groupId, domain: $domain) {
133144
groupId
134145
domain
135-
data {
136-
key
137-
value
146+
nodeStatus {
147+
nodeId
148+
groupId
149+
domain
150+
data {
151+
key
152+
value
153+
}
154+
timestamp
138155
}
139-
timestamp
140-
}
141-
}
142-
`;
143-
144-
const ON_BATCH_EVENT = gql`
145-
subscription OnBatchEventInGroup($groupId: ID!, $domain: String!) {
146-
onBatchEventInGroup(groupId: $groupId, domain: $domain) {
147-
events {
148-
name
156+
batchEvent {
157+
events {
158+
name
159+
firedByNodeId
160+
groupId
161+
domain
162+
payload
163+
timestamp
164+
}
149165
firedByNodeId
150166
groupId
151167
domain
152-
payload
153168
timestamp
154169
}
155-
firedByNodeId
156-
groupId
157-
domain
158-
timestamp
159-
}
160-
}
161-
`;
162-
163-
const ON_GROUP_DISSOLVE = gql`
164-
subscription OnGroupDissolve($groupId: ID!, $domain: String!) {
165-
onGroupDissolve(groupId: $groupId, domain: $domain) {
166-
groupId
167-
domain
168-
message
170+
groupDissolve {
171+
groupId
172+
domain
173+
message
174+
}
169175
}
170176
}
171177
`;
@@ -196,8 +202,6 @@ module.exports = {
196202
SEND_MEMBER_HEARTBEAT,
197203
REPORT_DATA,
198204
FIRE_EVENTS,
199-
ON_DATA_UPDATE,
200-
ON_BATCH_EVENT,
201-
ON_GROUP_DISSOLVE,
205+
ON_MESSAGE_IN_GROUP,
202206
LIST_GROUP_STATUSES
203207
};

src/extensions/scratch3_mesh_v2/mesh-service.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ const {
1414
SEND_MEMBER_HEARTBEAT,
1515
REPORT_DATA,
1616
FIRE_EVENTS,
17-
ON_DATA_UPDATE,
18-
ON_BATCH_EVENT,
19-
ON_GROUP_DISSOLVE,
17+
ON_MESSAGE_IN_GROUP,
2018
LIST_GROUP_STATUSES
2119
} = require('./gql-operations');
2220

@@ -399,35 +397,32 @@ class MeshV2Service {
399397
domain: this.domain
400398
};
401399

402-
const dataSub = this.client.subscribe({
403-
query: ON_DATA_UPDATE,
400+
const messageSub = this.client.subscribe({
401+
query: ON_MESSAGE_IN_GROUP,
404402
variables
405403
}).subscribe({
406-
next: result => this.handleDataUpdate(result.data.onDataUpdateInGroup),
407-
error: err => log.error(`Mesh V2: Data subscription error: ${err}`)
408-
});
409-
410-
const batchEventSub = this.client.subscribe({
411-
query: ON_BATCH_EVENT,
412-
variables
413-
}).subscribe({
414-
next: result => this.handleBatchEvent(result.data.onBatchEventInGroup),
415-
error: err => log.error(`Mesh V2: Batch event subscription error: ${err}`)
416-
});
417-
418-
const dissolveSub = this.client.subscribe({
419-
query: ON_GROUP_DISSOLVE,
420-
variables
421-
}).subscribe({
422-
next: () => {
423-
this.costTracking.dissolveReceived++;
424-
log.info('Mesh V2: Group dissolved by host');
425-
this.cleanupAndDisconnect();
404+
next: result => {
405+
const message = result.data.onMessageInGroup;
406+
if (!message) return;
407+
408+
// MeshMessage has three fields: nodeStatus, batchEvent, groupDissolve
409+
// Only one field will be non-null per message
410+
if (message.nodeStatus) {
411+
this.handleDataUpdate(message.nodeStatus);
412+
} else if (message.batchEvent) {
413+
this.handleBatchEvent(message.batchEvent);
414+
} else if (message.groupDissolve) {
415+
this.costTracking.dissolveReceived++;
416+
log.info('Mesh V2: Group dissolved by host');
417+
this.cleanupAndDisconnect();
418+
} else {
419+
log.warn('Mesh V2: Received message with all fields null');
420+
}
426421
},
427-
error: err => log.error(`Mesh V2: Dissolve subscription error: ${err}`)
422+
error: err => log.error(`Mesh V2: Subscription error: ${err}`)
428423
});
429424

430-
this.subscriptions.push(dataSub, batchEventSub, dissolveSub);
425+
this.subscriptions.push(messageSub);
431426
}
432427

433428
stopSubscriptions () {
@@ -622,8 +617,8 @@ class MeshV2Service {
622617
if (!this.groupId || !this.client || events.length === 0) return;
623618

624619
try {
625-
// データ送信完了を待つ
626-
await this.dataRateLimiter.waitForCompletion();
620+
// データ送信完了を待つ (Removed to prevent blocking, relying on unified subscription for order)
621+
// await this.dataRateLimiter.waitForCompletion();
627622

628623
this.costTracking.mutationCount++;
629624
this.costTracking.fireEventsCount++;

0 commit comments

Comments
 (0)