Skip to content

Commit

Permalink
Simplify app-runtime natives (#409)
Browse files Browse the repository at this point in the history
* feat: use eventbus from runtime for native events

* feat: get rid of the NativeDeviceInfoAccess

* fix: push test
  • Loading branch information
jkoenig134 authored Jan 29, 2025
1 parent de2c672 commit 2cf6664
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 79 deletions.
9 changes: 6 additions & 3 deletions packages/app-runtime/src/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defaultsDeep } from "lodash";
export interface AppConfig extends RuntimeConfig {
accountsDbName: string;
applicationId: string;
pushService: "apns" | "fcm" | "none" | "dummy";
applePushEnvironment?: "Development" | "Production";
allowMultipleAccountsWithSameAddress: boolean;
databaseFolder: string;
Expand All @@ -14,6 +15,7 @@ export interface AppConfigOverwrite {
transportLibrary?: Omit<IConfigOverwrite, "supportedIdentityVersion">;
accountsDbName?: string;
applicationId?: string;
pushService?: "apns" | "fcm" | "none" | "dummy";
applePushEnvironment?: "Development" | "Production";
allowMultipleAccountsWithSameAddress?: boolean;
databaseFolder?: string;
Expand All @@ -25,6 +27,9 @@ export function createAppConfig(...configs: (AppConfigOverwrite | AppConfig)[]):
transportLibrary: Omit<IConfigOverwrite, "supportedIdentityVersion" | "platformClientId" | "platformClientSecret" | "baseUrl">;
} = {
accountsDbName: "accounts",
pushService: "none",
allowMultipleAccountsWithSameAddress: false,
databaseFolder: "./data",
transportLibrary: {
datawalletEnabled: true
},
Expand Down Expand Up @@ -107,9 +112,7 @@ export function createAppConfig(...configs: (AppConfigOverwrite | AppConfig)[]):
displayName: "Notification Module",
location: "@nmshd/runtime:NotificationModule"
}
},
allowMultipleAccountsWithSameAddress: false,
databaseFolder: "./data"
}
};

const mergedConfig = defaultsDeep({}, ...configs, appConfig);
Expand Down
13 changes: 0 additions & 13 deletions packages/app-runtime/src/modules/AppRuntimeModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ILogger } from "@js-soft/logging-abstractions";
import { EventHandler, SubscriptionTarget } from "@js-soft/ts-utils";
import { ModuleConfiguration, RuntimeModule } from "@nmshd/runtime";
import { AppRuntime } from "../AppRuntime";

Expand All @@ -11,16 +10,4 @@ export interface AppRuntimeModuleConfiguration extends ModuleConfiguration {}

export abstract class AppRuntimeModule<TConfig extends AppRuntimeModuleConfiguration = AppRuntimeModuleConfiguration> extends RuntimeModule<TConfig, AppRuntime> {
private readonly nativeEventSubscriptionIds: number[] = [];

protected subscribeToNativeEvent<TEvent>(event: SubscriptionTarget<TEvent>, handler: EventHandler<TEvent>): void {
const subscriptionResult = this.runtime.nativeEnvironment.eventBus.subscribe(event, handler);
this.nativeEventSubscriptionIds.push(subscriptionResult);
}

protected override unsubscribeFromAllEvents(): void {
super.unsubscribeFromAllEvents();

this.nativeEventSubscriptionIds.forEach((id) => this.runtime.nativeEnvironment.eventBus.unsubscribe(id));
this.nativeEventSubscriptionIds.splice(0);
}
}
6 changes: 3 additions & 3 deletions packages/app-runtime/src/modules/PushNotificationModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class PushNotificationModule extends AppRuntimeModule<PushNotificationMod
}

public start(): void {
this.subscribeToNativeEvent(RemoteNotificationEvent, this.handleRemoteNotification.bind(this));
this.subscribeToNativeEvent(RemoteNotificationRegistrationEvent, this.handleTokenRegistration.bind(this));
this.subscribeToEvent(RemoteNotificationEvent, this.handleRemoteNotification.bind(this));
this.subscribeToEvent(RemoteNotificationRegistrationEvent, this.handleTokenRegistration.bind(this));
this.subscribeToEvent(AccountSelectedEvent, this.handleAccountSelected.bind(this));
}

Expand Down Expand Up @@ -102,9 +102,9 @@ export class PushNotificationModule extends AppRuntimeModule<PushNotificationMod
throw AppRuntimeErrors.modules.pushNotificationModule.tokenRegistrationNotPossible("No device for this account found", deviceResult.error).logWith(this.logger);
}

const platform = this.runtime.nativeEnvironment.deviceInfoAccess.deviceInfo.pushService;
const appId = this.runtime.config.applicationId;
const handle = token;
const platform = this.runtime.config.pushService;
const environment = this.runtime.config.applePushEnvironment;

