-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathremote.ts
More file actions
74 lines (61 loc) · 2.1 KB
/
remote.ts
File metadata and controls
74 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Manifest, SDK } from '@rsdoctor/types';
import { Manifest as ManifestShared } from '@rsdoctor/utils/common';
import { get } from 'es-toolkit/compat';
import { BaseDataLoader } from './base';
import { fetchShardingFile } from '../request';
export class RemoteDataLoader extends BaseDataLoader {
public isLocal() {
return false;
}
public async loadData<T extends Manifest.RsdoctorManifestMappingKeys>(
key: T,
): Promise<Manifest.InferManifestDataValue<T>>;
public async loadData(key: string): Promise<unknown> {
return this.limit(key, async () => {
const [scope, ...rest] = this.getKeys(key);
const data =
this.getData(
scope as keyof Manifest.RsdoctorManifestData,
'cloudData',
) ??
this.getData(scope as keyof Manifest.RsdoctorManifestData, 'data');
if (!data) return;
let res = data;
// sharding files
if (ManifestShared.isShardingData(res)) {
// only cache by the root key in data
if (!this.shardingDataMap.has(scope)) {
const task = ManifestShared.fetchShardingData(
res,
fetchShardingFile,
).catch((err) => {
this.log(`loadData error: `, res, key);
throw err;
});
this.shardingDataMap.set(scope, task);
}
res = await this.shardingDataMap.get(scope);
}
return rest.length > 0 ? get(res, this.joinKeys(rest)) : res;
});
}
public async loadAPI<
T extends SDK.ServerAPI.API,
B extends
SDK.ServerAPI.InferRequestBodyType<T> = SDK.ServerAPI.InferRequestBodyType<T>,
R extends
SDK.ServerAPI.InferResponseType<T> = SDK.ServerAPI.InferResponseType<T>,
>(...args: B extends void ? [api: T] : [api: T, body: B]): Promise<R> {
const [api, body] = args;
// request limitation key
const key = body ? `${api}_${JSON.stringify(body)}` : `${api}`;
return this.limit(key, async () => {
return this.loader.loadAPI(...args) as R;
});
}
public dispose() {
super.dispose();
}
public onDataUpdate() {}
public removeOnDataUpdate() {}
}