Skip to content

Commit 2cf6664

Browse files
authored
Simplify app-runtime natives (#409)
* feat: use eventbus from runtime for native events * feat: get rid of the NativeDeviceInfoAccess * fix: push test
1 parent de2c672 commit 2cf6664

File tree

11 files changed

+16
-79
lines changed

11 files changed

+16
-79
lines changed

packages/app-runtime/src/AppConfig.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defaultsDeep } from "lodash";
55
export interface AppConfig extends RuntimeConfig {
66
accountsDbName: string;
77
applicationId: string;
8+
pushService: "apns" | "fcm" | "none" | "dummy";
89
applePushEnvironment?: "Development" | "Production";
910
allowMultipleAccountsWithSameAddress: boolean;
1011
databaseFolder: string;
@@ -14,6 +15,7 @@ export interface AppConfigOverwrite {
1415
transportLibrary?: Omit<IConfigOverwrite, "supportedIdentityVersion">;
1516
accountsDbName?: string;
1617
applicationId?: string;
18+
pushService?: "apns" | "fcm" | "none" | "dummy";
1719
applePushEnvironment?: "Development" | "Production";
1820
allowMultipleAccountsWithSameAddress?: boolean;
1921
databaseFolder?: string;
@@ -25,6 +27,9 @@ export function createAppConfig(...configs: (AppConfigOverwrite | AppConfig)[]):
2527
transportLibrary: Omit<IConfigOverwrite, "supportedIdentityVersion" | "platformClientId" | "platformClientSecret" | "baseUrl">;
2628
} = {
2729
accountsDbName: "accounts",
30+
pushService: "none",
31+
allowMultipleAccountsWithSameAddress: false,
32+
databaseFolder: "./data",
2833
transportLibrary: {
2934
datawalletEnabled: true
3035
},
@@ -107,9 +112,7 @@ export function createAppConfig(...configs: (AppConfigOverwrite | AppConfig)[]):
107112
displayName: "Notification Module",
108113
location: "@nmshd/runtime:NotificationModule"
109114
}
110-
},
111-
allowMultipleAccountsWithSameAddress: false,
112-
databaseFolder: "./data"
115+
}
113116
};
114117

115118
const mergedConfig = defaultsDeep({}, ...configs, appConfig);
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ILogger } from "@js-soft/logging-abstractions";
2-
import { EventHandler, SubscriptionTarget } from "@js-soft/ts-utils";
32
import { ModuleConfiguration, RuntimeModule } from "@nmshd/runtime";
43
import { AppRuntime } from "../AppRuntime";
54

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

1211
export abstract class AppRuntimeModule<TConfig extends AppRuntimeModuleConfiguration = AppRuntimeModuleConfiguration> extends RuntimeModule<TConfig, AppRuntime> {
1312
private readonly nativeEventSubscriptionIds: number[] = [];
14-
15-
protected subscribeToNativeEvent<TEvent>(event: SubscriptionTarget<TEvent>, handler: EventHandler<TEvent>): void {
16-
const subscriptionResult = this.runtime.nativeEnvironment.eventBus.subscribe(event, handler);
17-
this.nativeEventSubscriptionIds.push(subscriptionResult);
18-
}
19-
20-
protected override unsubscribeFromAllEvents(): void {
21-
super.unsubscribeFromAllEvents();
22-
23-
this.nativeEventSubscriptionIds.forEach((id) => this.runtime.nativeEnvironment.eventBus.unsubscribe(id));
24-
this.nativeEventSubscriptionIds.splice(0);
25-
}
2613
}

packages/app-runtime/src/modules/PushNotificationModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export class PushNotificationModule extends AppRuntimeModule<PushNotificationMod
2424
}
2525

2626
public start(): void {
27-
this.subscribeToNativeEvent(RemoteNotificationEvent, this.handleRemoteNotification.bind(this));
28-
this.subscribeToNativeEvent(RemoteNotificationRegistrationEvent, this.handleTokenRegistration.bind(this));
27+
this.subscribeToEvent(RemoteNotificationEvent, this.handleRemoteNotification.bind(this));
28+
this.subscribeToEvent(RemoteNotificationRegistrationEvent, this.handleTokenRegistration.bind(this));
2929
this.subscribeToEvent(AccountSelectedEvent, this.handleAccountSelected.bind(this));
3030
}
3131

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

105-
const platform = this.runtime.nativeEnvironment.deviceInfoAccess.deviceInfo.pushService;
106105
const appId = this.runtime.config.applicationId;
107106
const handle = token;
107+
const platform = this.runtime.config.pushService;
108108
const environment = this.runtime.config.applePushEnvironment;
109109

