@lynx-js/lynx-runtime 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +354 -2
  3. package/dist/bts/data.d.ts +1 -0
  4. package/dist/bts/data.js +28 -0
  5. package/dist/bts/events.d.ts +1 -0
  6. package/dist/bts/events.js +57 -0
  7. package/dist/bts/scope.d.ts +1 -0
  8. package/dist/bts/scope.js +129 -0
  9. package/dist/bts/services.d.ts +1 -0
  10. package/dist/bts/services.js +34 -0
  11. package/dist/bts.d.ts +14 -0
  12. package/dist/bts.js +30 -0
  13. package/dist/common/host-types.d.ts +1 -0
  14. package/dist/common/host-types.js +1 -0
  15. package/dist/common/messages.d.ts +1 -0
  16. package/dist/common/messages.js +69 -0
  17. package/dist/common/performance.d.ts +1 -0
  18. package/dist/common/performance.js +26 -0
  19. package/dist/common/protocol.d.ts +20 -0
  20. package/dist/common/protocol.js +53 -0
  21. package/dist/common/types.d.ts +203 -0
  22. package/dist/common/types.js +1 -0
  23. package/dist/iife/bts.js +1 -0
  24. package/dist/iife/mts.js +1 -0
  25. package/dist/index.d.ts +16 -0
  26. package/dist/index.js +1 -0
  27. package/dist/mts/data.d.ts +71 -0
  28. package/dist/mts/data.js +46 -0
  29. package/dist/mts/flush.d.ts +11 -0
  30. package/dist/mts/flush.js +16 -0
  31. package/dist/mts/host.d.ts +35 -0
  32. package/dist/mts/host.js +124 -0
  33. package/dist/mts/services.d.ts +21 -0
  34. package/dist/mts/services.js +46 -0
  35. package/dist/mts.d.ts +5 -0
  36. package/dist/mts.js +4 -0
  37. package/package.json +54 -7
  38. package/skills/lynx-runtime/SKILL.md +67 -0
  39. package/src/bts/data.ts +94 -0
  40. package/src/bts/events.ts +100 -0
  41. package/src/bts/scope.ts +279 -0
  42. package/src/bts/services.ts +88 -0
  43. package/src/bts.ts +110 -0
  44. package/src/common/host-types.ts +161 -0
  45. package/src/common/messages.ts +129 -0
  46. package/src/common/performance.ts +65 -0
  47. package/src/common/protocol.ts +98 -0
  48. package/src/common/types.ts +259 -0
  49. package/src/index.ts +35 -0
  50. package/src/mts/data.ts +200 -0
  51. package/src/mts/flush.ts +50 -0
  52. package/src/mts/host.ts +297 -0
  53. package/src/mts/services.ts +102 -0
  54. package/src/mts.ts +36 -0
