floating-live 0.15.11 → 0.16.0-alpha.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.
Files changed (125) hide show
  1. package/README.md +14 -10
  2. package/dist/FloatingLive.d.ts +6 -0
  3. package/dist/FloatingLive.js +8 -0
  4. package/dist/app.d.ts +46 -0
  5. package/dist/app.js +126 -0
  6. package/dist/command/index.d.ts +1 -0
  7. package/dist/command/index.js +1 -0
  8. package/dist/command/manager.d.ts +23 -0
  9. package/dist/command/manager.js +46 -0
  10. package/dist/command/types.d.ts +25 -0
  11. package/{esm/types/command.js → dist/command/types.js} +1 -1
  12. package/dist/error/index.d.ts +11 -0
  13. package/dist/error/index.js +9 -0
  14. package/dist/error/types.d.ts +6 -0
  15. package/{esm/types/error.js → dist/error/types.js} +1 -1
  16. package/dist/event/index.d.ts +1 -0
  17. package/dist/event/index.js +1 -0
  18. package/dist/event/manager.d.ts +9 -0
  19. package/dist/event/manager.js +17 -0
  20. package/dist/event/types.d.ts +42 -0
  21. package/{esm/types/events.js → dist/event/types.js} +1 -1
  22. package/dist/hook/index.d.ts +1 -0
  23. package/dist/hook/index.js +1 -0
  24. package/dist/hook/manager.d.ts +25 -0
  25. package/dist/hook/manager.js +79 -0
  26. package/dist/hook/types.d.ts +22 -0
  27. package/{esm/types/hook.js → dist/hook/types.js} +1 -1
  28. package/dist/index.d.ts +10 -5
  29. package/dist/index.js +10 -23
  30. package/dist/live/index.d.ts +4 -0
  31. package/dist/live/index.js +4 -0
  32. package/dist/{types → live}/message.d.ts +185 -248
  33. package/{esm/types → dist/live}/message.js +1 -1
  34. package/dist/live/messageInfo.d.ts +93 -0
  35. package/dist/live/messageInfo.js +28 -0
  36. package/dist/live/platform.d.ts +40 -0
  37. package/dist/live/platform.js +1 -0
  38. package/dist/live/room.d.ts +150 -0
  39. package/dist/live/room.js +120 -0
  40. package/dist/plugin/context/CommonPluginContext.d.ts +45 -0
  41. package/dist/plugin/context/CommonPluginContext.js +273 -0
  42. package/dist/plugin/context/SafePluginContext.d.ts +34 -0
  43. package/dist/plugin/context/SafePluginContext.js +91 -0
  44. package/dist/plugin/index.d.ts +8 -0
  45. package/dist/plugin/index.js +5 -0
  46. package/dist/plugin/manager.d.ts +31 -0
  47. package/dist/plugin/manager.js +93 -0
  48. package/dist/plugin/types.d.ts +73 -0
  49. package/dist/plugin/types.js +1 -0
  50. package/dist/plugins/room/index.d.ts +117 -0
  51. package/dist/plugins/room/index.js +172 -0
  52. package/dist/utils/EventEmitter.d.ts +6 -0
  53. package/dist/utils/EventEmitter.js +26 -0
  54. package/dist/utils/index.d.ts +2 -0
  55. package/dist/utils/index.js +4 -0
  56. package/dist/value/index.d.ts +1 -0
  57. package/dist/value/index.js +1 -0
  58. package/dist/value/manager.d.ts +24 -0
  59. package/dist/value/manager.js +63 -0
  60. package/dist/value/types.d.ts +19 -0
  61. package/dist/value/types.js +1 -0
  62. package/package.json +9 -40
  63. package/src/source.md +29 -0
  64. package/tsconfig.tsbuildinfo +1 -0
  65. package/dist/core/LiveRoom.d.ts +0 -44
  66. package/dist/core/LiveRoom.js +0 -81
  67. package/dist/core/index.d.ts +0 -1
  68. package/dist/core/index.js +0 -17
  69. package/dist/core/plugin.d.ts +0 -17
  70. package/dist/core/plugin.js +0 -28
  71. package/dist/enums.d.ts +0 -51
  72. package/dist/enums.js +0 -59
  73. package/dist/live.d.ts +0 -39
  74. package/dist/live.js +0 -51
  75. package/dist/module/command.d.ts +0 -18
  76. package/dist/module/command.js +0 -42
  77. package/dist/module/hook.d.ts +0 -15
  78. package/dist/module/hook.js +0 -51
  79. package/dist/module/manifest.d.ts +0 -15
  80. package/dist/module/manifest.js +0 -44
  81. package/dist/module/plugin.d.ts +0 -16
  82. package/dist/module/plugin.js +0 -85
  83. package/dist/module/room.d.ts +0 -34
  84. package/dist/module/room.js +0 -164
  85. package/dist/module/value.d.ts +0 -26
  86. package/dist/module/value.js +0 -60
  87. package/dist/types/command.d.ts +0 -23
  88. package/dist/types/command.js +0 -2
  89. package/dist/types/error.d.ts +0 -10
  90. package/dist/types/error.js +0 -2
  91. package/dist/types/events.d.ts +0 -41
  92. package/dist/types/events.js +0 -2
  93. package/dist/types/hook.d.ts +0 -15
  94. package/dist/types/hook.js +0 -2
  95. package/dist/types/index.d.ts +0 -9
  96. package/dist/types/index.js +0 -25
  97. package/dist/types/manifest.d.ts +0 -4
  98. package/dist/types/manifest.js +0 -2
  99. package/dist/types/message.js +0 -2
  100. package/dist/types/plugin.d.ts +0 -13
  101. package/dist/types/plugin.js +0 -2
  102. package/dist/types/room.d.ts +0 -98
  103. package/dist/types/room.js +0 -2
  104. package/dist/types/value.d.ts +0 -2
  105. package/dist/types/value.js +0 -2
  106. package/dist/utils.d.ts +0 -2
  107. package/dist/utils.js +0 -21
  108. package/esm/core/LiveRoom.js +0 -77
  109. package/esm/core/index.js +0 -1
  110. package/esm/core/plugin.js +0 -24
  111. package/esm/enums.js +0 -56
  112. package/esm/index.js +0 -5
  113. package/esm/live.js +0 -47
  114. package/esm/module/command.js +0 -38
  115. package/esm/module/hook.js +0 -47
  116. package/esm/module/manifest.js +0 -40
  117. package/esm/module/plugin.js +0 -81
  118. package/esm/module/room.js +0 -160
  119. package/esm/module/value.js +0 -56
  120. package/esm/types/index.js +0 -9
  121. package/esm/types/manifest.js +0 -1
  122. package/esm/types/plugin.js +0 -1
  123. package/esm/types/room.js +0 -1
  124. package/esm/types/value.js +0 -1
  125. package/esm/utils.js +0 -17
