Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing around event emitter handlers #2180

Merged
merged 32 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e7b91aa
Remove unused Thread.Ready emit signature
t3chguy Feb 16, 2022
dc36e68
un-type local-storage-events-emitter.ts
t3chguy Feb 16, 2022
5021cf0
tidy re-emitter
t3chguy Feb 16, 2022
740a682
Fix memory leak in the memory store
t3chguy Feb 16, 2022
2d729d0
Improve TypedEventEmitter and create TypedReEmitter
t3chguy Feb 16, 2022
c2be05c
Type the RoomState & User EventEmitters
t3chguy Feb 16, 2022
69d23fb
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Feb 16, 2022
3ddc996
Type the RoomMember EventEmitter
t3chguy Feb 16, 2022
47a33e8
Type the MatrixCall EventEmitter
t3chguy Feb 16, 2022
0f35e81
Type the MatrixEvent EventEmitter
t3chguy Feb 16, 2022
466b688
Type the Relations EventEmitter
t3chguy Feb 16, 2022
3f5decd
Type the VerificationRequest EventEmitter
t3chguy Feb 16, 2022
42b308a
Type the DeviceList EventEmitter
t3chguy Feb 16, 2022
91bdf63
TS improvements and fix default verification methods bug
t3chguy Feb 16, 2022
6705a5e
Type the Crypto EventEmitter
t3chguy Feb 16, 2022
625548c
Type the CallFeed EventEmitter
t3chguy Feb 16, 2022
51c0c2d
Standardise
t3chguy Feb 16, 2022
d1b8d70
Fix CrossSigning wrongly extending EventEmitted
t3chguy Feb 16, 2022
9685fbb
Type the IAccountDataClient & AccountDataClientAdapter EventEmitters
t3chguy Feb 16, 2022
87ccd56
Re-type the LocalStorageErrorsEventsEmitter
t3chguy Feb 16, 2022
2712bfe
Type more EventEmitters
t3chguy Feb 16, 2022
147837c
Type the MatrixClient EventEmitter
t3chguy Feb 16, 2022
2cd0ce7
Tidy
t3chguy Feb 16, 2022
c1e3f9b
Remove entirely unused re-emits
t3chguy Feb 16, 2022
c85ea3a
Wrap up remaining types/enums
t3chguy Feb 16, 2022
78d5b34
delint
t3chguy Feb 16, 2022
f895fc7
Fix tests
t3chguy Feb 16, 2022
1f3f682
Fix tests
t3chguy Feb 17, 2022
72e5803
Use ESLint to prevent usage of EventEmitter directly
t3chguy Feb 17, 2022
d9844b3
Type the IDB EventEmitter without an enum
t3chguy Feb 17, 2022
eaeb458
Fix types
t3chguy Feb 22, 2022
d36200a
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Feb 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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