Skip to content

Commit

Permalink
WIP Extension mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
thoraj committed Nov 28, 2023
1 parent 76b7aa2 commit 24ef7a3
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/modules/ModuleRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ limitations under the License.

import { safeSet } from "matrix-js-sdk/src/utils";
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations";
import { AnyLifecycle } from "@matrix-org/react-sdk-module-api/lib/lifecycles/types";

import { AnyLifecycle, ProxiedExtensions } from "@matrix-org/react-sdk-module-api/lib/lifecycles/types";

Check failure on line 19 in src/modules/ModuleRunner.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Module '"@matrix-org/react-sdk-module-api/lib/lifecycles/types"' has no exported member 'ProxiedExtensions'.

Check failure on line 19 in src/modules/ModuleRunner.ts

View workflow job for this annotation

GitHub Actions / ESLint

There should be at least one empty line between import groups
import { AppModule } from "./AppModule";
import { ModuleFactory } from "./ModuleFactory";

Expand Down Expand Up @@ -85,4 +84,26 @@ export class ModuleRunner {
module.module.emit(lifecycleEvent, ...args);
}
}


public extensionMethods = new ProxiedExtensions(this.modules);

// /**
// * Invokes a method in any of the registeres modules (only a single module can register a fetcher of a given type).
// * @param extensionMethod The extension method to call
// * @param args The arguments for the method.
// */
// public invokeMethod<T>(extensionMethod: AnyExtensionMethod, ...args: any[]): T | undefined{
// console.log("Try invokeMethod: ", extensionMethod);
// for (const module of this.modules) {
// let method = module.module.methods.get(extensionMethod);
// if(method){
// console.log("Found. invoking method: ", extensionMethod);
// var methodResult = method(args);
// return methodResult;
// }
// }
// console.log("Method not found. returning undefined");
// return undefined;
// }
}
39 changes: 39 additions & 0 deletions test/modules/MockModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { RuntimeModule } from "@matrix-org/react-sdk-module-api/lib/RuntimeModul
import { ModuleApi } from "@matrix-org/react-sdk-module-api/lib/ModuleApi";

import { ModuleRunner } from "../../src/modules/ModuleRunner";

Check failure on line 20 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / ESLint

There should be at least one empty line between import groups

Check failure on line 20 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / ESLint

`../../src/modules/ModuleRunner` import should occur after import of `@matrix-org/react-sdk-module-api/lib/lifecycles/SecurityLifecycle`
import { AllExtensions } from "@matrix-org/react-sdk-module-api/lib/lifecycles/types";

Check failure on line 21 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Module '"@matrix-org/react-sdk-module-api/lib/lifecycles/types"' has no exported member 'AllExtensions'.
import { CryptoSetupExtensionsBase, IExtendedMatrixClientCreds } from "@matrix-org/react-sdk-module-api/lib/lifecycles/SecurityLifecycle";

Check failure on line 22 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Cannot find module '@matrix-org/react-sdk-module-api/lib/lifecycles/SecurityLifecycle' or its corresponding type declarations.

export class MockModule extends RuntimeModule {
public get apiInstance(): ModuleApi {
Expand All @@ -28,6 +30,27 @@ export class MockModule extends RuntimeModule {
super(moduleApi);
}
}
export class MockModuleWithCryptoSetupExtension extends RuntimeModule {
public get apiInstance(): ModuleApi {
return this.moduleApi;
}

extensions?: AllExtensions | undefined = {
cryptoSetup: new (class extends CryptoSetupExtensionsBase {
GetSecretStorageKey() {

Check failure on line 40 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / ESLint

Class Method name `GetSecretStorageKey` must match one of the following formats: camelCase
return "my secret storage key"
}

ExamineLoginResponse(response: any, credentials: IExtendedMatrixClientCreds): void {

Check failure on line 44 in test/modules/MockModule.ts

View workflow job for this annotation

GitHub Actions / ESLint

Class Method name `ExamineLoginResponse` must match one of the following formats: camelCase
credentials.secureBackupKey = "my secure backup key";
}
})()
}

public constructor(moduleApi: ModuleApi) {
super(moduleApi);
}
}

export function registerMockModule(): MockModule {
let module: MockModule | undefined;
Expand All @@ -43,3 +66,19 @@ export function registerMockModule(): MockModule {
}
return module;
}

export function registerMockModuleWithCryptoSetupExtension(): MockModuleWithCryptoSetupExtension {
let module: MockModuleWithCryptoSetupExtension | undefined;

ModuleRunner.instance.registerModule((api) => {
if (module) {
throw new Error("State machine error: ModuleRunner created the module twice");
}
module = new MockModuleWithCryptoSetupExtension(api);
return module;
});
if (!module) {
throw new Error("State machine error: ModuleRunner did not create module");
}
return module;
}
18 changes: 17 additions & 1 deletion test/modules/ModuleRunner-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

import { RoomPreviewOpts, RoomViewLifecycle } from "@matrix-org/react-sdk-module-api/lib/lifecycles/RoomViewLifecycle";

import { MockModule, registerMockModule } from "./MockModule";
import {

Check failure on line 19 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

There should be no empty line within import group
MockModule,
registerMockModule,
registerMockModuleWithCryptoSetupExtension
} from "./MockModule";

import { ModuleRunner } from "../../src/modules/ModuleRunner";

describe("ModuleRunner", () => {
Expand Down Expand Up @@ -49,4 +54,15 @@ describe("ModuleRunner", () => {
]);
});
});

describe("extensionMethods", () => {

it("should return value when extension provided by a registered module", async () => {
const module1 = registerMockModule();

Check failure on line 61 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

'module1' is declared but its value is never read.

Check failure on line 61 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

'module1' is assigned a value but never used
const module2 = registerMockModuleWithCryptoSetupExtension();

Check failure on line 62 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

'module2' is declared but its value is never read.

Check failure on line 62 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

'module2' is assigned a value but never used

var result = ModuleRunner.instance.extensionMethods.extensions.cryptoSetup?.GetSecretStorageKey();

Check failure on line 64 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

'result' is declared but its value is never read.

Check failure on line 64 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected var, use let or const instead

Check failure on line 64 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

'result' is assigned a value but never used
expect(results).toEqual("")

Check failure on line 65 in test/modules/ModuleRunner-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Cannot find name 'results'. Did you mean 'result'?
});
});
});
88 changes: 88 additions & 0 deletions test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap

Large diffs are not rendered by default.

0 comments on commit 24ef7a3

Please sign in to comment.