@singcl/ad-execute-manager 1.5.1 → 1.5.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present, singcl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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.
@@ -17,7 +17,7 @@ export type ICommonInfo = {
17
17
  [x: string]: any;
18
18
  };
19
19
  /**
20
- * @template {Object.<string, object>} EventDictionary
20
+ * @template {Object.<string, object>} EventDict
21
21
  */
22
22
  /**
23
23
  * @typedef {object} IConstructorArgs
@@ -66,13 +66,13 @@ declare class AdAnalyticsJS {
66
66
  * 追踪自定义事件
67
67
  * 用户交互,如点击、提交
68
68
  *
69
- * @template {keyof EventDictionary} K
69
+ * @template {keyof EventDict} K
70
70
  * @param {K} eventName - The name of the event to track.
71
- * @param {EventDictionary[K]} properties - The properties associated with the event.
71
+ * @param {EventDict[K]} properties - The properties associated with the event.
72
72
  * @param {object} [debugParams] - The debug parameters.
73
73
  * @param {string} [debugParams.sign] - Whether to enable debug mode.
74
74
  */
75
- track<K extends keyof EventDictionary>(eventName: K, properties: EventDictionary[K], debugParams?: {
75
+ track<K extends keyof EventDict>(eventName: K, properties: EventDict[K], debugParams?: {
76
76
  sign?: string;
77
77
  }): void;
78
78
  /**
@@ -112,15 +112,15 @@ declare class AdAnalyticsJS {
112
112
  *
113
113
  * @template {'cpage'} K
114
114
  * @param {K} eventName - The name of the event to track.
115
- * @param {EventDictionary[K]} properties - The properties associated with the event.
115
+ * @param {EventDict[K]} properties - The properties associated with the event.
116
116
  */
117
- page<K extends "cpage">(eventName: K, properties: EventDictionary[K]): void;
117
+ page<K extends "cpage">(eventName: K, properties: EventDict[K]): void;
118
118
  /**
119
119
  *
120
- * @param {EventDictionary['cpage']} properties
120
+ * @param {EventDict['cpage']} properties
121
121
  * @returns
122
122
  */
123
- cpage(properties: EventDictionary["cpage"]): void;
123
+ cpage(properties: EventDict["cpage"]): void;
124
124
  /**
125
125
  * 站位方法,没有任何左右
126
126
  * @returns
@@ -1,115 +1 @@
1
- /**
2
- * A dictionary mapping event names to their corresponding properties.
3
- */
4
- export type EventDictionary = {
5
- /**
6
- * - 免费解锁弹窗
7
- */
8
- free_unlock_popup: {
9
- that: 0 | 1 | 2 | 3;
10
- trigger_time: 0 | 1 | 2 | 3;
11
- result: 0 | 1 | any;
12
- };
13
- /**
14
- * - 加载激励广告 加载激励广告时触发 - 无太大参考意义,可以忽略
15
- */
16
- incentive_ad_load: {
17
- scene: number;
18
- result: string;
19
- };
20
- /**
21
- * 展示激励广告 激励广告展示时触发
22
- */
23
- incentive_ad_show: {
24
- scene: number;
25
- result: string;
26
- msg: string;
27
- };
28
- /**
29
- * 关闭激励广告 激励广告关闭时触发
30
- */
31
- incentive_ad_close: {
32
- scene: number;
33
- ad_count: number;
34
- ad_view_duration: string;
35
- ad_is_completed: 0 | 1;
36
- };
37
- /**
38
- * 注册登录 触发时机 "打开小程序 进行注册/登录
39
- */
40
- login: {
41
- trigger_time: 1 | 2;
42
- result: 0 | 1;
43
- };
44
- /**
45
- * 简介页交互 触发时机 点击简介页的按钮
46
- */
47
- book_intro: {
48
- trigger_time: 1 | 2 | 3 | 4;
49
- book_title: 0 | 1;
50
- };
51
- /**
52
- * 进入小程序页面
53
- */
54
- cpage: {
55
- page: number;
56
- };
57
- /**
58
- * 点击书籍
59
- */
60
- click_book: {
61
- scene: number;
62
- book_title: 0 | 1;
63
- };
64
- /**
65
- * 添加桌面图标
66
- */
67
- add_shortcut: {
68
- result: 0 | 1;
69
- scene: number;
70
- };
71
- /**
72
- * 小说章节展示 "1、用户首次展示新的章节(该章节如果未解锁,不上报;2s内只上报一次)2、用户解锁新的章节"
73
- */
74
- chapter_show: {
75
- book_title: 0 | 1;
76
- chapter_number: number;
77
- is_end: number;
78
- };
79
- /**
80
- * 小说阅读开始
81
- */
82
- speed_read_card: {
83
- book_title: 0 | 1;
84
- result: number;
85
- trigger_time: number;
86
- };
87
- /**
88
- * 展示首页福利弹窗
89
- */
90
- home_welfare_popup_show: {
91
- trigger_time: 1 | 2;
92
- result: 0 | 1;
93
- };
94
- /**
95
- * 关闭首页福利弹窗
96
- */
97
- home_welfare_popup_close: {
98
- trigger_way: 0 | 1 | 2;
99
- result: 0 | 1;
100
- };
101
- /**
102
- * 展示免费领取弹窗
103
- */
104
- free_popup_show: {
105
- trigger_time: 1 | 2;
106
- result: 0 | 1;
107
- };
108
- /**
109
- * 关闭免费领取弹窗
110
- */
111
- free_popup_close: {
112
- trigger_way: 0 | 1 | 2;
113
- result: 0 | 1;
114
- };
115
- };
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@singcl/ad-execute-manager",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -15,6 +15,7 @@
15
15
  "dist"
16
16
  ],
17
17
  "author": "singcl",
18
+ "license": "MIT",
18
19
  "homepage": "https://npmjs.com/package/@singcl/ad-execute-manager",
19
20
  "scripts": {
20
21
  "build": "rslib build && tsc",