@jolibox/implement 1.1.4-beta.4
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/.eslintrc.js +3 -0
- package/.rush/temp/package-deps_build.json +104 -0
- package/.rush/temp/shrinkwrap-deps.json +79 -0
- package/README.md +1 -0
- package/dist/common/api-factory/index.d.ts +21 -0
- package/dist/common/api-factory/validator/__tests__/validate/any.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/array.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/arraybuffer.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/boolean.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/enum.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/function.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/literal.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/nullish.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/number.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/object.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/or.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/record.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/string.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/symbol.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/tuple.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/type-asserts.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/utils.test.d.ts +1 -0
- package/dist/common/api-factory/validator/index.d.ts +29 -0
- package/dist/common/api-factory/validator/validate.d.ts +119 -0
- package/dist/common/can-i-use.d.ts +2 -0
- package/dist/common/context/index.d.ts +16 -0
- package/dist/common/context/types.d.ts +5 -0
- package/dist/common/context/url-parse.d.ts +22 -0
- package/dist/common/http/index.d.ts +13 -0
- package/dist/common/http/uuid.d.ts +2 -0
- package/dist/common/http/xua.d.ts +17 -0
- package/dist/common/report/base-tracker.d.ts +13 -0
- package/dist/common/report/create-trace.d.ts +7 -0
- package/dist/common/report/errors/error-types.d.ts +122 -0
- package/dist/common/report/errors/index.d.ts +13 -0
- package/dist/common/report/errors/report/index.d.ts +51 -0
- package/dist/common/report/errors/report/listeners.d.ts +1 -0
- package/dist/common/report/index.d.ts +3 -0
- package/dist/common/report/task-track/index.d.ts +23 -0
- package/dist/common/report/track.d.ts +3 -0
- package/dist/common/report/types.d.ts +75 -0
- package/dist/h5/ads/ads-action-detection.d.ts +6 -0
- package/dist/h5/ads/anti-cheating.d.ts +61 -0
- package/dist/h5/ads/index.d.ts +275 -0
- package/dist/h5/api/base.d.ts +13 -0
- package/dist/h5/api/get-system-info.d.ts +1 -0
- package/dist/h5/api/index.d.ts +4 -0
- package/dist/h5/api/lifecycle.d.ts +1 -0
- package/dist/h5/api/storage.d.ts +27 -0
- package/dist/h5/api/task.d.ts +1 -0
- package/dist/h5/bootstrap/index.d.ts +1 -0
- package/dist/h5/http/index.d.ts +33 -0
- package/dist/h5/http/utils/__tests__/uuid.test.d.ts +1 -0
- package/dist/h5/http/utils/__tests__/xua.test.d.ts +1 -0
- package/dist/h5/http/utils/index.d.ts +14 -0
- package/dist/h5/http/utils/session.d.ts +1 -0
- package/dist/h5/report/errors/index.d.ts +4 -0
- package/dist/h5/report/event-tracker.d.ts +8 -0
- package/dist/h5/report/index.d.ts +10 -0
- package/dist/h5/report/task-tracker.d.ts +14 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5026 -0
- package/dist/index.native.d.ts +2 -0
- package/dist/index.native.js +3743 -0
- package/dist/native/api/base.d.ts +13 -0
- package/dist/native/api/get-system-info.d.ts +1 -0
- package/dist/native/api/index.d.ts +7 -0
- package/dist/native/api/keyboard.d.ts +3 -0
- package/dist/native/api/lifecycle.d.ts +1 -0
- package/dist/native/api/login.d.ts +1 -0
- package/dist/native/api/request.d.ts +1 -0
- package/dist/native/api/storage.d.ts +25 -0
- package/dist/native/api/task.d.ts +1 -0
- package/dist/native/bootstrap/bridge.d.ts +4 -0
- package/dist/native/bootstrap/index.d.ts +1 -0
- package/dist/native/js-bridge/const.d.ts +5 -0
- package/dist/native/js-bridge/index.d.ts +2 -0
- package/dist/native/js-bridge/invoke.d.ts +21 -0
- package/dist/native/js-bridge/js-bridge.d.ts +6 -0
- package/dist/native/js-bridge/report.d.ts +63 -0
- package/dist/native/js-bridge/subscribe.d.ts +8 -0
- package/dist/native/js-bridge/types.d.ts +14 -0
- package/dist/native/js-bridge/utils.d.ts +17 -0
- package/dist/native/js-core/index.d.ts +3 -0
- package/dist/native/js-core/jolibox-js-core.d.ts +45 -0
- package/dist/native/js-core/message-port.d.ts +12 -0
- package/dist/native/js-core/utils.d.ts +7 -0
- package/dist/native/network/create-fetch.d.ts +27 -0
- package/dist/native/network/index.d.ts +11 -0
- package/dist/native/network/report.d.ts +15 -0
- package/dist/native/network/types.d.ts +61 -0
- package/dist/native/network/utils.d.ts +9 -0
- package/dist/native/report/errors/index.d.ts +4 -0
- package/dist/native/report/index.d.ts +10 -0
- package/dist/native/report/task-tracker.d.ts +20 -0
- package/dist/utils/index.d.ts +0 -0
- package/esbuild.config.js +66 -0
- package/implement.build.log +9 -0
- package/package.json +30 -0
- package/src/common/api-factory/index.ts +188 -0
- package/src/common/api-factory/validator/__tests__/validate/any.test.ts +68 -0
- package/src/common/api-factory/validator/__tests__/validate/array.test.ts +402 -0
- package/src/common/api-factory/validator/__tests__/validate/arraybuffer.test.ts +48 -0
- package/src/common/api-factory/validator/__tests__/validate/boolean.test.ts +27 -0
- package/src/common/api-factory/validator/__tests__/validate/enum.test.ts +106 -0
- package/src/common/api-factory/validator/__tests__/validate/function.test.ts +54 -0
- package/src/common/api-factory/validator/__tests__/validate/literal.test.ts +130 -0
- package/src/common/api-factory/validator/__tests__/validate/nullish.test.ts +41 -0
- package/src/common/api-factory/validator/__tests__/validate/number.test.ts +147 -0
- package/src/common/api-factory/validator/__tests__/validate/object.test.ts +131 -0
- package/src/common/api-factory/validator/__tests__/validate/or.test.ts +96 -0
- package/src/common/api-factory/validator/__tests__/validate/record.test.ts +274 -0
- package/src/common/api-factory/validator/__tests__/validate/string.test.ts +187 -0
- package/src/common/api-factory/validator/__tests__/validate/symbol.test.ts +23 -0
- package/src/common/api-factory/validator/__tests__/validate/tuple.test.ts +86 -0
- package/src/common/api-factory/validator/__tests__/validate/type-asserts.test.ts +13 -0
- package/src/common/api-factory/validator/__tests__/validate/utils.test.ts +44 -0
- package/src/common/api-factory/validator/index.ts +107 -0
- package/src/common/api-factory/validator/validate.ts +639 -0
- package/src/common/can-i-use.ts +19 -0
- package/src/common/context/index.ts +91 -0
- package/src/common/context/types.ts +5 -0
- package/src/common/context/url-parse.ts +63 -0
- package/src/common/http/index.ts +29 -0
- package/src/common/http/uuid.ts +11 -0
- package/src/common/http/xua.ts +79 -0
- package/src/common/report/base-tracker.ts +134 -0
- package/src/common/report/create-trace.ts +17 -0
- package/src/common/report/errors/error-types.ts +206 -0
- package/src/common/report/errors/index.ts +20 -0
- package/src/common/report/errors/report/index.ts +63 -0
- package/src/common/report/errors/report/listeners.ts +80 -0
- package/src/common/report/index.ts +3 -0
- package/src/common/report/task-track/index.ts +97 -0
- package/src/common/report/track.ts +49 -0
- package/src/common/report/types.ts +90 -0
- package/src/h5/ads/ads-action-detection.ts +31 -0
- package/src/h5/ads/anti-cheating.ts +244 -0
- package/src/h5/ads/index.ts +641 -0
- package/src/h5/api/base.ts +9 -0
- package/src/h5/api/get-system-info.ts +55 -0
- package/src/h5/api/index.ts +4 -0
- package/src/h5/api/lifecycle.ts +110 -0
- package/src/h5/api/storage.ts +173 -0
- package/src/h5/api/task.ts +197 -0
- package/src/h5/bootstrap/index.ts +16 -0
- package/src/h5/http/index.ts +189 -0
- package/src/h5/http/utils/__tests__/uuid.test.ts +16 -0
- package/src/h5/http/utils/__tests__/xua.test.ts +27 -0
- package/src/h5/http/utils/index.ts +19 -0
- package/src/h5/http/utils/session.ts +10 -0
- package/src/h5/report/errors/index.ts +40 -0
- package/src/h5/report/event-tracker.ts +40 -0
- package/src/h5/report/index.ts +56 -0
- package/src/h5/report/task-tracker.ts +35 -0
- package/src/index.native.ts +7 -0
- package/src/index.ts +9 -0
- package/src/native/api/base.ts +8 -0
- package/src/native/api/get-system-info.ts +41 -0
- package/src/native/api/index.ts +7 -0
- package/src/native/api/keyboard.ts +75 -0
- package/src/native/api/lifecycle.ts +76 -0
- package/src/native/api/login.ts +71 -0
- package/src/native/api/request.ts +154 -0
- package/src/native/api/storage.ts +287 -0
- package/src/native/api/task.ts +267 -0
- package/src/native/bootstrap/bridge.ts +59 -0
- package/src/native/bootstrap/index.ts +58 -0
- package/src/native/js-bridge/const.ts +11 -0
- package/src/native/js-bridge/index.ts +2 -0
- package/src/native/js-bridge/invoke.ts +210 -0
- package/src/native/js-bridge/js-bridge.ts +23 -0
- package/src/native/js-bridge/report.ts +311 -0
- package/src/native/js-bridge/subscribe.ts +50 -0
- package/src/native/js-bridge/types.ts +26 -0
- package/src/native/js-bridge/utils.ts +116 -0
- package/src/native/js-core/index.ts +4 -0
- package/src/native/js-core/jolibox-js-core.ts +188 -0
- package/src/native/js-core/message-port.ts +52 -0
- package/src/native/js-core/utils.ts +9 -0
- package/src/native/network/create-fetch.ts +237 -0
- package/src/native/network/index.ts +15 -0
- package/src/native/network/report.ts +58 -0
- package/src/native/network/types.ts +77 -0
- package/src/native/network/utils.ts +90 -0
- package/src/native/report/errors/index.ts +27 -0
- package/src/native/report/index.ts +51 -0
- package/src/native/report/task-tracker.ts +64 -0
- package/src/native/types/global.d.ts +26 -0
- package/src/native/types/native-method-map.d.ts +282 -0
- package/src/native/types/native-method.d.ts +30 -0
- package/src/utils/index.ts +0 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { createAPI, createSyncAPI, registerCanIUse, t } from './base';
|
|
2
|
+
import { applyNative, invokeNative } from '../bootstrap/bridge';
|
|
3
|
+
import { createCommands } from '@jolibox/common';
|
|
4
|
+
import { innerFetch as fetch } from '../network';
|
|
5
|
+
import { context } from '@/common/context';
|
|
6
|
+
import { API_CODE, StandardResponse, StorageResponse } from '@jolibox/types';
|
|
7
|
+
|
|
8
|
+
const commands = createCommands();
|
|
9
|
+
|
|
10
|
+
const getStorage = createAPI('getLocalStorage', {
|
|
11
|
+
paramsSchema: t.tuple(t.string()),
|
|
12
|
+
async implement(key) {
|
|
13
|
+
const { response } = await fetch<
|
|
14
|
+
StorageResponse<{
|
|
15
|
+
key: string;
|
|
16
|
+
value: string | null;
|
|
17
|
+
}>
|
|
18
|
+
>(`/api/games/user-storage/${context.game_id}`, {
|
|
19
|
+
method: 'GET',
|
|
20
|
+
responseType: 'json',
|
|
21
|
+
appendHostCookie: true,
|
|
22
|
+
query: {
|
|
23
|
+
key
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const {
|
|
27
|
+
data: { code, message, data: originData }
|
|
28
|
+
} = await response;
|
|
29
|
+
|
|
30
|
+
if (code === 'SUCCESS') {
|
|
31
|
+
return {
|
|
32
|
+
code: code as API_CODE,
|
|
33
|
+
message,
|
|
34
|
+
data: originData?.value
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const {
|
|
40
|
+
data: { data, dataType }
|
|
41
|
+
} = await applyNative('getLocalStorageAsync', { key });
|
|
42
|
+
const parsedData = parse(data, dataType);
|
|
43
|
+
return {
|
|
44
|
+
code,
|
|
45
|
+
message: `${message}. fallback to native`,
|
|
46
|
+
data: parsedData
|
|
47
|
+
};
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return {
|
|
50
|
+
code: 'INTERNAL_ERROR',
|
|
51
|
+
message: '[Jolibox SDK] get local storage failed',
|
|
52
|
+
data: null
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
commands.registerCommand('StorageSDK.getItem', getStorage);
|
|
59
|
+
|
|
60
|
+
registerCanIUse('storage.getItem', {
|
|
61
|
+
version: '1.0.0',
|
|
62
|
+
properties: {
|
|
63
|
+
key: '1.0.0',
|
|
64
|
+
value: '1.0.0'
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const setStorage = createAPI('setStorage', {
|
|
69
|
+
paramsSchema: t.tuple(t.string(), t.or(t.string(), t.boolean(), t.number())),
|
|
70
|
+
async implement(key, value) {
|
|
71
|
+
const gameId = context.game_id;
|
|
72
|
+
const valueToStore = typeof value == 'string' ? value : String(value);
|
|
73
|
+
const { response } = await fetch<StorageResponse<void>>(`/api/games/user-storage/${gameId}`, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
responseType: 'json',
|
|
76
|
+
appendHostCookie: true,
|
|
77
|
+
data: {
|
|
78
|
+
key,
|
|
79
|
+
value: valueToStore
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const {
|
|
84
|
+
data: { code, message }
|
|
85
|
+
} = response;
|
|
86
|
+
const dataType = getType(value);
|
|
87
|
+
const data = stringify(value, dataType);
|
|
88
|
+
// 同步备份
|
|
89
|
+
await applyNative('setLocalStorageAsync', {
|
|
90
|
+
key,
|
|
91
|
+
data,
|
|
92
|
+
dataType
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (code == 'SUCCESS') {
|
|
96
|
+
return {
|
|
97
|
+
code,
|
|
98
|
+
message
|
|
99
|
+
};
|
|
100
|
+
} else {
|
|
101
|
+
return {
|
|
102
|
+
code,
|
|
103
|
+
message: `${message}. fallback to native`
|
|
104
|
+
} as StandardResponse<void>;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
commands.registerCommand('StorageSDK.setItem', setStorage);
|
|
110
|
+
|
|
111
|
+
registerCanIUse('storage.setItem', {
|
|
112
|
+
version: '1.0.0',
|
|
113
|
+
properties: {
|
|
114
|
+
key: '1.0.0',
|
|
115
|
+
value: '1.0.0'
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 移除指定key
|
|
121
|
+
*/
|
|
122
|
+
export const removeStorage = createAPI('removeStorage', {
|
|
123
|
+
paramsSchema: t.tuple(t.string()),
|
|
124
|
+
async implement(key) {
|
|
125
|
+
const gameId = context.game_id;
|
|
126
|
+
const { response } = await fetch<StorageResponse<void>>(`/api/games/user-storage/${gameId}/remove`, {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
responseType: 'json',
|
|
129
|
+
appendHostCookie: true,
|
|
130
|
+
data: {
|
|
131
|
+
key
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const {
|
|
136
|
+
data: { code, message }
|
|
137
|
+
} = response;
|
|
138
|
+
await applyNative('removeLocalStorageAsync', { key });
|
|
139
|
+
if (code === 'SUCCESS') {
|
|
140
|
+
return {
|
|
141
|
+
code,
|
|
142
|
+
message
|
|
143
|
+
};
|
|
144
|
+
} else {
|
|
145
|
+
return {
|
|
146
|
+
code,
|
|
147
|
+
message: '[Jolibox SDK] remove item succ'
|
|
148
|
+
} as StandardResponse<void>;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
commands.registerCommand('StorageSDK.removeItem', removeStorage);
|
|
154
|
+
|
|
155
|
+
registerCanIUse('storage.removeItem', {
|
|
156
|
+
version: '1.0.0',
|
|
157
|
+
properties: {
|
|
158
|
+
key: '1.0.0'
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 清理本地数据缓存
|
|
164
|
+
*/
|
|
165
|
+
const clearStorage = createAPI('clearStorage', {
|
|
166
|
+
async implement() {
|
|
167
|
+
const gameId = context.game_id;
|
|
168
|
+
const { response } = await fetch<StorageResponse<void>>(`/api/games/user-storage/${gameId}/clear`, {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
appendHostCookie: true
|
|
171
|
+
});
|
|
172
|
+
applyNative('clearLocalStorageAsync');
|
|
173
|
+
const {
|
|
174
|
+
data: { code, message }
|
|
175
|
+
} = response;
|
|
176
|
+
if (code === 'SUCCESS') {
|
|
177
|
+
return {
|
|
178
|
+
code,
|
|
179
|
+
message
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
code,
|
|
185
|
+
message: `${message}.fallback to native`
|
|
186
|
+
} as StandardResponse<void>;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
commands.registerCommand('StorageSDK.clear', clearStorage);
|
|
191
|
+
|
|
192
|
+
registerCanIUse('storage.clear', {
|
|
193
|
+
version: '1.0.0'
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 异步获取当前storage的相关信息
|
|
198
|
+
*/
|
|
199
|
+
export const getStorageInfo = createAPI('getStorageInfo', {
|
|
200
|
+
async implement() {
|
|
201
|
+
const { data } = await applyNative('getStorageInfoAsync');
|
|
202
|
+
return data;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
registerCanIUse('getStorageInfo', {
|
|
207
|
+
version: '1.0.0',
|
|
208
|
+
success: {
|
|
209
|
+
keys: '1.0.0', // string[] 本地数据缓存中的所有键名列表,如本地数据则返回空数组
|
|
210
|
+
currentSize: '1.0.0', // number 当前占用的空间大小,以 KB 为单位
|
|
211
|
+
limitSize: '1.0.0' // number 存储空间上限,以 KB 为单位,一般来说会返回 10240
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 同步获取当前storage的相关信息
|
|
217
|
+
*/
|
|
218
|
+
export const getStorageInfoSync = createSyncAPI('getStorageInfoSync', {
|
|
219
|
+
implement() {
|
|
220
|
+
return invokeNative('getStorageInfoSync');
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const DATA_TYPE = [
|
|
225
|
+
'string',
|
|
226
|
+
'number',
|
|
227
|
+
'boolean',
|
|
228
|
+
'object',
|
|
229
|
+
'' // 表示不支持的类型
|
|
230
|
+
] as const;
|
|
231
|
+
|
|
232
|
+
type DataType = typeof DATA_TYPE[number];
|
|
233
|
+
|
|
234
|
+
function getType(data: unknown): DataType {
|
|
235
|
+
const type = Object.prototype.toString
|
|
236
|
+
.call(data)
|
|
237
|
+
.split(' ')[1]
|
|
238
|
+
.split(']')[0]
|
|
239
|
+
.toLocaleLowerCase() as DataType;
|
|
240
|
+
|
|
241
|
+
if (DATA_TYPE.includes(type)) {
|
|
242
|
+
return type;
|
|
243
|
+
}
|
|
244
|
+
return '';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 解析数据
|
|
249
|
+
*/
|
|
250
|
+
function parse(str: string, type: string) {
|
|
251
|
+
try {
|
|
252
|
+
switch (type) {
|
|
253
|
+
case 'string':
|
|
254
|
+
return str;
|
|
255
|
+
case 'object':
|
|
256
|
+
return JSON.parse(str);
|
|
257
|
+
case 'number':
|
|
258
|
+
return parseFloat(str);
|
|
259
|
+
case 'boolean':
|
|
260
|
+
return str === 'true';
|
|
261
|
+
default:
|
|
262
|
+
return '';
|
|
263
|
+
}
|
|
264
|
+
} catch {
|
|
265
|
+
return '';
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 序列化数据
|
|
271
|
+
*/
|
|
272
|
+
function stringify(data: unknown, type: DataType) {
|
|
273
|
+
try {
|
|
274
|
+
switch (type) {
|
|
275
|
+
case 'string':
|
|
276
|
+
case 'number':
|
|
277
|
+
case 'boolean':
|
|
278
|
+
return `${data}`;
|
|
279
|
+
case 'object':
|
|
280
|
+
return JSON.stringify(data);
|
|
281
|
+
default:
|
|
282
|
+
return '';
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
return '';
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { createCommands } from '@jolibox/common';
|
|
2
|
+
|
|
3
|
+
import { createAPI, registerCanIUse, t } from './base';
|
|
4
|
+
import { taskTracker } from '../report';
|
|
5
|
+
|
|
6
|
+
const commands = createCommands();
|
|
7
|
+
|
|
8
|
+
const onLevelFinished = createAPI('levelFinished', {
|
|
9
|
+
paramsSchema: t.tuple(
|
|
10
|
+
t.string(),
|
|
11
|
+
t.object({
|
|
12
|
+
result: t.boolean(),
|
|
13
|
+
duration: t.number()
|
|
14
|
+
})
|
|
15
|
+
),
|
|
16
|
+
implement: async (levelId, parmas) => {
|
|
17
|
+
const { result, duration } = parmas;
|
|
18
|
+
const tasks = [];
|
|
19
|
+
tasks.push(
|
|
20
|
+
taskTracker.reportToNative({
|
|
21
|
+
event: 'LEVEL_FINISHED',
|
|
22
|
+
params: {
|
|
23
|
+
levelId,
|
|
24
|
+
result,
|
|
25
|
+
duration
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
tasks.push(
|
|
31
|
+
taskTracker.reporter({
|
|
32
|
+
event: 'COMPLETE_QUEST'
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
await Promise.all(tasks);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const onTaskFinished = createAPI('taskFinished', {
|
|
40
|
+
paramsSchema: t.tuple(
|
|
41
|
+
t.string(),
|
|
42
|
+
t.object({
|
|
43
|
+
duration: t.number()
|
|
44
|
+
})
|
|
45
|
+
),
|
|
46
|
+
implement: async (taskId, params) => {
|
|
47
|
+
const { duration } = params;
|
|
48
|
+
return await taskTracker.reportToNative({
|
|
49
|
+
event: 'TASK_FINISHED',
|
|
50
|
+
params: {
|
|
51
|
+
duration,
|
|
52
|
+
taskId
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const onLevelUpgrade = createAPI('levelUpgrade', {
|
|
59
|
+
paramsSchema: t.tuple(t.string(), t.string()),
|
|
60
|
+
implement: async (levelId, name) => {
|
|
61
|
+
const tasks = [];
|
|
62
|
+
tasks.push(
|
|
63
|
+
taskTracker.reporter({
|
|
64
|
+
event: 'COMPLETE_QUEST'
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
tasks.push(
|
|
68
|
+
taskTracker.reportToNative({
|
|
69
|
+
event: 'LEVEL_UPGRADE',
|
|
70
|
+
params: {
|
|
71
|
+
name,
|
|
72
|
+
levelId
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
await Promise.all(tasks);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const onHistoryUserLevel = createAPI('onHistoryUserLevel', {
|
|
81
|
+
paramsSchema: t.tuple(t.number()),
|
|
82
|
+
implement: async (level) => {
|
|
83
|
+
return await taskTracker.reportToNative({
|
|
84
|
+
event: 'HISTORY_USER_LEVEL',
|
|
85
|
+
params: {
|
|
86
|
+
level
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const onHistoryUserScore = createAPI('onHistoryUserScore', {
|
|
93
|
+
paramsSchema: t.tuple(t.number()),
|
|
94
|
+
implement: async (score) => {
|
|
95
|
+
return await taskTracker.reportToNative({
|
|
96
|
+
event: 'HISTORY_USER_SCORE',
|
|
97
|
+
params: {
|
|
98
|
+
score
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const onTaskEvent = createAPI('taskEvent', {
|
|
105
|
+
paramsSchema: t.tuple(
|
|
106
|
+
t.string(),
|
|
107
|
+
t.object({
|
|
108
|
+
tools: t
|
|
109
|
+
.array(
|
|
110
|
+
t.object({
|
|
111
|
+
id: t.string(),
|
|
112
|
+
name: t.string(),
|
|
113
|
+
count: t.number(),
|
|
114
|
+
description: t.string().optional(),
|
|
115
|
+
price: t
|
|
116
|
+
.object({
|
|
117
|
+
amount: t.number(),
|
|
118
|
+
unit: t.string()
|
|
119
|
+
})
|
|
120
|
+
.optional()
|
|
121
|
+
})
|
|
122
|
+
)
|
|
123
|
+
.optional(),
|
|
124
|
+
awards: t
|
|
125
|
+
.array(
|
|
126
|
+
t.object({
|
|
127
|
+
id: t.string(),
|
|
128
|
+
name: t.string()
|
|
129
|
+
})
|
|
130
|
+
)
|
|
131
|
+
.optional()
|
|
132
|
+
})
|
|
133
|
+
),
|
|
134
|
+
implement: async (taskId, params) => {
|
|
135
|
+
const tasks = [];
|
|
136
|
+
tasks.push(
|
|
137
|
+
taskTracker.reportToNative({
|
|
138
|
+
event: 'TASK_EVENT',
|
|
139
|
+
params: {
|
|
140
|
+
taskId,
|
|
141
|
+
...params
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
// 使用了道具,上报任务
|
|
146
|
+
if (params.tools?.length ?? 0 > 0) {
|
|
147
|
+
tasks.push(
|
|
148
|
+
taskTracker.reporter({
|
|
149
|
+
event: 'USE_GAME_ITEM'
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
await Promise.all(tasks);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const onTaskPayment = createAPI('taskPayment', {
|
|
158
|
+
paramsSchema: t.tuple(
|
|
159
|
+
t.string(),
|
|
160
|
+
t.object({
|
|
161
|
+
id: t.string(),
|
|
162
|
+
amount: t.number(),
|
|
163
|
+
unit: t.string()
|
|
164
|
+
})
|
|
165
|
+
),
|
|
166
|
+
implement: async (taskId, params) => {
|
|
167
|
+
const tasks = [];
|
|
168
|
+
tasks.push(
|
|
169
|
+
taskTracker.reportToNative({
|
|
170
|
+
event: 'TASK_PAYMENT',
|
|
171
|
+
params: {
|
|
172
|
+
taskId,
|
|
173
|
+
params
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
tasks.push(
|
|
178
|
+
taskTracker.reporter({
|
|
179
|
+
event: 'IN_GAME_PURCHASES'
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
await Promise.all(tasks);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
commands.registerCommand('TaskTrackerSDK.levelFinished', onLevelFinished);
|
|
187
|
+
commands.registerCommand('TaskTrackerSDK.taskFinished', onTaskFinished);
|
|
188
|
+
commands.registerCommand('TaskTrackerSDK.levelUpgrade', onLevelUpgrade);
|
|
189
|
+
commands.registerCommand('TaskTrackerSDK.historyUserLevel', onHistoryUserLevel);
|
|
190
|
+
commands.registerCommand('TaskTrackerSDK.historyUserScore', onHistoryUserScore);
|
|
191
|
+
commands.registerCommand('TaskTrackerSDK.taskEvent', onTaskEvent);
|
|
192
|
+
commands.registerCommand('TaskTrackerSDK.taskPayment', onTaskPayment);
|
|
193
|
+
|
|
194
|
+
registerCanIUse('TaskTrackerSDK.onLevelFinished', {
|
|
195
|
+
version: '1.0.0',
|
|
196
|
+
properties: {
|
|
197
|
+
levelId: '1.0.0',
|
|
198
|
+
params: {
|
|
199
|
+
result: '1.0.0',
|
|
200
|
+
duration: '1.0.0'
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
registerCanIUse('TaskTrackerSDK.onTaskFinished', {
|
|
206
|
+
version: '1.0.0',
|
|
207
|
+
properties: {
|
|
208
|
+
taskId: '1.0.0',
|
|
209
|
+
duration: '1.0.0'
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
registerCanIUse('TaskTrackerSDK.onLevelUpgrade', {
|
|
214
|
+
version: '1.0.0',
|
|
215
|
+
properties: {
|
|
216
|
+
levelId: '1.0.0',
|
|
217
|
+
name: '1.0.0'
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
registerCanIUse('TaskTrackerSDK.onHistoryUserLevel', {
|
|
222
|
+
version: '1.0.0',
|
|
223
|
+
properties: {
|
|
224
|
+
level: '1.0.0'
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
registerCanIUse('TaskTrackerSDK.onHistoryUserScore', {
|
|
229
|
+
version: '1.0.0',
|
|
230
|
+
properties: {
|
|
231
|
+
score: '1.0.0'
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
registerCanIUse('TaskTrackerSDK.onTaskEvent', {
|
|
236
|
+
version: '1.0.0',
|
|
237
|
+
properties: {
|
|
238
|
+
taskId: '1.0.0',
|
|
239
|
+
params: {
|
|
240
|
+
tools: {
|
|
241
|
+
id: '1.0.0',
|
|
242
|
+
name: '1.0.0',
|
|
243
|
+
count: '1.0.0',
|
|
244
|
+
description: '1.0.0',
|
|
245
|
+
price: {
|
|
246
|
+
amount: '1.0.0',
|
|
247
|
+
unit: '1.0.0'
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
awards: {
|
|
251
|
+
id: '1.0.0',
|
|
252
|
+
name: '1.0.0'
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
registerCanIUse('TaskTrackerSDK.onTaskPayment', {
|
|
259
|
+
version: '1.0.0',
|
|
260
|
+
properties: {
|
|
261
|
+
taskId: '1.0.0',
|
|
262
|
+
params: {
|
|
263
|
+
amount: '1.0.0',
|
|
264
|
+
unit: '1.0.0'
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createBridge, On } from '../js-bridge';
|
|
2
|
+
|
|
3
|
+
import { joliboxJSCore, initMessagePort } from '../js-core';
|
|
4
|
+
|
|
5
|
+
import { formatErrorCode } from '../../common/report/errors';
|
|
6
|
+
import { AnyFunction } from '@jolibox/types';
|
|
7
|
+
import { InternalJSCoreNotFoundError } from '@jolibox/common';
|
|
8
|
+
|
|
9
|
+
declare const globalThis: {
|
|
10
|
+
joliboxJSBridge?: {
|
|
11
|
+
callHandler: AnyFunction;
|
|
12
|
+
invokeHandler: AnyFunction;
|
|
13
|
+
subscribeHandler: AnyFunction;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
18
|
+
const jsCore = joliboxJSCore!;
|
|
19
|
+
|
|
20
|
+
if (!jsCore) {
|
|
21
|
+
throw new InternalJSCoreNotFoundError('No joliboxJScore is found, native bridge not found.');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
initMessagePort();
|
|
25
|
+
|
|
26
|
+
const core: jsb.JSCore = {
|
|
27
|
+
...jsCore
|
|
28
|
+
// publish(event: string, params: Record<string, unknown>, webviewIds: number[]) {
|
|
29
|
+
// if (messagePort) {
|
|
30
|
+
// messagePort.publish(event, params);
|
|
31
|
+
// return;
|
|
32
|
+
// }
|
|
33
|
+
// jsCore.publish(event, params, webviewIds);
|
|
34
|
+
// }
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const bridge = createBridge(core);
|
|
38
|
+
|
|
39
|
+
const { invokeHandler } = bridge;
|
|
40
|
+
|
|
41
|
+
export const { applyNative, invokeNative, onNative, offNative, subscribeHandler } = bridge;
|
|
42
|
+
|
|
43
|
+
export const onNativeWithError: On = (event, handler) => {
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
+
onNative(event as keyof jsb.service.NativeEventMap, (...rest: any) => {
|
|
46
|
+
if (rest[0]?.errorCode) {
|
|
47
|
+
rest[0].errorCode = formatErrorCode(event, rest[0].errorCode);
|
|
48
|
+
}
|
|
49
|
+
handler(...rest);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
globalThis.joliboxJSBridge = {
|
|
54
|
+
callHandler: invokeHandler,
|
|
55
|
+
invokeHandler,
|
|
56
|
+
subscribeHandler
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { RuntimeLoader } from '../js-core';
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { onNative, RuntimeLoader } from './bridge';
|
|
2
|
+
import { joliboxJSCore } from '../js-core';
|
|
3
|
+
import { HostUserInfo } from '@/common/context/types';
|
|
4
|
+
import { context } from '@/common/context';
|
|
5
|
+
import { hostEmitter } from '@jolibox/common';
|
|
6
|
+
import { taskTracker, track } from '../report';
|
|
7
|
+
|
|
8
|
+
RuntimeLoader.onReady(() => {
|
|
9
|
+
// TODO: merge some env config
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
RuntimeLoader.doExit(() => {
|
|
13
|
+
//埋点上报
|
|
14
|
+
track('onBeforeExit', {
|
|
15
|
+
timestamp: Date.now()
|
|
16
|
+
});
|
|
17
|
+
return false; // 默认不打断退出
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The DOMContentLoaded event might be triggered very early,
|
|
22
|
+
* so the global loaded listener should be executed during the bootstrap process.
|
|
23
|
+
*/
|
|
24
|
+
function addWebviewReadyListener() {
|
|
25
|
+
hostEmitter.on('onDocumentReady', () => {
|
|
26
|
+
joliboxJSCore?.onDocumentReady(window.location.href);
|
|
27
|
+
track('onDocumentReady', {
|
|
28
|
+
start_timestamp,
|
|
29
|
+
timestamp: Date.now()
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function addGameServiceReadyListener() {
|
|
35
|
+
onNative('onJoliboxServiceReady', ({ runtimeInfo, loadDuration }) => {
|
|
36
|
+
runtimeInfo && context.onEnvConfigChanged({ hostUserInfo: runtimeInfo });
|
|
37
|
+
hostEmitter.emit('LifecycleEvent.onReady', {
|
|
38
|
+
...(context.hostUserInfo ? context.hostUserInfo : { isLogin: false })
|
|
39
|
+
});
|
|
40
|
+
track('joliboxServiceReady', {
|
|
41
|
+
start_timestamp,
|
|
42
|
+
timestamp: Date.now()
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 任务上报
|
|
46
|
+
taskTracker.start(loadDuration);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
onNative('onBeforeExit', ({ uuid }) => {
|
|
50
|
+
joliboxJSCore?.doExit(uuid);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
let start_timestamp: number;
|
|
54
|
+
export function config(): void {
|
|
55
|
+
start_timestamp = Date.now();
|
|
56
|
+
addGameServiceReadyListener();
|
|
57
|
+
addWebviewReadyListener();
|
|
58
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const CUSTOM_EVENT_PREFIX = 'custom_event_';
|
|
2
|
+
|
|
3
|
+
export const HOST_EVENT_PREFIX = 'host_event_';
|
|
4
|
+
|
|
5
|
+
//TODO: add buffer supported method
|
|
6
|
+
export const BUFFER_METHODS: string[] = [];
|
|
7
|
+
|
|
8
|
+
// TODO: add method cannot invoke background
|
|
9
|
+
export const BACKGROUND_FORBIDDEN_METHODS: string[] = [];
|
|
10
|
+
|
|
11
|
+
export const SYNC_METHODS: string[] = ['env', 'createRequestTask', 'login'];
|