@voicemaster/react 1.0.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.
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +115 -0
- package/dist/index.mjs +88 -0
- package/package.json +37 -0
- package/src/index.ts +1 -0
- package/src/useVoice.ts +106 -0
- package/tsconfig.json +21 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { VoiceClientConfig, VoiceClient } from '@voicemaster/core';
|
|
2
|
+
|
|
3
|
+
interface UseVoiceOptions extends VoiceClientConfig {
|
|
4
|
+
autoConnect?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare function useVoice(options: UseVoiceOptions): {
|
|
7
|
+
isConnected: boolean;
|
|
8
|
+
isMuted: boolean;
|
|
9
|
+
remoteStream: MediaStream | null;
|
|
10
|
+
peers: string[];
|
|
11
|
+
speakingUsers: Set<string>;
|
|
12
|
+
connect: () => void;
|
|
13
|
+
disconnect: () => void;
|
|
14
|
+
toggleMute: () => void;
|
|
15
|
+
client: VoiceClient | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { useVoice };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { VoiceClientConfig, VoiceClient } from '@voicemaster/core';
|
|
2
|
+
|
|
3
|
+
interface UseVoiceOptions extends VoiceClientConfig {
|
|
4
|
+
autoConnect?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare function useVoice(options: UseVoiceOptions): {
|
|
7
|
+
isConnected: boolean;
|
|
8
|
+
isMuted: boolean;
|
|
9
|
+
remoteStream: MediaStream | null;
|
|
10
|
+
peers: string[];
|
|
11
|
+
speakingUsers: Set<string>;
|
|
12
|
+
connect: () => void;
|
|
13
|
+
disconnect: () => void;
|
|
14
|
+
toggleMute: () => void;
|
|
15
|
+
client: VoiceClient | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { useVoice };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useVoice: () => useVoice
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/useVoice.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_core = require("@voicemaster/core");
|
|
30
|
+
function useVoice(options) {
|
|
31
|
+
const [isConnected, setIsConnected] = (0, import_react.useState)(false);
|
|
32
|
+
const [isMuted, setIsMuted] = (0, import_react.useState)(false);
|
|
33
|
+
const [remoteStream, setRemoteStream] = (0, import_react.useState)(null);
|
|
34
|
+
const [speakingUsers, setSpeakingUsers] = (0, import_react.useState)(/* @__PURE__ */ new Set());
|
|
35
|
+
const [peers, setPeers] = (0, import_react.useState)([]);
|
|
36
|
+
const clientRef = (0, import_react.useRef)(null);
|
|
37
|
+
(0, import_react.useEffect)(() => {
|
|
38
|
+
const client = new import_core.VoiceClient({
|
|
39
|
+
signalingUrl: options.signalingUrl,
|
|
40
|
+
roomId: options.roomId,
|
|
41
|
+
userId: options.userId,
|
|
42
|
+
iceServers: options.iceServers,
|
|
43
|
+
autoConnect: false
|
|
44
|
+
});
|
|
45
|
+
client.on("connected", () => {
|
|
46
|
+
setIsConnected(true);
|
|
47
|
+
});
|
|
48
|
+
client.on("disconnected", () => {
|
|
49
|
+
setIsConnected(false);
|
|
50
|
+
setPeers([]);
|
|
51
|
+
setRemoteStream(null);
|
|
52
|
+
});
|
|
53
|
+
client.on("remoteStream", (stream) => {
|
|
54
|
+
setRemoteStream(stream);
|
|
55
|
+
});
|
|
56
|
+
client.on("userJoined", (userId) => {
|
|
57
|
+
setPeers((prev) => [...prev, userId]);
|
|
58
|
+
});
|
|
59
|
+
client.on("userLeft", (userId) => {
|
|
60
|
+
setPeers((prev) => prev.filter((id) => id !== userId));
|
|
61
|
+
setSpeakingUsers((prev) => {
|
|
62
|
+
const newSet = new Set(prev);
|
|
63
|
+
newSet.delete(userId);
|
|
64
|
+
return newSet;
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
client.on("speaking", (userId) => {
|
|
68
|
+
setSpeakingUsers((prev) => new Set(prev).add(userId));
|
|
69
|
+
});
|
|
70
|
+
client.on("stoppedSpeaking", (userId) => {
|
|
71
|
+
setSpeakingUsers((prev) => {
|
|
72
|
+
const newSet = new Set(prev);
|
|
73
|
+
newSet.delete(userId);
|
|
74
|
+
return newSet;
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
client.on("error", (error) => {
|
|
78
|
+
console.error("Voice client error:", error);
|
|
79
|
+
});
|
|
80
|
+
clientRef.current = client;
|
|
81
|
+
if (options.autoConnect !== false) {
|
|
82
|
+
client.connect();
|
|
83
|
+
}
|
|
84
|
+
return () => {
|
|
85
|
+
client.disconnect();
|
|
86
|
+
};
|
|
87
|
+
}, [options.signalingUrl, options.roomId, options.userId, options.iceServers]);
|
|
88
|
+
const connect = (0, import_react.useCallback)(() => {
|
|
89
|
+
clientRef.current?.connect();
|
|
90
|
+
}, []);
|
|
91
|
+
const disconnect = (0, import_react.useCallback)(() => {
|
|
92
|
+
clientRef.current?.disconnect();
|
|
93
|
+
}, []);
|
|
94
|
+
const toggleMute = (0, import_react.useCallback)(() => {
|
|
95
|
+
if (clientRef.current) {
|
|
96
|
+
clientRef.current.toggleMute();
|
|
97
|
+
setIsMuted((prev) => !prev);
|
|
98
|
+
}
|
|
99
|
+
}, []);
|
|
100
|
+
return {
|
|
101
|
+
isConnected,
|
|
102
|
+
isMuted,
|
|
103
|
+
remoteStream,
|
|
104
|
+
peers,
|
|
105
|
+
speakingUsers,
|
|
106
|
+
connect,
|
|
107
|
+
disconnect,
|
|
108
|
+
toggleMute,
|
|
109
|
+
client: clientRef.current
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
useVoice
|
|
115
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// src/useVoice.ts
|
|
2
|
+
import { useEffect, useState, useRef, useCallback } from "react";
|
|
3
|
+
import { VoiceClient } from "@voicemaster/core";
|
|
4
|
+
function useVoice(options) {
|
|
5
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
6
|
+
const [isMuted, setIsMuted] = useState(false);
|
|
7
|
+
const [remoteStream, setRemoteStream] = useState(null);
|
|
8
|
+
const [speakingUsers, setSpeakingUsers] = useState(/* @__PURE__ */ new Set());
|
|
9
|
+
const [peers, setPeers] = useState([]);
|
|
10
|
+
const clientRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const client = new VoiceClient({
|
|
13
|
+
signalingUrl: options.signalingUrl,
|
|
14
|
+
roomId: options.roomId,
|
|
15
|
+
userId: options.userId,
|
|
16
|
+
iceServers: options.iceServers,
|
|
17
|
+
autoConnect: false
|
|
18
|
+
});
|
|
19
|
+
client.on("connected", () => {
|
|
20
|
+
setIsConnected(true);
|
|
21
|
+
});
|
|
22
|
+
client.on("disconnected", () => {
|
|
23
|
+
setIsConnected(false);
|
|
24
|
+
setPeers([]);
|
|
25
|
+
setRemoteStream(null);
|
|
26
|
+
});
|
|
27
|
+
client.on("remoteStream", (stream) => {
|
|
28
|
+
setRemoteStream(stream);
|
|
29
|
+
});
|
|
30
|
+
client.on("userJoined", (userId) => {
|
|
31
|
+
setPeers((prev) => [...prev, userId]);
|
|
32
|
+
});
|
|
33
|
+
client.on("userLeft", (userId) => {
|
|
34
|
+
setPeers((prev) => prev.filter((id) => id !== userId));
|
|
35
|
+
setSpeakingUsers((prev) => {
|
|
36
|
+
const newSet = new Set(prev);
|
|
37
|
+
newSet.delete(userId);
|
|
38
|
+
return newSet;
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
client.on("speaking", (userId) => {
|
|
42
|
+
setSpeakingUsers((prev) => new Set(prev).add(userId));
|
|
43
|
+
});
|
|
44
|
+
client.on("stoppedSpeaking", (userId) => {
|
|
45
|
+
setSpeakingUsers((prev) => {
|
|
46
|
+
const newSet = new Set(prev);
|
|
47
|
+
newSet.delete(userId);
|
|
48
|
+
return newSet;
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
client.on("error", (error) => {
|
|
52
|
+
console.error("Voice client error:", error);
|
|
53
|
+
});
|
|
54
|
+
clientRef.current = client;
|
|
55
|
+
if (options.autoConnect !== false) {
|
|
56
|
+
client.connect();
|
|
57
|
+
}
|
|
58
|
+
return () => {
|
|
59
|
+
client.disconnect();
|
|
60
|
+
};
|
|
61
|
+
}, [options.signalingUrl, options.roomId, options.userId, options.iceServers]);
|
|
62
|
+
const connect = useCallback(() => {
|
|
63
|
+
clientRef.current?.connect();
|
|
64
|
+
}, []);
|
|
65
|
+
const disconnect = useCallback(() => {
|
|
66
|
+
clientRef.current?.disconnect();
|
|
67
|
+
}, []);
|
|
68
|
+
const toggleMute = useCallback(() => {
|
|
69
|
+
if (clientRef.current) {
|
|
70
|
+
clientRef.current.toggleMute();
|
|
71
|
+
setIsMuted((prev) => !prev);
|
|
72
|
+
}
|
|
73
|
+
}, []);
|
|
74
|
+
return {
|
|
75
|
+
isConnected,
|
|
76
|
+
isMuted,
|
|
77
|
+
remoteStream,
|
|
78
|
+
peers,
|
|
79
|
+
speakingUsers,
|
|
80
|
+
connect,
|
|
81
|
+
disconnect,
|
|
82
|
+
toggleMute,
|
|
83
|
+
client: clientRef.current
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
useVoice
|
|
88
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voicemaster/react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React hooks for VoiceMaster voice communication",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean --external react --external @voicemaster/core",
|
|
17
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch --external react --external @voicemaster/core",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"react",
|
|
22
|
+
"webrtc",
|
|
23
|
+
"voice",
|
|
24
|
+
"hooks"
|
|
25
|
+
],
|
|
26
|
+
"author": "Sergey Minasyan",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@voicemaster/core": "^1.0.0",
|
|
30
|
+
"react": ">=18.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/react": "^18.0.0",
|
|
34
|
+
"tsup": "^8.0.0",
|
|
35
|
+
"typescript": "^5.3.0"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useVoice } from './useVoice';
|
package/src/useVoice.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { useEffect, useState, useRef, useCallback } from 'react';
|
|
2
|
+
import { VoiceClient } from '@voicemaster/core';
|
|
3
|
+
import type { VoiceClientConfig } from '@voicemaster/core';
|
|
4
|
+
|
|
5
|
+
interface UseVoiceOptions extends VoiceClientConfig {
|
|
6
|
+
autoConnect?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function useVoice(options: UseVoiceOptions) {
|
|
10
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
11
|
+
const [isMuted, setIsMuted] = useState(false);
|
|
12
|
+
const [remoteStream, setRemoteStream] = useState<MediaStream | null>(null);
|
|
13
|
+
const [speakingUsers, setSpeakingUsers] = useState<Set<string>>(new Set());
|
|
14
|
+
const [peers, setPeers] = useState<string[]>([]);
|
|
15
|
+
const clientRef = useRef<VoiceClient | null>(null);
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const client = new VoiceClient({
|
|
19
|
+
signalingUrl: options.signalingUrl,
|
|
20
|
+
roomId: options.roomId,
|
|
21
|
+
userId: options.userId,
|
|
22
|
+
iceServers: options.iceServers,
|
|
23
|
+
autoConnect: false
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
client.on('connected', () => {
|
|
27
|
+
setIsConnected(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
client.on('disconnected', () => {
|
|
31
|
+
setIsConnected(false);
|
|
32
|
+
setPeers([]);
|
|
33
|
+
setRemoteStream(null);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
client.on('remoteStream', (stream) => {
|
|
37
|
+
setRemoteStream(stream);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
client.on('userJoined', (userId) => {
|
|
41
|
+
setPeers(prev => [...prev, userId]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
client.on('userLeft', (userId) => {
|
|
45
|
+
setPeers(prev => prev.filter(id => id !== userId));
|
|
46
|
+
setSpeakingUsers(prev => {
|
|
47
|
+
const newSet = new Set(prev);
|
|
48
|
+
newSet.delete(userId);
|
|
49
|
+
return newSet;
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
client.on('speaking', (userId) => {
|
|
54
|
+
setSpeakingUsers(prev => new Set(prev).add(userId));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
client.on('stoppedSpeaking', (userId) => {
|
|
58
|
+
setSpeakingUsers(prev => {
|
|
59
|
+
const newSet = new Set(prev);
|
|
60
|
+
newSet.delete(userId);
|
|
61
|
+
return newSet;
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
client.on('error', (error) => {
|
|
66
|
+
console.error('Voice client error:', error);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
clientRef.current = client;
|
|
70
|
+
|
|
71
|
+
if (options.autoConnect !== false) {
|
|
72
|
+
client.connect();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return () => {
|
|
76
|
+
client.disconnect();
|
|
77
|
+
};
|
|
78
|
+
}, [options.signalingUrl, options.roomId, options.userId, options.iceServers]);
|
|
79
|
+
|
|
80
|
+
const connect = useCallback(() => {
|
|
81
|
+
clientRef.current?.connect();
|
|
82
|
+
}, []);
|
|
83
|
+
|
|
84
|
+
const disconnect = useCallback(() => {
|
|
85
|
+
clientRef.current?.disconnect();
|
|
86
|
+
}, []);
|
|
87
|
+
|
|
88
|
+
const toggleMute = useCallback(() => {
|
|
89
|
+
if (clientRef.current) {
|
|
90
|
+
clientRef.current.toggleMute();
|
|
91
|
+
setIsMuted(prev => !prev);
|
|
92
|
+
}
|
|
93
|
+
}, []);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
isConnected,
|
|
97
|
+
isMuted,
|
|
98
|
+
remoteStream,
|
|
99
|
+
peers,
|
|
100
|
+
speakingUsers,
|
|
101
|
+
connect,
|
|
102
|
+
disconnect,
|
|
103
|
+
toggleMute,
|
|
104
|
+
client: clientRef.current
|
|
105
|
+
};
|
|
106
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020", "DOM"],
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"jsx": "react-jsx",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"paths": {
|
|
16
|
+
"@voiceflow/core": ["../core/src"]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"exclude": ["node_modules", "dist"]
|
|
21
|
+
}
|