diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index aeb08827..f5ac8f61 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -14,25 +14,27 @@ export type EventSubscriberCallback = (event: T | null, synced: boolean) => v * When isEmpty() is true, no strings match this filter. * Otherwise, literal string matching is performed against a set. */ -export class SubscriptionFilters { +export class SubscriptionFilters extends Set { private constructor( private _all: boolean, - private _filters: Set, - ) {} + _filters: Iterable = [], + ) { + super(_filters); + } static jsonSchema = Type.Array(Type.String()); static fromJSON(json: Static) { - return new this(false, new Set(json)); + return new this(false, json); } toJSON() { - return [...this._filters]; + return [...this]; } /** Creates a subscription filter accepting all strings */ static all() { - return new this(true, new Set()); + return new this(true); } isAll() { @@ -41,11 +43,11 @@ export class SubscriptionFilters { /** Creates a subscription filter rejecting all strings */ static empty() { - return new this(false, new Set()); + return new this(false); } isEmpty() { - return !this._all && this._filters.size === 0; + return !this._all && this.size === 0; } /** @@ -67,20 +69,20 @@ export class SubscriptionFilters { if (this._all) { return "[SubscriptionFilters All]"; } - if (this._filters.size === 0) { + if (this.size === 0) { return "[SubscriptionFilters Empty]"; } - return `[SubscriptionFilters Set<${this._filters.size}>]`; + return `[SubscriptionFilters Set<${this.size}>]`; } /** Returns true if this filter accepts the given string */ accepts(value: string) { - return this._all || this._filters.has(value); + return this._all || this.has(value); } /** Returns true if this filter extends the other */ extends(other: this) { - return this._all || (!other._all && [...other._filters.values()].every(value => this._filters.has(value))); + return this._all || (!other._all && [...other.values()].every(value => this.has(value))); } /** Returns true if there is overlap between two filters */ @@ -91,8 +93,8 @@ export class SubscriptionFilters { if (other._all) { return !this.isEmpty(); } - for (const filter of other._filters) { - if (this._filters.has(filter)) { + for (const filter of other) { + if (this.has(filter)) { return true; } } @@ -106,11 +108,11 @@ export class SubscriptionFilters { } if (other._all) { this._all = true; - this._filters.clear(); + this.clear(); return; } - for (const filter of other._filters) { - this._filters.add(filter); + for (const filter of other) { + this.add(filter); } } @@ -122,14 +124,14 @@ export class SubscriptionFilters { subtract(other: this) { if (other._all) { this._all = false; - this._filters.clear(); + this.clear(); return; } if (this._all) { return; // Cannot subtract from all } - for (const filter of other._filters) { - this._filters.delete(filter); + for (const filter of other) { + this.delete(filter); } } }