@tiny-codes/react-easy 1.4.6 → 1.4.8

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 (144) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/es/assets/request-camera-en.js +2 -0
  3. package/es/assets/request-camera-en.js.d.ts +2 -0
  4. package/es/assets/request-camera-en.js.map +1 -0
  5. package/es/assets/request-camera-en.png +0 -0
  6. package/es/assets/request-camera-zh.js +2 -0
  7. package/es/assets/request-camera-zh.js.d.ts +2 -0
  8. package/es/assets/request-camera-zh.js.map +1 -0
  9. package/es/assets/request-camera-zh.png +0 -0
  10. package/es/assets/request-microphone-en.js +2 -0
  11. package/es/assets/request-microphone-en.js.d.ts +2 -0
  12. package/es/assets/request-microphone-en.js.map +1 -0
  13. package/es/assets/request-microphone-en.png +0 -0
  14. package/es/assets/request-microphone-zh.js +2 -0
  15. package/es/assets/request-microphone-zh.js.d.ts +2 -0
  16. package/es/assets/request-microphone-zh.js.map +1 -0
  17. package/es/assets/request-microphone-zh.png +0 -0
  18. package/es/assets/reset-camera-en.js +2 -0
  19. package/es/assets/reset-camera-en.js.d.ts +2 -0
  20. package/es/assets/reset-camera-en.js.map +1 -0
  21. package/es/assets/reset-camera-en.png +0 -0
  22. package/es/assets/reset-camera-zh.js +2 -0
  23. package/es/assets/reset-camera-zh.js.d.ts +2 -0
  24. package/es/assets/reset-camera-zh.js.map +1 -0
  25. package/es/assets/reset-camera-zh.png +0 -0
  26. package/es/assets/reset-microphone-en.js +2 -0
  27. package/es/assets/reset-microphone-en.js.d.ts +2 -0
  28. package/es/assets/reset-microphone-en.js.map +1 -0
  29. package/es/assets/reset-microphone-en.png +0 -0
  30. package/es/assets/reset-microphone-zh.js +2 -0
  31. package/es/assets/reset-microphone-zh.js.d.ts +2 -0
  32. package/es/assets/reset-microphone-zh.js.map +1 -0
  33. package/es/assets/reset-microphone-zh.png +0 -0
  34. package/es/assets/save-default-audio1-en.js +2 -0
  35. package/es/assets/save-default-audio1-en.js.d.ts +2 -0
  36. package/es/assets/save-default-audio1-en.js.map +1 -0
  37. package/es/assets/save-default-audio1-en.png +0 -0
  38. package/es/assets/save-default-audio1-zh.js +2 -0
  39. package/es/assets/save-default-audio1-zh.js.d.ts +2 -0
  40. package/es/assets/save-default-audio1-zh.js.map +1 -0
  41. package/es/assets/save-default-audio1-zh.png +0 -0
  42. package/es/assets/save-default-audio2-en.js +2 -0
  43. package/es/assets/save-default-audio2-en.js.d.ts +2 -0
  44. package/es/assets/save-default-audio2-en.js.map +1 -0
  45. package/es/assets/save-default-audio2-en.png +0 -0
  46. package/es/assets/save-default-audio2-zh.js +2 -0
  47. package/es/assets/save-default-audio2-zh.js.d.ts +2 -0
  48. package/es/assets/save-default-audio2-zh.js.map +1 -0
  49. package/es/assets/save-default-audio2-zh.png +0 -0
  50. package/es/components/ConfigProvider/index.js +1 -0
  51. package/es/components/ConfigProvider/index.js.map +1 -1
  52. package/es/hooks/index.d.ts +4 -0
  53. package/es/hooks/index.js +4 -0
  54. package/es/hooks/index.js.map +1 -1
  55. package/es/hooks/useRefValue.d.ts +2 -2
  56. package/es/hooks/useRefValue.js.map +1 -1
  57. package/es/hooks/useStompSocket.d.ts +63 -0
  58. package/es/hooks/useStompSocket.js +182 -0
  59. package/es/hooks/useStompSocket.js.map +1 -0
  60. package/es/hooks/useUserMedia.d.ts +104 -0
  61. package/es/hooks/useUserMedia.js +787 -0
  62. package/es/hooks/useUserMedia.js.map +1 -0
  63. package/es/locales/index.d.ts +57 -0
  64. package/es/locales/langs/en-US.d.ts +19 -0
  65. package/es/locales/langs/en-US.js +20 -1
  66. package/es/locales/langs/en-US.js.map +1 -1
  67. package/es/locales/langs/zh-CN.d.ts +19 -0
  68. package/es/locales/langs/zh-CN.js +20 -1
  69. package/es/locales/langs/zh-CN.js.map +1 -1
  70. package/es/utils/stream.d.ts +68 -0
  71. package/es/utils/stream.js +122 -0
  72. package/es/utils/stream.js.map +1 -0
  73. package/lib/assets/request-camera-en.js +26 -0
  74. package/lib/assets/request-camera-en.js.d.ts +2 -0
  75. package/lib/assets/request-camera-en.js.map +7 -0
  76. package/lib/assets/request-camera-en.png +0 -0
  77. package/lib/assets/request-camera-zh.js +26 -0
  78. package/lib/assets/request-camera-zh.js.d.ts +2 -0
  79. package/lib/assets/request-camera-zh.js.map +7 -0
  80. package/lib/assets/request-camera-zh.png +0 -0
  81. package/lib/assets/request-microphone-en.js +26 -0
  82. package/lib/assets/request-microphone-en.js.d.ts +2 -0
  83. package/lib/assets/request-microphone-en.js.map +7 -0
  84. package/lib/assets/request-microphone-en.png +0 -0
  85. package/lib/assets/request-microphone-zh.js +26 -0
  86. package/lib/assets/request-microphone-zh.js.d.ts +2 -0
  87. package/lib/assets/request-microphone-zh.js.map +7 -0
  88. package/lib/assets/request-microphone-zh.png +0 -0
  89. package/lib/assets/reset-camera-en.js +26 -0
  90. package/lib/assets/reset-camera-en.js.d.ts +2 -0
  91. package/lib/assets/reset-camera-en.js.map +7 -0
  92. package/lib/assets/reset-camera-en.png +0 -0
  93. package/lib/assets/reset-camera-zh.js +26 -0
  94. package/lib/assets/reset-camera-zh.js.d.ts +2 -0
  95. package/lib/assets/reset-camera-zh.js.map +7 -0
  96. package/lib/assets/reset-camera-zh.png +0 -0
  97. package/lib/assets/reset-microphone-en.js +26 -0
  98. package/lib/assets/reset-microphone-en.js.d.ts +2 -0
  99. package/lib/assets/reset-microphone-en.js.map +7 -0
  100. package/lib/assets/reset-microphone-en.png +0 -0
  101. package/lib/assets/reset-microphone-zh.js +26 -0
  102. package/lib/assets/reset-microphone-zh.js.d.ts +2 -0
  103. package/lib/assets/reset-microphone-zh.js.map +7 -0
  104. package/lib/assets/reset-microphone-zh.png +0 -0
  105. package/lib/assets/save-default-audio1-en.js +26 -0
  106. package/lib/assets/save-default-audio1-en.js.d.ts +2 -0
  107. package/lib/assets/save-default-audio1-en.js.map +7 -0
  108. package/lib/assets/save-default-audio1-en.png +0 -0
  109. package/lib/assets/save-default-audio1-zh.js +26 -0
  110. package/lib/assets/save-default-audio1-zh.js.d.ts +2 -0
  111. package/lib/assets/save-default-audio1-zh.js.map +7 -0
  112. package/lib/assets/save-default-audio1-zh.png +0 -0
  113. package/lib/assets/save-default-audio2-en.js +26 -0
  114. package/lib/assets/save-default-audio2-en.js.d.ts +2 -0
  115. package/lib/assets/save-default-audio2-en.js.map +7 -0
  116. package/lib/assets/save-default-audio2-en.png +0 -0
  117. package/lib/assets/save-default-audio2-zh.js +26 -0
  118. package/lib/assets/save-default-audio2-zh.js.d.ts +2 -0
  119. package/lib/assets/save-default-audio2-zh.js.map +7 -0
  120. package/lib/assets/save-default-audio2-zh.png +0 -0
  121. package/lib/components/ConfigProvider/index.js +1 -0
  122. package/lib/components/ConfigProvider/index.js.map +2 -2
  123. package/lib/hooks/index.d.ts +4 -0
  124. package/lib/hooks/index.js +12 -1
  125. package/lib/hooks/index.js.map +2 -2
  126. package/lib/hooks/useRefValue.d.ts +2 -2
  127. package/lib/hooks/useRefValue.js.map +2 -2
  128. package/lib/hooks/useStompSocket.d.ts +63 -0
  129. package/lib/hooks/useStompSocket.js +154 -0
  130. package/lib/hooks/useStompSocket.js.map +7 -0
  131. package/lib/hooks/useUserMedia.d.ts +104 -0
  132. package/lib/hooks/useUserMedia.js +541 -0
  133. package/lib/hooks/useUserMedia.js.map +7 -0
  134. package/lib/locales/index.d.ts +57 -0
  135. package/lib/locales/langs/en-US.d.ts +19 -0
  136. package/lib/locales/langs/en-US.js +20 -1
  137. package/lib/locales/langs/en-US.js.map +2 -2
  138. package/lib/locales/langs/zh-CN.d.ts +19 -0
  139. package/lib/locales/langs/zh-CN.js +20 -1
  140. package/lib/locales/langs/zh-CN.js.map +2 -2
  141. package/lib/utils/stream.d.ts +68 -0
  142. package/lib/utils/stream.js +101 -0
  143. package/lib/utils/stream.js.map +7 -0
  144. package/package.json +9 -3
