@singcl/ad-execute-manager 1.5.2 → 1.5.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/README.md CHANGED
@@ -1 +1,200 @@
1
- # AD Execute Manager
1
+ # AD Execute Manager
2
+
3
+ A powerful and flexible ad execution management library for handling reward-based ads, interstitial ads, and other advertising formats in JavaScript applications.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Features](#features)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [API Reference](#api-reference)
11
+ - [Examples](#examples)
12
+ - [Contributing](#contributing)
13
+ - [License](#license)
14
+
15
+ ## Features
16
+
17
+ - **Unified Ad Execution**: Single interface for managing different types of ads
18
+ - **Task Queue Management**: Handles multiple ad execution tasks in a queue
19
+ - **Flexible Control**: Manual control over ad execution flow with `next` function
20
+ - **Error Handling**: Comprehensive error handling and logging
21
+ - **State Persistence**: Built-in storage for ad state management
22
+ - **Analytics Integration**: Built-in analytics support
23
+ - **Middleware Pattern**: Uses middleware pattern for ad execution flow
24
+ - **Cancellation Support**: Ability to clear and cancel pending tasks
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @singcl/ad-execute-manager
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Basic Usage
35
+
36
+ ```javascript
37
+ import { AdExecuteManager, RewardAdFather } from '@singcl/ad-execute-manager';
38
+
39
+ // Get the singleton instance
40
+ const adManager = AdExecuteManager.getInstance();
41
+
42
+ // Create an ad instance (extend RewardAdFather)
43
+ class MyRewardAd extends RewardAdFather {
44
+ async ad(ctx, next) {
45
+ // Your ad logic here
46
+ console.log('Executing reward ad');
47
+
48
+ // Call next when ready to proceed
49
+ await next();
50
+
51
+ return { success: true, message: 'Ad executed successfully' };
52
+ }
53
+ }
54
+
55
+ // Create ad instance
56
+ const myAd = new MyRewardAd();
57
+
58
+ // Add task to execution queue
59
+ const result = await adManager.addTask(myAd, {
60
+ options: { /* ad options */ },
61
+ collection: { /* callback collection */ }
62
+ });
63
+ ```
64
+
65
+ ### Advanced Usage
66
+
67
+ ```javascript
68
+ import { AdExecuteManager, RewardAdSceneTriggerManager } from '@singcl/ad-execute-manager';
69
+
70
+ // Initialize with logging enabled
71
+ const adManager = AdExecuteManager.getInstance({ log: true });
72
+
73
+ // Check if manager is running
74
+ if (adManager.isRunning()) {
75
+ console.log('Ad manager is currently executing tasks');
76
+ }
77
+
78
+ // Get current task ID
79
+ const currentTaskId = adManager.getCurrentTaskId();
80
+
81
+ // Get total number of pending tasks
82
+ const taskCount = adManager.getTaskCount();
83
+
84
+ // Wait for all tasks to complete
85
+ await adManager.whenAllTasksComplete();
86
+
87
+ // Clear all pending tasks
88
+ adManager.clearTasks();
89
+ ```
90
+
91
+ ## API Reference
92
+
93
+ ### AdExecuteManager
94
+
95
+ The main class for managing ad execution flow.
96
+
97
+ #### Methods
98
+
99
+ - `getInstance(args)`: Get the singleton instance of AdExecuteManager
100
+ - `getSafeInstance()`: Get the instance, returns null if not initialized
101
+ - `addTask(adInstance, ctx)`: Add an ad task to the execution queue
102
+ - `clearTasks()`: Cancel all pending tasks
103
+ - `getTaskCount()`: Get the number of pending tasks
104
+ - `isRunning()`: Check if tasks are currently running
105
+ - `getCurrentTaskId()`: Get the ID of the current executing task
106
+ - `whenAllTasksComplete()`: Returns a Promise that resolves when all tasks are complete
107
+
108
+ ### RewardAdFather
109
+
110
+ Base class for reward ad implementations.
111
+
112
+ ### InterstitialAdFather
113
+
114
+ Base class for interstitial ad implementations.
115
+
116
+ ### Other Exports
117
+
118
+ - `SerializableError`: Error class that can be serialized
119
+ - `Logger`: Logging utility
120
+ - `Storage`: Storage management
121
+ - `CountRecorder`: Ad execution counter
122
+ - `RewardAdGlobalRecorder`: Global ad recorder
123
+ - `RewardAdSceneTriggerManager`: Scene-based ad trigger manager
124
+ - `AdAnalyticsJS`: Analytics integration
125
+ - `RewardAdNovel`: Novel-specific reward ad implementation
126
+ - `InterstitialAdNovel`: Novel-specific interstitial ad implementation
127
+ - `PubSub`: Publish-subscribe pattern implementation
128
+
129
+ ## Examples
130
+
131
+ ### Reward Ad Unlock
132
+
133
+ ```javascript
134
+ import { AdExecuteManager, RewardAdFather } from '@singcl/ad-execute-manager';
135
+
136
+ class UnlockAd extends RewardAdFather {
137
+ async ad(ctx, next) {
138
+ const { options, collection } = ctx;
139
+
140
+ try {
141
+ // Show reward ad
142
+ const result = await this.showAd();
143
+
144
+ if (result.isRewarded) {
145
+ // Execute success callback
146
+ collection.onSuccess && collection.onSuccess(result);
147
+
148
+ // Proceed to next task
149
+ await next();
150
+
151
+ return { success: true, data: result };
152
+ } else {
153
+ // Ad not completed
154
+ collection.onCancel && collection.onCancel();
155
+ return { success: false, message: 'Ad not completed' };
156
+ }
157
+ } catch (error) {
158
+ collection.onError && collection.onError(error);
159
+ throw error;
160
+ }
161
+ }
162
+ }
163
+
164
+ const adManager = AdExecuteManager.getInstance();
165
+ const unlockAd = new UnlockAd();
166
+
167
+ await adManager.addTask(unlockAd, {
168
+ options: { /* ad options */ },
169
+ collection: {
170
+ onSuccess: () => console.log('Reward received!'),
171
+ onCancel: () => console.log('Ad cancelled'),
172
+ onError: (error) => console.error('Ad error:', error)
173
+ }
174
+ });
175
+ ```
176
+
177
+ More examples can be found in the [examples](./example/) directory.
178
+
179
+ ## Contributing
180
+
181
+ We welcome contributions to this project! Please follow these steps:
182
+
183
+ 1. Fork the repository
184
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
185
+ 3. Make your changes
186
+ 4. Commit your changes (`git commit -m 'Add some amazing feature'`)
187
+ 5. Push to the branch (`git push origin feature/amazing-feature`)
188
+ 6. Open a Pull Request
189
+
190
+ ### Development Scripts
191
+
192
+ - `npm run build`: Build the library for production
193
+ - `npm run dev`: Turn on watch mode
194
+ - `npm run lint`: Lint your code
195
+ - `npm run format`: Format your code
196
+ - `npm run test`: Run tests
197
+
198
+ ## License
199
+
200
+ This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
@@ -1,4 +1,7 @@
1
1
  export default AdAnalyticsJS;
2
+ export type EventDict = {
3
+ [x: string]: any;
4
+ };
2
5
  export type IConstructorArgs = {
3
6
  /**
4
7
  * 初始化标识
@@ -17,7 +20,7 @@ export type ICommonInfo = {
17
20
  [x: string]: any;
18
21
  };
19
22
  /**
20
- * @template {Object.<string, object>} EventDict
23
+ * @typedef {Object.<string, object>} EventDict
21
24
  */
22
25
  /**
23
26
  * @typedef {object} IConstructorArgs
@@ -257,13 +257,13 @@ declare class InterstitialAdNovel extends InterstitialAdFather {
257
257
  * @param {object} _args
258
258
  * @param {string} _args.scene 广告执行场景 必填
259
259
  * @param {string} [_args.msg] 广告执行成功原因
260
- * @param {result} _args.result 广告执行成功结果
260
+ * @param {0|1} _args.result 广告执行成功结果
261
261
  * @returns
262
262
  */
263
263
  protected _adShowSuccessAnalytics(_args: {
264
264
  scene: string;
265
265
  msg?: string;
266
- result: result;
266
+ result: 0 | 1;
267
267
  }): any;
268
268
  /**
269
269
  * 广告展示失败分析
@@ -273,14 +273,14 @@ declare class InterstitialAdNovel extends InterstitialAdFather {
273
273
  * @param {string} _args.scene 广告执行场景 必填
274
274
  * @param {string} _args.msg 广告执行失败原因 必填
275
275
  * @param {number} _args.errorCode 广告执行失败错误码 必填
276
- * @param {result} _args.result 广告执行失败结果
276
+ * @param {0|1} _args.result 广告执行失败结果
277
277
  * @returns
278
278
  */
279
279
  protected _adShowFailureAnalytics(_args: {
280
280
  scene: string;
281
281
  msg: string;
282
282
  errorCode: number;
283
- result: result;
283
+ result: 0 | 1;
284
284
  }): any;
285
285
  /**
286
286
  * 广告加载成功分析
@@ -289,13 +289,13 @@ declare class InterstitialAdNovel extends InterstitialAdFather {
289
289
  * @param {object} _args
290
290
  * @param {string} _args.scene 广告执行场景 必填
291
291
  * @param {string} [_args.msg] 广告执行成功原因
292
- * @param {result} _args.result 广告执行成功结果
292
+ * @param {0|1} _args.result 广告执行成功结果
293
293
  * @returns
294
294
  */
295
295
  protected _adLoadSuccessAnalytics(_args: {
296
296
  scene: string;
297
297
  msg?: string;
298
- result: result;
298
+ result: 0 | 1;
299
299
  }): any;
300
300
  /**
301
301
  * 广告加载失败分析
@@ -304,13 +304,13 @@ declare class InterstitialAdNovel extends InterstitialAdFather {
304
304
  * @param {object} _args
305
305
  * @param {string} _args.scene 广告执行场景 必填
306
306
  * @param {string} _args.msg 广告执行失败原因 必填
307
- * @param {result} _args.result 广告执行失败结果
307
+ * @param {0|1} _args.result 广告执行失败结果
308
308
  * @returns
309
309
  */
310
310
  protected _adLoadFailureAnalytics(_args: {
311
311
  scene: string;
312
312
  msg: string;
313
- result: result;
313
+ result: 0 | 1;
314
314
  }): any;
315
315
  /**
316
316
  * 广告关闭成功分析
@@ -257,13 +257,13 @@ declare class RewardAdNovel extends RewardAdFather {
257
257
  * @param {object} _args
258
258
  * @param {string} _args.scene 广告执行场景 必填
259
259
  * @param {string} [_args.msg] 广告执行成功原因
260
- * @param {result} _args.result 广告执行成功结果
260
+ * @param {0|1} _args.result 广告执行成功结果
261
261
  * @returns
262
262
  */
263
263
  protected _adShowSuccessAnalytics(_args: {
264
264
  scene: string;
265
265
  msg?: string;
266
- result: result;
266
+ result: 0 | 1;
267
267
  }): any;
268
268
  /**
269
269
  * 广告展示失败分析
@@ -273,14 +273,14 @@ declare class RewardAdNovel extends RewardAdFather {
273
273
  * @param {string} _args.scene 广告执行场景 必填
274
274
  * @param {string} _args.msg 广告执行失败原因 必填
275
275
  * @param {number} _args.errorCode 广告执行失败错误码 必填
276
- * @param {result} _args.result 广告执行失败结果
276
+ * @param {0|1} _args.result 广告执行失败结果
277
277
  * @returns
278
278
  */
279
279
  protected _adShowFailureAnalytics(_args: {
280
280
  scene: string;
281
281
  msg: string;
282
282
  errorCode: number;
283
- result: result;
283
+ result: 0 | 1;
284
284
  }): any;
285
285
  /**
286
286
  * 广告加载成功分析
@@ -289,13 +289,13 @@ declare class RewardAdNovel extends RewardAdFather {
289
289
  * @param {object} _args
290
290
  * @param {string} _args.scene 广告执行场景 必填
291
291
  * @param {string} [_args.msg] 广告执行成功原因
292
- * @param {result} _args.result 广告执行成功结果
292
+ * @param {0|1} _args.result 广告执行成功结果
293
293
  * @returns
294
294
  */
295
295
  protected _adLoadSuccessAnalytics(_args: {
296
296
  scene: string;
297
297
  msg?: string;
298
- result: result;
298
+ result: 0 | 1;
299
299
  }): any;
300
300
  /**
301
301
  * 广告加载失败分析
@@ -304,13 +304,13 @@ declare class RewardAdNovel extends RewardAdFather {
304
304
  * @param {object} _args
305
305
  * @param {string} _args.scene 广告执行场景 必填
306
306
  * @param {string} _args.msg 广告执行失败原因 必填
307
- * @param {result} _args.result 广告执行失败结果
307
+ * @param {0|1} _args.result 广告执行失败结果
308
308
  * @returns
309
309
  */
310
310
  protected _adLoadFailureAnalytics(_args: {
311
311
  scene: string;
312
312
  msg: string;
313
- result: result;
313
+ result: 0 | 1;
314
314
  }): any;
315
315
  /**
316
316
  * 广告关闭成功分析
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@singcl/ad-execute-manager",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {