Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add promises #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions lib/otr.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@
}

OTR.prototype.sendMsg = function (msg, meta) {
var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined;

if ( this.REQUIRE_ENCRYPTION ||
this.msgstate !== CONST.MSGSTATE_PLAINTEXT
) {
Expand All @@ -572,7 +574,7 @@
if (this.REQUIRE_ENCRYPTION) {
this.storedMgs.push({msg: msg, meta: meta})
this.sendQueryMsg()
return
return promise
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This promise is never rejected or fulfilled, similarly all the cases below where the functions return early.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was by purpose, to resolve a Promise only if there were some data to send. But yes in this case sendQueryMsg should return a promise and also notify should return an rejected promise. Will update the pr accordingly.

}
if (this.SEND_WHITESPACE_TAG && !this.receivedPlaintext) {
msg += CONST.WHITESPACE_TAG // 16 byte tag
Expand All @@ -583,45 +585,53 @@
case CONST.MSGSTATE_FINISHED:
this.storedMgs.push({msg: msg, meta: meta})
this.notify('Message cannot be sent at this time.', 'warn')
return
return promise
case CONST.MSGSTATE_ENCRYPTED:
msg = this.prepareMsg(msg)
break
default:
throw new Error('Unknown message state.')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to reject here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an exception is the same as rejecting.

}

if (msg) this.io(msg, meta)
if (msg) {
this.io(msg, meta)

if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function')
return Promise.resolve(msg)
}

return promise
}

OTR.prototype.receiveMsg = function (msg, meta) {
var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined;

// parse type
msg = Parse.parseMsg(this, msg)

if (!msg) return
if (!msg) return promise

switch (msg.cls) {
case 'error':
this.notify(msg.msg)
return
return promise
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return promise.reject(msg.msg), no?

case 'ake':
if ( msg.version === CONST.OTR_VERSION_3 &&
this.checkInstanceTags(msg.instance_tags)
) {
this.notify(
'Received a message intended for a different session.', 'warn')
return // ignore
return promise // ignore
}
this.ake.handleAKE(msg)
return
return promise
case 'data':
if ( msg.version === CONST.OTR_VERSION_3 &&
this.checkInstanceTags(msg.instance_tags)
) {
this.notify(
'Received a message intended for a different session.', 'warn')
return // ignore
return promise // ignore
}
msg.msg = this.handleDataMsg(msg)
msg.encrypted = true
Expand All @@ -645,7 +655,14 @@
this.doAKE(msg)
}

if (msg.msg) this.trigger('ui', [msg.msg, !!msg.encrypted, meta])
if (msg.msg) {
this.trigger('ui', [msg.msg, !!msg.encrypted, meta])

if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function')
return Promise.resolve([msg.msg, !!msg.encrypted])
}

return promise;
}

OTR.prototype.checkInstanceTags = function (it) {
Expand Down Expand Up @@ -718,6 +735,20 @@
}

OTR.prototype.endOtr = function (cb) {
var promise

if (typeof Promise === 'function') {
promise = new Promise(function(resolve) {
var original_cb = cb

cb = function() {
if (typeof original_cb === 'function')
original_cb()
resolve()
}
})
}

if (this.msgstate === CONST.MSGSTATE_ENCRYPTED) {
if (typeof cb === 'function')
cb = new OTRCB(cb)
Expand All @@ -732,6 +763,8 @@
this.msgstate = CONST.MSGSTATE_PLAINTEXT
this.receivedPlaintext = false
this.trigger('status', [CONST.STATUS_END_OTR])

if (promise) return promise
}

// attach methods
Expand Down