@@ -16,6 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  }
17
17
  return to;
18
18
  };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
21
  // If the importer is in node compatibility mode or this is not an ESM
21
22
  // file that has been converted to a CommonJS file using a Babel-
@@ -31,6 +32,8 @@ var hooks_exports = {};
31
32
  __export(hooks_exports, {
32
33
  useRefFunction: () => import_useRefFunction.default,
33
34
  useRefValue: () => import_useRefValue.default,
35
+ useStompSocket: () => import_useStompSocket.default,
36
+ useUserMedia: () => import_useUserMedia.default,
34
37
  useValidator: () => import_useValidator.default,
35
38
  useValidatorBuilder: () => import_useValidatorBuilder.default,
36
39
  useValidators: () => import_useValidators.default
@@ -38,15 +41,23 @@ __export(hooks_exports, {
38
41
  module.exports = __toCommonJS(hooks_exports);
39
42
  var import_useRefValue = __toESM(require("./useRefValue"));
40
43
  var import_useRefFunction = __toESM(require("./useRefFunction"));
44
+ __reExport(hooks_exports, require("./useStompSocket"), module.exports);
45
+ var import_useStompSocket = __toESM(require("./useStompSocket"));
41
46
  var import_useValidators = __toESM(require("./useValidators"));
42
47
  var import_useValidator = __toESM(require("./useValidator"));
43
48
  var import_useValidatorBuilder = __toESM(require("./useValidatorBuilder"));
49
+ __reExport(hooks_exports, require("./useUserMedia"), module.exports);
50
+ var import_useUserMedia = __toESM(require("./useUserMedia"));
44
51
  // Annotate the CommonJS export names for ESM import in node:
45
52
  0 && (module.exports = {
46
53
  useRefFunction,
47
54
  useRefValue,
55
+ useStompSocket,
56
+ useUserMedia,
48
57
  useValidator,
49
58
  useValidatorBuilder,
50
- useValidators
59
+ useValidators,
60
+ ...require("./useStompSocket"),
61
+ ...require("./useUserMedia")
51
62
  });
52
63
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/hooks/index.ts"],
4
- "sourcesContent": ["export { default as useRefValue } from './useRefValue';\nexport { default as useRefFunction } from './useRefFunction';\n\nexport type { ValidatorRuleMap } from './useValidators';\nexport { default as useValidators } from './useValidators';\nexport { default as useValidator } from './useValidator';\nexport type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';\nexport { default as useValidatorBuilder } from './useValidatorBuilder';\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAuC;AACvC,4BAA0C;AAG1C,2BAAyC;AACzC,0BAAwC;AAExC,iCAA+C;",
4
+ "sourcesContent": ["export { default as useRefValue } from './useRefValue';\nexport { default as useRefFunction } from './useRefFunction';\nexport * from './useStompSocket';\nexport { default as useStompSocket } from './useStompSocket';\nexport type { ValidatorRuleMap } from './useValidators';\nexport { default as useValidators } from './useValidators';\nexport { default as useValidator } from './useValidator';\nexport type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';\nexport { default as useValidatorBuilder } from './useValidatorBuilder';\nexport * from './useUserMedia';\nexport { default as useUserMedia } from './useUserMedia';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAuC;AACvC,4BAA0C;AAC1C,0BAAc,6BAFd;AAGA,4BAA0C;AAE1C,2BAAyC;AACzC,0BAAwC;AAExC,iCAA+C;AAC/C,0BAAc,2BATd;AAUA,0BAAwC;",
6
6
  "names": []
7
7
  }
@@ -1,4 +1,4 @@
1
- import type { RefObject } from 'react';
1
+ import type { MutableRefObject } from 'react';
2
2
  /**
3
3
  * - **EN:** Get a mutable ref object and automatically update the value change
4
4
  * - **CN:** 获取一个可变的ref对象,并自动更新值变化
@@ -7,5 +7,5 @@ import type { RefObject } from 'react';
7
7
  *
8
8
  * @returns A mutable ref object, but the reference is immutable | 可变的ref对象,但引用不可变
9
9
  */
10
- declare const useRefValue: <T>(value: T) => RefObject<T>;
10
+ declare const useRefValue: <T>(value: T) => MutableRefObject<T>;
11
11
  export default useRefValue;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/hooks/useRefValue.ts"],
4
- "sourcesContent": ["import type { RefObject } from 'react';\nimport { useRef } from 'react';\n\n/**\n * - **EN:** Get a mutable ref object and automatically update the value change\n * - **CN:** 获取一个可变的ref对象,并自动更新值变化\n *\n * @param value the wrapped value | 被包装的值\n *\n * @returns A mutable ref object, but the reference is immutable | 可变的ref对象,但引用不可变\n */\nconst useRefValue = <T>(value: T): RefObject<T> => {\n const ref = useRef<T>(value);\n ref.current = value;\n return ref;\n};\n\nexport default useRefValue;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAuB;AAUvB,IAAM,cAAc,CAAI,UAA2B;AACjD,QAAM,UAAM,qBAAU,KAAK;AAC3B,MAAI,UAAU;AACd,SAAO;AACT;AAEA,IAAO,sBAAQ;",
4
+ "sourcesContent": ["import type { MutableRefObject } from 'react';\nimport { useRef } from 'react';\n\n/**\n * - **EN:** Get a mutable ref object and automatically update the value change\n * - **CN:** 获取一个可变的ref对象,并自动更新值变化\n *\n * @param value the wrapped value | 被包装的值\n *\n * @returns A mutable ref object, but the reference is immutable | 可变的ref对象,但引用不可变\n */\nconst useRefValue = <T>(value: T): MutableRefObject<T> => {\n const ref = useRef<T>(value);\n ref.current = value;\n return ref;\n};\n\nexport default useRefValue;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAuB;AAUvB,IAAM,cAAc,CAAI,UAAkC;AACxD,QAAM,UAAM,qBAAU,KAAK;AAC3B,MAAI,UAAU;AACd,SAAO;AACT;AAEA,IAAO,sBAAQ;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,63 @@
1
+ import type { StompConfig } from '@stomp/stompjs';
2
+ import { Client } from '@stomp/stompjs';
3
+ export interface UseSocketOptions<M = string> {
4
+ /**
5
+ * - **EN:** Socket connection address
6
+ * - **CN:** Socket连接地址
7
+ */
8
+ url: string;
9
+ /**
10
+ * - **EN:** STOMP connection configuration
11
+ * - **CN:** - **CN:** STOMP连接配置
12
+ */
13
+ connectConfig?: StompConfig;
14
+ /**
15
+ * - **EN:** Channel path for publishing messages
16
+ * - **CN:** 发布消息的频道路径
17
+ */
18
+ sendEndpoint?: string;
19
+ /**
20
+ * - **EN:** Endpoint path for receiving messages
21
+ * - **CN:** 接收消息的端点路径
22
+ */
23
+ subscribeEndpoint?: string;
24
+ /**
25
+ * - **EN:** Connection success callback
26
+ * - **CN:** 连接成功回调
27
+ */
28
+ onConnected?: () => void;
29
+ /**
30
+ * - **EN:** Message received callback
31
+ * - **CN:** 接收到消息回调
32
+ */
33
+ onMessage?: (message: M) => void;
34
+ /**
35
+ * - **EN:** Parse message body, return value as input parameter for `onMessage`
36
+ * - **CN:** 解析消息体,返回值作为`onMessage`的输入参数。
37
+ *
38
+ * @param body - The raw message body received from the server, needs to be deserialized based on
39
+ * actual conditions. | 从服务端接收到的原始消息体,需要根据实际情况进行反序列化。
40
+ *
41
+ * @returns Parsed message body as the input parameter of `onMessage`| 解析后的消息体,作为`onMessage`的输入参数
42
+ */
43
+ parseMessageBody?: (body: string) => M;
44
+ /**
45
+ * - **EN:** Connection close callback
46
+ * - **CN:** 连接关闭回调
47
+ */
48
+ onClose?: () => void;
49
+ }
50
+ /**
51
+ * - **EN:** Establish a WebSocket based on the STOMP protocol, mainly used for bidirectional
52
+ * transmission of serializable character messages.
53
+ * - **CN:** 建立基于STOMP协议的WebSocket,主要用于双向传递可序列化的字符型消息
54
+ */
55
+ declare function useStompSocket<M = string>(options: UseSocketOptions<M>): {
56
+ connect: () => Promise<void>;
57
+ close: () => void;
58
+ send: (body: string) => void;
59
+ connecting: boolean;
60
+ socket: WebSocket | undefined;
61
+ stompClient: Client | undefined;
62
+ };
63
+ export default useStompSocket;
@@ -0,0 +1,154 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/hooks/useStompSocket.ts
30
+ var useStompSocket_exports = {};
31
+ __export(useStompSocket_exports, {
32
+ default: () => useStompSocket_default
33
+ });
34
+ module.exports = __toCommonJS(useStompSocket_exports);
35
+ var import_react = require("react");
36
+ var import_stompjs = require("@stomp/stompjs");
37
+ var import_antd = require("antd");
38
+ var import_sockjs_client = __toESM(require("sockjs-client"));
39
+ var import_useRefFunction = __toESM(require("./useRefFunction"));
40
+ var import_useT = __toESM(require("./useT"));
41
+ function useStompSocket(options) {
42
+ const { url, sendEndpoint, subscribeEndpoint, connectConfig, onMessage, parseMessageBody, onConnected, onClose } = options;
43
+ const t = (0, import_useT.default)();
44
+ const socketRef = (0, import_react.useRef)(void 0);
45
+ const stompClientRef = (0, import_react.useRef)(void 0);
46
+ const [connecting, setConnecting] = (0, import_react.useState)(false);
47
+ const isConnectedRef = (0, import_react.useRef)(false);
48
+ const [, refresh] = (0, import_react.useState)();
49
+ const connect = (0, import_useRefFunction.default)(async () => {
50
+ const promise = new Promise((resolve, reject) => {
51
+ try {
52
+ setConnecting(true);
53
+ socketRef.current = new import_sockjs_client.default(url);
54
+ stompClientRef.current = new import_stompjs.Client({
55
+ heartbeatIncoming: 5e3,
56
+ heartbeatOutgoing: 5e3,
57
+ ...connectConfig,
58
+ webSocketFactory: () => socketRef.current
59
+ });
60
+ stompClientRef.current.activate();
61
+ stompClientRef.current.onConnect = () => {
62
+ var _a;
63
+ setConnecting(false);
64
+ isConnectedRef.current = true;
65
+ onConnected == null ? void 0 : onConnected();
66
+ if (subscribeEndpoint) {
67
+ (_a = stompClientRef.current) == null ? void 0 : _a.subscribe(subscribeEndpoint, (response) => {
68
+ if (parseMessageBody) {
69
+ onMessage == null ? void 0 : onMessage(parseMessageBody(response.body));
70
+ } else {
71
+ onMessage == null ? void 0 : onMessage(response.body);
72
+ }
73
+ });
74
+ }
75
+ resolve();
76
+ };
77
+ stompClientRef.current.onStompError = (error) => {
78
+ console.error("STOMP Error:", error);
79
+ };
80
+ stompClientRef.current.onWebSocketError = (error) => {
81
+ console.error("WebSocket Error:", error);
82
+ };
83
+ if (socketRef.current) {
84
+ socketRef.current.onerror = (error) => {
85
+ console.error(error);
86
+ };
87
+ }
88
+ stompClientRef.current.onWebSocketClose = (event) => {
89
+ var _a;
90
+ setConnecting(false);
91
+ if (event.type === "close" && event.code === 1e3) {
92
+ return;
93
+ }
94
+ (_a = stompClientRef.current) == null ? void 0 : _a.debug("StompClient closed");
95
+ if (isConnectedRef.current) {
96
+ isConnectedRef.current = false;
97
+ onClose == null ? void 0 : onClose();
98
+ import_antd.notification.error({ message: void 0, description: t("hooks.useStompSocket.serverDisconnected") });
99
+ } else {
100
+ import_antd.notification.error({ message: t("hooks.useStompSocket.connectError") });
101
+ }
102
+ };
103
+ if (socketRef.current) {
104
+ socketRef.current.onclose = (event) => {
105
+ var _a;
106
+ setConnecting(false);
107
+ isConnectedRef.current = false;
108
+ (_a = stompClientRef.current) == null ? void 0 : _a.debug("Socket closed");
109
+ console.log("event", event);
110
+ onClose == null ? void 0 : onClose();
111
+ };
112
+ }
113
+ } catch (error) {
114
+ console.error(error);
115
+ reject(error);
116
+ }
117
+ });
118
+ refresh();
119
+ await new Promise((resolve) => setTimeout(resolve));
120
+ return promise;
121
+ });
122
+ const close = (0, import_useRefFunction.default)(() => {
123
+ var _a, _b;
124
+ try {
125
+ (_a = stompClientRef.current) == null ? void 0 : _a.deactivate();
126
+ (_b = socketRef.current) == null ? void 0 : _b.close();
127
+ isConnectedRef.current = false;
128
+ setConnecting(false);
129
+ } catch (error) {
130
+ console.error(error);
131
+ }
132
+ });
133
+ const send = (0, import_useRefFunction.default)((body) => {
134
+ var _a;
135
+ if (!sendEndpoint) {
136
+ console.error("No publish endpoint defined, unable to send message");
137
+ return;
138
+ }
139
+ (_a = stompClientRef.current) == null ? void 0 : _a.publish({
140
+ destination: sendEndpoint,
141
+ body
142
+ });
143
+ });
144
+ return {
145
+ connect,
146
+ close,
147
+ send,
148
+ connecting,
149
+ socket: socketRef.current,
150
+ stompClient: stompClientRef.current
151
+ };
152
+ }
153
+ var useStompSocket_default = useStompSocket;
154
+ //# sourceMappingURL=useStompSocket.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/hooks/useStompSocket.ts"],
4
+ "sourcesContent": ["import { useRef, useState } from 'react';\nimport type { StompConfig } from '@stomp/stompjs';\nimport { Client } from '@stomp/stompjs';\nimport { notification } from 'antd';\nimport SockJS from 'sockjs-client';\nimport useRefFunction from './useRefFunction';\nimport useT from './useT';\n\nexport interface UseSocketOptions<M = string> {\n /**\n * - **EN:** Socket connection address\n * - **CN:** Socket连接地址\n */\n url: string;\n /**\n * - **EN:** STOMP connection configuration\n * - **CN:** - **CN:** STOMP连接配置\n */\n connectConfig?: StompConfig;\n /**\n * - **EN:** Channel path for publishing messages\n * - **CN:** 发布消息的频道路径\n */\n sendEndpoint?: string;\n /**\n * - **EN:** Endpoint path for receiving messages\n * - **CN:** 接收消息的端点路径\n */\n subscribeEndpoint?: string;\n /**\n * - **EN:** Connection success callback\n * - **CN:** 连接成功回调\n */\n onConnected?: () => void;\n /**\n * - **EN:** Message received callback\n * - **CN:** 接收到消息回调\n */\n onMessage?: (message: M) => void;\n /**\n * - **EN:** Parse message body, return value as input parameter for `onMessage`\n * - **CN:** 解析消息体,返回值作为`onMessage`的输入参数。\n *\n * @param body - The raw message body received from the server, needs to be deserialized based on\n * actual conditions. | 从服务端接收到的原始消息体,需要根据实际情况进行反序列化。\n *\n * @returns Parsed message body as the input parameter of `onMessage`| 解析后的消息体,作为`onMessage`的输入参数\n */\n parseMessageBody?: (body: string) => M;\n /**\n * - **EN:** Connection close callback\n * - **CN:** 连接关闭回调\n */\n onClose?: () => void;\n}\n\n/**\n * - **EN:** Establish a WebSocket based on the STOMP protocol, mainly used for bidirectional\n * transmission of serializable character messages.\n * - **CN:** 建立基于STOMP协议的WebSocket,主要用于双向传递可序列化的字符型消息\n */\nfunction useStompSocket<M = string>(options: UseSocketOptions<M>) {\n const { url, sendEndpoint, subscribeEndpoint, connectConfig, onMessage, parseMessageBody, onConnected, onClose } =\n options;\n const t = useT();\n const socketRef = useRef<WebSocket | undefined>(undefined);\n const stompClientRef = useRef<Client | undefined>(undefined);\n const [connecting, setConnecting] = useState(false);\n const isConnectedRef = useRef(false);\n // eslint-disable-next-line @typescript-eslint/no-invalid-void-type\n const [, refresh] = useState<void>();\n\n const connect = useRefFunction(async () => {\n const promise = new Promise<void>((resolve, reject) => {\n try {\n setConnecting(true);\n // Create SockJS instance\n socketRef.current = new SockJS(url);\n\n // Create STOMP client\n stompClientRef.current = new Client({\n heartbeatIncoming: 5000,\n heartbeatOutgoing: 5000,\n ...connectConfig,\n webSocketFactory: () => socketRef.current,\n });\n // Connect to STOMP server\n stompClientRef.current.activate();\n\n // STOMP server connection established\n stompClientRef.current.onConnect = () => {\n setConnecting(false);\n isConnectedRef.current = true;\n onConnected?.();\n if (subscribeEndpoint) {\n stompClientRef.current?.subscribe(subscribeEndpoint, (response) => {\n if (parseMessageBody) {\n onMessage?.(parseMessageBody(response.body));\n } else {\n onMessage?.(response.body as M);\n }\n });\n }\n resolve();\n };\n stompClientRef.current.onStompError = (error) => {\n console.error('STOMP Error:', error);\n };\n stompClientRef.current.onWebSocketError = (error) => {\n console.error('WebSocket Error:', error);\n };\n if (socketRef.current) {\n socketRef.current.onerror = (error: unknown) => {\n console.error(error);\n };\n }\n\n stompClientRef.current.onWebSocketClose = (event) => {\n setConnecting(false);\n // Normal close\n if (event.type === 'close' && event.code === 1000) {\n return;\n }\n stompClientRef.current?.debug('StompClient closed');\n if (isConnectedRef.current) {\n isConnectedRef.current = false;\n onClose?.();\n notification.error({ message: undefined, description: t('hooks.useStompSocket.serverDisconnected') });\n } else {\n // Client connection failed\n notification.error({ message: t('hooks.useStompSocket.connectError') });\n }\n };\n if (socketRef.current) {\n socketRef.current.onclose = (event) => {\n setConnecting(false);\n isConnectedRef.current = false;\n stompClientRef.current?.debug('Socket closed');\n console.log('event', event);\n onClose?.();\n };\n }\n } catch (error: unknown) {\n console.error(error);\n // notification.error({ message: error?.message ?? JSON.stringify(error) });\n reject(error);\n }\n });\n refresh();\n await new Promise((resolve) => setTimeout(resolve));\n return promise;\n });\n const close = useRefFunction(() => {\n try {\n stompClientRef.current?.deactivate();\n socketRef.current?.close();\n isConnectedRef.current = false;\n setConnecting(false);\n } catch (error) {\n console.error(error);\n }\n });\n const send = useRefFunction((body: string) => {\n if (!sendEndpoint) {\n console.error('No publish endpoint defined, unable to send message');\n return;\n }\n stompClientRef.current?.publish({\n destination: sendEndpoint,\n body,\n });\n });\n return {\n connect,\n close,\n send,\n connecting,\n socket: socketRef.current,\n stompClient: stompClientRef.current,\n };\n}\n\nexport default useStompSocket;\n"],
5
+ "mappings": "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",
6
+ "names": ["useT", "useRefFunction", "SockJS"]
7
+ }
@@ -0,0 +1,104 @@
1
+ export interface UseUserMediaProps {
2
+ /**
3
+ * - **EN:** The media constraints for the audio and video stream.
4
+ * - **CN:** 媒体流的媒体约束。
5
+ */
6
+ media: Pick<MediaStreamConstraints, 'audio' | 'video'>;
7
+ /**
8
+ * - **EN:** Whether to enable PCM output, only effective when recording audio. Please use
9
+ * `onPcmStreamChunk` callback to get PCM data. Default is `false`.
10
+ * - **CN:** 是否启用 PCM 输出,仅在录制音频时有效。请使用 `onPcmStreamChunk` 回调获取 PCM 数据。默认`false`
11
+ */
12
+ pcmAudioOptions?: {
13
+ /**
14
+ * - **EN:** The audio context options for the PCM output.
15
+ * - **CN:** PCM 输出的音频上下文选项。
16
+ */
17
+ audioContext?: AudioContextOptions;
18
+ /**
19
+ * - **EN:** The worklet options for the PCM output.
20
+ * - **CN:** PCM 输出的工作线程选项。
21
+ */
22
+ workletOptions?: AudioWorkletNodeOptions;
23
+ };
24
+ /**
25
+ * - **EN:** Callback function that is triggered when the recording starts, providing the media
26
+ * stream.
27
+ * - **CN:** 开始录制时触发的回调函数,提供媒体流。
28
+ *
29
+ * @param {MediaStream} stream - The media stream.
30
+ */
31
+ onStartRecording?: (stream: MediaStream) => void;
32
+ /**
33
+ * - **EN:** Callback function that is triggered when the recording stops.
34
+ * - **CN:** 停止录制时触发的回调函数。
35
+ */
36
+ onStopRecording?: () => void;
37
+ /**
38
+ * - **EN:** Callback function that is triggered when a new chunk of media data is available.
39
+ * - **CN:** 当录制媒体流时,每个时间分片会触发一次 `onStreamChunk` 回调,提供媒体数据块。
40
+ *
41
+ * > 注意音频流编码格式为:audio/webm;codecs=opus。如果希望获取 PCM 数据,请使用 `onPcmData` 回调。
42
+ *
43
+ * @param {Blob} chunk - The media data chunk (MIME: audio/webm;codecs=opus) | 媒体数据块 (MIME:
44
+ * audio/webm;codecs=opus)
45
+ */
46
+ onStreamChunk?: (chunk: Blob) => void;
47
+ /**
48
+ * - **EN:** Callback for raw PCM float data (per render quantum)
49
+ * - **CN:** 获取原始 PCM 浮点数据的回调(每个渲染量子)
50
+ *
51
+ * @param data Monophonic or polyphonic spliced data | 单声道或多声道拼接数据
52
+ * @param sampleRate Sample rate | 采样率
53
+ */
54
+ onPcmStreamChunk?: (channels: Float32Array[], sampleRate: number) => void;
55
+ /**
56
+ * - **EN:** Whether to disable this hook.
57
+ * - **CN:** 是否禁用此工具
58
+ */
59
+ disabled?: boolean;
60
+ /**
61
+ * - **EN:** The slicing time period (milliseconds) for each fragment of the audio and video stream,
62
+ * each time slice will trigger the `onStreamChunk` callback. Default is `500`.
63
+ * - **CN:** 媒体流每个分片的切片时间段(毫秒),每个时间分片会触发一次 `onStreamChunk` 回调,默认值为 `500`。
64
+ */
65
+ streamSliceMs?: number;
66
+ /**
67
+ * - **EN:** The silence detection threshold (0-1) for the audio stream, below which the audio is
68
+ * considered silent. Default is `0`.
69
+ * - **CN:** 音频流的静音检测阈值(0-1),低于该值音频被视为静音。默认值为 `0`。
70
+ */
71
+ soundDetectionThreshold?: number;
72
+ /**
73
+ * - **EN:** The timeout duration (milliseconds) for detecting sound input. If no sound is detected
74
+ * within this period, the user will be prompted to re-select the audio device. Default is
75
+ * `3000`.
76
+ * - **CN:** 检测是否有声音输入的超时时间(毫秒),如果在该时间段内没有检测到声音,则会提示用户重新选择音频设备。默认值为 `3000`。
77
+ */
78
+ soundDetectionTimeout?: number;
79
+ }
80
+ declare const useUserMedia: (props: UseUserMediaProps) => UseUserMediaResult;
81
+ export interface UseUserMediaResult {
82
+ /**
83
+ * - **EN** Whether the media stream is currently being recorded
84
+ * - **CN** 是否正在录制媒体流
85
+ */
86
+ isRecording: boolean;
87
+ /**
88
+ * - **EN** Start recording the media stream
89
+ * - **CN** 开始录制媒体流
90
+ */
91
+ startRecording: () => Promise<MediaRecorder>;
92
+ /**
93
+ * - **EN** Stop recording the media stream
94
+ * - **CN** 停止录制媒体流
95
+ */
96
+ stopRecording: () => void;
97
+ /**
98
+ * - **EN** Get the media stream being recorded, returns the stream if recording, otherwise returns
99
+ * `undefined`
100
+ * - **CN** 获取正在录制的媒体流,如果正在录制则返回该流,否则返回 `undefined`
101
+ */
102
+ mediaStream?: MediaStream;
103
+ }
104
+ export default useUserMedia;