Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use Map, not Record, for listing all roles #512

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,20 @@ export class MapeoProject extends TypedEmitter {
throw new Error('Cannot leave a project as a blocked device')
}

const knownDevices = Object.keys(await this.#roles.getAll())
const projectCreatorDeviceId = await this.#coreOwnership.getOwner(
this.#projectId
)
const allRoles = await this.#roles.getAll()

// 1.2 Check that we are not the only device in the project
if (knownDevices.length === 1) {
if (allRoles.size <= 1) {
throw new Error('Cannot leave a project as the only device')
}

// 1.3 Check if there are other known devices that are either the project creator or a coordinator
const projectCreatorDeviceId = await this.#coreOwnership.getOwner(
this.#projectId
)
let otherCreatorOrCoordinatorExists = false

for (const deviceId of knownDevices) {
for (const deviceId of allRoles.keys()) {
// Skip self (see 1.1 and 1.2 for relevant checks)
if (deviceId === this.#deviceId) continue

Expand Down
2 changes: 1 addition & 1 deletion src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class MemberApi extends TypedEmitter {
])

return Promise.all(
Object.entries(allRoles).map(async ([deviceId, role]) => {
[...allRoles.entries()].map(async ([deviceId, role]) => {
/** @type {MemberInfo} */
const memberInfo = { deviceId, role }

Expand Down
18 changes: 7 additions & 11 deletions src/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ export class Roles {
* returned. The project creator will have the creator role unless a
* different one has been assigned.
*
* @returns {Promise<Record<string, Role>>} Map of deviceId to Role
* @returns {Promise<Map<string, Role>>} Map of deviceId to Role
*/
async getAll() {
const roles = await this.#dataType.getMany()
/** @type {Record<string, Role>} */
const result = {}
/** @type {Map<string, Role>} */
const result = new Map()
/** @type {undefined | string} */
let projectCreatorDeviceId
try {
Expand All @@ -294,7 +294,7 @@ export class Roles {
)
// Default to creator role, but can be overwritten if a different role is
// set below
result[projectCreatorDeviceId] = CREATOR_ROLE
result.set(projectCreatorDeviceId, CREATOR_ROLE)
} catch (e) {
// Not found, we don't know who the project creator is so we can't include
// them in the returned map
Expand All @@ -310,16 +310,12 @@ export class Roles {
continue
}
const deviceId = role.docId
result[deviceId] = ROLES[role.roleId]
result.set(deviceId, ROLES[role.roleId])
}
const includesSelf = Boolean(result[this.#ownDeviceId])
const includesSelf = result.has(this.#ownDeviceId)
if (!includesSelf) {
const isProjectCreator = this.#ownDeviceId === projectCreatorDeviceId
if (isProjectCreator) {
result[this.#ownDeviceId] = CREATOR_ROLE
} else {
result[this.#ownDeviceId] = NO_ROLE
}
result.set(this.#ownDeviceId, isProjectCreator ? CREATOR_ROLE : NO_ROLE)
}
return result
}
Expand Down
Loading