Commit 936fad75 by Chavee Issariyapat

change toicstore from promise to return

parent b163fa47
...@@ -18,49 +18,49 @@ describe("Test TopicStore", function () { ...@@ -18,49 +18,49 @@ describe("Test TopicStore", function () {
done(); done();
}); });
it("After addTopic(), topicstore should hold that topic", async function () { it("After addTopic(), topicstore should hold that topic", function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']); expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
}); });
it("If add duplicate topics, topicstore.addTopic() should return false", async function () { it("If add duplicate topics, topicstore.addTopic() should return false", function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']); expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
}); });
it("If add more topics, topicstore.addTopic() should hold all", async function () { it("If add more topics, topicstore.addTopic() should hold all", function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/ok','1234.5678')).to.equal(true); expect(topicstore.addTopic('@msg/ok','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/bye','1234.5678')).to.equal(true); expect(topicstore.addTopic('@msg/bye','1234.5678')).to.equal(true);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello','@msg/ok','@msg/bye']); expect(topicstore.getTopics()).to.deep.equal(['@msg/hello','@msg/ok','@msg/bye']);
}); });
it("If add duplicate topics by different block, topicstore.addTopic() should return false", async function () { it("If add duplicate topics by different block, topicstore.addTopic() should return false", function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']); expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
}); });
it("topicstore.delTopic() should return true if one block holds that topic", async function () { it("topicstore.delTopic() should return true if one block holds that topic", function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(true); expect(topicstore.delTopic('@msg/hello','1234.1234')).to.equal(true);
}); });
it("topicstore.delTopic() should return false if two block holds that topic", async function () { it("topicstore.delTopic() should return false if two block holds that topic", function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false); expect(topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
}); });
it("topicstore.delTopic() should return true only if delete by the last block holding that topic", async function () { it("topicstore.delTopic() should return true only if delete by the last block holding that topic", function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true); expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false); expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.addTopic('@msg/hello','1234.9012')).to.equal(false); expect(topicstore.addTopic('@msg/hello','1234.9012')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.9012')).to.equal(false); expect(topicstore.delTopic('@msg/hello','1234.9012')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false); expect(topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.5678')).to.equal(true); expect(topicstore.delTopic('@msg/hello','1234.5678')).to.equal(true);
}); });
......
...@@ -23,41 +23,38 @@ TopicStore.prototype.getRawData = function() { ...@@ -23,41 +23,38 @@ TopicStore.prototype.getRawData = function() {
} }
TopicStore.prototype.addTopic = function(topic, blockid) { TopicStore.prototype.addTopic = function(topic, blockid) {
return new Promise((resolve, reject) => { if (this.data[topic] == undefined || this.data[topic].length==0) {
if (this.data[topic] == undefined || this.data[topic].length==0) { this.data[topic] = [blockid];
this.data[topic] = [blockid]; return(true)
resolve(true) }
else {
let p = this.data[topic].indexOf(blockid);
if (p>=0) {
return(false);
} }
else { else {
let p = this.data[topic].indexOf(blockid); this.data[topic].push(blockid);
if (p>=0) { return(false)
resolve(false);
}
else {
this.data[topic].push(blockid);
resolve(false)
}
} }
}); }
} }
TopicStore.prototype.delTopic = function(topic, blockid) { TopicStore.prototype.delTopic = function(topic, blockid) {
return new Promise((resolve, reject) => { if (this.data[topic] == undefined || this.data[topic].length==0) {
if (this.data[topic] == undefined || this.data[topic].length==0) { return(true); // still force unsub
resolve(true); // still force unsub }
else {
let p = this.data[topic].indexOf(blockid);
if (p>=0) {
this.data[topic].splice(p,1);
if (this.data[topic].length == 0) return(true);
else return(false);
} }
else { else {
let p = this.data[topic].indexOf(blockid); return(false);
if (p>=0) {
this.data[topic].splice(p,1);
if (this.data[topic].length == 0) resolve(true);
else resolve(false);
}
else {
resolve(false);
}
} }
}); }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment