Add Hogfather.Create() factory.

This commit is contained in:
Ian Gulliver
2015-12-25 14:23:04 -08:00
parent 30a3e89bfc
commit 7be8b4e9b6
2 changed files with 57 additions and 13 deletions

View File

@@ -20,23 +20,42 @@
/**
* @constructor
* @param {Cosmopolite} cosmo
* @param {string} name
* @param {string} prefix
*/
var Hogfather = function(cosmo, name) {
var Hogfather = function(cosmo, prefix) {
this.cosmo_ = cosmo;
this.name_ = name;
this.prefix_ = prefix;
this.cosmo_.getProfile().then(this.onProfile_.bind(this));
console.log(this.loggingPrefix_(), 'create');
};
/**
* @param {string} profile_id
* @param {Cosmopolite} cosmo
* @return {Promise}
*/
Hogfather.prototype.onProfile_ = function(profile_id) {
this.prefix_ = '/hogfather/' + profile_id + '/' + this.name_ + '/';
this.cosmo_.subscribe(this.prefix_ + 'control');
console.log(this.prefix_);
Hogfather.Create = function(cosmo) {
return new Promise(function(resolve, reject) {
var prefix;
cosmo.getProfile().then(function(profile_id) {
prefix = '/hogfather/' + profile_id + '/' + cosmo.uuid() + '/';
var subject = {
name: prefix + 'control',
readable_only_by: 'me',
writeable_only_by: 'me',
};
var msg = {
owners: [profile_id],
writers: [profile_id],
readers: [profile_id],
};
return cosmo.sendMessage(subject, msg);
}).then(function(msg) {
resolve(new Hogfather(cosmo, prefix));
}).catch(function(err) {
reject(err);
});
});
};
@@ -44,3 +63,12 @@ Hogfather.prototype.onProfile_ = function(profile_id) {
*/
Hogfather.prototype.shutdown = function() {
};
/**
* @private
* @return {string}
*/
Hogfather.prototype.loggingPrefix_ = function() {
return 'hogfather (' + this.prefix_ + '):';
};

View File

@@ -947,7 +947,7 @@ QUnit.asyncTest('sendMessage admin ACL', function(assert) {
QUnit.module('Hogfather');
QUnit.asyncTest('Construct/shutdown', function(assert) {
QUnit.test('Construct/shutdown', function(assert) {
assert.expect(4);
var cosmo = new Cosmopolite(null, randstring());
@@ -956,7 +956,24 @@ QUnit.asyncTest('Construct/shutdown', function(assert) {
var hogfather = new Hogfather(cosmo, randstring());
assert.ok(true, 'new Hogfather()) succeeds');
window.setTimeout(function() {
hogfather.shutdown();
assert.ok(true, 'Hogfather.shutdown() succeeds');
cosmo.shutdown();
assert.ok(true, 'Cosmopolite.shutdown() succeeds');
});
QUnit.asyncTest('Create', function(assert) {
assert.expect(5);
var cosmo = new Cosmopolite(null, randstring());
assert.ok(true, 'new Cosmopolite() succeeds');
Hogfather.Create(cosmo).then(function(hogfather) {
assert.ok(true, 'Hogfather.Create() succeeds');
assert.ok(hogfather, 'Hogfather.Create() returns something');
hogfather.shutdown();
assert.ok(true, 'Hogfather.shutdown() succeeds');
@@ -964,6 +981,5 @@ QUnit.asyncTest('Construct/shutdown', function(assert) {
assert.ok(true, 'Cosmopolite.shutdown() succeeds');
QUnit.start();
}, 10 * 1000);
});
});