110110
const result = await services.transportServices.account.registerPushNotificationToken({

packages/app-runtime/src/modules/appEvents/AppLaunchModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class AppLaunchModule extends AppRuntimeModule<AppLaunchModuleConfig> {
1212
}
1313

1414
public start(): void {
15-
this.subscribeToNativeEvent(UrlOpenEvent, this.handleUrlOpen.bind(this));
15+
this.subscribeToEvent(UrlOpenEvent, this.handleUrlOpen.bind(this));
1616
}
1717

1818
private async handleUrlOpen(event: UrlOpenEvent) {

packages/app-runtime/src/natives/INativeDeviceInfoAccess.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { ILokiJsDatabaseFactory } from "@js-soft/docdb-access-loki";
22
import { ILoggerFactory } from "@js-soft/logging-abstractions";
3-
import { EventBus } from "@js-soft/ts-utils";
43
import { INativeConfigAccess } from "./INativeConfigAccess";
5-
import { INativeDeviceInfoAccess } from "./INativeDeviceInfoAccess";
64
import { INativeNotificationAccess } from "./INativeNotificationAccess";
75

86
export interface INativeEnvironment {
97
databaseFactory: ILokiJsDatabaseFactory;
108
configAccess: INativeConfigAccess;
119
loggerFactory: ILoggerFactory;
1210
notificationAccess: INativeNotificationAccess;
13-
eventBus: EventBus;
14-
deviceInfoAccess: INativeDeviceInfoAccess;
1511
}

packages/app-runtime/src/natives/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from "./events";
22
export * from "./INativeBootstrapper";
33
export * from "./INativeConfigAccess";
4-
export * from "./INativeDeviceInfoAccess";
54
export * from "./INativeEnvironment";
65
export * from "./INativeNotificationAccess";
76
export * from "./INativeTranslationProvider";

packages/app-runtime/test/lib/TestUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class TestUtil {
3434

3535
const config = this.createAppConfig(configOverride);
3636

37-
const nativeBootstrapper = new FakeNativeBootstrapper(eventBus);
37+
const nativeBootstrapper = new FakeNativeBootstrapper();
3838
await nativeBootstrapper.init();
3939
const runtime = await AppRuntime.create(nativeBootstrapper, config, eventBus);
4040
runtime.registerUIBridge(uiBridge);

packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { NodeLoggerFactory } from "@js-soft/node-logger";
2-
import { EventBus, EventEmitter2EventBus, Result } from "@js-soft/ts-utils";
2+
import { Result } from "@js-soft/ts-utils";
33
import { INativeBootstrapper, INativeEnvironment } from "../../../src";
44
import { FakeNativeConfigAccess } from "./FakeNativeConfigAccess";
55
import { FakeNativeDatabaseFactory } from "./FakeNativeDatabaseFactory";
6-
import { FakeNativeDeviceInfoAccess } from "./FakeNativeDeviceInfoAccess";
76
import { FakeNativeNotificationAccess } from "./FakeNativeNotificationAccess";
87

98
export class FakeNativeBootstrapper implements INativeBootstrapper {
10-
public constructor(private readonly eventBus?: EventBus) {}
11-
129
private _nativeEnvironment: INativeEnvironment;
1310
public get nativeEnvironment(): INativeEnvironment {
1411
return this._nativeEnvironment;
@@ -45,17 +42,10 @@ export class FakeNativeBootstrapper implements INativeBootstrapper {
4542
this._nativeEnvironment = {
4643
configAccess: new FakeNativeConfigAccess(),
4744
databaseFactory: new FakeNativeDatabaseFactory(),
48-
deviceInfoAccess: new FakeNativeDeviceInfoAccess(),
49-
eventBus:
50-
this.eventBus ??
51-
new EventEmitter2EventBus(() => {
52-
// noop
53-
}),
5445
loggerFactory,
5546
notificationAccess: new FakeNativeNotificationAccess(nativeLogger)
5647
};
5748

58-
await this._nativeEnvironment.deviceInfoAccess.init();
5949
await this._nativeEnvironment.notificationAccess.init();
6050

6151
return Result.ok(undefined);

packages/app-runtime/test/lib/natives/FakeNativeDeviceInfoAccess.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/app-runtime/test/modules/PushNotification.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("PushNotificationModuleTest", function () {
1010
let devicePushIdentifier = "dummy value";
1111

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

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

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

2929
await eventBus.waitForRunningEventHandlers();
3030

@@ -35,7 +35,7 @@ describe("PushNotificationModuleTest", function () {
3535
});
3636

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

5252
test("should do a sync everything when ExternalEventCreated is received", async function () {
53-
runtime.nativeEnvironment.eventBus.publish(
53+
runtime.eventBus.publish(
5454
new RemoteNotificationEvent({
5555
content: {
5656
devicePushIdentifier: devicePushIdentifier,

0 commit comments

Comments
 (0)