Commit 936fad75 by Chavee Issariyapat

change toicstore from promise to return

parent b163fa47
......@@ -18,49 +18,49 @@ describe("Test TopicStore", function () {
done();
});
it("After addTopic(), topicstore should hold that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
it("After addTopic(), topicstore should hold that topic", function () {
expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("If add duplicate topics, topicstore.addTopic() should return false", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
it("If add duplicate topics, topicstore.addTopic() should return false", function () {
expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("If add more topics, topicstore.addTopic() should hold all", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/ok','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/bye','1234.5678')).to.equal(true);
it("If add more topics, topicstore.addTopic() should hold all", function () {
expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(topicstore.addTopic('@msg/ok','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']);
});
it("If add duplicate topics by different block, topicstore.addTopic() should return false", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
it("If add duplicate topics by different block, topicstore.addTopic() should return false", function () {
expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("topicstore.delTopic() should return true if one block holds that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(true);
it("topicstore.delTopic() should return true if one block holds that topic", function () {
expect(topicstore.addTopic('@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 () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
it("topicstore.delTopic() should return false if two block holds that topic", function () {
expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(topicstore.addTopic('@msg/hello','1234.5678')).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 () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.addTopic('@msg/hello','1234.9012')).to.equal(false);
it("topicstore.delTopic() should return true only if delete by the last block holding that topic", function () {
expect(topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(topicstore.addTopic('@msg/hello','1234.5678')).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(await 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.9012')).to.equal(false);
expect(topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
expect(topicstore.delTopic('@msg/hello','1234.5678')).to.equal(true);
});
......
......@@ -23,41 +23,38 @@ TopicStore.prototype.getRawData = function() {
}
TopicStore.prototype.addTopic = function(topic, blockid) {
return new Promise((resolve, reject) => {
if (this.data[topic] == undefined || this.data[topic].length==0) {
this.data[topic] = [blockid];
resolve(true)
return(true)
}
else {
let p = this.data[topic].indexOf(blockid);
if (p>=0) {
resolve(false);
return(false);
}
else {
this.data[topic].push(blockid);
resolve(false)
return(false)
}
}
});
}
TopicStore.prototype.delTopic = function(topic, blockid) {
return new Promise((resolve, reject) => {
if (this.data[topic] == undefined || this.data[topic].length==0) {
resolve(true); // still force unsub
return(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) resolve(true);
else resolve(false);
if (this.data[topic].length == 0) return(true);
else return(false);
}
else {
resolve(false);
return(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