@ad-execute-manager/core 2.0.0-alpha.3 → 2.0.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/README.md +367 -110
- package/dist/AdExecuteManager.d.ts +26 -9
- package/dist/typings/ad.d.ts +208 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,110 +1,367 @@
|
|
|
1
|
-
# @
|
|
2
|
-
|
|
3
|
-
Core functionality for ad execution management including AdExecuteManager, utility functions, and middleware composition.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **AdExecuteManager**: A powerful ad execution management class for handling reward-based ads, interstitial ads, and other advertising formats
|
|
14
|
-
- **compose**: A middleware composition utility inspired by Koa
|
|
15
|
-
- **needRetryAdError**: A utility function for determining if an ad error should be retried
|
|
16
|
-
|
|
17
|
-
## Usage
|
|
18
|
-
|
|
19
|
-
### AdExecuteManager
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
import { AdExecuteManager } from '@
|
|
23
|
-
|
|
24
|
-
const adManager = new AdExecuteManager({
|
|
25
|
-
// Configuration options
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
// Initialize ad
|
|
29
|
-
const result = await adManager.init();
|
|
30
|
-
|
|
31
|
-
// Show ad
|
|
32
|
-
const showResult = await adManager.show();
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### Middleware Composition
|
|
36
|
-
|
|
37
|
-
```javascript
|
|
38
|
-
import { compose } from '@
|
|
39
|
-
|
|
40
|
-
const middlewares = [
|
|
41
|
-
async (ctx, next) => {
|
|
42
|
-
console.log('Middleware 1 start');
|
|
43
|
-
await next();
|
|
44
|
-
console.log('Middleware 1 end');
|
|
45
|
-
},
|
|
46
|
-
async (ctx, next) => {
|
|
47
|
-
console.log('Middleware 2 start');
|
|
48
|
-
await next();
|
|
49
|
-
console.log('Middleware 2 end');
|
|
50
|
-
}
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const composedMiddleware = compose(middlewares);
|
|
54
|
-
await composedMiddleware({});
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Error Retry Utility
|
|
58
|
-
|
|
59
|
-
```javascript
|
|
60
|
-
import { needRetryAdError } from '@
|
|
61
|
-
|
|
62
|
-
const apiError = {
|
|
63
|
-
errMsg: 'ad_show_timeout: normal',
|
|
64
|
-
timeout: 5000
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const shouldRetry = needRetryAdError({
|
|
68
|
-
apiError,
|
|
69
|
-
configuredAdTimeout: 5000,
|
|
70
|
-
errorRetryStrategy: {
|
|
71
|
-
timeout: true,
|
|
72
|
-
background: true
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
console.log('Should retry:', shouldRetry);
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
##
|
|
80
|
-
|
|
81
|
-
###
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
1
|
+
# @ad-execute-manager/core
|
|
2
|
+
|
|
3
|
+
Core functionality for ad execution management including AdExecuteManager, utility functions, and middleware composition.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ad-execute-manager/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **AdExecuteManager**: A powerful ad execution management class for handling reward-based ads, interstitial ads, and other advertising formats
|
|
14
|
+
- **compose**: A middleware composition utility inspired by Koa
|
|
15
|
+
- **needRetryAdError**: A utility function for determining if an ad error should be retried
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### AdExecuteManager
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { AdExecuteManager } from '@ad-execute-manager/core';
|
|
23
|
+
|
|
24
|
+
const adManager = new AdExecuteManager({
|
|
25
|
+
// Configuration options
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Initialize ad
|
|
29
|
+
const result = await adManager.init();
|
|
30
|
+
|
|
31
|
+
// Show ad
|
|
32
|
+
const showResult = await adManager.show();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Middleware Composition
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import { compose } from '@ad-execute-manager/core';
|
|
39
|
+
|
|
40
|
+
const middlewares = [
|
|
41
|
+
async (ctx, next) => {
|
|
42
|
+
console.log('Middleware 1 start');
|
|
43
|
+
await next();
|
|
44
|
+
console.log('Middleware 1 end');
|
|
45
|
+
},
|
|
46
|
+
async (ctx, next) => {
|
|
47
|
+
console.log('Middleware 2 start');
|
|
48
|
+
await next();
|
|
49
|
+
console.log('Middleware 2 end');
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
const composedMiddleware = compose(middlewares);
|
|
54
|
+
await composedMiddleware({});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Error Retry Utility
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
import { needRetryAdError } from '@ad-execute-manager/core';
|
|
61
|
+
|
|
62
|
+
const apiError = {
|
|
63
|
+
errMsg: 'ad_show_timeout: normal',
|
|
64
|
+
timeout: 5000
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const shouldRetry = needRetryAdError({
|
|
68
|
+
apiError,
|
|
69
|
+
configuredAdTimeout: 5000,
|
|
70
|
+
errorRetryStrategy: {
|
|
71
|
+
timeout: true,
|
|
72
|
+
background: true
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log('Should retry:', shouldRetry);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Examples
|
|
80
|
+
|
|
81
|
+
### 实际应用场景示例
|
|
82
|
+
|
|
83
|
+
#### 1. 完整的广告执行流程
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
import { AdExecuteManager } from '@ad-execute-manager/core';
|
|
87
|
+
|
|
88
|
+
// 创建广告执行管理器实例
|
|
89
|
+
const adManager = new AdExecuteManager({
|
|
90
|
+
log: true,
|
|
91
|
+
enableVisibilityListener: true,
|
|
92
|
+
maxRetryCount: 2,
|
|
93
|
+
errorRetryStrategy: {
|
|
94
|
+
timeout: true,
|
|
95
|
+
background: true
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// 假设我们有一个广告实例类
|
|
100
|
+
class MyRewardAd {
|
|
101
|
+
constructor() {
|
|
102
|
+
this._adTimeoutTime = 5000;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
initialize(options) {
|
|
106
|
+
console.log('Initializing ad with options:', options);
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async ad(ctx, next) {
|
|
111
|
+
const { options, collection, recovered } = ctx;
|
|
112
|
+
console.log('Showing ad with options:', options);
|
|
113
|
+
console.log('Recovered info:', recovered);
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
// 模拟广告加载和显示
|
|
117
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
118
|
+
|
|
119
|
+
// 模拟广告成功
|
|
120
|
+
if (collection && collection.onSuccess) {
|
|
121
|
+
collection.onSuccess();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
await next({ success: true });
|
|
125
|
+
return { success: true };
|
|
126
|
+
} catch (error) {
|
|
127
|
+
// 模拟广告失败
|
|
128
|
+
if (collection && collection.onFail) {
|
|
129
|
+
collection.onFail(error);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
await next({ success: false, error });
|
|
133
|
+
return { success: false, error };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
clear() {
|
|
138
|
+
console.log('Clearing ad resources');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
record(info) {
|
|
142
|
+
console.log('Recording ad info:', info);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 创建广告实例
|
|
147
|
+
const rewardAd = new MyRewardAd();
|
|
148
|
+
|
|
149
|
+
// 添加广告任务
|
|
150
|
+
const result = await adManager.addTask(rewardAd, {
|
|
151
|
+
options: {
|
|
152
|
+
adUnitId: 'your-ad-unit-id',
|
|
153
|
+
userId: 'user123'
|
|
154
|
+
},
|
|
155
|
+
collection: {
|
|
156
|
+
onSuccess: () => console.log('Ad success callback'),
|
|
157
|
+
onFail: (error) => console.log('Ad fail callback:', error),
|
|
158
|
+
onCancel: () => console.log('Ad cancel callback')
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
console.log('Ad execution result:', result);
|
|
163
|
+
|
|
164
|
+
// 等待所有任务完成
|
|
165
|
+
await adManager.whenAllTasksComplete();
|
|
166
|
+
console.log('All tasks completed');
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### 2. 自定义中间件组合
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
import { compose } from '@ad-execute-manager/core';
|
|
173
|
+
|
|
174
|
+
// 定义中间件
|
|
175
|
+
const middleware1 = async (ctx, next) => {
|
|
176
|
+
console.log('Middleware 1 start');
|
|
177
|
+
ctx.value1 = 'value1';
|
|
178
|
+
await next();
|
|
179
|
+
console.log('Middleware 1 end');
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const middleware2 = async (ctx, next) => {
|
|
183
|
+
console.log('Middleware 2 start');
|
|
184
|
+
console.log('Received value1:', ctx.value1);
|
|
185
|
+
ctx.value2 = 'value2';
|
|
186
|
+
await next();
|
|
187
|
+
console.log('Middleware 2 end');
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const middleware3 = async (ctx, next) => {
|
|
191
|
+
console.log('Middleware 3 start');
|
|
192
|
+
console.log('Received value1:', ctx.value1);
|
|
193
|
+
console.log('Received value2:', ctx.value2);
|
|
194
|
+
ctx.value3 = 'value3';
|
|
195
|
+
await next();
|
|
196
|
+
console.log('Middleware 3 end');
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// 组合中间件
|
|
200
|
+
const composedMiddleware = compose([middleware1, middleware2, middleware3]);
|
|
201
|
+
|
|
202
|
+
// 执行组合后的中间件
|
|
203
|
+
const ctx = { initialValue: 'initial' };
|
|
204
|
+
await composedMiddleware(ctx);
|
|
205
|
+
|
|
206
|
+
console.log('Final ctx:', ctx);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### 3. 错误处理和重试策略
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
import { needRetryAdError } from '@ad-execute-manager/core';
|
|
213
|
+
|
|
214
|
+
// 模拟不同类型的错误
|
|
215
|
+
const timeoutError = {
|
|
216
|
+
errMsg: 'ad_show_timeout: normal',
|
|
217
|
+
timeout: 5000
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const backgroundError = {
|
|
221
|
+
errMsg: 'app in background is not support show ad'
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const otherError = {
|
|
225
|
+
errMsg: 'ad_load_fail: network error'
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// 配置重试策略
|
|
229
|
+
const retryStrategy = {
|
|
230
|
+
timeout: true,
|
|
231
|
+
background: true
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// 检查各种错误是否需要重试
|
|
235
|
+
const shouldRetryTimeout = needRetryAdError({
|
|
236
|
+
apiError: timeoutError,
|
|
237
|
+
configuredAdTimeout: 5000,
|
|
238
|
+
errorRetryStrategy: retryStrategy
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const shouldRetryBackground = needRetryAdError({
|
|
242
|
+
apiError: backgroundError,
|
|
243
|
+
configuredAdTimeout: 5000,
|
|
244
|
+
errorRetryStrategy: retryStrategy
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const shouldRetryOther = needRetryAdError({
|
|
248
|
+
apiError: otherError,
|
|
249
|
+
configuredAdTimeout: 5000,
|
|
250
|
+
errorRetryStrategy: retryStrategy
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
console.log('Should retry timeout error:', shouldRetryTimeout);
|
|
254
|
+
console.log('Should retry background error:', shouldRetryBackground);
|
|
255
|
+
console.log('Should retry other error:', shouldRetryOther);
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## API
|
|
259
|
+
|
|
260
|
+
### AdExecuteManager
|
|
261
|
+
|
|
262
|
+
The main class for managing ad execution with support for initialization, showing, and error handling.
|
|
263
|
+
|
|
264
|
+
#### Constructor
|
|
265
|
+
|
|
266
|
+
```javascript
|
|
267
|
+
new AdExecuteManager(args)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
- **args** (Object): 构造函数参数
|
|
271
|
+
- **options** (Object, optional): 广告执行选项
|
|
272
|
+
- **log** (Boolean, optional): 是否打印日志
|
|
273
|
+
- **enableVisibilityListener** (Boolean, optional): 是否启用前后台监听
|
|
274
|
+
- **maxRetryCount** (Number, optional): 最大重试次数,默认为 1,0 表示不重试
|
|
275
|
+
- **errorRetryStrategy** (Object, optional): 错误重试策略
|
|
276
|
+
- **timeout** (Boolean, optional): 是否重试超时错误
|
|
277
|
+
- **background** (Boolean, optional): 是否重试后台错误
|
|
278
|
+
|
|
279
|
+
#### Methods
|
|
280
|
+
|
|
281
|
+
- **initialize(_args)**: 初始化 AdExecuteManager 实例
|
|
282
|
+
- **_args** (Any): 初始化参数
|
|
283
|
+
- 返回: AdExecuteManager 实例
|
|
284
|
+
|
|
285
|
+
- **addTask(adInstance, ctx)**: 添加广告任务
|
|
286
|
+
- **adInstance** (Object): RewardAdFather 的子类实例
|
|
287
|
+
- **ctx** (Object): 广告执行上下文
|
|
288
|
+
- **options** (Object): 广告执行选项
|
|
289
|
+
- **collection** (Object): 回调集合
|
|
290
|
+
- 返回: Promise,广告执行结果的 Promise
|
|
291
|
+
|
|
292
|
+
- **clearTasks()**: 清空任务栈并取消所有任务
|
|
293
|
+
|
|
294
|
+
- **getTaskCount()**: 获取当前未完成的任务总数
|
|
295
|
+
- 返回: Number,未完成的任务数量
|
|
296
|
+
|
|
297
|
+
- **isRunning()**: 是否有任务正在运行
|
|
298
|
+
- 返回: Boolean
|
|
299
|
+
|
|
300
|
+
- **getCurrentTaskId()**: 获取当前执行的任务 ID
|
|
301
|
+
- 返回: String|null,当前任务 ID
|
|
302
|
+
|
|
303
|
+
- **whenAllTasksComplete()**: 返回一个 Promise,当任务队列中的所有任务都完成时 resolve
|
|
304
|
+
- 返回: Promise<void>
|
|
305
|
+
|
|
306
|
+
- **enableVisibilityListener()**: 启用前后台监听
|
|
307
|
+
|
|
308
|
+
- **disableVisibilityListener()**: 禁用前后台监听
|
|
309
|
+
|
|
310
|
+
- **isVisibilityListenerEnabled()**: 获取前后台监听器状态
|
|
311
|
+
- 返回: Boolean,是否启用
|
|
312
|
+
|
|
313
|
+
- **destroyVisibilityListener()**: 销毁前后台监听器
|
|
314
|
+
|
|
315
|
+
- **static getInstance(args)**: 获取单例实例
|
|
316
|
+
- **args** (Object, optional): 构造函数参数
|
|
317
|
+
- 返回: AdExecuteManager 实例
|
|
318
|
+
|
|
319
|
+
- **static build(args)**: 获取单例实例
|
|
320
|
+
- **args** (Object, optional): 构造函数参数
|
|
321
|
+
- 返回: AdExecuteManager 实例
|
|
322
|
+
|
|
323
|
+
- **static new(args)**: 创建新实例
|
|
324
|
+
- **args** (Object, optional): 构造函数参数
|
|
325
|
+
- 返回: AdExecuteManager 实例
|
|
326
|
+
|
|
327
|
+
- **static getSafeInstance()**: 获取单例实例,如果不存在则返回 null
|
|
328
|
+
- 返回: AdExecuteManager|null
|
|
329
|
+
|
|
330
|
+
### compose
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
function compose(middlewares: Array<(ctx: any, next: () => Promise<void>) => Promise<void>>): (ctx: any) => Promise<void>
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
- **middlewares** (Array<Function>): KOA 中间件数组,每个中间件函数接收 ctx 和 next 参数
|
|
337
|
+
- 返回: Function,返回一个组合后的中间件函数,接收 ctx 参数并按顺序执行所有中间件
|
|
338
|
+
|
|
339
|
+
### needRetryAdError
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
function needRetryAdError({
|
|
343
|
+
apiError,
|
|
344
|
+
configuredAdTimeout,
|
|
345
|
+
errorRetryStrategy
|
|
346
|
+
}: {
|
|
347
|
+
apiError: { errMsg?: string; timeout?: number };
|
|
348
|
+
configuredAdTimeout: number;
|
|
349
|
+
errorRetryStrategy?: {
|
|
350
|
+
timeout?: boolean;
|
|
351
|
+
background?: boolean;
|
|
352
|
+
};
|
|
353
|
+
}): boolean
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
- **apiError** (Object): 广告错误信息
|
|
357
|
+
- **errMsg** (String, optional): 错误信息
|
|
358
|
+
- **timeout** (Number, optional): 超时时间
|
|
359
|
+
- **configuredAdTimeout** (Number): 配置的广告超时时间
|
|
360
|
+
- **errorRetryStrategy** (Object, optional): 错误重试策略
|
|
361
|
+
- **timeout** (Boolean, optional): 是否重试超时错误
|
|
362
|
+
- **background** (Boolean, optional): 是否重试后台错误
|
|
363
|
+
- 返回: Boolean,是否需要重试
|
|
364
|
+
|
|
365
|
+
## License
|
|
366
|
+
|
|
367
|
+
MIT
|
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
export default AdExecuteManager;
|
|
2
|
+
export type IAdInstance = {
|
|
3
|
+
/**
|
|
4
|
+
* 超时时间
|
|
5
|
+
*/
|
|
6
|
+
_adTimeoutTime: number;
|
|
7
|
+
/**
|
|
8
|
+
* 初始化广告
|
|
9
|
+
*/
|
|
10
|
+
initialize: (ctx: unknown) => IAdInstance;
|
|
11
|
+
/**
|
|
12
|
+
* 显示广告
|
|
13
|
+
*/
|
|
14
|
+
ad: (options: unknown, ctx: unknown, recovered: unknown) => Promise<unknown>;
|
|
15
|
+
clear: () => void;
|
|
16
|
+
};
|
|
17
|
+
export type IRewordAdConfig = import("./typings/ad.js").IRewordAdConfig;
|
|
18
|
+
export type CallbackCollection = import("./typings/ad.js").CallbackCollection;
|
|
2
19
|
export type AdTask = {
|
|
3
20
|
/**
|
|
4
21
|
* RewardAdFather的子类实例
|
|
5
22
|
*/
|
|
6
|
-
adInstance:
|
|
23
|
+
adInstance: IAdInstance;
|
|
7
24
|
/**
|
|
8
25
|
* 广告执行选项
|
|
9
26
|
*/
|
|
10
|
-
options:
|
|
27
|
+
options: IRewordAdConfig;
|
|
11
28
|
/**
|
|
12
29
|
* 回调集合
|
|
13
30
|
*/
|
|
14
|
-
callbackCollection:
|
|
31
|
+
callbackCollection: CallbackCollection;
|
|
15
32
|
/**
|
|
16
33
|
* 广告执行成功的回调函数
|
|
17
34
|
*/
|
|
@@ -191,15 +208,15 @@ declare class AdExecuteManager {
|
|
|
191
208
|
isVisibilityListenerEnabled(): boolean;
|
|
192
209
|
/**
|
|
193
210
|
* 添加广告任务
|
|
194
|
-
* @param {
|
|
211
|
+
* @param {IAdInstance} adInstance RewardAdFather的子类实例
|
|
195
212
|
* @param {Object} ctx 广告执行上下文
|
|
196
|
-
* @param {
|
|
197
|
-
* @param {
|
|
213
|
+
* @param {IRewordAdConfig} ctx.options 广告执行选项
|
|
214
|
+
* @param {CallbackCollection} ctx.collection 回调集合
|
|
198
215
|
* @returns {Promise} 广告执行结果的Promise
|
|
199
216
|
*/
|
|
200
|
-
addTask(adInstance:
|
|
201
|
-
options:
|
|
202
|
-
collection:
|
|
217
|
+
addTask(adInstance: IAdInstance, ctx: {
|
|
218
|
+
options: IRewordAdConfig;
|
|
219
|
+
collection: CallbackCollection;
|
|
203
220
|
}): Promise<any>;
|
|
204
221
|
/**
|
|
205
222
|
* 组合所有任务
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
export type IRewordAdConfig = {
|
|
2
|
+
/**
|
|
3
|
+
* 激励视频广告id - 必填
|
|
4
|
+
*/
|
|
5
|
+
adUnitId: string;
|
|
6
|
+
/**
|
|
7
|
+
* 是否开启进度提醒。开启时广告文案为「再看 n 个xxx」,关闭时为「再看 1 个xxx」。其中 n 表示用户当前还需额外观看广告的次数
|
|
8
|
+
*/
|
|
9
|
+
progressTip?: boolean | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* - 再得广告的奖励文案,用户每看完一个广告都会展示,multiton 为 true 时必填。
|
|
12
|
+
* - 文案完整内容为「再看 1 个xxx」,其中 xxx 是 multitonRewardMsg 配置的文案内容,最大长度为 7,
|
|
13
|
+
* - 文案内容根据 multitonRewardMsg 的配置按顺序展示。若 multitonRewardMsg 长度小于 multitonRewardTimes ,
|
|
14
|
+
* - 则后续的激励再得广告文案取 multitonRewardMsg 数组最后一个。
|
|
15
|
+
*/
|
|
16
|
+
multitonRewardMsg?: Array<string> | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* 是否开启激励再得广告
|
|
19
|
+
*/
|
|
20
|
+
multiton?: boolean | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* 额外观看广告的次数,合法的数据范围为 1~4,multiton 为 true 时必填
|
|
23
|
+
*/
|
|
24
|
+
multitonRewardTimes?: number | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* 场景值 - 自用参数,非tt API要求的参数
|
|
27
|
+
*/
|
|
28
|
+
scene?: number | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* 广告超时时间 单位ms - 自用参数,非tt API要求的参数
|
|
31
|
+
*/
|
|
32
|
+
timeout?: number | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* 重试次数 - 自用参数 ,非tt API要求的参数
|
|
35
|
+
*/
|
|
36
|
+
retry: number;
|
|
37
|
+
/**
|
|
38
|
+
* 是否绑定永远的错误事件 - 自用参数,非tt API要求的参数
|
|
39
|
+
*/
|
|
40
|
+
foreverErrorBind?: boolean | undefined;
|
|
41
|
+
};
|
|
42
|
+
export type InterstitialAdConfig = {
|
|
43
|
+
/**
|
|
44
|
+
* 单个广告单元的 id。可从「开放平台控制台-进入对应小程序-运营-商业化变现-广告管理」中获取 - 必填
|
|
45
|
+
*/
|
|
46
|
+
adUnitId: string;
|
|
47
|
+
/**
|
|
48
|
+
* 场景值 - 自用参数
|
|
49
|
+
*/
|
|
50
|
+
scene?: number | undefined;
|
|
51
|
+
};
|
|
52
|
+
export type IExeCallbackArgs = {
|
|
53
|
+
/**
|
|
54
|
+
* 广告执行场景
|
|
55
|
+
*/
|
|
56
|
+
scene: number;
|
|
57
|
+
/**
|
|
58
|
+
* 是否看完
|
|
59
|
+
*/
|
|
60
|
+
isEnded: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* 完整观看次数
|
|
63
|
+
*/
|
|
64
|
+
count: number;
|
|
65
|
+
/**
|
|
66
|
+
* 剩余观看次数
|
|
67
|
+
*/
|
|
68
|
+
remain?: number;
|
|
69
|
+
};
|
|
70
|
+
export type CallbackCollection = {
|
|
71
|
+
/**
|
|
72
|
+
* 每次外部回调
|
|
73
|
+
*/
|
|
74
|
+
always?: (ctx?: IExeCallbackArgs) => void;
|
|
75
|
+
/**
|
|
76
|
+
* 完整看完广告外部回调 不管一个还是几个
|
|
77
|
+
*/
|
|
78
|
+
finished?: (ctx?: IExeCallbackArgs) => void;
|
|
79
|
+
/**
|
|
80
|
+
* 半途退出广告外部回调
|
|
81
|
+
*/
|
|
82
|
+
halfway?: (ctx?: IExeCallbackArgs) => void;
|
|
83
|
+
/**
|
|
84
|
+
* 完成广告外部回调 不管看不看完
|
|
85
|
+
*/
|
|
86
|
+
complete?: (ctx?: IExeCallbackArgs) => void;
|
|
87
|
+
/**
|
|
88
|
+
* 取消广告外部回调
|
|
89
|
+
*/
|
|
90
|
+
onCancel?: (ctx?: IExeCallbackArgs) => void;
|
|
91
|
+
/**
|
|
92
|
+
* 展示广告外部回调
|
|
93
|
+
*/
|
|
94
|
+
before?: (ctx?: IExeCallbackArgs) => void;
|
|
95
|
+
/**
|
|
96
|
+
* 展示广告成功外部回调
|
|
97
|
+
*/
|
|
98
|
+
success?: (ctx?: IExeCallbackArgs) => void;
|
|
99
|
+
/**
|
|
100
|
+
* 计时回调
|
|
101
|
+
*/
|
|
102
|
+
prelude?: (ctx?: unknown) => void;
|
|
103
|
+
};
|
|
104
|
+
export type RecoveredInfo = {
|
|
105
|
+
/**
|
|
106
|
+
* 恢复重试次数
|
|
107
|
+
*/
|
|
108
|
+
count: number;
|
|
109
|
+
/**
|
|
110
|
+
* 是否恢复重试
|
|
111
|
+
*/
|
|
112
|
+
retry: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* 恢复重试原因
|
|
115
|
+
*/
|
|
116
|
+
message: string;
|
|
117
|
+
};
|
|
118
|
+
export type ICallbackArgs = {
|
|
119
|
+
/**
|
|
120
|
+
* 广告执行场景
|
|
121
|
+
*/
|
|
122
|
+
scene: number;
|
|
123
|
+
/**
|
|
124
|
+
* 是否看完
|
|
125
|
+
*/
|
|
126
|
+
isEnded: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* 完整观看次数
|
|
129
|
+
*/
|
|
130
|
+
count: number;
|
|
131
|
+
/**
|
|
132
|
+
* 剩余观看次数
|
|
133
|
+
*/
|
|
134
|
+
remain?: number;
|
|
135
|
+
/**
|
|
136
|
+
* 执行下一个任务的回调函数,手动调用以继续执行流程[end]
|
|
137
|
+
*/
|
|
138
|
+
end?: () => void;
|
|
139
|
+
/**
|
|
140
|
+
* 执行下一个任务的回调函数, 手动调用以继续执行流程[circle]
|
|
141
|
+
*/
|
|
142
|
+
circle?: (args: {
|
|
143
|
+
ignoreRemain: boolean;
|
|
144
|
+
scene: number;
|
|
145
|
+
}) => void;
|
|
146
|
+
};
|
|
147
|
+
export type IConnection = {
|
|
148
|
+
/**
|
|
149
|
+
* 广告中途退出回调
|
|
150
|
+
*/
|
|
151
|
+
onHalfway?: (args: ICallbackArgs) => void;
|
|
152
|
+
/**
|
|
153
|
+
* 广告展示回调
|
|
154
|
+
*/
|
|
155
|
+
onShow?: (args: ICallbackArgs) => void;
|
|
156
|
+
/**
|
|
157
|
+
* 广告执行成功回调
|
|
158
|
+
*/
|
|
159
|
+
onFinish?: (args: ICallbackArgs) => void;
|
|
160
|
+
/**
|
|
161
|
+
* 广告执行失败回调
|
|
162
|
+
*/
|
|
163
|
+
onError?: (e: unknown) => void;
|
|
164
|
+
};
|
|
165
|
+
export type IConstructArgs = {
|
|
166
|
+
/**
|
|
167
|
+
* 初始化标识
|
|
168
|
+
*/
|
|
169
|
+
sign: string;
|
|
170
|
+
/**
|
|
171
|
+
* 是否开启日志
|
|
172
|
+
*/
|
|
173
|
+
log?: boolean | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* 重试次数
|
|
176
|
+
*/
|
|
177
|
+
retry: number;
|
|
178
|
+
/**
|
|
179
|
+
* 是否保留tt激励视频广告实例
|
|
180
|
+
*/
|
|
181
|
+
preserveOnEnd: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* 激励视频参数 (可选)
|
|
184
|
+
*/
|
|
185
|
+
adConfig?: IRewordAdConfig | undefined;
|
|
186
|
+
/**
|
|
187
|
+
* 回调集合
|
|
188
|
+
*/
|
|
189
|
+
collection?: IConnection | undefined;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* 广告场景类型映射对象
|
|
193
|
+
*/
|
|
194
|
+
export type SceneTypeMap = {
|
|
195
|
+
readonly [x: number]: string;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* 广告场景类型文本映射对象(反向映射)
|
|
199
|
+
*/
|
|
200
|
+
export type SceneTextMap = { readonly [K in SceneTypeMap[keyof SceneTypeMap]]: { [P in keyof SceneTypeMap]: SceneTypeMap[P] extends K ? P : never; }[keyof SceneTypeMap]; };
|
|
201
|
+
/**
|
|
202
|
+
* 场景值枚举 可持续添加
|
|
203
|
+
*/
|
|
204
|
+
export type RewardAdTriggerScene = keyof SceneTextMap;
|
|
205
|
+
/**
|
|
206
|
+
* 场景值枚举 可持续添加
|
|
207
|
+
*/
|
|
208
|
+
export type RewardAdTriggerSceneType = keyof SceneTypeMap;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ad-execute-manager/core",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Core functionality for ad execution management including AdExecuteManager, utility functions, and middleware composition.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://github.com/singcl"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
|
-
"homepage": "https://npmjs.com/package/@
|
|
24
|
+
"homepage": "https://npmjs.com/package/@ad-execute-manager/core",
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
27
|
"url": "git+https://github.com/singcl/ad-execute-manager.git"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@ad-execute-manager/helper": "^2.0.
|
|
55
|
+
"@ad-execute-manager/helper": "^2.0.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@babel/eslint-parser": "^7.28.5",
|