@ad-execute-manager/core 2.0.0-alpha.4 → 2.0.1
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/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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ad-execute-manager/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
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",
|