@neogis-auxiliary/adapters 0.1.0 → 0.2.0-next.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/bindings.js +8 -9
- package/dist/card-actions.js +1 -1
- package/dist/compiler.d.ts +3 -4
- package/dist/compiler.js +45 -63
- package/dist/index.js +4 -6
- package/dist/types.d.ts +0 -6
- package/package.json +21 -9
package/dist/bindings.js
CHANGED
|
@@ -17,21 +17,20 @@ export function createCardBindings(options) {
|
|
|
17
17
|
}
|
|
18
18
|
inputs.push(...rule.inputs);
|
|
19
19
|
}
|
|
20
|
-
const compiler = new RuleCompiler(options.targets, { ...builtinConverters, ...options.converters },
|
|
20
|
+
const compiler = new RuleCompiler(options.targets, { ...builtinConverters, ...options.converters }, inputs, true);
|
|
21
21
|
const input = record(rule.read, name + '.read');
|
|
22
|
-
keys(input, ['call', 'read', 'compose', '
|
|
22
|
+
keys(input, ['call', 'read', 'compose', 'args', 'convert', 'extract'], name + '.read');
|
|
23
23
|
const { extract, ...mapping } = input;
|
|
24
24
|
if (extract !== undefined && (typeof extract !== 'string' || !extract))
|
|
25
25
|
throw new Error('extract 需要非空字段路径');
|
|
26
|
-
if (extract !== undefined &&
|
|
27
|
-
throw new Error('extract 不可与 convert
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
: { type: 'getOptionalProperty', args: ['$result', extract.split('.')] };
|
|
26
|
+
if (extract !== undefined && mapping.convert !== undefined)
|
|
27
|
+
throw new Error('extract 不可与 convert 同用');
|
|
28
|
+
mapping.convert ??= extract === undefined
|
|
29
|
+
? { type: 'identity' }
|
|
30
|
+
: { type: 'getOptionalProperty', args: ['$result', extract.split('.')] };
|
|
32
31
|
const read = compiler.compile(mapping, name + '.read', true);
|
|
33
32
|
const subscribe = rule.refreshOn === undefined ? undefined
|
|
34
|
-
: compileSubscription(rule.refreshOn, name + '.refreshOn', options.targets
|
|
33
|
+
: compileSubscription(rule.refreshOn, name + '.refreshOn', options.targets);
|
|
35
34
|
return { name, inputs, read, subscribe };
|
|
36
35
|
});
|
|
37
36
|
const shared = new Map();
|
package/dist/card-actions.js
CHANGED
|
@@ -15,7 +15,7 @@ export function createCardActions(options) {
|
|
|
15
15
|
if (!Array.isArray(inputs) || !inputs.every(isAlias) || inputs.includes('scope') || new Set(inputs).size !== inputs.length) {
|
|
16
16
|
throw new Error(path + '.inputs 需要唯一参数名,scope 由 SDK 提供');
|
|
17
17
|
}
|
|
18
|
-
const compiler = new RuleCompiler(options.targets, { ...builtinConverters, ...options.converters },
|
|
18
|
+
const compiler = new RuleCompiler(options.targets, { ...builtinConverters, ...options.converters }, [...inputs, 'scope'], true, args => args[inputs.length].active, true);
|
|
19
19
|
const args = rule.args === undefined ? inputs.map(input => '$input.' + input) : rule.args;
|
|
20
20
|
const invoke = compiler.compile({ call: rule.call, args, convert: { type: 'identity' } }, path);
|
|
21
21
|
return [name, { inputs, invoke }];
|
package/dist/compiler.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import type { AdapterFunction, ConverterRegistry,
|
|
1
|
+
import type { AdapterFunction, ConverterRegistry, TargetRegistry } from './types.js';
|
|
2
2
|
export declare class RuleCompiler {
|
|
3
3
|
private readonly targets;
|
|
4
4
|
private readonly converters;
|
|
5
|
-
private readonly handlers;
|
|
6
5
|
private readonly inputNames;
|
|
7
6
|
private readonly asyncAllowed;
|
|
8
7
|
private readonly isActive?;
|
|
9
8
|
private readonly preserveLateResult;
|
|
10
9
|
private readonly active;
|
|
11
|
-
constructor(targets: TargetRegistry, converters: ConverterRegistry,
|
|
10
|
+
constructor(targets: TargetRegistry, converters: ConverterRegistry, inputNames: readonly string[], asyncAllowed: boolean, isActive?: ((sdkArgs: unknown[]) => boolean) | undefined, preserveLateResult?: boolean);
|
|
12
11
|
private visit;
|
|
13
12
|
/** Resolve dependencies in order, without adding a Promise to synchronous calls. */
|
|
14
13
|
private collect;
|
|
@@ -20,4 +19,4 @@ export declare class RuleCompiler {
|
|
|
20
19
|
private rule;
|
|
21
20
|
compile(value: unknown, path: string, nullable?: boolean): AdapterFunction;
|
|
22
21
|
}
|
|
23
|
-
export declare function compileSubscription(value: unknown, path: string, targets: TargetRegistry
|
|
22
|
+
export declare function compileSubscription(value: unknown, path: string, targets: TargetRegistry): (listener: () => void) => () => void;
|
package/dist/compiler.js
CHANGED
|
@@ -28,16 +28,14 @@ function checkCallable(value, parts, path) {
|
|
|
28
28
|
export class RuleCompiler {
|
|
29
29
|
targets;
|
|
30
30
|
converters;
|
|
31
|
-
handlers;
|
|
32
31
|
inputNames;
|
|
33
32
|
asyncAllowed;
|
|
34
33
|
isActive;
|
|
35
34
|
preserveLateResult;
|
|
36
35
|
active = new Set();
|
|
37
|
-
constructor(targets, converters,
|
|
36
|
+
constructor(targets, converters, inputNames, asyncAllowed, isActive, preserveLateResult = false) {
|
|
38
37
|
this.targets = targets;
|
|
39
38
|
this.converters = converters;
|
|
40
|
-
this.handlers = handlers;
|
|
41
39
|
this.inputNames = inputNames;
|
|
42
40
|
this.asyncAllowed = asyncAllowed;
|
|
43
41
|
this.isActive = isActive;
|
|
@@ -169,17 +167,11 @@ export class RuleCompiler {
|
|
|
169
167
|
rule(value, path, nullable) {
|
|
170
168
|
const rule = record(value, path);
|
|
171
169
|
return this.visit(rule, path, () => {
|
|
172
|
-
const sources = ['call', 'read', 'compose'
|
|
170
|
+
const sources = ['call', 'read', 'compose'].filter(key => Object.hasOwn(rule, key));
|
|
173
171
|
if (sources.length !== 1)
|
|
174
|
-
fail(path, '需要且只能提供 call/read/compose
|
|
172
|
+
fail(path, '需要且只能提供 call/read/compose 之一');
|
|
175
173
|
const source = sources[0];
|
|
176
|
-
keys(rule, source === '
|
|
177
|
-
if (source === 'handler') {
|
|
178
|
-
const handlerName = name(rule.handler, path + '.handler');
|
|
179
|
-
if (!Object.hasOwn(this.handlers, handlerName) || typeof this.handlers[handlerName] !== 'function')
|
|
180
|
-
fail(path + '.handler', '能力函数未注册');
|
|
181
|
-
return scope => withPath(path, () => this.result(this.handlers[handlerName](...scope.sdkArgs), path, value => value));
|
|
182
|
-
}
|
|
174
|
+
keys(rule, source === 'call' ? ['call', 'args', 'convert'] : [source, 'convert'], path);
|
|
183
175
|
const convert = this.conversion(rule.convert, path + '.convert', { result: true, item: false });
|
|
184
176
|
let read;
|
|
185
177
|
if (source === 'compose') {
|
|
@@ -219,62 +211,52 @@ export class RuleCompiler {
|
|
|
219
211
|
};
|
|
220
212
|
}
|
|
221
213
|
}
|
|
222
|
-
export function compileSubscription(value, path, targets
|
|
214
|
+
export function compileSubscription(value, path, targets) {
|
|
223
215
|
const rule = record(value, path);
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
216
|
+
const mode = rule.mode;
|
|
217
|
+
if (mode !== 'on-off' && mode !== 'returned-cleanup')
|
|
218
|
+
fail(path + '.mode', '未知订阅模式');
|
|
219
|
+
keys(rule, mode === 'on-off' ? ['mode', 'target', 'subscribe', 'unsubscribe', 'event'] : ['mode', 'target', 'subscribe'], path);
|
|
220
|
+
const targetName = name(rule.target, path + '.target');
|
|
221
|
+
if (!Object.hasOwn(targets, targetName))
|
|
222
|
+
fail(path, '订阅目标未注册');
|
|
223
|
+
const target = targets[targetName];
|
|
224
|
+
const subscribeName = name(rule.subscribe, path + '.subscribe');
|
|
225
|
+
safeKey(subscribeName, path + '.subscribe');
|
|
226
|
+
checkCallable(target, [subscribeName], path + '.subscribe');
|
|
227
|
+
const unsubscribeName = mode === 'on-off' ? name(rule.unsubscribe, path + '.unsubscribe') : '';
|
|
228
|
+
const eventName = mode === 'on-off' ? name(rule.event, path + '.event') : '';
|
|
229
|
+
if (mode === 'on-off') {
|
|
230
|
+
safeKey(unsubscribeName, path + '.unsubscribe');
|
|
231
|
+
checkCallable(target, [unsubscribeName], path + '.unsubscribe');
|
|
231
232
|
}
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
if (
|
|
235
|
-
fail(path
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
233
|
+
const invoke = (method, args) => {
|
|
234
|
+
const fn = getPath(target, [method], path);
|
|
235
|
+
if (typeof fn !== 'function')
|
|
236
|
+
fail(path, '事件成员不是方法');
|
|
237
|
+
return fn.apply(target, args);
|
|
238
|
+
};
|
|
239
|
+
function subscribe(listener) {
|
|
240
|
+
if (mode === 'returned-cleanup')
|
|
241
|
+
return invoke(subscribeName, [listener]);
|
|
242
|
+
try {
|
|
243
|
+
const returned = invoke(subscribeName, [eventName, listener]);
|
|
244
|
+
if (isPromise(returned)) {
|
|
245
|
+
// Invalid async subscriptions can register after the immediate rollback.
|
|
246
|
+
// Release again after either settlement, including register-then-reject.
|
|
247
|
+
const release = () => invoke(unsubscribeName, [eventName, listener]);
|
|
248
|
+
void Promise.resolve(returned).then(release, release).catch(() => { });
|
|
249
|
+
}
|
|
250
|
+
synchronous(returned, path);
|
|
249
251
|
}
|
|
250
|
-
|
|
251
|
-
const fn = getPath(target, [method], path);
|
|
252
|
-
if (typeof fn !== 'function')
|
|
253
|
-
fail(path, '事件成员不是方法');
|
|
254
|
-
return fn.apply(target, args);
|
|
255
|
-
};
|
|
256
|
-
subscribe = listener => {
|
|
257
|
-
if (mode === 'returned-cleanup')
|
|
258
|
-
return invoke(subscribeName, [listener]);
|
|
252
|
+
catch (error) {
|
|
259
253
|
try {
|
|
260
|
-
|
|
261
|
-
if (isPromise(returned)) {
|
|
262
|
-
// Invalid async subscriptions can register after the immediate rollback.
|
|
263
|
-
// Release again after either settlement, including register-then-reject.
|
|
264
|
-
const release = () => invoke(unsubscribeName, [eventName, listener]);
|
|
265
|
-
void Promise.resolve(returned).then(release, release).catch(() => { });
|
|
266
|
-
}
|
|
267
|
-
synchronous(returned, path);
|
|
268
|
-
}
|
|
269
|
-
catch (error) {
|
|
270
|
-
try {
|
|
271
|
-
synchronous(invoke(unsubscribeName, [eventName, listener]), path);
|
|
272
|
-
}
|
|
273
|
-
catch { /* Preserve the subscription failure. */ }
|
|
274
|
-
throw error;
|
|
254
|
+
synchronous(invoke(unsubscribeName, [eventName, listener]), path);
|
|
275
255
|
}
|
|
276
|
-
|
|
277
|
-
|
|
256
|
+
catch { /* Preserve the subscription failure. */ }
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
return () => synchronous(invoke(unsubscribeName, [eventName, listener]), path);
|
|
278
260
|
}
|
|
279
261
|
return listener => withPath(path, () => {
|
|
280
262
|
let active = true;
|
package/dist/index.js
CHANGED
|
@@ -63,18 +63,16 @@ function prepare(options, role) {
|
|
|
63
63
|
fail(role + '.' + method, '缺少必需能力');
|
|
64
64
|
}
|
|
65
65
|
const converters = { ...builtinConverters };
|
|
66
|
-
const handlers = {};
|
|
67
66
|
let bound;
|
|
68
67
|
if (preset?.bind)
|
|
69
68
|
bound = withPath(role + '.preset.bind', () => synchronous(preset.bind(targets), role + '.preset.bind'));
|
|
70
69
|
if (bound !== undefined) {
|
|
71
|
-
keys(record(bound, role + '.preset.bind'), ['converters'
|
|
70
|
+
keys(record(bound, role + '.preset.bind'), ['converters'], role + '.preset.bind');
|
|
72
71
|
}
|
|
73
72
|
for (const [label, registry] of [['preset', preset], ['bound', bound], ['options', options]]) {
|
|
74
73
|
mergeFunctions(converters, registry?.converters, role + '.' + label + '.converters');
|
|
75
|
-
mergeFunctions(handlers, registry?.handlers, role + '.' + label + '.handlers');
|
|
76
74
|
}
|
|
77
|
-
return { config: cloneData(merged), targets, converters,
|
|
75
|
+
return { config: cloneData(merged), targets, converters, inputs };
|
|
78
76
|
}
|
|
79
77
|
function createMethods(options, role) {
|
|
80
78
|
const prepared = prepare(options, role);
|
|
@@ -86,7 +84,7 @@ function createMethods(options, role) {
|
|
|
86
84
|
const path = role + '.' + method;
|
|
87
85
|
// 参数转换可能异步完成;动作实际派发前仍需读取原会话的动态有效性。
|
|
88
86
|
const isActive = method === 'performAction' ? (args) => args[1].active : undefined;
|
|
89
|
-
const compiler = new RuleCompiler(prepared.targets, prepared.converters,
|
|
87
|
+
const compiler = new RuleCompiler(prepared.targets, prepared.converters, inputNames, asynchronous.has(method), isActive);
|
|
90
88
|
const execute = compiler.compile(rule, path, ['getLayer', 'resolveFeature', 'readDocument'].includes(method));
|
|
91
89
|
methods[method] = (...args) => withPath(path, () => {
|
|
92
90
|
const result = execute(...args);
|
|
@@ -103,7 +101,7 @@ function createMethods(options, role) {
|
|
|
103
101
|
fail(role + '.events.' + event, '缺少必需事件');
|
|
104
102
|
continue;
|
|
105
103
|
}
|
|
106
|
-
subscriptions[event] = compileSubscription(events[event], role + '.events.' + event, prepared.targets
|
|
104
|
+
subscriptions[event] = compileSubscription(events[event], role + '.events.' + event, prepared.targets);
|
|
107
105
|
}
|
|
108
106
|
return { methods, subscriptions };
|
|
109
107
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/** Platform functions keep their own parameter types. Values are checked at the adapter boundary. */
|
|
2
2
|
export type AdapterFunction = (...args: any[]) => unknown;
|
|
3
3
|
export type ConverterRegistry = Readonly<Record<string, AdapterFunction>>;
|
|
4
|
-
export type HandlerRegistry = Readonly<Record<string, AdapterFunction>>;
|
|
5
4
|
export type TargetRegistry = Readonly<Record<string, unknown>>;
|
|
6
5
|
export interface ConversionRule {
|
|
7
6
|
readonly type: string;
|
|
@@ -18,8 +17,6 @@ export type MethodRule = {
|
|
|
18
17
|
} | {
|
|
19
18
|
readonly compose: Readonly<Record<string, MethodRule>>;
|
|
20
19
|
readonly convert: ConversionRule;
|
|
21
|
-
} | {
|
|
22
|
-
readonly handler: string;
|
|
23
20
|
};
|
|
24
21
|
export type EventRule = {
|
|
25
22
|
readonly mode: 'on-off';
|
|
@@ -31,8 +28,6 @@ export type EventRule = {
|
|
|
31
28
|
readonly mode: 'returned-cleanup';
|
|
32
29
|
readonly target: string;
|
|
33
30
|
readonly subscribe: string;
|
|
34
|
-
} | {
|
|
35
|
-
readonly handler: string;
|
|
36
31
|
};
|
|
37
32
|
export type AdapterSection = string | Readonly<Record<string, unknown>>;
|
|
38
33
|
export type MapEngineConfig = AdapterSection;
|
|
@@ -44,7 +39,6 @@ export interface MapCardAdapterConfig {
|
|
|
44
39
|
}
|
|
45
40
|
export interface BoundAdapterFunctions {
|
|
46
41
|
readonly converters?: ConverterRegistry;
|
|
47
|
-
readonly handlers?: HandlerRegistry;
|
|
48
42
|
}
|
|
49
43
|
export interface AdapterPreset extends BoundAdapterFunctions {
|
|
50
44
|
readonly role: 'engine' | 'host';
|
package/package.json
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@neogis-auxiliary/adapters",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"publishConfig": {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@neogis-auxiliary/adapters",
|
|
3
|
+
"version": "0.2.0-next.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"registry": "https://registry.npmjs.org/",
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@neogis-auxiliary/core": "0.1.0"
|
|
20
|
+
}
|
|
21
|
+
}
|