Skip to content

Commit

Permalink
add clientID as a property
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 31, 2020
1 parent 679b8c1 commit 59234fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const awareness = new awarenessProtocol.Awareness()
```

<dl>
<b><code>clientID:number</code></b>
<dd>A unique identifier that identifies this client.</dd>
<b><code>getLocalState():Object&lt;string,any&gt;|null</code></b>
<dd>Get the local awareness state.</dd>
<b><code>setLocalState(Object&lt;string,any&gt;|null)</code></b>
Expand Down
23 changes: 17 additions & 6 deletions awareness.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class Awareness extends Observable {
constructor (doc) {
super()
this.doc = doc
/**
* @type {number}
*/
this.clientID = doc.clientID
/**
* Maps from client id to client state
* @type {Map<number, Object<string, any>>}
Expand All @@ -54,7 +58,7 @@ export class Awareness extends Observable {
this.meta = new Map()
this._checkInterval = setInterval(() => {
const now = time.getUnixTime()
if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(doc.clientID)).lastUpdated)) {
if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(this.clientID)).lastUpdated)) {
// renew local clock
this.setLocalState(this.getLocalState())
}
Expand All @@ -63,7 +67,7 @@ export class Awareness extends Observable {
*/
const remove = []
this.meta.forEach((meta, clientid) => {
if (clientid !== doc.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {
if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {
remove.push(clientid)
}
})
Expand All @@ -76,21 +80,26 @@ export class Awareness extends Observable {
})
this.setLocalState({})
}

destroy () {
this.emit('destroy', [this])
this.setLocalState(null)
super.destroy()
clearInterval(this._checkInterval)
}

/**
* @return {Object<string,any>|null}
*/
getLocalState () {
return this.states.get(this.doc.clientID) || null
return this.states.get(this.clientID) || null
}

/**
* @param {Object<string,any>|null} state
*/
setLocalState (state) {
const clientID = this.doc.clientID
const clientID = this.clientID
const currLocalMeta = this.meta.get(clientID)
const clock = currLocalMeta === undefined ? 0 : currLocalMeta.clock + 1
const prevState = this.states.get(clientID)
Expand Down Expand Up @@ -124,6 +133,7 @@ export class Awareness extends Observable {
}
this.emit('update', [{ added, updated, removed }, 'local'])
}

/**
* @param {string} field
* @param {any} value
Expand All @@ -135,6 +145,7 @@ export class Awareness extends Observable {
this.setLocalState(state)
}
}

/**
* @return {Map<number,Object<string,any>>}
*/
Expand All @@ -157,7 +168,7 @@ export const removeAwarenessStates = (awareness, clients, origin) => {
const clientID = clients[i]
if (awareness.states.has(clientID)) {
awareness.states.delete(clientID)
if (clientID === awareness.doc.clientID) {
if (clientID === awareness.clientID) {
const curMeta = /** @type {MetaClientState} */ (awareness.meta.get(clientID))
awareness.meta.set(clientID, {
clock: curMeta.clock + 1,
Expand Down Expand Up @@ -243,7 +254,7 @@ export const applyAwarenessUpdate = (awareness, update, origin) => {
if (currClock < clock || (currClock === clock && state === null && awareness.states.has(clientID))) {
if (state === null) {
// never let a remote client remove this local state
if (clientID === awareness.doc.clientID && awareness.getLocalState() != null) {
if (clientID === awareness.clientID && awareness.getLocalState() != null) {
// remote client removed the local state. Do not remote state. Broadcast a message indicating
// that this client still exists by increasing the clock
clock++
Expand Down

0 comments on commit 59234fb

Please sign in to comment.