@mingto/version-polling 1.0.31

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 版本轮询 Worker 主函数
3
+ * - 支持两种检测模式:
4
+ * 1)VERSION:基于 {version: X.Y.Z} 版本号比较
5
+ * 2)IDENTIFIER:基于 ETag/Last-Modified 变化
6
+ */
7
+ declare function pollingWorker(): void;
8
+ export default pollingWorker;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * 轮询检测方式
3
+ */
4
+ declare enum DetectionMode {
5
+ IDENTIFIER = "identifier",
6
+ VERSION = "version"
7
+ }
8
+ /**
9
+ * 轮询配置项
10
+ */
11
+ interface VersionPollingOptions {
12
+ readonly pollingInterval: number;
13
+ readonly fileUrl: string;
14
+ readonly detectionMode: DetectionMode;
15
+ }
16
+ /**
17
+ * 发送消息给worker
18
+ */
19
+ interface SendWorkerMessage<T = any> {
20
+ readonly type: 'init' | 'start' | 'resume' | 'pause';
21
+ readonly data?: T;
22
+ }
23
+ /**
24
+ * 接收来自worker的消息
25
+ */
26
+ type ReplyWorkerMessage<T = any> = {
27
+ readonly type: 'update';
28
+ readonly data: T;
29
+ } | {
30
+ readonly type: 'check';
31
+ readonly data: T;
32
+ };
33
+ /**
34
+ * 对外暴露的事件名称
35
+ */
36
+ type PublicCustomEventName = 'update';
37
+ export { DetectionMode };
38
+ export type { PublicCustomEventName, ReplyWorkerMessage, SendWorkerMessage, VersionPollingOptions };
@@ -0,0 +1,10 @@
1
+ interface EventCallback {
2
+ (...args: any[]): any;
3
+ }
4
+ declare class EventBus<E = any> {
5
+ private listeners;
6
+ on(eventName: E, callback: EventCallback): void;
7
+ emit<T = any>(eventName: E, data?: T): void;
8
+ }
9
+ declare function createEventBus<E>(): EventBus<E>;
10
+ export { createEventBus };
@@ -0,0 +1 @@
1
+ export * from './event-bus';
@@ -0,0 +1,61 @@
1
+ import { PublicCustomEventName, VersionPollingOptions } from './types';
2
+ /**
3
+ * 版本轮询类
4
+ * 用于定期检查服务器上的文件版本(HTML 或 JSON),并在检测到更新时通知客户端
5
+ */
6
+ declare class VersionPolling {
7
+ private worker;
8
+ private options;
9
+ private lastIdentifier;
10
+ private lastVersion;
11
+ constructor(options: Partial<VersionPollingOptions>);
12
+ /**
13
+ * 初始化配置项
14
+ * @param options - 用户传入的配置部分
15
+ */
16
+ private initializeOptions;
17
+ /**
18
+ * 初始化 Worker 并设置消息监听
19
+ */
20
+ private initializeWorker;
21
+ /**
22
+ * 处理检查消息
23
+ * @param data - Worker 返回的数据
24
+ */
25
+ private handleCheckMessage;
26
+ /**
27
+ * 设置页面可见性监听器
28
+ * 当页面不可见时暂停轮询,可见时恢复轮询
29
+ */
30
+ private setupVisibilityListener;
31
+ /**
32
+ * 处理页面可见性变化事件
33
+ */
34
+ private onVisibilityChange;
35
+ /**
36
+ * 当检测到版本更新时触发
37
+ */
38
+ private onVersionUpdate;
39
+ /**
40
+ * 开始轮询
41
+ */
42
+ private startPolling;
43
+ /**
44
+ * 停止轮询并清理资源
45
+ */
46
+ private stopPolling;
47
+ /**
48
+ * 触发事件
49
+ * @param eventName - 事件名称
50
+ * @param data - 事件数据
51
+ */
52
+ private emit;
53
+ /**
54
+ * 监听事件
55
+ * @param eventName - 事件名称
56
+ * @param callback - 回调函数
57
+ * @returns 当前实例,支持链式调用
58
+ */
59
+ on(eventName: PublicCustomEventName, callback: (data?: any) => void): VersionPolling;
60
+ }
61
+ export default VersionPolling;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@mingto/version-polling",
3
+ "type": "module",
4
+ "version": "1.0.31",
5
+ "description": "实时检测是否发布新版本",
6
+ "author": "hcc",
7
+ "license": "ISC",
8
+ "keywords": [
9
+ "检测",
10
+ "线上环境版本"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "vite build"
23
+ },
24
+ "dependencies": {
25
+ "@mingto/tools": "1.0.670",
26
+ "@types/semver": "^7.7.0",
27
+ "semver": "^7.7.2"
28
+ }
29
+ }