mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 00:23:12 +09:00
Fixed bugs found during pre-review
This commit is contained in:
3 files changed
+27
-22
No files matched your search
@@ -9,6 +9,7 @@ export * from "./dist/src/logging";
|
|||||||
export * from "./dist/src/plugin";
|
export * from "./dist/src/plugin";
|
||||||
export * from "./dist/src/schema";
|
export * from "./dist/src/schema";
|
||||||
export * from "./dist/src/users";
|
export * from "./dist/src/users";
|
||||||
|
export * from "./dist/src/subscriptions";
|
||||||
|
|
||||||
export { default as ExponentialBackoff } from "./dist/src/ExponentialBackoff";
|
export { default as ExponentialBackoff } from "./dist/src/ExponentialBackoff";
|
||||||
export { default as PlayerStats } from "./dist/src/PlayerStats";
|
export { default as PlayerStats } from "./dist/src/PlayerStats";
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ export class SubscriptionRequest {
|
|||||||
export class SubscriptionController {
|
export class SubscriptionController {
|
||||||
_events = new Map<string, {
|
_events = new Map<string, {
|
||||||
subscriptionUpdate?: SubscriptionRequestHandler<unknown>,
|
subscriptionUpdate?: SubscriptionRequestHandler<unknown>,
|
||||||
subscriptions: Map<Link, { all: boolean, channels: Array<string | number> }>,
|
subscriptions: Map<Link, { all: boolean, channels: Array<string | number>, onceClose: () => void }>,
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -186,14 +186,18 @@ export class SubscriptionController {
|
|||||||
const eventReplay = eventData.subscriptionUpdate ? await eventData.subscriptionUpdate(event, src, dst) : null;
|
const eventReplay = eventData.subscriptionUpdate ? await eventData.subscriptionUpdate(event, src, dst) : null;
|
||||||
const link: Link = this.controller.wsServer.controlConnections.get(src.id);
|
const link: Link = this.controller.wsServer.controlConnections.get(src.id);
|
||||||
if (event.allChannels === false && event.channels.length === 0) {
|
if (event.allChannels === false && event.channels.length === 0) {
|
||||||
eventData.subscriptions.delete(link);
|
let onceClose = eventData.subscriptions.get(link)?.onceClose;
|
||||||
} else {
|
if (onceClose) {
|
||||||
if (!eventData.subscriptions.has(link)) {
|
link.connector.off("close", onceClose);
|
||||||
link.connector.once("close", () => {
|
eventData.subscriptions.delete(link);
|
||||||
eventData.subscriptions.delete(link);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
eventData.subscriptions.set(link, { all: event.allChannels, channels: event.channels });
|
} else {
|
||||||
|
let onceClose = eventData.subscriptions.get(link)?.onceClose;
|
||||||
|
if (!onceClose) {
|
||||||
|
onceClose = () => eventData.subscriptions.delete(link);
|
||||||
|
link.connector.once("close", onceClose);
|
||||||
|
}
|
||||||
|
eventData.subscriptions.set(link, { all: event.allChannels, channels: event.channels, onceClose: onceClose });
|
||||||
}
|
}
|
||||||
return new SubscriptionResponse(eventReplay);
|
return new SubscriptionResponse(eventReplay);
|
||||||
}
|
}
|
||||||
@@ -249,51 +253,51 @@ export class EventSubscriber<T, V=T> {
|
|||||||
/**
|
/**
|
||||||
* If a control link was not connected at creation, it can be connected here (normally during onControllerConnectionEvent)
|
* If a control link was not connected at creation, it can be connected here (normally during onControllerConnectionEvent)
|
||||||
*/
|
*/
|
||||||
connectControl(control: Link) {
|
async connectControl(control: Link) {
|
||||||
if (this.control === control) return Promise.resolve();
|
if (this.control === control) return;
|
||||||
this.control = control;
|
this.control = control;
|
||||||
this.control.handle(this.event, this._handle.bind(this));
|
this.control.handle(this.event, this._handle.bind(this));
|
||||||
return this._updateSubscription();
|
await this._updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscribe to receive all event notifications
|
* Subscribe to receive all event notifications
|
||||||
*/
|
*/
|
||||||
subscribe(handler: EventSubscriberCallback<V>) {
|
async subscribe(handler: EventSubscriberCallback<V>) {
|
||||||
this._callbacks.push(handler);
|
this._callbacks.push(handler);
|
||||||
return this._updateSubscription();
|
await this._updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscribe to receive event notifications for the given channel
|
* Subscribe to receive event notifications for the given channel
|
||||||
*/
|
*/
|
||||||
subscribeToChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
async subscribeToChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
||||||
if (!this._channelCallbacks.get(channel)) {
|
if (!this._channelCallbacks.get(channel)) {
|
||||||
this._channelCallbacks.set(channel, []);
|
this._channelCallbacks.set(channel, []);
|
||||||
}
|
}
|
||||||
this._channelCallbacks.get(channel)!.push(handler);
|
this._channelCallbacks.get(channel)!.push(handler);
|
||||||
return this._updateSubscription();
|
await this._updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unsubscribe from receiving all event notifications, does not effect the subscriptions of other handlers
|
* Unsubscribe from receiving all event notifications, does not effect the subscriptions of other handlers
|
||||||
* Will throw an error if your handler is not subscribed to all channels, does not accept anonymous functions
|
* Will throw an error if your handler is not subscribed to all channels, does not accept anonymous functions
|
||||||
*/
|
*/
|
||||||
unsubscribe(handler: EventSubscriberCallback<V>) {
|
async unsubscribe(handler: EventSubscriberCallback<V>) {
|
||||||
let index = this._callbacks.lastIndexOf(handler);
|
let index = this._callbacks.lastIndexOf(handler);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
throw new Error("handler is not registered");
|
throw new Error("handler is not registered");
|
||||||
}
|
}
|
||||||
|
|
||||||
this._callbacks.splice(index, 1);
|
this._callbacks.splice(index, 1);
|
||||||
return this._updateSubscription();
|
await this._updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unsubscribe from receiving event notifications for a given channel, does not effect the subscriptions of other handlers
|
* Unsubscribe from receiving event notifications for a given channel, does not effect the subscriptions of other handlers
|
||||||
* Will throw an error if your handler is not subscribed to the channel, does not accept anonymous functions
|
* Will throw an error if your handler is not subscribed to the channel, does not accept anonymous functions
|
||||||
*/
|
*/
|
||||||
unsubscribeFromChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
async unsubscribeFromChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
||||||
const channelCallbacks = this._channelCallbacks.get(channel);
|
const channelCallbacks = this._channelCallbacks.get(channel);
|
||||||
if (!channelCallbacks) {
|
if (!channelCallbacks) {
|
||||||
throw new Error("handler is not registered");
|
throw new Error("handler is not registered");
|
||||||
@@ -309,7 +313,7 @@ export class EventSubscriber<T, V=T> {
|
|||||||
} else {
|
} else {
|
||||||
channelCallbacks.splice(index, 1);
|
channelCallbacks.splice(index, 1);
|
||||||
}
|
}
|
||||||
return this._updateSubscription();
|
await this._updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -59,9 +59,9 @@ export class Control extends lib.Link {
|
|||||||
hostUpdate = new lib.EventSubscriber<lib.HostUpdateEvent, lib.HostDetails>(lib.HostUpdateEvent, event => event.update);
|
hostUpdate = new lib.EventSubscriber<lib.HostUpdateEvent, lib.HostDetails>(lib.HostUpdateEvent, event => event.update);
|
||||||
instanceUpdate = new lib.EventSubscriber<lib.InstanceDetailsUpdateEvent, lib.InstanceDetails>(lib.InstanceDetailsUpdateEvent, event => event.details);
|
instanceUpdate = new lib.EventSubscriber<lib.InstanceDetailsUpdateEvent, lib.InstanceDetails>(lib.InstanceDetailsUpdateEvent, event => event.details);
|
||||||
saveListUpdate = new lib.EventSubscriber<lib.InstanceSaveListUpdateEvent>(lib.InstanceSaveListUpdateEvent);
|
saveListUpdate = new lib.EventSubscriber<lib.InstanceSaveListUpdateEvent>(lib.InstanceSaveListUpdateEvent);
|
||||||
modPackUpdate = new lib.EventSubscriber<lib.ModPackUpdateEvent, lib.ModPack>(lib.InstanceSaveListUpdateEvent, event => event.modPack);
|
modPackUpdate = new lib.EventSubscriber<lib.ModPackUpdateEvent, lib.ModPack>(lib.ModPackUpdateEvent, event => event.modPack);
|
||||||
modUpdate = new lib.EventSubscriber<lib.ModUpdateEvent, lib.ModInfo>(lib.InstanceSaveListUpdateEvent, event => event.mod);
|
modUpdate = new lib.EventSubscriber<lib.ModUpdateEvent, lib.ModInfo>(lib.ModUpdateEvent, event => event.mod);
|
||||||
userUpdate = new lib.EventSubscriber<lib.UserUpdateEvent, lib.RawUser>(lib.InstanceSaveListUpdateEvent, event => event.user);
|
userUpdate = new lib.EventSubscriber<lib.UserUpdateEvent, lib.RawUser>(lib.UserUpdateEvent, event => event.user);
|
||||||
|
|
||||||
declare connector: ControlConnector;
|
declare connector: ControlConnector;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user