Skip to content

Commit

Permalink
feat: add initialization check for creating and joining room, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
therealPaulPlay committed Jan 26, 2025
1 parent c78efbc commit aa63646
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playpeerjs",
"version": "1.1.3",
"version": "1.1.4",
"description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.",
"type": "module",
"main": "dist/playpeer.js",
Expand Down
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class PlayPeer {
id;
#peer;
#options;
#initialized = false;

// Event callbacks stored in a map
#callbacks = new Map();
Expand Down Expand Up @@ -119,6 +120,7 @@ export default class PlayPeer {
this.#peer.on('open', () => {
this.#triggerEvent("status", "Connected to signalling server!");
clearTimeout(connectionOpenTimeout);
this.#initialized = true;
resolve();
});
});
Expand Down Expand Up @@ -256,12 +258,13 @@ export default class PlayPeer {
* @returns {Promise} Promise resolves with peer id
*/
createRoom(initialStorage = {}) {
if (!this.#peer || this.#peer.destroyed) {
this.#triggerEvent("error", "Cannot create room if peer is not initialized.");
console.error(ERROR_PREFIX + "Cannot create room if peer is not initialized.");
reject(new Error("Peer not initialized."));
}
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
if (!this.#peer || this.#peer.destroyed || !this.#initialized) {
this.#triggerEvent("error", "Cannot create room if peer is not initialized. Note that .init() is async.");
console.error(ERROR_PREFIX + "Cannot create room if peer is not initialized. Note that .init() is async.");
reject(new Error("Peer not initialized."));
}

this.#isHost = true;
this.#storage = initialStorage;
this.#triggerEvent("storageUpdate", { ...this.#storage });
Expand All @@ -276,9 +279,9 @@ export default class PlayPeer {
*/
async joinRoom(hostId) {
return new Promise((resolve, reject) => {
if (!this.#peer || this.#peer.destroyed) {
this.#triggerEvent("error", "Cannot join room if peer is not initialized.");
console.error(ERROR_PREFIX + "Cannot join room if peer is not initialized.");
if (!this.#peer || this.#peer.destroyed || !this.#initialized) {
this.#triggerEvent("error", "Cannot join room if peer is not initialized. Note that .init() is async.");
console.error(ERROR_PREFIX + "Cannot join room if peer is not initialized. Note that .init() is async.");
reject(new Error("Peer not initialized."));
}
try {
Expand Down Expand Up @@ -562,6 +565,7 @@ export default class PlayPeer {
this.#isHost = false;
this.#hostConnections.clear();
this.#hostConnectionsIdArray = [];
this.#initialized = false;

// Clear intervals
clearInterval(this.#heartbeatSendInterval);
Expand Down

0 comments on commit aa63646

Please sign in to comment.