@@ -0,0 +1,150 @@
1
+ import { CustomEventEmitter } from "../utils/EventEmitter";
2
+ import { LiveMessage } from "./message";
3
+ import { UserInfo } from "./messageInfo";
4
+ export declare abstract class LiveRoom extends CustomEventEmitter implements LiveRoomData {
5
+ constructor();
6
+ /** 平台 */
7
+ abstract readonly platform: string;
8
+ /** 房间id */
9
+ abstract readonly id: string | number;
10
+ /** 房间详情 */
11
+ abstract detail: LiveRoomDetailInfo;
12
+ /** 统计信息 */
13
+ stats?: LiveRoomStatsInfo;
14
+ /** 主播信息 */
15
+ abstract anchor: UserInfo;
16
+ /** 直播状态 */
17
+ status: LiveRoomStatus;
18
+ /** 时间戳 */
19
+ timestamp: number;
20
+ /** 当前直播id */
21
+ liveId?: string;
22
+ /** 房间是否打开 */
23
+ opened: boolean;
24
+ /** 是否连接上房间 */
25
+ connection: LiveConnectionStatus;
26
+ /** 房间是否可用 */
27
+ abstract available: boolean;
28
+ /** 打开连接 */
29
+ abstract open(): void;
30
+ /** 关闭连接 */
31
+ abstract close(): void;
32
+ /** 从服务器获取信息 */
33
+ abstract getInfo(): Promise<void>;
34
+ /** 销毁事件 */
35
+ protected onDestroy?(): void;
36
+ /** 销毁 */
37
+ destroy(): void;
38
+ protected emitMessage(msg: LiveMessage.All): void;
39
+ /** 发送源消息 */
40
+ protected emitRaw(msg: any): void;
41
+ /** 获取数据 */
42
+ toData(): LiveRoomData;
43
+ get key(): string;
44
+ on<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
45
+ once<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
46
+ off<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
47
+ emit<K extends keyof LiveRoomEventMap>(type: LiveRoomEventMap[K] extends Record<string, never> ? K : never): void;
48
+ emit<K extends keyof LiveRoomEventMap>(type: K, detail: LiveRoomEventMap[K]): void;
49
+ }
50
+ /** 房间数据 */
51
+ export interface LiveRoomData {
52
+ /** 平台id */
53
+ platform: string;
54
+ /** 房间id */
55
+ id: number | string;
56
+ /** 房间key */
57
+ key: string;
58
+ /** 基本信息 */
59
+ detail: LiveRoomDetailInfo;
60
+ /** 统计信息 */
61
+ stats?: LiveRoomStatsInfo;
62
+ /** 主播信息 */
63
+ anchor: UserInfo;
64
+ /** 时间戳 */
65
+ timestamp: number;
66
+ /** 当前直播id */
67
+ liveId?: string;
68
+ /** 直播状态 */
69
+ status: LiveRoomStatus;
70
+ /** 直播间是否可用 */
71
+ available: boolean;
72
+ /** 房间监听是否打开 */
73
+ opened: boolean;
74
+ /** 房间连接状态 */
75
+ connection: LiveConnectionStatus;
76
+ }
77
+ /** 房间展示信息 */
78
+ export interface LiveRoomDetailInfo {
79
+ /** 直播标题 */
80
+ title?: string;
81
+ /** 分区 */
82
+ area?: string[];
83
+ /** 封面 */
84
+ cover?: string;
85
+ }
86
+ /** 房间统计数据信息 */
87
+ export interface LiveRoomStatsInfo {
88
+ /** 点赞数 */
89
+ like?: number;
90
+ /** 观看数 */
91
+ view?: number;
92
+ /** 在线人数 */
93
+ online?: number;
94
+ /** 人气值 */
95
+ popularity?: number;
96
+ }
97
+ /** 直播间状态 */
98
+ export declare enum LiveRoomStatus {
99
+ /** 未开播 */
100
+ off = 0,
101
+ /** 正在直播 */
102
+ live = 1,
103
+ /** 正在轮播 */
104
+ round = 2,
105
+ /** 被封禁 */
106
+ banned = -1,
107
+ /** 已上锁 */
108
+ locked = -2
109
+ }
110
+ /** 直播间状态 */
111
+ export declare enum LiveConnectionStatus {
112
+ /** 未连接 */
113
+ off = 0,
114
+ /** 连接中 */
115
+ connecting = 1,
116
+ /** 连接到服务器 */
117
+ connected = 2,
118
+ /** 连接到房间 */
119
+ entered = 3,
120
+ /** 连接失败或断开 */
121
+ disconnected = -1
122
+ }
123
+ export interface LiveRoomEventMap {
124
+ status: {
125
+ status: LiveRoomStatus;
126
+ timestamp: number;
127
+ liveId?: string | number;
128
+ };
129
+ update: {};
130
+ detail: {
131
+ detail: Partial<LiveRoomDetailInfo>;
132
+ };
133
+ stats: {
134
+ stats: LiveRoomStatsInfo;
135
+ };
136
+ message: {
137
+ message: LiveMessage.All;
138
+ };
139
+ raw: {
140
+ platform: string;
141
+ roomId: string | number;
142
+ data: any;
143
+ };
144
+ connecting: {};
145
+ connected: {};
146
+ disconnect: {};
147
+ enter: {};
148
+ open: {};
149
+ close: {};
150
+ }
@@ -0,0 +1,120 @@
1
+ import { CustomEventEmitter } from "../utils/EventEmitter";
2
+ export class LiveRoom extends CustomEventEmitter {
3
+ constructor() {
4
+ super();
5
+ /** 直播状态 */
6
+ this.status = LiveRoomStatus.off;
7
+ /** 时间戳 */
8
+ this.timestamp = 0;
9
+ /** 房间是否打开 */
10
+ this.opened = false;
11
+ /** 是否连接上房间 */
12
+ this.connection = LiveConnectionStatus.off;
13
+ }
14
+ /** 销毁 */
15
+ destroy() {
16
+ this.close();
17
+ this.onDestroy?.();
18
+ }
19
+ // 发送消息
20
+ emitMessage(msg) {
21
+ // 其他消息 => 先更改状态再发送消息
22
+ // 直播结束消息 => 先发送消息再更改状态
23
+ switch (msg.type) {
24
+ case "live_start":
25
+ this.status = LiveRoomStatus.live;
26
+ this.timestamp = msg.timestamp;
27
+ this.emit("status", {
28
+ status: LiveRoomStatus.live,
29
+ timestamp: msg.timestamp,
30
+ liveId: msg.info.id,
31
+ });
32
+ break;
33
+ case "live_detail":
34
+ this.detail = Object.assign(this.detail, msg.info);
35
+ this.emit("detail", { detail: msg.info });
36
+ break;
37
+ case "live_stats":
38
+ this.stats = Object.assign(this.stats, msg.info);
39
+ this.emit("stats", { stats: msg.info });
40
+ break;
41
+ case "live_end":
42
+ case "live_cut":
43
+ this.status = msg.status || LiveRoomStatus.off;
44
+ this.timestamp = msg.timestamp;
45
+ this.emit("message", { message: msg });
46
+ this.emit("status", {
47
+ status: msg.status || LiveRoomStatus.off,
48
+ timestamp: msg.timestamp,
49
+ liveId: this.liveId,
50
+ });
51
+ return;
52
+ }
53
+ this.emit("message", { message: msg });
54
+ }
55
+ /** 发送源消息 */
56
+ emitRaw(msg) {
57
+ this.emit("raw", { platform: this.platform, roomId: this.id, data: msg });
58
+ }
59
+ /** 获取数据 */
60
+ toData() {
61
+ return {
62
+ platform: this.platform,
63
+ id: this.id,
64
+ detail: this.detail,
65
+ stats: this.stats,
66
+ anchor: this.anchor,
67
+ timestamp: this.timestamp,
68
+ status: this.status,
69
+ liveId: this.liveId,
70
+ available: this.available,
71
+ opened: this.opened,
72
+ connection: this.connection,
73
+ key: this.key,
74
+ };
75
+ }
76
+ get key() {
77
+ return `${this.platform}:${this.id}`;
78
+ }
79
+ on(type, listener) {
80
+ super.on(type, listener);
81
+ }
82
+ once(type, listener) {
83
+ super.once(type, listener);
84
+ this.emit("connecting");
85
+ }
86
+ off(type, listener) {
87
+ super.off(type, listener);
88
+ }
89
+ emit(type, detail) {
90
+ super.emit(type, detail);
91
+ }
92
+ }
93
+ /** 直播间状态 */
94
+ export var LiveRoomStatus;
95
+ (function (LiveRoomStatus) {
96
+ /** 未开播 */
97
+ LiveRoomStatus[LiveRoomStatus["off"] = 0] = "off";
98
+ /** 正在直播 */
99
+ LiveRoomStatus[LiveRoomStatus["live"] = 1] = "live";
100
+ /** 正在轮播 */
101
+ LiveRoomStatus[LiveRoomStatus["round"] = 2] = "round";
102
+ /** 被封禁 */
103
+ LiveRoomStatus[LiveRoomStatus["banned"] = -1] = "banned";
104
+ /** 已上锁 */
105
+ LiveRoomStatus[LiveRoomStatus["locked"] = -2] = "locked";
106
+ })(LiveRoomStatus || (LiveRoomStatus = {}));
107
+ /** 直播间状态 */
108
+ export var LiveConnectionStatus;
109
+ (function (LiveConnectionStatus) {
110
+ /** 未连接 */
111
+ LiveConnectionStatus[LiveConnectionStatus["off"] = 0] = "off";
112
+ /** 连接中 */
113
+ LiveConnectionStatus[LiveConnectionStatus["connecting"] = 1] = "connecting";
114
+ /** 连接到服务器 */
115
+ LiveConnectionStatus[LiveConnectionStatus["connected"] = 2] = "connected";
116
+ /** 连接到房间 */
117
+ LiveConnectionStatus[LiveConnectionStatus["entered"] = 3] = "entered";
118
+ /** 连接失败或断开 */
119
+ LiveConnectionStatus[LiveConnectionStatus["disconnected"] = -1] = "disconnected";
120
+ })(LiveConnectionStatus || (LiveConnectionStatus = {}));
@@ -0,0 +1,45 @@
1
+ import { App } from "../../app";
2
+ import type { CommandOptions, CommandCallOptions, CommandFunction, AppCommandMap } from "../../command";
3
+ import { AppError, ErrorOptions } from "../../error";
4
+ import { AppEventListener, AppEventEmitOptions, AppEventDetailMap } from "../../event";
5
+ import { HookUseOptions, HookCallOptions, AppHookMap, HookFunction, HookContext } from "../../hook";
6
+ import { AppValueMap, ValueOptions } from "../../value";
7
+ import type { PluginContext, PluginConstructor, PluginRegisterOptions } from "../types";
8
+ export declare class CommonPluginContext implements PluginContext {
9
+ #private;
10
+ get safe(): boolean;
11
+ getRegisteredPlugins(): string[];
12
+ /** 插件名称 */
13
+ readonly pluginName: string;
14
+ constructor(app: App, pluginName: string);
15
+ /** 抛出错误
16
+ *
17
+ * 一般用于自身导致的错误,若非插件本身导致的错误,使用throw语句即可
18
+ */
19
+ throw(err: AppError): void;
20
+ error(id: string, options: ErrorOptions): AppError;
21
+ register(plugin: PluginConstructor, options?: PluginRegisterOptions): void;
22
+ unregister(pluginName: string): void;
23
+ getPluginExposes<T>(pluginName: string): T;
24
+ whenRegister<T>(pluginName: string, callback: (exposes: any) => (() => void) | undefined): (() => void) | void;
25
+ hasPlugin(pluginName: string): boolean;
26
+ registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): void;
27
+ unregisterValue(name: string): void;
28
+ registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
29
+ unregisterCommand(name: string): void;
30
+ on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
31
+ once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
32
+ off<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
33
+ emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
34
+ call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
35
+ callWithOptions<T extends keyof AppCommandMap>(name: T, options: CommandCallOptions, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
36
+ useHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>, options?: HookUseOptions): void;
37
+ unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
38
+ callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
39
+ callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
40
+ watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
41
+ unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
42
+ getValue<K extends keyof AppValueMap>(name: K): undefined;
43
+ setValue<K extends keyof AppValueMap>(name: K, value: AppValueMap[K]): void;
44
+ destroy(): void;
45
+ }
@@ -0,0 +1,273 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _CommonPluginContext_app, _CommonPluginContext_registered;
13
+ import { AppError } from "../../error";
14
+ class ListMap extends Map {
15
+ addItem(key, value) {
16
+ const list = this.get(key);
17
+ if (list) {
18
+ list.add(value);
19
+ }
20
+ else {
21
+ this.set(key, new Set([value]));
22
+ }
23
+ }
24
+ deleteItem(key, value) {
25
+ this.get(key)?.delete(value);
26
+ }
27
+ }
28
+ export class CommonPluginContext {
29
+ get safe() {
30
+ return false;
31
+ }
32
+ getRegisteredPlugins() {
33
+ return [...__classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins];
34
+ }
35
+ constructor(app, pluginName) {
36
+ /** 主程序,插件不可访问 */
37
+ _CommonPluginContext_app.set(this, void 0);
38
+ _CommonPluginContext_registered.set(this, {
39
+ plugins: new Set(),
40
+ values: new Set(),
41
+ commands: new Set(),
42
+ hooks: new ListMap(),
43
+ events: new ListMap(),
44
+ });
45
+ __classPrivateFieldSet(this, _CommonPluginContext_app, app, "f");
46
+ this.pluginName = pluginName;
47
+ }
48
+ /** 抛出错误
49
+ *
50
+ * 一般用于自身导致的错误,若非插件本身导致的错误,使用throw语句即可
51
+ */
52
+ throw(err) {
53
+ err.source ?? (err.source = `plugin:${this.pluginName}`);
54
+ throw err;
55
+ }
56
+ error(id, options) {
57
+ return new AppError(id, options);
58
+ }
59
+ register(plugin, options) {
60
+ try {
61
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").register(plugin, options);
62
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins.add(plugin.name);
63
+ }
64
+ catch (err) {
65
+ this.throw(err);
66
+ }
67
+ }
68
+ unregister(pluginName) {
69
+ try {
70
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregister(pluginName);
71
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins.delete(pluginName);
72
+ }
73
+ catch (err) {
74
+ this.throw(err);
75
+ }
76
+ }
77
+ getPluginExposes(pluginName) {
78
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").getPluginExposes(pluginName);
79
+ }
80
+ whenRegister(pluginName, callback) {
81
+ let registered = this.hasPlugin(pluginName);
82
+ let whenUnregistered;
83
+ if (registered) {
84
+ whenUnregistered = callback(this.getPluginExposes(pluginName));
85
+ }
86
+ this.on("plugin:register", ({ pluginName: name }) => {
87
+ if (pluginName == name) {
88
+ if (registered) {
89
+ whenUnregistered?.();
90
+ }
91
+ whenUnregistered = callback(this.getPluginExposes(pluginName));
92
+ registered = true;
93
+ }
94
+ });
95
+ this.on("plugin:unregister", ({ pluginName: name }) => {
96
+ if (pluginName == name) {
97
+ whenUnregistered?.();
98
+ whenUnregistered = undefined;
99
+ registered = false;
100
+ }
101
+ });
102
+ }
103
+ hasPlugin(pluginName) {
104
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasPlugin(pluginName);
105
+ }
106
+ registerValue(name, options) {
107
+ try {
108
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerValue(name, options);
109
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").values.add(name);
110
+ }
111
+ catch (err) {
112
+ this.throw(err);
113
+ }
114
+ }
115
+ unregisterValue(name) {
116
+ try {
117
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterValue(name);
118
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").values.delete(name);
119
+ }
120
+ catch (err) {
121
+ this.throw(err);
122
+ }
123
+ }
124
+ registerCommand(name, func, options) {
125
+ try {
126
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerCommand(name, func, options);
127
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").commands.add(name);
128
+ }
129
+ catch (err) {
130
+ this.throw(err);
131
+ }
132
+ }
133
+ unregisterCommand(name) {
134
+ try {
135
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterCommand(name);
136
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").commands.delete(name);
137
+ }
138
+ catch (err) {
139
+ this.throw(err);
140
+ }
141
+ }
142
+ on(type, listener) {
143
+ try {
144
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").on(type, listener);
145
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.addItem(type, listener);
146
+ }
147
+ catch (err) {
148
+ this.throw(err);
149
+ }
150
+ }
151
+ once(type, listener) {
152
+ try {
153
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").once(type, listener);
154
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.addItem(type, listener);
155
+ }
156
+ catch (err) {
157
+ this.throw(err);
158
+ }
159
+ }
160
+ off(type, listener) {
161
+ try {
162
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(type, listener);
163
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.deleteItem(type, listener);
164
+ }
165
+ catch (err) {
166
+ this.throw(err);
167
+ }
168
+ }
169
+ emit(type, detail, options) {
170
+ try {
171
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").emit(type, detail || {}, {
172
+ source: `plugin:${this.pluginName}`,
173
+ ...options,
174
+ });
175
+ }
176
+ catch (err) {
177
+ this.throw(err);
178
+ }
179
+ }
180
+ call(name, ...args) {
181
+ try {
182
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callWithOptions(name, { source: `plugin:${this.pluginName}` }, ...args);
183
+ }
184
+ catch (err) {
185
+ this.throw(err);
186
+ throw "";
187
+ }
188
+ }
189
+ callWithOptions(name, options, ...args) {
190
+ try {
191
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callWithOptions(name, options, ...args);
192
+ }
193
+ catch (err) {
194
+ this.throw(err);
195
+ throw "";
196
+ }
197
+ }
198
+ useHook(name, func, options) {
199
+ try {
200
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").useHook(name, func, options);
201
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").hooks.addItem(name, func);
202
+ }
203
+ catch (err) {
204
+ this.throw(err);
205
+ }
206
+ }
207
+ unuseHook(name, func) {
208
+ try {
209
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unuseHook(name, func);
210
+ __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").hooks.deleteItem(name, func);
211
+ }
212
+ catch (err) {
213
+ throw this.throw(err);
214
+ }
215
+ }
216
+ callHook(name, ctx, options) {
217
+ try {
218
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callHook(name, ctx);
219
+ }
220
+ catch (err) {
221
+ throw this.throw(err);
222
+ }
223
+ }
224
+ callHookSync(name, ctx, options) {
225
+ try {
226
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callHookSync(name, ctx);
227
+ }
228
+ catch (err) {
229
+ throw this.throw(err);
230
+ }
231
+ }
232
+ watch(name, watcher) {
233
+ try {
234
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").watch(name, watcher);
235
+ }
236
+ catch (err) {
237
+ this.throw(err);
238
+ }
239
+ }
240
+ unwatch(name, watcher) {
241
+ try {
242
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unwatch(name, watcher);
243
+ }
244
+ catch (err) {
245
+ this.throw(err);
246
+ }
247
+ }
248
+ getValue(name) {
249
+ try {
250
+ return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").getValue(name);
251
+ }
252
+ catch (err) {
253
+ this.throw(err);
254
+ }
255
+ }
256
+ setValue(name, value) {
257
+ try {
258
+ __classPrivateFieldGet(this, _CommonPluginContext_app, "f").setValue(name, value);
259
+ }
260
+ catch (err) {
261
+ this.throw(err);
262
+ }
263
+ }
264
+ destroy() {
265
+ const { plugins, events, values, hooks, commands } = __classPrivateFieldGet(this, _CommonPluginContext_registered, "f");
266
+ plugins.forEach((pluginName) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregister(pluginName));
267
+ events.forEach((listenerList, eventName) => listenerList.forEach((listener) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(eventName, listener)));
268
+ hooks.forEach((hookList, name) => hookList.forEach((hookFunc) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(name, hookFunc)));
269
+ commands.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterCommand(name));
270
+ values.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterValue(name));
271
+ }
272
+ }
273
+ _CommonPluginContext_app = new WeakMap(), _CommonPluginContext_registered = new WeakMap();
@@ -0,0 +1,34 @@
1
+ import { App } from "../../app";
2
+ import { AppCommandMap, CommandCallOptions, CommandFunction, CommandOptions } from "../../command";
3
+ import { AppEventDetailMap, AppEventEmitOptions } from "../../event";
4
+ import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "../../hook";
5
+ import { PluginConstructor, PluginRegisterOptions } from "../types";
6
+ import { CommonPluginContext } from "./CommonPluginContext";
7
+ export interface PluginPermissions {
8
+ "plugin.register"?: boolean;
9
+ "event.emit"?: string[];
10
+ "event.emit.advanced"?: boolean;
11
+ "command.register"?: string[];
12
+ "command.call"?: string[];
13
+ "command.call.advanced"?: boolean;
14
+ "value.register"?: string[];
15
+ "hook.use"?: string[];
16
+ "hook.call"?: string[];
17
+ }
18
+ export declare class SafePluginContext extends CommonPluginContext {
19
+ #private;
20
+ get safe(): boolean;
21
+ constructor(app: App, pluginName: string, options: {
22
+ permissions: PluginPermissions;
23
+ });
24
+ private throwNotPermitted;
25
+ register(plugin: PluginConstructor, options?: PluginRegisterOptions): void;
26
+ unregister(pluginName: string): void;
27
+ emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
28
+ call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
29
+ callWithOptions<T extends keyof AppCommandMap>(name: T, options: CommandCallOptions, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
30
+ registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
31
+ useHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>, options?: HookUseOptions): void;
32
+ unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
33
+ callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
34
+ }