yeow-api 0.2.52 → 0.2.54
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/package.json +1 -1
- package/src/global.d.ts +3 -0
- package/src/index.ts +2 -0
- package/src/service.ts +60 -0
package/package.json
CHANGED
package/src/global.d.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -79,4 +79,6 @@ export { createBoard as createScoreboard, deleteBoard as deleteScoreboard,
|
|
|
79
79
|
setPlayerBoard } from './scoreboard.js';
|
|
80
80
|
export { getMaterials, getBlocks, getItems } from './material.js';
|
|
81
81
|
export type { MaterialInfo } from './material.js';
|
|
82
|
+
export { registerService, registerNativeService, request as serviceRequest, subscribe as serviceSubscribe, publish as servicePublish } from './service.js';
|
|
83
|
+
export type { ServiceResult } from './service.js';
|
|
82
84
|
export { log, Logger } from './log.js';
|
package/src/service.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { post } from './task.js';
|
|
2
|
+
|
|
3
|
+
export interface ServiceResult {
|
|
4
|
+
serviceId: string;
|
|
5
|
+
token: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let _reqSeq = 0;
|
|
9
|
+
|
|
10
|
+
// ── Register Plugin Service ──
|
|
11
|
+
|
|
12
|
+
export async function registerService(refName: string, onRequest: (path: string, body: any) => any, isPublic = true): Promise<ServiceResult> {
|
|
13
|
+
const svcCbId = _registerCallback((payload: any) => {
|
|
14
|
+
if (payload?._svc === 'request') {
|
|
15
|
+
let body: any = null;
|
|
16
|
+
try { body = JSON.parse(payload.body); } catch { body = {}; }
|
|
17
|
+
const result = onRequest(payload.path, body);
|
|
18
|
+
$send('service', { t: 'response', requestId: payload.requestId, body: result });
|
|
19
|
+
}
|
|
20
|
+
}, { persistent: true });
|
|
21
|
+
|
|
22
|
+
const r = $send('service', { t: 'register', refName, onRequest: svcCbId, public: isPublic }) as ServiceResult;
|
|
23
|
+
return r;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function registerNativeService(refName: string, platforms: Record<string, string>, isPublic = true): Promise<ServiceResult> {
|
|
27
|
+
const r = $send('service', { t: 'registerNative', refName, platforms, public: isPublic }) as ServiceResult;
|
|
28
|
+
return r;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ── Request ──
|
|
32
|
+
|
|
33
|
+
export function request(serviceId: string, path: string, body?: any): Promise<any> {
|
|
34
|
+
const reqId = 'svcreq_' + (++_reqSeq);
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
_registerCallback((result: any) => {
|
|
37
|
+
if (result?.err) { reject(new Error(result.err)); } else resolve(result);
|
|
38
|
+
});
|
|
39
|
+
$send('service', { t: 'request', serviceId, path, body: body || {}, requestId: reqId });
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ── Subscribe ──
|
|
44
|
+
|
|
45
|
+
export function subscribe(serviceId: string, eventPath: string, handler: (eventPath: string, body: any) => void): () => void {
|
|
46
|
+
const cbId = _registerCallback((payload: any) => {
|
|
47
|
+
handler(payload.eventPath, payload.body);
|
|
48
|
+
}, { persistent: true });
|
|
49
|
+
$send('service', { t: 'subscribe', serviceId, eventPath, cb: cbId });
|
|
50
|
+
return () => {
|
|
51
|
+
$send('service', { t: 'unsubscribe', serviceId, eventPath });
|
|
52
|
+
_unregisterCallback(cbId);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── Publish ──
|
|
57
|
+
|
|
58
|
+
export function publish(token: string, eventPath: string, body?: any): void {
|
|
59
|
+
$send('service', { t: 'publish', token, eventPath, body: body || {} });
|
|
60
|
+
}
|