Skip to content

Commit

Permalink
Improve typing around event emitter handlers (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Feb 22, 2022
1 parent 1ac4cc4 commit 12e525b
Show file tree
Hide file tree
Showing 41 changed files with 906 additions and 416 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = {
"no-async-promise-executor": "off",
// We use a `logger` intermediary module
"no-console": "error",

// restrict EventEmitters to force callers to use TypedEventEmitter
"no-restricted-imports": ["error", "events"],
},
overrides: [{
files: [
Expand Down
4 changes: 2 additions & 2 deletions spec/integ/matrix-client-retrying.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventStatus } from "../../src/matrix";
import { EventStatus, RoomEvent } from "../../src/matrix";
import { MatrixScheduler } from "../../src/scheduler";
import { Room } from "../../src/models/room";
import { TestClient } from "../TestClient";
Expand Down Expand Up @@ -95,7 +95,7 @@ describe("MatrixClient retrying", function() {

// wait for the localecho of ev1 to be updated
const p3 = new Promise<void>((resolve, reject) => {
room.on("Room.localEchoUpdated", (ev0) => {
room.on(RoomEvent.LocalEchoUpdated, (ev0) => {
if (ev0 === ev1) {
resolve();
}
Expand Down
1 change: 1 addition & 0 deletions spec/unit/ReEmitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// eslint-disable-next-line no-restricted-imports
import { EventEmitter } from "events";

import { ReEmitter } from "../../src/ReEmitter";
Expand Down
1 change: 1 addition & 0 deletions spec/unit/crypto.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '../olm-loader';
// eslint-disable-next-line no-restricted-imports
import { EventEmitter } from "events";

import { Crypto } from "../../src/crypto";
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/crypto/crypto-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function resetCrossSigningKeys(client, {
crypto.crossSigningInfo.keys = oldKeys;
throw e;
}
crypto.baseApis.emit("crossSigning.keysChanged", {});
crypto.emit("crossSigning.keysChanged", {});
await crypto.afterCrossSigningLocalKeyChange();
}

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/crypto/verification/secret_request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { VerificationBase } from '../../../../src/crypto/verification/Base';
import { CrossSigningInfo } from '../../../../src/crypto/CrossSigning';
import { encodeBase64 } from "../../../../src/crypto/olmlib";
import { setupWebcrypto, teardownWebcrypto } from './util';
import { VerificationBase } from '../../../../src/crypto/verification/Base';

jest.useFakeTimers();

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { EventTimelineSet } from "../../src/models/event-timeline-set";
import { MatrixEvent } from "../../src/models/event";
import { MatrixEvent, MatrixEventEvent } from "../../src/models/event";
import { Room } from "../../src/models/room";
import { Relations } from "../../src/models/relations";

Expand Down Expand Up @@ -103,7 +103,7 @@ describe("Relations", function() {
// Add the target event first, then the relation event
{
const relationsCreated = new Promise(resolve => {
targetEvent.once("Event.relationsCreated", resolve);
targetEvent.once(MatrixEventEvent.RelationsCreated, resolve);
});

const timelineSet = new EventTimelineSet(room, {
Expand All @@ -118,7 +118,7 @@ describe("Relations", function() {
// Add the relation event first, then the target event
{
const relationsCreated = new Promise(resolve => {
targetEvent.once("Event.relationsCreated", resolve);
targetEvent.once(MatrixEventEvent.RelationsCreated, resolve);
});

const timelineSet = new EventTimelineSet(room, {
Expand Down
27 changes: 21 additions & 6 deletions src/ReEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// eslint-disable-next-line no-restricted-imports
import { EventEmitter } from "events";

export class ReEmitter {
private target: EventEmitter;
import { ListenerMap, TypedEventEmitter } from "./models/typed-event-emitter";

constructor(target: EventEmitter) {
this.target = target;
}
export class ReEmitter {
constructor(private readonly target: EventEmitter) {}

reEmit(source: EventEmitter, eventNames: string[]) {
public reEmit(source: EventEmitter, eventNames: string[]): void {
for (const eventName of eventNames) {
// We include the source as the last argument for event handlers which may need it,
// such as read receipt listeners on the client class which won't have the context
Expand All @@ -48,3 +47,19 @@ export class ReEmitter {
}
}
}

export class TypedReEmitter<
Events extends string,
Arguments extends ListenerMap<Events>,
> extends ReEmitter {
constructor(target: TypedEventEmitter<Events, Arguments>) {
super(target);
}

public reEmit<ReEmittedEvents extends string, T extends Events & ReEmittedEvents>(
source: TypedEventEmitter<ReEmittedEvents, any>,
eventNames: T[],
): void {
super.reEmit(source, eventNames);
}
}
Loading

0 comments on commit 12e525b

Please sign in to comment.