floating-live 0.15.10 → 0.16.0-alpha.0

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 (123) 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 +8 -5
  29. package/dist/index.js +8 -23
  30. package/dist/{types → live}/message.d.ts +185 -248
  31. package/{esm/types → dist/live}/message.js +1 -1
  32. package/dist/live/messageInfo.d.ts +93 -0
  33. package/dist/live/messageInfo.js +28 -0
  34. package/dist/live/platform.d.ts +40 -0
  35. package/dist/live/platform.js +1 -0
  36. package/dist/live/room.d.ts +150 -0
  37. package/dist/live/room.js +120 -0
  38. package/dist/plugin/context/CommonPluginContext.d.ts +45 -0
  39. package/dist/plugin/context/CommonPluginContext.js +273 -0
  40. package/dist/plugin/context/SafePluginContext.d.ts +34 -0
  41. package/dist/plugin/context/SafePluginContext.js +91 -0
  42. package/dist/plugin/index.d.ts +8 -0
  43. package/dist/plugin/index.js +5 -0
  44. package/dist/plugin/manager.d.ts +31 -0
  45. package/dist/plugin/manager.js +93 -0
  46. package/dist/plugin/types.d.ts +73 -0
  47. package/dist/plugin/types.js +1 -0
  48. package/dist/plugins/room/index.d.ts +116 -0
  49. package/dist/plugins/room/index.js +172 -0
  50. package/dist/utils/EventEmitter.d.ts +6 -0
  51. package/dist/utils/EventEmitter.js +26 -0
  52. package/dist/utils/index.d.ts +2 -0
  53. package/dist/utils/index.js +4 -0
  54. package/dist/value/index.d.ts +1 -0
  55. package/dist/value/index.js +1 -0
  56. package/dist/value/manager.d.ts +24 -0
  57. package/dist/value/manager.js +63 -0
  58. package/dist/value/types.d.ts +19 -0
  59. package/dist/value/types.js +1 -0
  60. package/package.json +9 -40
  61. package/src/source.md +29 -0
  62. package/tsconfig.tsbuildinfo +1 -0
  63. package/dist/core/LiveRoom.d.ts +0 -44
  64. package/dist/core/LiveRoom.js +0 -81
  65. package/dist/core/index.d.ts +0 -1
  66. package/dist/core/index.js +0 -17
  67. package/dist/core/plugin.d.ts +0 -17
  68. package/dist/core/plugin.js +0 -28
  69. package/dist/enums.d.ts +0 -51
  70. package/dist/enums.js +0 -59
  71. package/dist/live.d.ts +0 -39
  72. package/dist/live.js +0 -51
  73. package/dist/module/command.d.ts +0 -18
  74. package/dist/module/command.js +0 -42
  75. package/dist/module/hook.d.ts +0 -15
  76. package/dist/module/hook.js +0 -51
  77. package/dist/module/manifest.d.ts +0 -15
  78. package/dist/module/manifest.js +0 -44
  79. package/dist/module/plugin.d.ts +0 -16
  80. package/dist/module/plugin.js +0 -84
  81. package/dist/module/room.d.ts +0 -32
  82. package/dist/module/room.js +0 -159
  83. package/dist/module/value.d.ts +0 -26
  84. package/dist/module/value.js +0 -60
  85. package/dist/types/command.d.ts +0 -23
  86. package/dist/types/command.js +0 -2
  87. package/dist/types/error.d.ts +0 -10
  88. package/dist/types/error.js +0 -2
  89. package/dist/types/events.d.ts +0 -41
  90. package/dist/types/events.js +0 -2
  91. package/dist/types/hook.d.ts +0 -15
  92. package/dist/types/hook.js +0 -2
  93. package/dist/types/index.d.ts +0 -9
  94. package/dist/types/index.js +0 -25
  95. package/dist/types/manifest.d.ts +0 -4
  96. package/dist/types/manifest.js +0 -2
  97. package/dist/types/message.js +0 -2
  98. package/dist/types/plugin.d.ts +0 -13
  99. package/dist/types/plugin.js +0 -2
  100. package/dist/types/room.d.ts +0 -98
  101. package/dist/types/room.js +0 -2
  102. package/dist/types/value.d.ts +0 -2
  103. package/dist/types/value.js +0 -2
  104. package/dist/utils.d.ts +0 -2
  105. package/dist/utils.js +0 -21
  106. package/esm/core/LiveRoom.js +0 -77
  107. package/esm/core/index.js +0 -1
  108. package/esm/core/plugin.js +0 -24
  109. package/esm/enums.js +0 -56
  110. package/esm/index.js +0 -5
  111. package/esm/live.js +0 -47
  112. package/esm/module/command.js +0 -38
  113. package/esm/module/hook.js +0 -47
  114. package/esm/module/manifest.js +0 -40
  115. package/esm/module/plugin.js +0 -80
  116. package/esm/module/room.js +0 -155
  117. package/esm/module/value.js +0 -56
  118. package/esm/types/index.js +0 -9
  119. package/esm/types/manifest.js +0 -1
  120. package/esm/types/plugin.js +0 -1
  121. package/esm/types/room.js +0 -1
  122. package/esm/types/value.js +0 -1
  123. package/esm/utils.js +0 -17
