From 32af681ed2adead44b1398e91d299b1354b676b0 Mon Sep 17 00:00:00 2001 From: Justin Bakse Date: Thu, 3 Feb 2022 15:12:39 -0600 Subject: [PATCH] improve property names on Room --- public/index.html | 5 +-- src/Client.js | 4 +-- src/Record.js | 22 ++++++------- src/Room.js | 80 ++++++++++++++++++++++++----------------------- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/public/index.html b/public/index.html index 9a46b5c..43f0437 100644 --- a/public/index.html +++ b/public/index.html @@ -53,16 +53,13 @@

Contributed Examples

tic_tac_toe
  • chat_log
  • -
  • - chat_log_no_p5 -
  • Tests

  • test_identity
  • test_types
  • Expiremental Examples

    -
  • no_p5
  • +
  • empty_sketch
  • stickies
  • ultra_dart_city
  • diff --git a/src/Client.js b/src/Client.js index 84394b3..cb8df5c 100644 --- a/src/Client.js +++ b/src/Client.js @@ -55,9 +55,9 @@ export class Client { } } - getRecord(name) { + getDsRecord(name) { if (!this.#isReady) { - log.error("Client.getRecord() called before client ready."); + log.error("Client.getDsRecord() called before client ready."); } return this.#dsClient.record.getRecord(name); } diff --git a/src/Record.js b/src/Record.js index 9a6546f..e0474f3 100644 --- a/src/Record.js +++ b/src/Record.js @@ -21,7 +21,7 @@ export class Record { #name; // string: full name of record (e.g. "appName-roomName/_recordName") #shared; // {}: internal read/write object to be synced with other clients #watchedShared; // Proxy: observable object wrapping #shared - #record; // ds.Record: the record this party.Record is managing + #dsRecord; // ds.Record: the record this party.Record is managing #ownerUid; // uid: the client that "owns" this record, or null if no "owner" // ownerUid is used to warn if a client tries to change "someone elses" data #isReady; @@ -82,22 +82,22 @@ export class Record { // resets shared object to data // does not warn non-owners on write #setShared(data) { - this.#record.set("shared", data); + this.#dsRecord.set("shared", data); } async delete() { // todo: is below line needed? is there a need to setShared to {} before deleting a record? this.#setShared({}); - await this.#record.whenReady(); - this.#record.delete(); + await this.#dsRecord.whenReady(); + this.#dsRecord.delete(); } async watchShared(path_or_cb, cb) { await this.whenReady(); if (typeof path_or_cb === "string") { - this.#record.subscribe("shared." + path_or_cb, cb); + this.#dsRecord.subscribe("shared." + path_or_cb, cb); } else if (typeof path_or_cb === "function") { - this.#record.subscribe("shared", path_or_cb); + this.#dsRecord.subscribe("shared", path_or_cb); } } @@ -109,14 +109,14 @@ export class Record { await this.#client.whenReady(); // get and subscribe to record - this.#record = this.#client.getRecord(this.#name); - this.#record.subscribe("shared", this.#onServerChangedData.bind(this)); + this.#dsRecord = this.#client.getDsRecord(this.#name); + this.#dsRecord.subscribe("shared", this.#onServerChangedData.bind(this)); - await this.#record.whenReady(); + await this.#dsRecord.whenReady(); // initialize shared object // #todo should we use setWithAck or await #record.whenReady()? - if (!this.#record.get("shared")) this.#record.set("shared", {}); + if (!this.#dsRecord.get("shared")) this.#dsRecord.set("shared", {}); // report log.debug("RecordManager: Record ready.", this.#name); @@ -139,7 +139,7 @@ path: ${path} newValue: ${JSON.stringify(newValue)}` ); } - this.#record.set("shared." + path, newValue); + this.#dsRecord.set("shared." + path, newValue); } // _onServerChangedData diff --git a/src/Room.js b/src/Room.js index 90a95fa..6fff595 100644 --- a/src/Room.js +++ b/src/Room.js @@ -16,9 +16,9 @@ export class Room { #appName; // string: user provide name for the app #roomName; // string: user provide name for the room - #roomDataRecord; // ds.Record: {participants: [uid], host: uid} - #participants; // [uid]: cache of #roomDataRecord.participants - #recordList; // ds.List: user records created in this room + #roomDataDsRecord; // ds.Record: {participants: [uid], host: uid} + #participantUids; // [uid]: cache of #roomDataRecord.participants + #recordDsList; // ds.List: user records created in this room #participantRecords; // {uid: party.Record}: map of particpant records for room #participantShareds; // [(watched)shared] cache of #participantRecords.getShared()s #clientParticpantRecord; // party.Record, participant record this client @@ -48,23 +48,23 @@ export class Room { await this.#client.whenReady(); const connectRoomData = async () => { // load the _room_data record - this.#roomDataRecord = this.#client.getRecord( + this.#roomDataDsRecord = this.#client.getDsRecord( `${this.#appName}-${this.#roomName}/_room_data` ); - await this.#roomDataRecord.whenReady(); + await this.#roomDataDsRecord.whenReady(); // initialize the participants array - this.#participants = this.#roomDataRecord.get("participants"); - if (!this.#participants) { - this.#participants = []; + this.#participantUids = this.#roomDataDsRecord.get("participants"); + if (!this.#participantUids) { + this.#participantUids = []; // @todo change next two lines to setWithAck? - this.#roomDataRecord.set("participants", []); - await this.#roomDataRecord.whenReady(); + this.#roomDataDsRecord.set("participants", []); + await this.#roomDataDsRecord.whenReady(); } // subscribe to changes on the participans array - this.#roomDataRecord.subscribe("participants", (data) => { - this.#participants = data; + this.#roomDataDsRecord.subscribe("participants", (data) => { + this.#participantUids = data; this.#chooseHost(); this.#updateParticpantRecords(); }); @@ -72,10 +72,10 @@ export class Room { const connectRecordList = async () => { // load the record list - this.#recordList = this.#client.getList( + this.#recordDsList = this.#client.getList( `${this.#appName}-${this.#roomName}/_records` ); - await this.#recordList.whenReady(); + await this.#recordDsList.whenReady(); }; // let part A and part B happen in parallel @@ -111,10 +111,10 @@ export class Room { const record = new Record(this.#client, name); record.whenReady(async () => { - await this.#recordList.whenReady(); - const entries = this.#recordList.getEntries(); + await this.#recordDsList.whenReady(); + const entries = this.#recordDsList.getEntries(); if (!entries.includes(name)) { - this.#recordList.addEntry(name); + this.#recordDsList.addEntry(name); } }); @@ -124,9 +124,9 @@ export class Room { // add this client to the room join() { const name = this.#client.getUid(); - if (!this.#participants.includes(name)) { - this.#roomDataRecord.set( - `participants.${this.#participants.length}`, + if (!this.#participantUids.includes(name)) { + this.#roomDataDsRecord.set( + `participants.${this.#participantUids.length}`, name ); } @@ -134,19 +134,19 @@ export class Room { // remove this client from the room leave() { - const participants = this.#participants.filter( + const participants = this.#participantUids.filter( (p) => p !== this.#client.getUid() ); - this.#roomDataRecord.set(`participants`, participants); + this.#roomDataDsRecord.set(`participants`, participants); } // check if this client is in the room contains(username) { - return this.#participants.includes(username); + return this.#participantUids.includes(username); } getHostName() { - return this.#roomDataRecord.get(`host`); + return this.#roomDataDsRecord.get(`host`); } getMyRecord() { @@ -164,15 +164,15 @@ export class Room { async removeDisconnectedClients() { const online = await this.#client.getAllClients(); - const newParticipants = this.#participants.filter((p) => + const newParticipants = this.#participantUids.filter((p) => online.includes(p) ); - this.#roomDataRecord.set(`participants`, newParticipants); + this.#roomDataDsRecord.set(`participants`, newParticipants); } // async reset() { // for (const entry of this.#recordList.getEntries()) { - // const record = this.#client.getRecord(entry); + // const record = this.#client.getDsRecord(entry); // await record.whenReady(); // record.delete(); // } @@ -195,19 +195,21 @@ export class Room { } async #chooseHost() { - const host = this.#roomDataRecord.get("host"); + const host = this.#roomDataDsRecord.get("host"); const onlineClients = await this.#client.getAllClients(); // if host is onlin and in the room, we don't need a new one - if (onlineClients.includes(host) && this.#participants.includes(host)) + if (onlineClients.includes(host) && this.#participantUids.includes(host)) return; // pick the first participant that is online as the new host - const newHost = this.#participants.find((p) => onlineClients.includes(p)); + const newHost = this.#participantUids.find((p) => + onlineClients.includes(p) + ); // if we didn't find one, return if (!newHost) { - log.debug("Couldn't find a host in participants:", this.#participants); + log.debug("Couldn't find a host in participants:", this.#participantUids); return; } @@ -215,18 +217,18 @@ export class Room { // don't try to set the host at once, causing a conflict if (newHost === this.#client.getUid()) { // todo: can this be setWithAck? - this.#roomDataRecord.set("host", newHost); - await this.#roomDataRecord.whenReady(); + this.#roomDataDsRecord.set("host", newHost); + await this.#roomDataDsRecord.whenReady(); } } async #updateParticpantRecords() { await this.whenReady(); - await this.#roomDataRecord.whenReady(); + await this.#roomDataDsRecord.whenReady(); // collect data const participantRecordIds = Object.keys(this.#participantRecords); - const participantIds = this.#participants; + const participantIds = this.#participantUids; const allIds = [...new Set([...participantRecordIds, ...participantIds])]; //log.debug("participantRecordIds", participantRecordIds); @@ -240,7 +242,7 @@ export class Room { // remove stale participant records if (participantRecordIds.includes(id) && !participantIds.includes(id)) { // only the host should delete the record - if (this.#client.getUid() === this.#roomDataRecord.get("host")) { + if (this.#client.getUid() === this.#roomDataDsRecord.get("host")) { this.#participantRecords[id].delete(); } delete this.#participantRecords[id]; @@ -296,9 +298,9 @@ export class Room { output += `
    ${this.#roomName}
    `; output += `
    Participants
    `; - for (const name of this.#participants) { + for (const name of this.#participantUids) { const shortName = name.substr(-4); - const host = this.#roomDataRecord.get(`host`) === name ? "host" : ""; + const host = this.#roomDataDsRecord.get(`host`) === name ? "host" : ""; const missing = onlineClients.includes(name) ? "" : "missing"; const me = this.#client.getUid() === name ? "me" : ""; @@ -306,7 +308,7 @@ export class Room { } output += `
    Shared Objects
    `; - for (const entry of this.#recordList.getEntries()) { + for (const entry of this.#recordDsList.getEntries()) { output += `
    ${entry.split("/")[1]}
    `; }