@@ -0,0 +1,100 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { assertScopeAlive, getScopeLynx } from './scope.js';
6
+ import type { BTSEventEntry, BTSPageScope } from './scope.js';
7
+ import type { HostGlobalEventEmitter } from '../common/host-types.js';
8
+ import { subscribeMultiplexed } from '../common/messages.js';
9
+ import type { BTSEventHandler, Unsubscribe } from '../common/types.js';
10
+
11
+ function createEventEntry(): BTSEventEntry {
12
+ const dispatch: BTSEventHandler = (handlerName, data, componentId) => {
13
+ const handler = entry.current?.handler;
14
+ return handler?.(handlerName, data, componentId);
15
+ };
16
+ const entry: BTSEventEntry = {
17
+ current: undefined,
18
+ publishEvent: (handlerName, data) => dispatch(handlerName, data),
19
+ publicComponentEvent: (componentId, handlerName, data) =>
20
+ dispatch(handlerName, data, componentId),
21
+ };
22
+ return entry;
23
+ }
24
+
25
+ /** @internal */
26
+ export function scopeSetEventHandler<T>(
27
+ scope: BTSPageScope,
28
+ handler: BTSEventHandler<T>,
29
+ ): Unsubscribe {
30
+ assertScopeAlive(scope);
31
+ let entry = scope.eventEntry;
32
+ if (!entry) {
33
+ entry = scope.eventEntry = createEventEntry();
34
+ // Both host slots share one owner; never chain an unmigrated DSL handler.
35
+ scope.app.publishEvent = entry.publishEvent;
36
+ scope.app.publicComponentEvent = entry.publicComponentEvent;
37
+ }
38
+ const registration = { handler: handler as BTSEventHandler };
39
+ entry.current = registration;
40
+ const currentEntry = entry;
41
+ return () => {
42
+ if (currentEntry.current === registration) {
43
+ currentEntry.current = undefined;
44
+ }
45
+ };
46
+ }
47
+
48
+ function getGlobalEventEmitter(scope: BTSPageScope): HostGlobalEventEmitter {
49
+ let emitter = (scope.app.getJSModule?.('GlobalEventEmitter') as
50
+ | HostGlobalEventEmitter
51
+ | null
52
+ | undefined) ?? scope.app.GlobalEventEmitter;
53
+ if (!emitter) {
54
+ const scopeLynx = getScopeLynx(scope);
55
+ if (scopeLynx && (!scopeLynx.getApp || scopeLynx.getApp() === scope.app)) {
56
+ emitter = scopeLynx.getJSModule?.('GlobalEventEmitter') as
57
+ | HostGlobalEventEmitter
58
+ | null
59
+ | undefined;
60
+ }
61
+ }
62
+ if (!emitter) {
63
+ throw new Error(
64
+ '[@lynx-js/lynx-runtime/bts] Global event subscription requires GlobalEventEmitter. Ensure the BTS host environment is initialized before calling onGlobalEvent().',
65
+ );
66
+ }
67
+ return emitter;
68
+ }
69
+
70
+ /**
71
+ * Share one host listener per event name on the bound page's GlobalEventEmitter so
72
+ * disposing one subscriber does not disable shared host detectors (such as
73
+ * `keyboardstatuschanged`) or mutate lynx-core's array during `forEach` dispatch.
74
+ * @internal
75
+ */
76
+ export function scopeOnGlobalEvent<Args extends unknown[]>(
77
+ scope: BTSPageScope,
78
+ name: string,
79
+ listener: (this: void, ...args: Args) => void,
80
+ ): Unsubscribe {
81
+ assertScopeAlive(scope);
82
+ scope.globalEvents ??= new Map();
83
+ return subscribeMultiplexed(
84
+ scope.globalEvents,
85
+ name,
86
+ listener as (...args: unknown[]) => void,
87
+ (dispatch) => {
88
+ const emitter = getGlobalEventEmitter(scope);
89
+ const onEvent = (...args: unknown[]): void => {
90
+ dispatch((fn) => {
91
+ fn(...args);
92
+ });
93
+ };
94
+ emitter.addListener(name, onEvent);
95
+ return () => {
96
+ emitter.removeListener(name, onEvent);
97
+ };
98
+ },
99
+ );
100
+ }
@@ -0,0 +1,279 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import type {
6
+ HostBTSApp,
7
+ HostGlobalScope,
8
+ HostLynxObject,
9
+ } from '../common/host-types.js';
10
+ import { clearMultiplexed } from '../common/messages.js';
11
+ import type { MessageSubscriptions, MultiplexMap } from '../common/messages.js';
12
+ import {
13
+ copyHostData,
14
+ invokeAll,
15
+ subscribeListener,
16
+ } from '../common/protocol.js';
17
+ import type {
18
+ BTSEventHandler,
19
+ BTSPageRuntime,
20
+ Unsubscribe,
21
+ } from '../common/types.js';
22
+
23
+ declare const lynx: HostLynxObject | undefined;
24
+
25
+ /** @internal */
26
+ export type AnySlotFn = (...args: unknown[]) => void;
27
+
28
+ /** @internal */
29
+ export type BTSSlotListeners = Map<
30
+ object,
31
+ { invoke: AnySlotFn; dispose: Unsubscribe }
32
+ >;
33
+
34
+ /** @internal */
35
+ export interface BTSEventEntry {
36
+ current: { handler: BTSEventHandler } | undefined;
37
+ publishEvent: NonNullable<HostBTSApp['publishEvent']>;
38
+ publicComponentEvent: NonNullable<HostBTSApp['publicComponentEvent']>;
39
+ }
40
+
41
+ /** @internal */
42
+ export interface BTSPageScope {
43
+ appId: string;
44
+ app: HostBTSApp;
45
+ lynx: HostLynxObject | undefined;
46
+ rawInitData: Record<string, unknown> | undefined;
47
+ initData: Record<string, unknown>;
48
+ globalProps: Record<string, unknown>;
49
+ destroyed: boolean;
50
+ runtime?: BTSPageRuntime | undefined;
51
+ eventEntry?: BTSEventEntry | undefined;
52
+ globalEvents?: MultiplexMap<(...args: unknown[]) => void> | undefined;
53
+ messages?: MessageSubscriptions | undefined;
54
+ slots: Map<string, BTSSlotListeners>;
55
+ destroyListeners: Map<() => void, Unsubscribe>;
56
+ }
57
+
58
+ const btsScopeMap = new WeakMap<HostBTSApp, BTSPageScope>();
59
+
60
+ /** @internal */
61
+ export function assertScopeAlive(scope: BTSPageScope): void {
62
+ if (scope.destroyed) {
63
+ throw new Error(
64
+ '[@lynx-js/lynx-runtime/bts] Cannot register or activate a capability on a destroying or destroyed page. Check page.isDestroyed() before registering in async callbacks.',
65
+ );
66
+ }
67
+ }
68
+
69
+ /** @internal */
70
+ export function resolveHostLynx(
71
+ app: HostBTSApp,
72
+ host: HostGlobalScope,
73
+ ): HostLynxObject | undefined {
74
+ const hostLynx = host.lynx;
75
+ if (
76
+ hostLynx
77
+ && (hostLynx.getApp?.() === app
78
+ || (!hostLynx.getApp && !host.multiApps && host.tt === app))
79
+ ) {
80
+ return hostLynx;
81
+ }
82
+ return undefined;
83
+ }
84
+
85
+ /** @internal */
86
+ export function getScopeLynx(scope: BTSPageScope): HostLynxObject | undefined {
87
+ return (
88
+ scope.app.lynx
89
+ ?? scope.lynx
90
+ ?? resolveHostLynx(scope.app, globalThis as unknown as HostGlobalScope)
91
+ );
92
+ }
93
+
94
+ function mergeBTSInitData(
95
+ rawInitData: Record<string, unknown> | undefined,
96
+ dynamicData: Record<string, unknown> | null | undefined,
97
+ ): Record<string, unknown> {
98
+ return {
99
+ ...rawInitData,
100
+ ...(dynamicData ? copyHostData(dynamicData) : undefined),
101
+ };
102
+ }
103
+
104
+ /** @internal */
105
+ export function ensureBTSSlot(
106
+ scope: BTSPageScope,
107
+ key: string,
108
+ before?: AnySlotFn,
109
+ ): BTSSlotListeners {
110
+ let listeners = scope.slots.get(key);
111
+ if (listeners) return listeners;
112
+
113
+ const appRecord = scope.app as Record<string, AnySlotFn | undefined>;
114
+ const prev = appRecord[key];
115
+ listeners = new Map();
116
+ const currentListeners = listeners;
117
+ appRecord[key] = function(this: unknown, ...args: unknown[]): void {
118
+ if (!scope.destroyed) {
119
+ before?.(...args);
120
+ }
121
+ prev?.apply(this, args);
122
+ if (scope.destroyed) return;
123
+ for (const [identity, reg] of [...currentListeners]) {
124
+ if (currentListeners.get(identity) === reg) {
125
+ reg.invoke(...args);
126
+ }
127
+ }
128
+ };
129
+ scope.slots.set(key, listeners);
130
+ return listeners;
131
+ }
132
+
133
+ /** @internal */
134
+ export function subscribeBTSSlot<Args extends unknown[]>(
135
+ scope: BTSPageScope,
136
+ key: string,
137
+ invoke: (...args: Args) => void,
138
+ identity: object = invoke,
139
+ ): Unsubscribe {
140
+ assertScopeAlive(scope);
141
+ const listeners = ensureBTSSlot(scope, key);
142
+ const existing = listeners.get(identity);
143
+ if (existing) return existing.dispose;
144
+
145
+ const dispose = (): void => {
146
+ if (listeners.get(identity)?.dispose === dispose) {
147
+ listeners.delete(identity);
148
+ }
149
+ };
150
+ listeners.set(identity, { invoke: invoke as AnySlotFn, dispose });
151
+ return dispose;
152
+ }
153
+
154
+ /** @internal */
155
+ export function scopeOnDestroy(
156
+ scope: BTSPageScope,
157
+ listener: () => void,
158
+ ): Unsubscribe {
159
+ assertScopeAlive(scope);
160
+ return subscribeListener(scope.destroyListeners, listener);
161
+ }
162
+
163
+ function createBTSPageScope(
164
+ targetApp: HostBTSApp,
165
+ host: HostGlobalScope,
166
+ lexicalLynx: HostLynxObject | undefined,
167
+ ): BTSPageScope {
168
+ const existing = btsScopeMap.get(targetApp);
169
+ if (existing) {
170
+ return existing;
171
+ }
172
+
173
+ let boundLynx = targetApp.lynx;
174
+ if (
175
+ !boundLynx
176
+ && lexicalLynx
177
+ && (!lexicalLynx.getApp || lexicalLynx.getApp() === targetApp)
178
+ ) {
179
+ boundLynx = lexicalLynx;
180
+ }
181
+ boundLynx ??= resolveHostLynx(targetApp, host);
182
+
183
+ const params = targetApp._params;
184
+ const rawInitData = params?.initData
185
+ ? copyHostData(params.initData)
186
+ : undefined;
187
+ const initData = mergeBTSInitData(rawInitData, params?.updateData);
188
+
189
+ const scope: BTSPageScope = {
190
+ appId: params?.appGUID ?? targetApp.nativeAppId ?? '',
191
+ app: targetApp,
192
+ lynx: boundLynx,
193
+ rawInitData,
194
+ initData,
195
+ globalProps: boundLynx?.__globalProps ?? {},
196
+ destroyed: false,
197
+ slots: new Map(),
198
+ destroyListeners: new Map(),
199
+ };
200
+
201
+ ensureBTSSlot(scope, 'onAppReload', (newData) => {
202
+ scope.initData = mergeBTSInitData(
203
+ scope.rawInitData,
204
+ newData as Record<string, unknown> | undefined,
205
+ );
206
+ });
207
+ ensureBTSSlot(scope, 'updateGlobalProps', (newData) => {
208
+ scope.globalProps = (newData as Record<string, unknown> | null | undefined)
209
+ ?? {};
210
+ });
211
+
212
+ const prevDestroy = targetApp.callDestroyLifetimeFun;
213
+ targetApp.callDestroyLifetimeFun = function(this: unknown): void {
214
+ if (scope.destroyed) {
215
+ prevDestroy?.call(this);
216
+ return;
217
+ }
218
+ scope.destroyed = true;
219
+ if (scope.globalEvents) {
220
+ clearMultiplexed(scope.globalEvents);
221
+ scope.globalEvents = undefined;
222
+ }
223
+ if (scope.messages) {
224
+ clearMultiplexed(scope.messages);
225
+ scope.messages = undefined;
226
+ }
227
+ if (scope.eventEntry) {
228
+ scope.eventEntry.current = undefined;
229
+ scope.eventEntry = undefined;
230
+ }
231
+ try {
232
+ invokeAll([
233
+ ...(prevDestroy ? [() => prevDestroy.call(this)] : []),
234
+ ...[...scope.destroyListeners].map(([listener, reg]) => () => {
235
+ if (scope.destroyListeners.get(listener) === reg) {
236
+ listener();
237
+ }
238
+ }),
239
+ ]);
240
+ } finally {
241
+ scope.destroyListeners.clear();
242
+ for (const listeners of scope.slots.values()) {
243
+ listeners.clear();
244
+ }
245
+ scope.slots.clear();
246
+ }
247
+ };
248
+
249
+ btsScopeMap.set(targetApp, scope);
250
+ return scope;
251
+ }
252
+
253
+ function resolveOptionalHostApp(
254
+ host: HostGlobalScope,
255
+ lexicalLynx: HostLynxObject | undefined,
256
+ ): HostBTSApp | undefined {
257
+ const currentMultiApp = host.currentAppId && host.multiApps
258
+ ? host.multiApps[host.currentAppId]
259
+ : undefined;
260
+ return (
261
+ lexicalLynx?.getApp?.()
262
+ ?? host.lynx?.getApp?.()
263
+ ?? currentMultiApp
264
+ ?? host.tt
265
+ );
266
+ }
267
+
268
+ /** @internal */
269
+ export function resolveCurrentHostScope(): BTSPageScope {
270
+ const host = globalThis as unknown as HostGlobalScope;
271
+ const lexicalLynx = typeof lynx === 'undefined' ? undefined : lynx;
272
+ const resolved = resolveOptionalHostApp(host, lexicalLynx);
273
+ if (!resolved) {
274
+ throw new Error(
275
+ '[@lynx-js/lynx-runtime/bts] Unable to resolve BTS App instance (`tt`). Call bindPage() synchronously during BTS page bootstrap and retain the returned handle.',
276
+ );
277
+ }
278
+ return createBTSPageScope(resolved, host, lexicalLynx);
279
+ }
@@ -0,0 +1,88 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { assertScopeAlive, getScopeLynx } from './scope.js';
6
+ import type { BTSPageScope } from './scope.js';
7
+ import type {
8
+ HostContextProxy,
9
+ HostGlobalScope,
10
+ } from '../common/host-types.js';
11
+ import { subscribeMessage } from '../common/messages.js';
12
+ import { createHostPipeline, markHostTiming } from '../common/performance.js';
13
+ import type {
14
+ CreatePipelineOptions,
15
+ CreatedPipelineOptions,
16
+ LynxSystemInfo,
17
+ PipelineOptions,
18
+ Unsubscribe,
19
+ } from '../common/types.js';
20
+
21
+ /** @internal */
22
+ export function scopeGetSystemInfo(
23
+ scope: BTSPageScope,
24
+ ): Readonly<LynxSystemInfo> {
25
+ const host = globalThis as unknown as HostGlobalScope;
26
+ return (
27
+ getScopeLynx(scope)?.SystemInfo
28
+ ?? host.SystemInfo
29
+ ?? host.lynx?.SystemInfo
30
+ ?? {}
31
+ );
32
+ }
33
+
34
+ /** @internal */
35
+ export function scopeCreatePipeline(
36
+ scope: BTSPageScope,
37
+ options?: CreatePipelineOptions,
38
+ ): CreatedPipelineOptions | undefined {
39
+ if (scope.destroyed) return undefined;
40
+ return createHostPipeline(
41
+ getScopeLynx(scope)?.performance,
42
+ options,
43
+ 'bts',
44
+ scopeGetSystemInfo(scope).lynxSdkVersion,
45
+ );
46
+ }
47
+
48
+ /** @internal */
49
+ export function scopeMarkTiming(
50
+ scope: BTSPageScope,
51
+ pipeline: string | PipelineOptions | undefined,
52
+ timingKey: string,
53
+ ): void {
54
+ if (scope.destroyed) return;
55
+ markHostTiming(getScopeLynx(scope)?.performance, pipeline, timingKey);
56
+ }
57
+
58
+ function getCoreContext(scope: BTSPageScope): HostContextProxy {
59
+ const proxy = getScopeLynx(scope)?.getCoreContext?.();
60
+ if (!proxy) {
61
+ throw new Error(
62
+ '[@lynx-js/lynx-runtime/bts] Message communication requires lynx.getCoreContext(). Ensure the BTS host context is initialized before calling sendMessage() or onMessage().',
63
+ );
64
+ }
65
+ return proxy;
66
+ }
67
+
68
+ /** @internal */
69
+ export function scopeSendMessage<T>(
70
+ scope: BTSPageScope,
71
+ type: string,
72
+ data?: T,
73
+ ): void {
74
+ assertScopeAlive(scope);
75
+ getCoreContext(scope).dispatchEvent({ type, data });
76
+ }
77
+
78
+ /** @internal */
79
+ export function scopeOnMessage<T>(
80
+ scope: BTSPageScope,
81
+ type: string,
82
+ listener: (this: void, data: T) => void,
83
+ ): Unsubscribe {
84
+ assertScopeAlive(scope);
85
+ const proxy = getCoreContext(scope);
86
+ scope.messages ??= new Map();
87
+ return subscribeMessage(scope.messages, proxy, type, listener);
88
+ }
package/src/bts.ts ADDED
@@ -0,0 +1,110 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import {
6
+ scopeOnConfigChanged,
7
+ scopeOnGlobalPropsChanged,
8
+ scopeOnReload,
9
+ scopeOnUpdateData,
10
+ } from './bts/data.js';
11
+ import { scopeOnGlobalEvent, scopeSetEventHandler } from './bts/events.js';
12
+ import {
13
+ resolveCurrentHostScope,
14
+ scopeOnDestroy,
15
+ subscribeBTSSlot,
16
+ } from './bts/scope.js';
17
+ import {
18
+ scopeCreatePipeline,
19
+ scopeGetSystemInfo,
20
+ scopeMarkTiming,
21
+ scopeOnMessage,
22
+ scopeSendMessage,
23
+ } from './bts/services.js';
24
+ import { copyHostData } from './common/protocol.js';
25
+ import type {
26
+ BTSEventHandler,
27
+ BTSPageRuntime,
28
+ BTSReloadOptions,
29
+ BTSUpdateOptions,
30
+ CreatePipelineOptions,
31
+ CreatedPipelineOptions,
32
+ LynxSystemInfo,
33
+ PipelineOptions,
34
+ Unsubscribe,
35
+ } from './common/types.js';
36
+
37
+ export type {
38
+ BTSEventHandler,
39
+ BTSPageRuntime,
40
+ BTSReloadOptions,
41
+ BTSUpdateOptions,
42
+ CreatedPipelineOptions,
43
+ CreatePipelineOptions,
44
+ LynxSystemInfo,
45
+ PipelineOptions,
46
+ Unsubscribe,
47
+ };
48
+
49
+ /**
50
+ * Bind the BTS runtime to the page currently bootstrapping in the host environment
51
+ * and return a page-scoped runtime handle isolated from subsequent `LynxGroup` page loads.
52
+ * Call `bindPage()` synchronously during page bootstrap and retain the returned
53
+ * `BTSPageRuntime` handle for all subsequent asynchronous work on that page.
54
+ * Repeated binding to the same App returns the same handle, including its destruction
55
+ * state. Binding captures the initial baseline and `GlobalProps`, and tracks
56
+ * reload, `GlobalProps` updates, and destruction; other channels attach lazily on use.
57
+ * Binding alone does not claim the raw event handler.
58
+ * Throws when the host provides no BTS App; it does not create a page.
59
+ */
60
+ export function bindPage(): BTSPageRuntime {
61
+ const scope = resolveCurrentHostScope();
62
+ scope.runtime ??= {
63
+ isDestroyed: () => scope.destroyed,
64
+ getSystemInfo: () => scopeGetSystemInfo(scope),
65
+ createPipeline: (options) => scopeCreatePipeline(scope, options),
66
+ markTiming: (pipeline, timingKey) =>
67
+ scopeMarkTiming(scope, pipeline, timingKey),
68
+ getId: () => scope.appId,
69
+ getInitData: <T = Record<string, unknown>>() =>
70
+ copyHostData(scope.initData) as T,
71
+ getGlobalProps: <T = Record<string, unknown>>() =>
72
+ scope.globalProps as Readonly<T>,
73
+ onGlobalPropsChanged: <T = Record<string, unknown>>(
74
+ listener: (globalProps: Readonly<T>) => void,
75
+ ) => scopeOnGlobalPropsChanged<T>(scope, listener),
76
+ onUpdateData: <T = Record<string, unknown>>(
77
+ listener: (
78
+ data: Readonly<T>,
79
+ options: Readonly<BTSUpdateOptions>,
80
+ ) => void,
81
+ ) => scopeOnUpdateData<T>(scope, listener),
82
+ onReload: <T = Record<string, unknown>>(
83
+ listener: (
84
+ data: Readonly<T>,
85
+ options: Readonly<BTSReloadOptions>,
86
+ ) => void,
87
+ ) => scopeOnReload<T>(scope, listener),
88
+ onConfigChanged: <T = Record<string, unknown>>(
89
+ listener: (config: Readonly<T>) => void,
90
+ ) => scopeOnConfigChanged<T>(scope, listener),
91
+ onEnterForeground: (listener: () => void) =>
92
+ subscribeBTSSlot(scope, 'onAppEnterForeground', listener),
93
+ onEnterBackground: (listener: () => void) =>
94
+ subscribeBTSSlot(scope, 'onAppEnterBackground', listener),
95
+ onGlobalEvent: <Args extends unknown[] = unknown[]>(
96
+ name: string,
97
+ listener: (this: void, ...args: Args) => void,
98
+ ) => scopeOnGlobalEvent(scope, name, listener),
99
+ setEventHandler: <T = unknown>(handler: BTSEventHandler<T>) =>
100
+ scopeSetEventHandler(scope, handler),
101
+ sendMessage: (type: string, data?: unknown) =>
102
+ scopeSendMessage(scope, type, data),
103
+ onMessage: <T = unknown>(
104
+ type: string,
105
+ listener: (this: void, data: T) => void,
106
+ ) => scopeOnMessage(scope, type, listener),
107
+ onDestroy: (listener: () => void) => scopeOnDestroy(scope, listener),
108
+ };
109
+ return scope.runtime;
110
+ }