@jolibox/implement 1.1.4-beta.10
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 +13 -0
- package/.rush/temp/package-deps_build.json +105 -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 +25 -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 +18 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +13 -0
- package/dist/index.native.d.ts +2 -0
- package/dist/index.native.js +3 -0
- package/dist/native/api/ads.d.ts +1 -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 +8 -0
- package/dist/native/api/keyboard.d.ts +9 -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 +24 -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 +641 -0
- package/src/common/can-i-use.ts +19 -0
- package/src/common/context/index.ts +85 -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 +102 -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 +658 -0
- package/src/h5/api/base.ts +9 -0
- package/src/h5/api/get-system-info.ts +59 -0
- package/src/h5/api/index.ts +4 -0
- package/src/h5/api/lifecycle.ts +95 -0
- package/src/h5/api/storage.ts +173 -0
- package/src/h5/api/task.ts +190 -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 +42 -0
- package/src/index.native.ts +7 -0
- package/src/index.ts +9 -0
- package/src/native/api/ads.ts +52 -0
- package/src/native/api/base.ts +8 -0
- package/src/native/api/get-system-info.ts +44 -0
- package/src/native/api/index.ts +8 -0
- package/src/native/api/keyboard.ts +75 -0
- package/src/native/api/lifecycle.ts +76 -0
- package/src/native/api/login.ts +73 -0
- package/src/native/api/request.ts +154 -0
- package/src/native/api/storage.ts +287 -0
- package/src/native/api/task.ts +227 -0
- package/src/native/bootstrap/bridge.ts +59 -0
- package/src/native/bootstrap/index.ts +59 -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 +72 -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,227 @@
|
|
|
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.tracker('LevelFinished', {
|
|
21
|
+
levelId,
|
|
22
|
+
result,
|
|
23
|
+
duration
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
tasks.push(
|
|
28
|
+
taskTracker.reporter({
|
|
29
|
+
event: 'COMPLETE_GAME_LEVEL'
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
await Promise.all(tasks);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const onTaskFinished = createAPI('taskFinished', {
|
|
37
|
+
paramsSchema: t.tuple(
|
|
38
|
+
t.string(),
|
|
39
|
+
t.object({
|
|
40
|
+
duration: t.number()
|
|
41
|
+
})
|
|
42
|
+
),
|
|
43
|
+
implement: async (taskId, params) => {
|
|
44
|
+
const { duration } = params;
|
|
45
|
+
return await taskTracker.reportToNative({
|
|
46
|
+
event: 'TaskFinished',
|
|
47
|
+
params: {
|
|
48
|
+
duration,
|
|
49
|
+
taskId
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const onLevelUpgrade = createAPI('levelUpgrade', {
|
|
56
|
+
paramsSchema: t.tuple(t.string(), t.string()),
|
|
57
|
+
implement: async (levelId, name) => {
|
|
58
|
+
const tasks = [];
|
|
59
|
+
tasks.push(
|
|
60
|
+
taskTracker.reportToNative({
|
|
61
|
+
event: 'LevelUpgrade',
|
|
62
|
+
params: {
|
|
63
|
+
name,
|
|
64
|
+
levelId
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
);
|
|
68
|
+
await Promise.all(tasks);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const onHistoryUserLevel = createAPI('onHistoryUserLevel', {
|
|
73
|
+
paramsSchema: t.tuple(t.number()),
|
|
74
|
+
implement: async (level) => {
|
|
75
|
+
return await taskTracker.reportToNative({
|
|
76
|
+
event: 'HistoryUserLevel',
|
|
77
|
+
params: {
|
|
78
|
+
level
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const onHistoryUserScore = createAPI('onHistoryUserScore', {
|
|
85
|
+
paramsSchema: t.tuple(t.number()),
|
|
86
|
+
implement: async (score) => {
|
|
87
|
+
return await taskTracker.reportToNative({
|
|
88
|
+
event: 'HistoryUserScore',
|
|
89
|
+
params: {
|
|
90
|
+
score
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const onTaskEvent = createAPI('taskEvent', {
|
|
97
|
+
paramsSchema: t.tuple(
|
|
98
|
+
t.string(),
|
|
99
|
+
t.object({
|
|
100
|
+
tools: t
|
|
101
|
+
.array(
|
|
102
|
+
t.object({
|
|
103
|
+
id: t.string(),
|
|
104
|
+
name: t.string(),
|
|
105
|
+
count: t.number(),
|
|
106
|
+
description: t.string().optional(),
|
|
107
|
+
price: t
|
|
108
|
+
.object({
|
|
109
|
+
amount: t.number(),
|
|
110
|
+
unit: t.string()
|
|
111
|
+
})
|
|
112
|
+
.optional()
|
|
113
|
+
})
|
|
114
|
+
)
|
|
115
|
+
.optional(),
|
|
116
|
+
awards: t
|
|
117
|
+
.array(
|
|
118
|
+
t.object({
|
|
119
|
+
id: t.string(),
|
|
120
|
+
name: t.string()
|
|
121
|
+
})
|
|
122
|
+
)
|
|
123
|
+
.optional()
|
|
124
|
+
})
|
|
125
|
+
),
|
|
126
|
+
implement: async (taskId, params) => {
|
|
127
|
+
const tasks = [];
|
|
128
|
+
tasks.push(
|
|
129
|
+
taskTracker.reportToNative({
|
|
130
|
+
event: 'TaskEvent',
|
|
131
|
+
params: {
|
|
132
|
+
taskId,
|
|
133
|
+
...params
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
);
|
|
137
|
+
// 使用了道具,上报任务
|
|
138
|
+
if (params.tools?.length ?? 0 > 0) {
|
|
139
|
+
tasks.push(
|
|
140
|
+
taskTracker.reporter({
|
|
141
|
+
event: 'USE_GAME_ITEM'
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
tasks.push(
|
|
145
|
+
taskTracker.reportToNative({
|
|
146
|
+
event: 'UseGameItem',
|
|
147
|
+
params: {
|
|
148
|
+
taskId,
|
|
149
|
+
...params
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
await Promise.all(tasks);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
commands.registerCommand('TaskTrackerSDK.levelFinished', onLevelFinished);
|
|
159
|
+
commands.registerCommand('TaskTrackerSDK.taskFinished', onTaskFinished);
|
|
160
|
+
commands.registerCommand('TaskTrackerSDK.levelUpgrade', onLevelUpgrade);
|
|
161
|
+
commands.registerCommand('TaskTrackerSDK.historyUserLevel', onHistoryUserLevel);
|
|
162
|
+
commands.registerCommand('TaskTrackerSDK.historyUserScore', onHistoryUserScore);
|
|
163
|
+
commands.registerCommand('TaskTrackerSDK.taskEvent', onTaskEvent);
|
|
164
|
+
|
|
165
|
+
registerCanIUse('TaskTrackerSDK.onLevelFinished', {
|
|
166
|
+
version: '1.0.0',
|
|
167
|
+
properties: {
|
|
168
|
+
levelId: '1.0.0',
|
|
169
|
+
params: {
|
|
170
|
+
result: '1.0.0',
|
|
171
|
+
duration: '1.0.0'
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
registerCanIUse('TaskTrackerSDK.onTaskFinished', {
|
|
177
|
+
version: '1.0.0',
|
|
178
|
+
properties: {
|
|
179
|
+
taskId: '1.0.0',
|
|
180
|
+
duration: '1.0.0'
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
registerCanIUse('TaskTrackerSDK.onLevelUpgrade', {
|
|
185
|
+
version: '1.0.0',
|
|
186
|
+
properties: {
|
|
187
|
+
levelId: '1.0.0',
|
|
188
|
+
name: '1.0.0'
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
registerCanIUse('TaskTrackerSDK.onHistoryUserLevel', {
|
|
193
|
+
version: '1.0.0',
|
|
194
|
+
properties: {
|
|
195
|
+
level: '1.0.0'
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
registerCanIUse('TaskTrackerSDK.onHistoryUserScore', {
|
|
200
|
+
version: '1.0.0',
|
|
201
|
+
properties: {
|
|
202
|
+
score: '1.0.0'
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
registerCanIUse('TaskTrackerSDK.onTaskEvent', {
|
|
207
|
+
version: '1.0.0',
|
|
208
|
+
properties: {
|
|
209
|
+
taskId: '1.0.0',
|
|
210
|
+
params: {
|
|
211
|
+
tools: {
|
|
212
|
+
id: '1.0.0',
|
|
213
|
+
name: '1.0.0',
|
|
214
|
+
count: '1.0.0',
|
|
215
|
+
description: '1.0.0',
|
|
216
|
+
price: {
|
|
217
|
+
amount: '1.0.0',
|
|
218
|
+
unit: '1.0.0'
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
awards: {
|
|
222
|
+
id: '1.0.0',
|
|
223
|
+
name: '1.0.0'
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
@@ -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,59 @@
|
|
|
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
|
+
taskTracker.close(Date.now() - start_timestamp);
|
|
18
|
+
return false; // 默认不打断退出
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The DOMContentLoaded event might be triggered very early,
|
|
23
|
+
* so the global loaded listener should be executed during the bootstrap process.
|
|
24
|
+
*/
|
|
25
|
+
function addWebviewReadyListener() {
|
|
26
|
+
hostEmitter.on('onDocumentReady', () => {
|
|
27
|
+
joliboxJSCore?.onDocumentReady(window.location.href);
|
|
28
|
+
track('onDocumentReady', {
|
|
29
|
+
start_timestamp,
|
|
30
|
+
timestamp: Date.now()
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function addGameServiceReadyListener() {
|
|
36
|
+
onNative('onJoliboxServiceReady', ({ runtimeInfo, loadDuration }) => {
|
|
37
|
+
runtimeInfo && context.onEnvConfigChanged({ hostUserInfo: runtimeInfo });
|
|
38
|
+
hostEmitter.emit('LifecycleEvent.onReady', {
|
|
39
|
+
...(context.hostUserInfo ? context.hostUserInfo : { isLogin: false })
|
|
40
|
+
});
|
|
41
|
+
track('joliboxServiceReady', {
|
|
42
|
+
start_timestamp,
|
|
43
|
+
timestamp: Date.now()
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 任务上报
|
|
47
|
+
taskTracker.start(loadDuration);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
onNative('onBeforeExit', ({ uuid }) => {
|
|
51
|
+
joliboxJSCore?.doExit(uuid);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
let start_timestamp: number;
|
|
55
|
+
export function config(): void {
|
|
56
|
+
start_timestamp = Date.now();
|
|
57
|
+
addGameServiceReadyListener();
|
|
58
|
+
addWebviewReadyListener();
|
|
59
|
+
}
|
|
@@ -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'];
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { pack, unpack } from './utils';
|
|
2
|
+
import { Deferred, isObject, isPromiseLike, InternalApplyNativeError, logger } from '@jolibox/common';
|
|
3
|
+
import { formatErrorCode } from '../../common/report/errors';
|
|
4
|
+
import { BACKGROUND_FORBIDDEN_METHODS, BUFFER_METHODS, SYNC_METHODS } from './const';
|
|
5
|
+
import { createReportInvokeMetrics } from './report';
|
|
6
|
+
import { InvokeHandler, On } from './types';
|
|
7
|
+
|
|
8
|
+
import { AnyFunction } from '@jolibox/types';
|
|
9
|
+
|
|
10
|
+
interface Invokes {
|
|
11
|
+
/** 暴露给宿主, 触发调用的方法 */
|
|
12
|
+
invokeHandler: InvokeHandler;
|
|
13
|
+
/** 基础库内部, 调用宿主的方法, 返回 native 返回对象 */
|
|
14
|
+
invokeNative: (method: string, args?: Record<string, unknown>, webviewId?: number) => any;
|
|
15
|
+
/** 基础库内部, 调用宿主的方法, 会根据 errMsg throwError */
|
|
16
|
+
applyNative: (method: string, args?: Record<string, unknown>, webviewId?: number) => any;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Response = {
|
|
20
|
+
errMsg: string;
|
|
21
|
+
errNo?: number;
|
|
22
|
+
__timing?: {
|
|
23
|
+
receiveJSInvoke: number;
|
|
24
|
+
invokeCallback: number;
|
|
25
|
+
};
|
|
26
|
+
} & Record<string, unknown>;
|
|
27
|
+
|
|
28
|
+
// 后台期间积压的请求
|
|
29
|
+
const asyncInvokes: {
|
|
30
|
+
method: string;
|
|
31
|
+
args?: Record<string, unknown>;
|
|
32
|
+
resolve: AnyFunction;
|
|
33
|
+
}[] = [];
|
|
34
|
+
|
|
35
|
+
//TODO: support arrayBuffer
|
|
36
|
+
const NOT_SUPPORT_ARRAY_BUFFER = false;
|
|
37
|
+
|
|
38
|
+
export function createInvoke(jsCore: jsb.JSCore, onNative: On): Invokes {
|
|
39
|
+
let resolveId = 0;
|
|
40
|
+
let IS_FOREGROUND = true;
|
|
41
|
+
|
|
42
|
+
const resolveMap = new Map<number, AnyFunction>();
|
|
43
|
+
const reportInvokeMetrics = createReportInvokeMetrics(jsCore, onNative);
|
|
44
|
+
|
|
45
|
+
onNative('onJoliboxEnterBackground', () => {
|
|
46
|
+
IS_FOREGROUND = false;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
onNative('onJoliboxEnterForeground', () => {
|
|
50
|
+
IS_FOREGROUND = true;
|
|
51
|
+
// 从后台进入前台后, 处理之前在后台挤压的任务
|
|
52
|
+
asyncInvokes.forEach(async ({ method, args, resolve }) => {
|
|
53
|
+
const res = await invokeNative(method, args);
|
|
54
|
+
resolve(res);
|
|
55
|
+
});
|
|
56
|
+
// 清空积压
|
|
57
|
+
asyncInvokes.length = 0;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const invokeHandler: InvokeHandler = (resolveId, data): void => {
|
|
61
|
+
/**
|
|
62
|
+
* Android resolveId 是 `string`
|
|
63
|
+
* iOS 上待确认, IDE 上是 `number`
|
|
64
|
+
*/
|
|
65
|
+
logger.info('----resolveId', resolveId, data);
|
|
66
|
+
const resolve = resolveMap.get(Number(resolveId));
|
|
67
|
+
if (!resolve) return;
|
|
68
|
+
const response = unpack(data);
|
|
69
|
+
resolve(response);
|
|
70
|
+
resolveMap.delete(Number(resolveId));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const invokeNative = (
|
|
74
|
+
method: string,
|
|
75
|
+
args?: Record<string, unknown>
|
|
76
|
+
): Record<string, unknown> | Promise<Record<string, unknown>> => {
|
|
77
|
+
const startTime = Date.now();
|
|
78
|
+
const isForeground = `${IS_FOREGROUND}`;
|
|
79
|
+
|
|
80
|
+
resolveId += 1;
|
|
81
|
+
|
|
82
|
+
const deferred = new Deferred<Response>();
|
|
83
|
+
resolveMap.set(resolveId, deferred.resolve);
|
|
84
|
+
|
|
85
|
+
if (!IS_FOREGROUND && BACKGROUND_FORBIDDEN_METHODS.includes(method)) {
|
|
86
|
+
// `FOREGROUND_FORBIDDEN_EVENTS` all async API
|
|
87
|
+
return new Promise<Record<string, unknown>>((resolve) => {
|
|
88
|
+
asyncInvokes.push({ method, args, resolve });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const basicReportParam = {
|
|
93
|
+
method,
|
|
94
|
+
startTime,
|
|
95
|
+
args,
|
|
96
|
+
isForeground
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const invokeReportParam = {
|
|
100
|
+
...basicReportParam,
|
|
101
|
+
invokeType: 'js-bridge'
|
|
102
|
+
} as const;
|
|
103
|
+
|
|
104
|
+
const useArrayBuffer = !NOT_SUPPORT_ARRAY_BUFFER && BUFFER_METHODS.includes(method);
|
|
105
|
+
|
|
106
|
+
const payload = pack(args, useArrayBuffer);
|
|
107
|
+
const params = useArrayBuffer ? payload : JSON.stringify(payload);
|
|
108
|
+
|
|
109
|
+
/** 调用结果 */
|
|
110
|
+
let response;
|
|
111
|
+
|
|
112
|
+
if (method.endsWith('Sync') || SYNC_METHODS.includes(method)) {
|
|
113
|
+
response = jsCore.call?.(method, payload, resolveId);
|
|
114
|
+
} else {
|
|
115
|
+
if (typeof params === 'string') {
|
|
116
|
+
console.log('-----params', method, resolveId);
|
|
117
|
+
response = jsCore.invoke(method, params, resolveId);
|
|
118
|
+
} else {
|
|
119
|
+
response = jsCore.call?.(method, params, resolveId);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 如果 invoke 返回了结果,则为同步调用
|
|
125
|
+
* - iOS 返回 `undefined`
|
|
126
|
+
* - Android 返回空字符串 `""`
|
|
127
|
+
*/
|
|
128
|
+
if (response) {
|
|
129
|
+
try {
|
|
130
|
+
if (typeof response === 'string') {
|
|
131
|
+
response = JSON.parse(response);
|
|
132
|
+
}
|
|
133
|
+
response = unpack(response);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
logger.error(error);
|
|
136
|
+
}
|
|
137
|
+
resolveMap.delete(resolveId);
|
|
138
|
+
if (response.errorCode) {
|
|
139
|
+
response.errorCode = formatErrorCode(method, response.errorCode);
|
|
140
|
+
}
|
|
141
|
+
reportInvokeMetrics({ ...invokeReportParam, ...response });
|
|
142
|
+
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (method.endsWith('Sync')) {
|
|
147
|
+
resolveMap.delete(resolveId);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return deferred.promise.then((response) => {
|
|
151
|
+
if (response.errorCode) {
|
|
152
|
+
response.errorCode = formatErrorCode(method, response.errorCode as number | undefined);
|
|
153
|
+
}
|
|
154
|
+
reportInvokeMetrics({ ...invokeReportParam, ...response });
|
|
155
|
+
return response;
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const applyNative = (method: string, arg?: Record<string, unknown>) => {
|
|
160
|
+
const res = invokeNative(method, arg);
|
|
161
|
+
if (isPromiseLike(res)) {
|
|
162
|
+
return res.then((r) =>
|
|
163
|
+
ifThrowError(
|
|
164
|
+
method,
|
|
165
|
+
r as {
|
|
166
|
+
errMsg?: string;
|
|
167
|
+
errNo?: number;
|
|
168
|
+
errorType?: string;
|
|
169
|
+
errorCode?: number;
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return ifThrowError(method, res);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
invokeNative,
|
|
179
|
+
applyNative,
|
|
180
|
+
invokeHandler
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function ifThrowError(
|
|
185
|
+
method: string,
|
|
186
|
+
res: {
|
|
187
|
+
errMsg?: string;
|
|
188
|
+
errNo?: number;
|
|
189
|
+
errorType?: string;
|
|
190
|
+
errorCode?: number;
|
|
191
|
+
}
|
|
192
|
+
) {
|
|
193
|
+
if (!isObject(res)) {
|
|
194
|
+
logger.warn(`[Jolibox SDK]${method} no response value`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const { errMsg, errNo, errorType, errorCode, ...data } = res;
|
|
198
|
+
|
|
199
|
+
if (errMsg && errMsg !== `${method}:ok`) {
|
|
200
|
+
throw new InternalApplyNativeError(errMsg, errNo, errorType, errorCode);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export class ApplyNativeError extends Error {
|
|
207
|
+
constructor(message: string, readonly errNo?: number) {
|
|
208
|
+
super(message);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createInvoke } from './invoke';
|
|
2
|
+
import { createSubscribe } from './subscribe';
|
|
3
|
+
import { JSBridge } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* build js-bridge
|
|
7
|
+
* @param jsCore jsCore function inject by native
|
|
8
|
+
*/
|
|
9
|
+
export function createBridge(jsCore: jsb.JSCore): JSBridge {
|
|
10
|
+
const { subscribeHandler, onNative, offNative } = createSubscribe(jsCore);
|
|
11
|
+
|
|
12
|
+
const { invokeNative, invokeHandler, applyNative } = createInvoke(jsCore, onNative);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
// 宿主调用
|
|
16
|
+
invokeHandler,
|
|
17
|
+
subscribeHandler,
|
|
18
|
+
applyNative,
|
|
19
|
+
invokeNative,
|
|
20
|
+
onNative,
|
|
21
|
+
offNative
|
|
22
|
+
};
|
|
23
|
+
}
|