const result = await services.transportServices.account.registerPushNotificationToken({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AppLaunchModule extends AppRuntimeModule<AppLaunchModuleConfig> {
}

public start(): void {
this.subscribeToNativeEvent(UrlOpenEvent, this.handleUrlOpen.bind(this));
this.subscribeToEvent(UrlOpenEvent, this.handleUrlOpen.bind(this));
}

private async handleUrlOpen(event: UrlOpenEvent) {
Expand Down
17 changes: 0 additions & 17 deletions packages/app-runtime/src/natives/INativeDeviceInfoAccess.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/app-runtime/src/natives/INativeEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { ILokiJsDatabaseFactory } from "@js-soft/docdb-access-loki";
import { ILoggerFactory } from "@js-soft/logging-abstractions";
import { EventBus } from "@js-soft/ts-utils";
import { INativeConfigAccess } from "./INativeConfigAccess";
import { INativeDeviceInfoAccess } from "./INativeDeviceInfoAccess";
import { INativeNotificationAccess } from "./INativeNotificationAccess";

export interface INativeEnvironment {
databaseFactory: ILokiJsDatabaseFactory;
configAccess: INativeConfigAccess;
loggerFactory: ILoggerFactory;
notificationAccess: INativeNotificationAccess;
eventBus: EventBus;
deviceInfoAccess: INativeDeviceInfoAccess;
}
1 change: 0 additions & 1 deletion packages/app-runtime/src/natives/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./events";
export * from "./INativeBootstrapper";
export * from "./INativeConfigAccess";
export * from "./INativeDeviceInfoAccess";
export * from "./INativeEnvironment";
export * from "./INativeNotificationAccess";
export * from "./INativeTranslationProvider";
Expand Down
2 changes: 1 addition & 1 deletion packages/app-runtime/test/lib/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class TestUtil {

const config = this.createAppConfig(configOverride);

const nativeBootstrapper = new FakeNativeBootstrapper(eventBus);
const nativeBootstrapper = new FakeNativeBootstrapper();
await nativeBootstrapper.init();
const runtime = await AppRuntime.create(nativeBootstrapper, config, eventBus);
runtime.registerUIBridge(uiBridge);
Expand Down
12 changes: 1 addition & 11 deletions packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { NodeLoggerFactory } from "@js-soft/node-logger";
import { EventBus, EventEmitter2EventBus, Result } from "@js-soft/ts-utils";
import { Result } from "@js-soft/ts-utils";
import { INativeBootstrapper, INativeEnvironment } from "../../../src";
import { FakeNativeConfigAccess } from "./FakeNativeConfigAccess";
import { FakeNativeDatabaseFactory } from "./FakeNativeDatabaseFactory";
import { FakeNativeDeviceInfoAccess } from "./FakeNativeDeviceInfoAccess";
import { FakeNativeNotificationAccess } from "./FakeNativeNotificationAccess";

export class FakeNativeBootstrapper implements INativeBootstrapper {
public constructor(private readonly eventBus?: EventBus) {}

private _nativeEnvironment: INativeEnvironment;
public get nativeEnvironment(): INativeEnvironment {
return this._nativeEnvironment;
Expand Down Expand Up @@ -45,17 +42,10 @@ export class FakeNativeBootstrapper implements INativeBootstrapper {
this._nativeEnvironment = {
configAccess: new FakeNativeConfigAccess(),
databaseFactory: new FakeNativeDatabaseFactory(),
deviceInfoAccess: new FakeNativeDeviceInfoAccess(),
eventBus:
this.eventBus ??
new EventEmitter2EventBus(() => {
// noop
}),
loggerFactory,
notificationAccess: new FakeNativeNotificationAccess(nativeLogger)
};

await this._nativeEnvironment.deviceInfoAccess.init();
await this._nativeEnvironment.notificationAccess.init();

return Result.ok(undefined);
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions packages/app-runtime/test/modules/PushNotification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("PushNotificationModuleTest", function () {
let devicePushIdentifier = "dummy value";

beforeAll(async function () {
runtime = await TestUtil.createRuntime({ modules: { pushNotification: { enabled: true } } }, undefined, eventBus);
runtime = await TestUtil.createRuntime({ pushService: "dummy", modules: { pushNotification: { enabled: true } } }, undefined, eventBus);
await runtime.start();

const accounts = await TestUtil.provideAccounts(runtime, 1);
Expand All @@ -24,7 +24,7 @@ describe("PushNotificationModuleTest", function () {
afterEach(() => eventBus.reset());

test("should persist push identifier", async function () {
runtime.nativeEnvironment.eventBus.publish(new RemoteNotificationRegistrationEvent("handleLongerThan10Characters"));
runtime.eventBus.publish(new RemoteNotificationRegistrationEvent("handleLongerThan10Characters"));

await eventBus.waitForRunningEventHandlers();

Expand All @@ -35,7 +35,7 @@ describe("PushNotificationModuleTest", function () {
});

test("should do a datawallet sync when DatawalletModificationsCreated is received", async function () {
runtime.nativeEnvironment.eventBus.publish(
runtime.eventBus.publish(
new RemoteNotificationEvent({
content: {
devicePushIdentifier: devicePushIdentifier,
Expand All @@ -50,7 +50,7 @@ describe("PushNotificationModuleTest", function () {
});

test("should do a sync everything when ExternalEventCreated is received", async function () {
runtime.nativeEnvironment.eventBus.publish(
runtime.eventBus.publish(
new RemoteNotificationEvent({
content: {
devicePushIdentifier: devicePushIdentifier,
Expand Down

0 comments on commit 2cf6664

Please sign in to comment.