package/README.md CHANGED
@@ -1,32 +1,36 @@
1
1
  # Floating Live
2
- 一个用于处理和保存直播弹幕的Node.js小工具,开箱即用。
2
+
3
+ 一个用于处理和保存直播弹幕的 Web 小工具,开箱即用。
3
4
 
4
5
  本体无弹幕获取功能,需要配合[floating-live-plugins](https://github.com/Minteea/floating-live-plugins)中的插件使用。
5
6
 
6
- 目前插件库支持bilibili和AcFun的直播弹幕,不同平台的弹幕会转换为同一种格式便于保存。
7
+ 目前插件库支持 bilibili 和 AcFun 的直播弹幕,不同平台的弹幕会转换为同一种格式便于保存。
7
8
 
8
9
  项目仍在开发中,功能尚未完善,目前除了记录保存直播弹幕外暂时没有别的用处啦,敬请期待。
9
10
 
10
11
  注意:当前项目未进入正式版本阶段,程序结构及导出数据结构可能随时改动,**非常不建议用于正式项目中**。
11
12
 
12
- GUI版本:[Minteea/floating-live-gui](https://github.com/Minteea/floating-live-gui)
13
+ GUI 版本:[Minteea/floating-live-gui](https://github.com/Minteea/floating-live-gui)
13
14
 
14
15
  ## 使用方法
15
- ### npm安装
16
+
17
+ ### npm 安装
18
+
16
19
  ```
17
20
  npm install floating-live
18
21
  ```
19
- ``` javascript
22
+
23
+ ```javascript
20
24
  // 导入FloatingLive本体
21
- const { FloatingLive } = require("floating-live")
25
+ import { FloatingLive } from "floating-live";
22
26
  // 导入bilibili房间生成插件
23
- const bilibili = require("@floating-live/bilibili")
27
+ import bilibili from "@floating-live/bilibili";
24
28
 
25
29
  // 创建一个FloatingLive实例
26
- const live = new FloatingLive()
30
+ const live = new FloatingLive();
27
31
  // 注册插件
28
- live.plugin.register(bilibili)
32
+ live.plugin.register(bilibili);
29
33
 
30
34
  // 添加房间并自动打开
31
- live.room.add("bilibili", 6, true)
35
+ live.room.add("bilibili", 6, true);
32
36
  ```
@@ -0,0 +1,6 @@
1
+ import { App } from "./app";
2
+ import Room from "./plugins/room";
3
+ export declare class FloatingLive extends App {
4
+ room: Room;
5
+ constructor();
6
+ }
@@ -0,0 +1,8 @@
1
+ import { App } from "./app";
2
+ import Room from "./plugins/room";
3
+ export class FloatingLive extends App {
4
+ constructor() {
5
+ super();
6
+ this.room = new Room(this);
7
+ }
8
+ }
package/dist/app.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { CommandOptions, CommandCallOptions, AppCommandMap, CommandFunction } from "./command";
2
+ import { AppError } from "./error";
3
+ import { AppEventDetailMap, AppEventEmitOptions, AppEventListener } from "./event";
4
+ import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "./hook";
5
+ import { PluginContext, PluginRegisterOptions } from "./plugin";
6
+ import { CustomEventEmitter } from "./utils/EventEmitter";
7
+ import { AppValueMap, ValueOptions } from "./value";
8
+ export declare class App extends CustomEventEmitter implements PluginContext {
9
+ private hookManager;
10
+ private pluginManager;
11
+ private commandManager;
12
+ private valueManager;
13
+ constructor();
14
+ on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
15
+ once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
16
+ off<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
17
+ emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
18
+ /** 发送错误 */
19
+ throw(err?: Error): void;
20
+ error(id: string, detail?: Record<string, any>): AppError;
21
+ /** 注册插件 */
22
+ register(plugin: any, options?: PluginRegisterOptions): void;
23
+ /** 取消注册插件 */
24
+ unregister(pluginName: string): void;
25
+ getPlugin(pluginName: string): void;
26
+ whenRegister(pluginName: string, callback: (exposes: any) => (() => void) | undefined): void;
27
+ getPluginExposes(pluginName: string): any;
28
+ hasPlugin(pluginName: string): boolean;
29
+ registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
30
+ unregisterCommand(name: string): void;
31
+ call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
32
+ callWithOptions<T extends keyof AppCommandMap>(name: T, options: CommandCallOptions, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
33
+ useHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>, options?: HookUseOptions): void;
34
+ unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
35
+ callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
36
+ callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
37
+ registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): void;
38
+ unregisterValue(name: string): void;
39
+ watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
40
+ unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
41
+ getValue<K extends keyof AppValueMap>(name: K): AppValueMap[K];
42
+ setValue(name: string, value: any): void;
43
+ destroy(): void;
44
+ /** 拓展值 */
45
+ define(): void;
46
+ }
package/dist/app.js ADDED
@@ -0,0 +1,126 @@
1
+ import { CommandManager } from "./command/manager";
2
+ import { AppError } from "./error";
3
+ import { HookManager } from "./hook/manager";
4
+ import { PluginManager } from "./plugin/manager";
5
+ import { CustomEventEmitter } from "./utils/EventEmitter";
6
+ import { ValueManager } from "./value/manager";
7
+ export class App extends CustomEventEmitter {
8
+ constructor() {
9
+ super();
10
+ this.hookManager = new HookManager(this);
11
+ this.pluginManager = new PluginManager(this);
12
+ this.commandManager = new CommandManager(this);
13
+ this.valueManager = new ValueManager(this);
14
+ }
15
+ on(type, listener) {
16
+ super.on(type, listener);
17
+ }
18
+ once(type, listener) {
19
+ super.once(type, listener);
20
+ }
21
+ off(type, listener) {
22
+ super.off(type, listener);
23
+ }
24
+ emit(type, detail, options) {
25
+ const { source, remote } = options || {};
26
+ super.emit(type, { ...detail, source, remote }, options);
27
+ }
28
+ //--- 错误机制 ---//
29
+ /** 发送错误 */
30
+ throw(err) {
31
+ throw err;
32
+ }
33
+ error(id, detail) {
34
+ return new AppError(id, detail || {});
35
+ }
36
+ //--- 插件机制 ---//
37
+ /** 注册插件 */
38
+ register(plugin, options) {
39
+ this.pluginManager.register(plugin, options);
40
+ }
41
+ /** 取消注册插件 */
42
+ unregister(pluginName) {
43
+ this.pluginManager.unregister(pluginName);
44
+ }
45
+ getPlugin(pluginName) {
46
+ this.pluginManager.getPlugin(pluginName);
47
+ }
48
+ whenRegister(pluginName, callback) {
49
+ let registered = this.hasPlugin(pluginName);
50
+ let whenUnregistered;
51
+ if (registered) {
52
+ whenUnregistered = callback(this.getPluginExposes(pluginName));
53
+ }
54
+ this.on("plugin:register", ({ pluginName: name }) => {
55
+ if (pluginName == name) {
56
+ if (registered) {
57
+ whenUnregistered?.();
58
+ }
59
+ whenUnregistered = callback(this.getPluginExposes(pluginName));
60
+ registered = true;
61
+ }
62
+ });
63
+ this.on("plugin:unregister", ({ pluginName: name }) => {
64
+ if (pluginName == name) {
65
+ whenUnregistered?.();
66
+ whenUnregistered = undefined;
67
+ registered = false;
68
+ }
69
+ });
70
+ }
71
+ getPluginExposes(pluginName) {
72
+ this.pluginManager.getExposes(pluginName);
73
+ }
74
+ hasPlugin(pluginName) {
75
+ return this.pluginManager.has(pluginName);
76
+ }
77
+ //--- 指令机制 ---//
78
+ registerCommand(name, func, options) {
79
+ this.commandManager.register(name, func, options);
80
+ }
81
+ unregisterCommand(name) {
82
+ this.commandManager.unregister(name);
83
+ }
84
+ call(name, ...args) {
85
+ return this.commandManager.call(name, ...args);
86
+ }
87
+ callWithOptions(name, options, ...args) {
88
+ return this.commandManager.callWithOptions(name, options, ...args);
89
+ }
90
+ //--- 钩子机制 ---//
91
+ useHook(name, func, options) {
92
+ this.hookManager.use(name, func, options);
93
+ }
94
+ unuseHook(name, func) {
95
+ this.hookManager.unuse(name, func);
96
+ }
97
+ callHook(name, ctx, options) {
98
+ return this.hookManager.call(name, ctx);
99
+ }
100
+ callHookSync(name, ctx, options) {
101
+ return this.hookManager.callSync(name, ctx);
102
+ }
103
+ //--- 值机制 ---//
104
+ registerValue(name, options) {
105
+ this.valueManager.register(name, options);
106
+ }
107
+ unregisterValue(name) {
108
+ this.valueManager.unregister(name);
109
+ }
110
+ watch(name, watcher) {
111
+ this.valueManager.watch(name, watcher);
112
+ }
113
+ unwatch(name, watcher) {
114
+ this.valueManager.unwatch(name, watcher);
115
+ }
116
+ getValue(name) {
117
+ return this.valueManager.get(name);
118
+ }
119
+ setValue(name, value) {
120
+ this.valueManager.set(name, value);
121
+ }
122
+ destroy() { }
123
+ //--- 其他 ---//
124
+ /** 拓展值 */
125
+ define() { }
126
+ }
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1,23 @@
1
+ import { App } from "../app";
2
+ import { CommandFunction, CommandCallOptions, CommandOptions } from "./types";
3
+ export declare class CommandManager {
4
+ /** 功能列表 */
5
+ private readonly list;
6
+ protected readonly app: App;
7
+ constructor(app: App);
8
+ /** 注册命令 */
9
+ register(name: string, call: CommandFunction<any>, options?: CommandOptions): void;
10
+ /** 取消注册命令 */
11
+ unregister(name: string): void;
12
+ /** 调用命令 */
13
+ call(name: string, ...args: any[]): any;
14
+ /** 调用命令(可配置) */
15
+ callWithOptions(name: string, options: CommandCallOptions, ...args: any[]): any;
16
+ /** 检测命令是否存在 */
17
+ has(name: string): boolean;
18
+ /** 获取数据 */
19
+ getData(): {
20
+ name: string;
21
+ pluginName: string | undefined;
22
+ }[];
23
+ }
@@ -0,0 +1,46 @@
1
+ export class CommandManager {
2
+ constructor(app) {
3
+ /** 功能列表 */
4
+ this.list = new Map();
5
+ this.app = app;
6
+ }
7
+ /** 注册命令 */
8
+ register(name, call, options) {
9
+ const { pluginName } = options || {};
10
+ this.list.set(name, { call, pluginName });
11
+ this.app.emit("command:register", { name });
12
+ }
13
+ /** 取消注册命令 */
14
+ unregister(name) {
15
+ this.list.delete(name);
16
+ this.app.emit("command:unregister", { name });
17
+ }
18
+ /** 调用命令 */
19
+ call(name, ...args) {
20
+ return this.callWithOptions(name, {}, ...args);
21
+ }
22
+ /** 调用命令(可配置) */
23
+ callWithOptions(name, options, ...args) {
24
+ const command = this.list.get(name);
25
+ if (!command) {
26
+ throw {
27
+ message: "命令不存在",
28
+ id: "command:call_unexist",
29
+ };
30
+ }
31
+ else {
32
+ return command.call({ source: options.source, client: options.client }, ...args);
33
+ }
34
+ }
35
+ /** 检测命令是否存在 */
36
+ has(name) {
37
+ return this.list.has(name);
38
+ }
39
+ /** 获取数据 */
40
+ getData() {
41
+ return [...this.list].map(([name, { pluginName }]) => ({
42
+ name,
43
+ pluginName,
44
+ }));
45
+ }
46
+ }
@@ -0,0 +1,25 @@
1
+ import { AppValueMap } from "../value";
2
+ export interface CommandOptions {
3
+ pluginName?: string;
4
+ }
5
+ export interface CommandCallOptions {
6
+ /** 命令来源 */
7
+ source?: string;
8
+ /** 命令发起客户端 */
9
+ client?: any;
10
+ }
11
+ export interface CommandContext {
12
+ /** 命令来源 */
13
+ source?: string;
14
+ /** 命令发起客户端 */
15
+ client?: any;
16
+ }
17
+ export type CommandFunction<T extends (...args: any) => any> = (e: CommandContext, ...args: Parameters<T>) => ReturnType<T>;
18
+ export interface CommandItem {
19
+ call: CommandFunction<any>;
20
+ pluginName?: string;
21
+ }
22
+ export interface AppCommandMap {
23
+ get: <T extends keyof AppValueMap>(name: T) => AppValueMap[T];
24
+ set: <T extends keyof AppValueMap>(name: T, value: AppValueMap[T]) => boolean;
25
+ }
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import { ErrorOptions } from "./types";
2
+ export * from "./types";
3
+ export declare class AppError extends Error {
4
+ /** 错误来源 */
5
+ source?: string;
6
+ /** 错误id */
7
+ id: string;
8
+ /** 错误原因 */
9
+ cause?: Error | string;
10
+ constructor(id: string, options: ErrorOptions);
11
+ }
@@ -0,0 +1,9 @@
1
+ export * from "./types";
2
+ export class AppError extends Error {
3
+ constructor(id, options) {
4
+ super(options.message);
5
+ this.id = id;
6
+ this.source = options?.source;
7
+ this.cause = options?.cause;
8
+ }
9
+ }
@@ -0,0 +1,6 @@
1
+ export interface ErrorOptions {
2
+ message?: string;
3
+ cause?: string | Error;
4
+ source?: string;
5
+ [key: string]: any;
6
+ }
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1,9 @@
1
+ import { App } from "../app";
2
+ export declare class EventManager {
3
+ constructor(app: App);
4
+ private registerMap;
5
+ private generalRegisterSet;
6
+ register(eventName: string, pluginName: string): void;
7
+ unregister(eventName: string): void;
8
+ has(eventName: string): boolean;
9
+ }
@@ -0,0 +1,17 @@
1
+ export class EventManager {
2
+ constructor(app) {
3
+ this.registerMap = new Map();
4
+ this.generalRegisterSet = new Set();
5
+ }
6
+ register(eventName, pluginName) {
7
+ if (this.registerMap.has(eventName))
8
+ throw new Error("事件名已注册");
9
+ this.registerMap.set(eventName, pluginName);
10
+ }
11
+ unregister(eventName) {
12
+ this.registerMap.delete(eventName);
13
+ }
14
+ has(eventName) {
15
+ return this.registerMap.has(eventName);
16
+ }
17
+ }
@@ -0,0 +1,42 @@
1
+ export interface AppEventEmitOptions {
2
+ /** 事件来源 */
3
+ source?: string;
4
+ /** 远程事件 */
5
+ remote?: boolean;
6
+ }
7
+ export interface AppEventBaseDetail {
8
+ /** 事件来源 */
9
+ source?: string;
10
+ /** 远程事件 */
11
+ remote?: boolean;
12
+ }
13
+ export type AppEventDetail<T> = T & AppEventBaseDetail;
14
+ export type AppEventListener<T> = (e: AppEventDetail<T>) => void;
15
+ export interface AppEventDetailMap {
16
+ "command:register": {
17
+ name: string;
18
+ };
19
+ "command:unregister": {
20
+ name: string;
21
+ };
22
+ "plugin:register": {
23
+ pluginName: string;
24
+ };
25
+ "plugin:unregister": {
26
+ pluginName: string;
27
+ };
28
+ "value:register": {
29
+ name: string;
30
+ value: any;
31
+ };
32
+ "value:change": {
33
+ name: string;
34
+ value: any;
35
+ };
36
+ "value:unregister": {
37
+ name: string;
38
+ };
39
+ [name: `change:${string}`]: {
40
+ value: any;
41
+ };
42
+ }
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1,25 @@
1
+ import { App } from "../app";
2
+ import { AppError, ErrorOptions } from "../error";
3
+ import { HookFunction, HookUseOptions } from "./types";
4
+ export interface HookErrorOptions extends ErrorOptions {
5
+ hookName: string;
6
+ }
7
+ export declare class HookError extends AppError {
8
+ hookName: string;
9
+ constructor(id: string, options: HookErrorOptions);
10
+ }
11
+ export declare class HookManager {
12
+ private list;
13
+ private pluginMap;
14
+ protected readonly app: App;
15
+ constructor(app: App);
16
+ /** 挂载钩子函数 */
17
+ use(name: string, call: HookFunction<any>, options?: HookUseOptions): void;
18
+ /** 取消挂载钩子函数 */
19
+ unuse(name: string, call: HookFunction<any>): void;
20
+ /** 调用钩子函数 */
21
+ call(name: string, ctx: any): Promise<any>;
22
+ /** 同步调用钩子函数 */
23
+ callSync(name: string, ctx: any): any;
24
+ getData(): void[];
25
+ }
@@ -0,0 +1,79 @@
1
+ import { AppError } from "../error";
2
+ export class HookError extends AppError {
3
+ constructor(id, options) {
4
+ super(id, options);
5
+ this.hookName = options.hookName;
6
+ }
7
+ }
8
+ export class HookManager {
9
+ constructor(app) {
10
+ this.list = new Map();
11
+ this.pluginMap = new Map();
12
+ this.app = app;
13
+ }
14
+ /** 挂载钩子函数 */
15
+ use(name, call, options) {
16
+ const { pluginName } = options || {};
17
+ let hooks = this.list.get(name);
18
+ if (!hooks) {
19
+ hooks = [];
20
+ this.list.set(name, hooks);
21
+ }
22
+ else if (hooks.includes(call)) {
23
+ throw new HookError("hook:use_duplicate", {
24
+ message: `钩子函数挂载失败: ${name}`,
25
+ cause: "重复挂载同一函数",
26
+ hookName: name,
27
+ });
28
+ }
29
+ hooks.push({ call, pluginName });
30
+ }
31
+ /** 取消挂载钩子函数 */
32
+ unuse(name, call) {
33
+ let hooks = this.list.get(name);
34
+ if (!hooks)
35
+ throw new HookError("hook:unuse_unexisted", {
36
+ message: `钩子函数卸载失败: ${name}`,
37
+ cause: "卸载不存在的钩子函数",
38
+ hookName: name,
39
+ });
40
+ const index = hooks.findIndex((item) => item.call == call);
41
+ if (index < 0)
42
+ throw {};
43
+ hooks.splice(index, 1);
44
+ }
45
+ /** 调用钩子函数 */
46
+ async call(name, ctx) {
47
+ const hooks = this.list.get(name);
48
+ if (!hooks) {
49
+ return ctx;
50
+ }
51
+ for (const item of hooks) {
52
+ const skip = await item.call(ctx);
53
+ if (skip) {
54
+ return ctx;
55
+ }
56
+ }
57
+ return ctx;
58
+ }
59
+ /** 同步调用钩子函数 */
60
+ callSync(name, ctx) {
61
+ const hooks = this.list.get(name);
62
+ if (!hooks) {
63
+ return ctx;
64
+ }
65
+ for (const item of hooks) {
66
+ const skip = item.call(ctx);
67
+ if (skip) {
68
+ return ctx;
69
+ }
70
+ }
71
+ return ctx;
72
+ }
73
+ getData() {
74
+ const map = {};
75
+ return [...this.list].map(([name, fnList]) => {
76
+ map[name] = fnList.length;
77
+ });
78
+ }
79
+ }
@@ -0,0 +1,22 @@
1
+ export type HookFunction<T> = (ctx: T) => boolean | void | Promise<boolean | void>;
2
+ export interface HookItem<T> {
3
+ call: HookFunction<T>;
4
+ pluginName?: string;
5
+ }
6
+ export interface HookUseOptions {
7
+ pluginName?: string;
8
+ }
9
+ export interface HookCallOptions {
10
+ pluginName?: string;
11
+ }
12
+ export interface BaseHookContext {
13
+ pluginName?: string;
14
+ defaultPrevented?: boolean;
15
+ }
16
+ export type HookContext<T> = BaseHookContext & T;
17
+ export interface AppHookMap {
18
+ "plugin.register": {
19
+ pluginName: string;
20
+ options: Record<string, any>;
21
+ };
22
+ }
@@ -1 +1 @@
1
- export {};
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- export { FloatingLive } from "./live";
2
- export * from "./types";
3
- export * from "./enums";
4
- export * from "./core/LiveRoom";
5
- export * from "./utils";
1
+ export * from "./app";
2
+ export * from "./value";
3
+ export * from "./plugin";
4
+ export * from "./hook";
5
+ export * from "./event";
6
+ export * from "./error";
7
+ export * from "./command";
8
+ export * from "./FloatingLive";
package/dist/index.js CHANGED
@@ -1,23 +1,8 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.FloatingLive = void 0;
18
- var live_1 = require("./live");
19
- Object.defineProperty(exports, "FloatingLive", { enumerable: true, get: function () { return live_1.FloatingLive; } });
20
- __exportStar(require("./types"), exports);
21
- __exportStar(require("./enums"), exports);
22
- __exportStar(require("./core/LiveRoom"), exports);
23
- __exportStar(require("./utils"), exports);
1
+ export * from "./app";
2
+ export * from "./value";
3
+ export * from "./plugin";
4
+ export * from "./hook";
5
+ export * from "./event";
6
+ export * from "./error";
7
+ export * from "./command";
8
+ export * from "./FloatingLive";