Skip to content

Commit

Permalink
improve property names on Room
Browse files Browse the repository at this point in the history
  • Loading branch information
jbakse committed Feb 3, 2022
1 parent 31d43f4 commit 32af681
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
5 changes: 1 addition & 4 deletions public/index.html
Expand Up @@ -53,16 +53,13 @@ <h2>Contributed Examples</h2>
<a href="show_example.html?tic_tac_toe">tic_tac_toe</a>
</li>
<li><a href="show_example.html?chat_log">chat_log</a></li>
<li>
<a href="show_example.html?chat_log_no_p5">chat_log_no_p5</a>
</li>

<h2>Tests</h2>
<li><a href="show_example.html?test_identity">test_identity</a></li>
<li><a href="show_example.html?test_types">test_types</a></li>

<h2>Expiremental Examples</h2>
<li><a href="show_example.html?no_p5">no_p5</a></li>

<li><a href="show_example.html?empty_sketch">empty_sketch</a></li>
<li><a href="show_example.html?stickies">stickies</a></li>
<li><a href="show_example.html?ultra_dart_city">ultra_dart_city</a></li>
Expand Down
4 changes: 2 additions & 2 deletions src/Client.js
Expand Up @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/Record.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand All @@ -139,7 +139,7 @@ path: ${path}
newValue: ${JSON.stringify(newValue)}`
);
}
this.#record.set("shared." + path, newValue);
this.#dsRecord.set("shared." + path, newValue);
}

// _onServerChangedData
Expand Down
80 changes: 41 additions & 39 deletions src/Room.js
Expand Up @@ -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
Expand Down Expand Up @@ -48,34 +48,34 @@ 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();
});
};

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
Expand Down Expand Up @@ -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);
}
});

Expand All @@ -124,29 +124,29 @@ 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
);
}
}

// 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() {
Expand All @@ -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();
// }
Expand All @@ -195,38 +195,40 @@ 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;
}

// have only the new host set host so that multiple clients
// 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);
Expand All @@ -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];
Expand Down Expand Up @@ -296,17 +298,17 @@ export class Room {
output += `<div class="room">${this.#roomName}</div>`;
output += `<div class="label">Participants</div>`;

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" : "";

output += `<div class="participant ${host} ${me} ${missing}">${shortName}</div>`;
}

output += `<div class="label">Shared Objects</div>`;
for (const entry of this.#recordList.getEntries()) {
for (const entry of this.#recordDsList.getEntries()) {
output += `<div class="record">${entry.split("/")[1]}</div>`;
}

Expand Down

0 comments on commit 32af681

Please sign in to comment.