Skip to content

Commit 929d515

Browse files
author
Alec Gibson
committed
Make local presence ID optional
Local presence IDs should be unique. If consumers do not want the responsibility of taking care of this (and don't care about what ID is assigned to them), then we will automatically assign a random ID for them.
1 parent 9e19efb commit 929d515

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ presence.create(presenceId): LocalPresence;
707707

708708
Create an instance of [`LocalPresence`](#class-sharedblocalpresence), which can be used to represent local presence. Many or none such local presences may exist on a `Presence` instance.
709709

710-
* `presenceId` _string_: a unique ID representing the local presence. Remember - depending on use-case - the same client might have multiple presences, so this might not necessarily be a user or client ID.
710+
* `presenceId` _string (optional)_: a unique ID representing the local presence. Remember - depending on use-case - the same client might have multiple presences, so this might not necessarily be a user or client ID. If one is not provided, a random ID will be assigned for you.
711711

712712
#### `destroy`
713713

lib/client/presence/presence.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var LocalPresence = require('./local-presence');
33
var RemotePresence = require('./remote-presence');
44
var util = require('../../util');
55
var async = require('async');
6+
var hat = require('hat');
67

78
module.exports = Presence;
89
function Presence(connection, channel) {
@@ -35,6 +36,7 @@ Presence.prototype.unsubscribe = function(callback) {
3536
};
3637

3738
Presence.prototype.create = function(id) {
39+
id = id || hat();
3840
var localPresence = this._createLocalPresence(id);
3941
this.localPresences[id] = localPresence;
4042
return localPresence;

test/client/presence/presence.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ describe('Presence', function() {
298298
}).to.throw();
299299
});
300300

301-
it('throws an error when trying to create a presence with an empty string ID', function() {
302-
expect(function() {
303-
presence1.create('');
304-
}).to.throw();
301+
it('assigns an ID if one is not provided', function() {
302+
var localPresence = presence1.create();
303+
expect(localPresence.presenceId).to.be.ok;
305304
});
306305

307306
it('returns the error if a local presence cannot be destroyed because of a bad submit', function(done) {

0 commit comments

Comments
 (0)