react-video-call
Version:
ReactVideoCall is a simple yet powerful WebRTC-based video call component for React. It uses Firebase for signaling and supports both desktop and mobile views with minimal setup.
78 lines • 3.15 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { initializeApp } from 'firebase/app';
import { doc, setDoc } from 'firebase/firestore';
import { onSnapshot, getFirestore } from 'firebase/firestore';
export class FirebaseWrapper {
constructor(config) {
this._room = {};
this._app = initializeApp(config);
this._db = getFirestore(this._app);
}
createRoom(roomName, ref) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._roomRef) {
throw new Error("Room have not initialized!");
}
this._roomName = roomName;
this._roomRef = doc(this._db, "rooms", this._roomName);
yield this.updateRoom("create", { createdAt: new Date(Date.now()).toISOString(), ref: ref });
return true;
});
}
joinRoom(roomName, onUpdateCb) {
return __awaiter(this, void 0, void 0, function* () {
if (this._roomName === roomName) {
console.debug("You alredy joined room.");
throw new Error("You alredy joined room.");
return;
}
this._roomName = roomName;
this._roomRef = doc(this._db, "rooms", this._roomName);
return this.onRoomUpdate(onUpdateCb);
});
}
leaveRoom() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._roomName) {
console.debug("No Connection Exist.");
throw new Error("No Connection Exist.");
return;
}
this.updateRoom("leave", null);
});
}
updateRoom(action, data) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._roomRef) {
throw new Error("room have not create yet.");
}
if (action === "leave") {
this._room = {};
}
else {
this._room[action] = data;
}
yield setDoc(this._roomRef, Object.assign({}, this._room));
});
}
onRoomUpdate(cb) {
if (!this._roomRef || !this._roomName) {
throw new Error("room have not create yet.");
}
const unsub = onSnapshot(this._roomRef, (doc) => {
const data = doc.data();
this._room = Object.assign(Object.assign({}, this._room), data);
cb(data);
});
return unsub;
}
}
//# sourceMappingURL=firebaseLib.js.map