mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15:33:12 +09:00
Merge pull request #958 from bbassie/feature/subscription-event-address
Stop addressing subscription events to a request id
This commit is contained in:
4 files changed
+120
-9
No files matched your search
@@ -114,6 +114,24 @@ export class Address {
|
||||
return `${this.type}:${this.id}` as const;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this address with any request id removed
|
||||
*
|
||||
* A request id identifies one request in flight and is set on the
|
||||
* source address of a request so that its response can be matched back
|
||||
* up with it. It is meaningless on anything else, so an address taken
|
||||
* from a request has to be stripped of it before it is used as the
|
||||
* destination of an event.
|
||||
*
|
||||
* @returns this address without a request id.
|
||||
*/
|
||||
withoutRequestId() {
|
||||
if (this.requestId === undefined) {
|
||||
return this;
|
||||
}
|
||||
return new Address(this.type, this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this address targets the given destination
|
||||
* @param dst - Destination to check
|
||||
|
||||
@@ -238,7 +238,7 @@ export class SubscriptionRequest {
|
||||
|
||||
type Subscriber = {
|
||||
link: Link,
|
||||
dst: Address,
|
||||
address: Address,
|
||||
filters: SubscriptionFilters,
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ export class SubscriptionController {
|
||||
continue;
|
||||
}
|
||||
if (subscriber.filters.intersects(broadcastFilters)) {
|
||||
subscriber.link.sendTo(subscriber.dst, event);
|
||||
subscriber.link.sendTo(subscriber.address, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,8 +343,9 @@ export class SubscriptionController {
|
||||
if (!eventData) {
|
||||
throw new Error(`Event ${request.eventName} is not a registered as subscribable`);
|
||||
}
|
||||
const addressIndex = src.addressIndex();
|
||||
const subscriber = eventData.subscriptions.get(addressIndex);
|
||||
const index = src.addressIndex();
|
||||
const address = src.withoutRequestId();
|
||||
const subscriber = eventData.subscriptions.get(index);
|
||||
switch (request.action) {
|
||||
case "unsubscribe":
|
||||
if (!subscriber) {
|
||||
@@ -352,13 +353,13 @@ export class SubscriptionController {
|
||||
}
|
||||
subscriber.filters.subtract(request.filters);
|
||||
if (subscriber.filters.isEmpty()) {
|
||||
eventData.subscriptions.delete(addressIndex);
|
||||
eventData.subscriptions.delete(index);
|
||||
}
|
||||
break;
|
||||
|
||||
case "subscribe":
|
||||
if (!subscriber) {
|
||||
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
|
||||
eventData.subscriptions.set(index, { link, address, filters: request.filters });
|
||||
} else {
|
||||
subscriber.filters.union(request.filters);
|
||||
}
|
||||
@@ -369,9 +370,9 @@ export class SubscriptionController {
|
||||
if (!subscriber) {
|
||||
return false;
|
||||
}
|
||||
eventData.subscriptions.delete(addressIndex);
|
||||
eventData.subscriptions.delete(index);
|
||||
} else if (!subscriber) {
|
||||
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
|
||||
eventData.subscriptions.set(index, { link, address, filters: request.filters });
|
||||
} else {
|
||||
subscriber.filters = request.filters;
|
||||
}
|
||||
@@ -384,7 +385,7 @@ export class SubscriptionController {
|
||||
if (eventData.subscriptionUpdate) {
|
||||
const eventReplay = await eventData.subscriptionUpdate(request, src, dst);
|
||||
if (eventReplay) {
|
||||
link.sendTo(src, eventReplay);
|
||||
link.sendTo(address, eventReplay);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user