@lingxia/bridge 0.18.0 → 0.19.0
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.
- package/dist/bridge-runtime.es2020.js +1 -1
- package/dist/bridge-runtime.es5.js +1 -1
- package/dist/error.d.ts +7 -0
- package/dist/es2020/bridge.js +46 -6
- package/dist/es2020/error.js +48 -7
- package/dist/es2020/host.js +53 -0
- package/dist/es2020/index.js +2 -2
- package/dist/es2020/runtime-env.js +34 -2
- package/dist/es2020/surface-context.js +86 -0
- package/dist/es5/bridge.js +58 -6
- package/dist/es5/error.js +45 -7
- package/dist/es5/host.js +53 -0
- package/dist/es5/index.js +2 -2
- package/dist/es5/runtime-env.js +38 -5
- package/dist/es5/surface-context.js +135 -0
- package/dist/host.d.ts +33 -0
- package/dist/index.d.ts +2 -2
- package/dist/invocation.d.ts +12 -1
- package/dist/runtime-env.d.ts +2 -0
- package/dist/surface-context.d.ts +27 -0
- package/dist/types.d.ts +17 -4
- package/package.json +2 -2
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
+
if (!m) return o;
|
|
4
|
+
var i = m.call(o), r, ar = [], e;
|
|
5
|
+
try {
|
|
6
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
+
}
|
|
8
|
+
catch (error) { e = { error: error }; }
|
|
9
|
+
finally {
|
|
10
|
+
try {
|
|
11
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
+
}
|
|
13
|
+
finally { if (e) throw e.error; }
|
|
14
|
+
}
|
|
15
|
+
return ar;
|
|
16
|
+
};
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
+
};
|
|
26
|
+
var __values = (this && this.__values) || function(o) {
|
|
27
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
28
|
+
if (m) return m.call(o);
|
|
29
|
+
if (o && typeof o.length === "number") return {
|
|
30
|
+
next: function () {
|
|
31
|
+
if (o && i >= o.length) o = void 0;
|
|
32
|
+
return { value: o && o[i++], done: !o };
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
36
|
+
};
|
|
37
|
+
/** What a host that sends nothing — one older than this API — is taken to say. */
|
|
38
|
+
var UNREPORTED = { sizeClass: 'compact', width: 0, height: 0, aside: false };
|
|
39
|
+
/**
|
|
40
|
+
* One store per document, on `window`, for the same reason as the display
|
|
41
|
+
* language: the bridge module is present twice in a page, and the host must
|
|
42
|
+
* reach the copy the framework hooks read.
|
|
43
|
+
*/
|
|
44
|
+
function store() {
|
|
45
|
+
var _a;
|
|
46
|
+
if (typeof window === 'undefined')
|
|
47
|
+
return fallbackStore;
|
|
48
|
+
if (!window.__lxSurfaceContext) {
|
|
49
|
+
// Seeded by the host in this document's bridge config, before any script.
|
|
50
|
+
var config = window.__LX_BRIDGE_CFG;
|
|
51
|
+
window.__lxSurfaceContext = {
|
|
52
|
+
value: (_a = parse(config === null || config === void 0 ? void 0 : config.surfaceContext)) !== null && _a !== void 0 ? _a : UNREPORTED,
|
|
53
|
+
revision: typeof (config === null || config === void 0 ? void 0 : config.surfaceContextRevision) === 'number' ? config.surfaceContextRevision : 0,
|
|
54
|
+
listeners: new Set(),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return window.__lxSurfaceContext;
|
|
58
|
+
}
|
|
59
|
+
var fallbackStore = { value: UNREPORTED, revision: 0, listeners: new Set() };
|
|
60
|
+
function parse(next) {
|
|
61
|
+
if (typeof next !== 'object' || next === null)
|
|
62
|
+
return null;
|
|
63
|
+
var _a = next, sizeClass = _a.sizeClass, width = _a.width, height = _a.height, aside = _a.aside;
|
|
64
|
+
if (sizeClass !== 'compact' && sizeClass !== 'regular')
|
|
65
|
+
return null;
|
|
66
|
+
if (typeof width !== 'number' || typeof height !== 'number')
|
|
67
|
+
return null;
|
|
68
|
+
return { sizeClass: sizeClass, width: width, height: height, aside: aside === true };
|
|
69
|
+
}
|
|
70
|
+
function same(a, b) {
|
|
71
|
+
return a.sizeClass === b.sizeClass
|
|
72
|
+
&& a.width === b.width
|
|
73
|
+
&& a.height === b.height
|
|
74
|
+
&& a.aside === b.aside;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Host entry point: once the page's bridge is up, and on every change. Pushes
|
|
78
|
+
* can run out of order across host threads, so each carries a revision and an
|
|
79
|
+
* older one than the store holds is dropped.
|
|
80
|
+
*/
|
|
81
|
+
function applySurfaceContext(next, revision) {
|
|
82
|
+
var e_1, _a;
|
|
83
|
+
var context = parse(next);
|
|
84
|
+
var current = store();
|
|
85
|
+
if (!context)
|
|
86
|
+
return;
|
|
87
|
+
if (typeof revision === 'number') {
|
|
88
|
+
if (revision <= current.revision)
|
|
89
|
+
return;
|
|
90
|
+
current.revision = revision;
|
|
91
|
+
}
|
|
92
|
+
if (same(current.value, context))
|
|
93
|
+
return;
|
|
94
|
+
// A new object per change, so `useSyncExternalStore` sees it changed.
|
|
95
|
+
current.value = context;
|
|
96
|
+
try {
|
|
97
|
+
for (var _b = __values(__spreadArray([], __read(current.listeners), false)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
98
|
+
var listener = _c.value;
|
|
99
|
+
listener();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
103
|
+
finally {
|
|
104
|
+
try {
|
|
105
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
106
|
+
}
|
|
107
|
+
finally { if (e_1) throw e_1.error; }
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (typeof window !== 'undefined' && !window.__lingxiaApplySurfaceContext) {
|
|
111
|
+
Object.defineProperty(window, '__lingxiaApplySurfaceContext', {
|
|
112
|
+
configurable: false,
|
|
113
|
+
enumerable: false,
|
|
114
|
+
value: applySurfaceContext,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The lxapp's adaptive context. The host seeds it into every page before any
|
|
119
|
+
* script runs and pushes each change, so there is always a value; a host older
|
|
120
|
+
* than this API reports `compact` with a 0×0 viewport.
|
|
121
|
+
*/
|
|
122
|
+
export function getSurfaceContext() {
|
|
123
|
+
return store().value;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Follow the adaptive context. Change-only, like `subscribeDisplayLanguage`,
|
|
127
|
+
* so it composes with `useSyncExternalStore`. Returns an unsubscribe.
|
|
128
|
+
*/
|
|
129
|
+
export function subscribeSurfaceContext(listener) {
|
|
130
|
+
var listeners = store().listeners;
|
|
131
|
+
listeners.add(listener);
|
|
132
|
+
return function () {
|
|
133
|
+
listeners.delete(listener);
|
|
134
|
+
};
|
|
135
|
+
}
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type PlatformOS } from './runtime-env';
|
|
2
|
+
/**
|
|
3
|
+
* What the host decided for this page: the facts a View picks its layout and
|
|
4
|
+
* copy from. Every field changes rarely or never, so reading them costs a
|
|
5
|
+
* re-render only when one of them actually changes — never while a window is
|
|
6
|
+
* dragged. Measured geometry belongs to CSS (`--lx-page-chrome-*`, container
|
|
7
|
+
* queries), not here.
|
|
8
|
+
*/
|
|
9
|
+
export interface LxHost {
|
|
10
|
+
/** `compact` (< 600 logical px) or `regular`, with hysteresis; the same value Logic's `lx.surface.watchContext()` reports. */
|
|
11
|
+
readonly sizeClass: 'compact' | 'regular';
|
|
12
|
+
/** Whether the host layout currently offers a docked aside. */
|
|
13
|
+
readonly aside: boolean;
|
|
14
|
+
/** The BCP-47 tag in effect, as Logic's `lx.host.displayLanguage.get()`. */
|
|
15
|
+
readonly displayLanguage: string;
|
|
16
|
+
/** Which kind of machine this is; fixed for the document. A narrowed desktop window is still `desktop`. */
|
|
17
|
+
readonly formFactor: 'mobile' | 'desktop';
|
|
18
|
+
/** The operating system, for the rare OS-specific feature; not a layout fact. */
|
|
19
|
+
readonly os: PlatformOS;
|
|
20
|
+
/** Whether the page runs in the LingXia Runner (the `lingxia dev` simulator). */
|
|
21
|
+
readonly runner: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The host facts for this page. The same object is returned until a field
|
|
25
|
+
* changes, so it composes with `useSyncExternalStore` and with memoization.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getHost(): LxHost;
|
|
28
|
+
/**
|
|
29
|
+
* Follow the host facts. Change-only: the listener runs when a field of
|
|
30
|
+
* `getHost()` changes — not on subscribe, and not when only the viewport's
|
|
31
|
+
* size moves within its size class. Returns an unsubscribe.
|
|
32
|
+
*/
|
|
33
|
+
export declare function subscribeHost(listener: () => void): () => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
export type { BridgeConfig, CallOptions, ChannelCloseEvent, ChannelOptions, ChannelOpenOptions, BridgeErrorCode, ChannelEventName, DataSubscriber, ErrorInfo, LingXiaBridgeInterface, LxChannel, LxBridgeError, LxMethod, LxMethodParams, LxMethodResult, LxMethodStreamData, LxStream, NativeComponentMessage, NativeChannel, NativeError, NativeStream, NotifyOptions, InvokeOptions, StateInfo, StreamCallOptions, StreamOptions, StreamEventName, } from './types';
|
|
9
9
|
export { LingXiaBridge, channel, initBridge, invoke, notify, stream, } from './bridge';
|
|
10
10
|
export { isNativeError } from './invocation';
|
|
11
|
-
export {
|
|
12
|
-
export { renderErrorUI, hasError, getErrorInfo } from './error';
|
|
11
|
+
export { getHost, subscribeHost, type LxHost } from './host';
|
|
12
|
+
export { renderErrorUI, renderPageFault, hasError, getErrorInfo } from './error';
|
|
13
13
|
export { boot, bootWhenReady } from './boot';
|
package/dist/invocation.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { LxBridgeError, LxChannel, LxStream } from "./types";
|
|
2
2
|
export type ParamsSource<T> = T | (() => T);
|
|
3
|
-
|
|
3
|
+
/** The one payload a method takes: `undefined` for none, `P | undefined` when optional. */
|
|
4
|
+
export type MethodParams<TMethod> = TMethod extends (...args: infer A) => any ? A extends [] ? undefined : A extends [infer P] ? P : A extends [(infer P)?] ? P | undefined : never : never;
|
|
5
|
+
/** Whether calling `TMethod` needs its payload. */
|
|
6
|
+
export type RequiresParams<TMethod> = TMethod extends (...args: infer A) => any ? [] extends A ? false : true : never;
|
|
7
|
+
/** `params`, required exactly when the method requires its payload. */
|
|
8
|
+
export type ParamsOption<TMethod, TSource> = RequiresParams<TMethod> extends true ? {
|
|
9
|
+
params: TSource;
|
|
10
|
+
} : {
|
|
11
|
+
params?: TSource;
|
|
12
|
+
};
|
|
13
|
+
/** A hook's options argument: required when `params` is. */
|
|
14
|
+
export type OptionsArg<TMethod, TOptions> = RequiresParams<TMethod> extends true ? [options: TOptions] : [options?: TOptions];
|
|
4
15
|
export type StreamData<TMethod> = TMethod extends (...args: any[]) => LxStream<infer TData, any> ? TData : never;
|
|
5
16
|
export type StreamResult<TMethod> = TMethod extends (...args: any[]) => LxStream<any, infer TResult> ? TResult : never;
|
|
6
17
|
export type ChannelIn<TMethod> = TMethod extends (...args: any[]) => Promise<LxChannel<infer TIn, any>> ? TIn : never;
|
package/dist/runtime-env.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type { BridgeConfig } from './types';
|
|
|
2
2
|
export type CommunicationMethod = 'messageport' | 'jsinterface' | 'webmessage' | 'webkit' | 'unknown';
|
|
3
3
|
export type PlatformOS = NonNullable<BridgeConfig['os']> | 'unknown';
|
|
4
4
|
export declare const BRIDGE_CONFIG: BridgeConfig;
|
|
5
|
+
/** The writing direction of a BCP-47 tag. */
|
|
6
|
+
export declare function textDirection(tag: string): 'ltr' | 'rtl';
|
|
5
7
|
export declare function getDisplayLanguage(): string;
|
|
6
8
|
/**
|
|
7
9
|
* Subscribe to host display-language changes. Returns an unsubscribe.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lxapp's adaptive context, from the host: the same value Logic's
|
|
3
|
+
* `lx.surface.watchContext()` delivers, so a View picks its layout without
|
|
4
|
+
* Logic forwarding it through `setData`. It describes the lxapp's main
|
|
5
|
+
* presentation; a page shown in an aside, float or window sees the same value.
|
|
6
|
+
*/
|
|
7
|
+
export interface SurfaceContext {
|
|
8
|
+
/** `compact` (< 600 logical px) or `regular`, with hysteresis. */
|
|
9
|
+
sizeClass: 'compact' | 'regular';
|
|
10
|
+
/** The presentation's viewport width in logical pixels; 0 until measured. */
|
|
11
|
+
width: number;
|
|
12
|
+
/** The presentation's viewport height in logical pixels; 0 until measured. */
|
|
13
|
+
height: number;
|
|
14
|
+
/** Whether the host layout currently offers a docked aside. */
|
|
15
|
+
aside: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The lxapp's adaptive context. The host seeds it into every page before any
|
|
19
|
+
* script runs and pushes each change, so there is always a value; a host older
|
|
20
|
+
* than this API reports `compact` with a 0×0 viewport.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getSurfaceContext(): SurfaceContext;
|
|
23
|
+
/**
|
|
24
|
+
* Follow the adaptive context. Change-only, like `subscribeDisplayLanguage`,
|
|
25
|
+
* so it composes with `useSyncExternalStore`. Returns an unsubscribe.
|
|
26
|
+
*/
|
|
27
|
+
export declare function subscribeSurfaceContext(listener: () => void): () => void;
|
package/dist/types.d.ts
CHANGED
|
@@ -8,6 +8,10 @@ export interface BridgeConfig {
|
|
|
8
8
|
hostClass?: 'mobile' | 'desktop';
|
|
9
9
|
/** Effective host display language; distinct from the raw system locale. */
|
|
10
10
|
displayLanguage?: string;
|
|
11
|
+
/** The lxapp's adaptive context when this document was built; see `surface-context.ts`. */
|
|
12
|
+
surfaceContext?: unknown;
|
|
13
|
+
/** The host revision of `surfaceContext`, so a later push can supersede it. */
|
|
14
|
+
surfaceContextRevision?: number;
|
|
11
15
|
nonce?: string;
|
|
12
16
|
appleDownstreamURL?: string;
|
|
13
17
|
dev?: boolean;
|
|
@@ -137,10 +141,19 @@ declare global {
|
|
|
137
141
|
interface Window {
|
|
138
142
|
__LX_BRIDGE_CFG?: BridgeConfig;
|
|
139
143
|
/** Host push for a display language changed while the page is open. */
|
|
140
|
-
__lingxiaApplyDisplayLanguage?: (language: string) => void;
|
|
144
|
+
__lingxiaApplyDisplayLanguage?: (language: string, revision?: number) => void;
|
|
141
145
|
/** Shared by every copy of the bridge module in this document. */
|
|
142
146
|
__lxDisplayLanguage?: {
|
|
143
147
|
value: string;
|
|
148
|
+
revision?: number;
|
|
149
|
+
listeners: Set<() => void>;
|
|
150
|
+
};
|
|
151
|
+
/** Host push for the page's adaptive context; see `surface-context.ts`. */
|
|
152
|
+
__lingxiaApplySurfaceContext?: (context: unknown, revision?: unknown) => void;
|
|
153
|
+
/** Shared by every copy of the bridge module in this document. */
|
|
154
|
+
__lxSurfaceContext?: {
|
|
155
|
+
value: import('./surface-context').SurfaceContext;
|
|
156
|
+
revision: number;
|
|
144
157
|
listeners: Set<() => void>;
|
|
145
158
|
};
|
|
146
159
|
__LX_BRIDGE_INIT_STATE?: 'initializing' | 'initialized';
|
|
@@ -192,10 +205,10 @@ export interface LingXiaBridgeInterface {
|
|
|
192
205
|
proto: boolean;
|
|
193
206
|
all: boolean;
|
|
194
207
|
};
|
|
195
|
-
|
|
196
|
-
get():
|
|
208
|
+
host: {
|
|
209
|
+
get(): import('./host').LxHost;
|
|
197
210
|
/** Change-only; read the current value with `get()`. */
|
|
198
|
-
subscribe(listener: (
|
|
211
|
+
subscribe(listener: () => void): () => void;
|
|
199
212
|
};
|
|
200
213
|
platform: {
|
|
201
214
|
isHarmony(): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingxia/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "LingXia Web Runtime - Bridge, boot, and error handling for WebView",
|
|
6
6
|
"repository": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"build:modules:es5": "tsc -p tsconfig.modules.legacy.json",
|
|
41
41
|
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
|
|
42
42
|
"build:v3-test": "tsc -p tsconfig.v3-test.json",
|
|
43
|
-
"test": "npm run build:es2020 && npm run build:es5 && npm run build:v3-test && node ./test-support/test-protocol-v3.mjs && node ./test-support/test-bridge-required-v3.mjs && node ./test-support/test-control-bootstrap.mjs",
|
|
43
|
+
"test": "npm run build:es2020 && npm run build:es5 && npm run build:v3-test && node ./test-support/test-protocol-v3.mjs && node ./test-support/test-bridge-required-v3.mjs && node ./test-support/test-control-bootstrap.mjs && npm run build:modules:es2020 && node ./test-support/test-surface-context.mjs && rolldown test-support/host-entry.ts --file dist/test/host.mjs --format esm && node ./test-support/test-host.mjs",
|
|
44
44
|
"dev": "rolldown -c -w",
|
|
45
45
|
"clean": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\"",
|
|
46
46
|
"prepublishOnly": "npm run build"
|