forked from peers/peerjs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Realm { | ||
constructor () { | ||
this.clients = new Map(); | ||
this.messageQueue = new Map(); | ||
} | ||
|
||
getClientsIds () { | ||
return [...this.clients.keys()]; | ||
} | ||
|
||
getClientById (clientId) { | ||
return this.clients.get(clientId); | ||
} | ||
|
||
setClient (client, id) { | ||
this.clients.set(id, client); | ||
} | ||
|
||
removeClientById (id) { | ||
const client = this.getClientById(id); | ||
|
||
if (!client) return false; | ||
|
||
this.clients.delete(id); | ||
} | ||
|
||
getMessageQueueById (id) { | ||
return this.messageQueue.get(id); | ||
} | ||
|
||
addMessageToQueue (id, message) { | ||
if (!this.getMessageQueueById(id)) { | ||
this.messageQueue.set(id, []); | ||
} | ||
|
||
this.getMessageQueueById(id).push(message); | ||
} | ||
|
||
clearMessageQueue (id) { | ||
this.messageQueue.delete(id); | ||
} | ||
|
||
generateClientId () { | ||
const randomId = () => (Math.random().toString(36) + '0000000000000000000').substr(2, 16); | ||
|
||
let clientId = randomId(); | ||
|
||
while (this.getClientById(clientId)) { | ||
clientId = randomId(); | ||
} | ||
|
||
return clientId; | ||
} | ||
} | ||
|
||
module.exports = Realm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,3 @@ | ||
class Realm { | ||
constructor () { | ||
this.clients = new Map(); | ||
this.messageQueue = new Map(); | ||
} | ||
|
||
getClientsIds () { | ||
return [...this.clients.keys()]; | ||
} | ||
|
||
getClientById (clientId) { | ||
return this.clients.get(clientId); | ||
} | ||
|
||
setClient (client, id) { | ||
this.clients.set(id, client); | ||
} | ||
|
||
removeClientById (id) { | ||
const client = this.getClientById(id); | ||
|
||
if (!client) return false; | ||
|
||
this.clients.delete(id); | ||
} | ||
|
||
getMessageQueueById (id) { | ||
return this.messageQueue.get(id); | ||
} | ||
|
||
addMessageToQueue (id, message) { | ||
if (!this.getMessageQueueById(id)) { | ||
this.messageQueue.set(id, []); | ||
} | ||
|
||
this.getMessageQueueById(id).push(message); | ||
} | ||
|
||
clearMessageQueue (id) { | ||
this.messageQueue.delete(id); | ||
} | ||
} | ||
const Realm = require('../../models/realm'); | ||
|
||
module.exports = new Realm(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { expect } = require('chai'); | ||
const Realm = require('../../src/models/realm'); | ||
const Client = require('../../src/models/client'); | ||
|
||
describe('Realm', () => { | ||
describe('#generateClientId', () => { | ||
it('should generate a 16-character ID', () => { | ||
const realm = new Realm(); | ||
expect(realm.generateClientId().length).to.eq(16); | ||
}); | ||
}); | ||
|
||
describe('#setClient', () => { | ||
it('should add client to realm', () => { | ||
const realm = new Realm(); | ||
const client = new Client({ id: 'id', token: '' }); | ||
|
||
realm.setClient(client, 'id'); | ||
expect(realm.getClientsIds()).to.deep.eq(['id']); | ||
}); | ||
}); | ||
|
||
describe('#removeClientById', () => { | ||
it('should remove client from realm', () => { | ||
const realm = new Realm(); | ||
const client = new Client({ id: 'id', token: '' }); | ||
|
||
realm.setClient(client, 'id'); | ||
realm.removeClientById('id'); | ||
|
||
expect(realm.getClientById('id')).to.be.undefined; | ||
}); | ||
}); | ||
|
||
describe('#getClientsIds', () => { | ||
it('should reflects on add/remove childs', () => { | ||
const realm = new Realm(); | ||
const client = new Client({ id: 'id', token: '' }); | ||
|
||
realm.setClient(client, 'id'); | ||
expect(realm.getClientsIds()).to.deep.eq(['id']); | ||
|
||
expect(realm.getClientById('id')).to.eq(client); | ||
|
||
realm.removeClientById('id'); | ||
expect(realm.getClientsIds()).to.deep.eq([]); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.