-
Notifications
You must be signed in to change notification settings - Fork 61
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
base: master
Are you sure you want to change the base?
Add promises #81
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
) { | ||
|
@@ -572,7 +574,7 @@ | |
if (this.REQUIRE_ENCRYPTION) { | ||
this.storedMgs.push({msg: msg, meta: meta}) | ||
this.sendQueryMsg() | ||
return | ||
return promise | ||
} | ||
if (this.SEND_WHITESPACE_TAG && !this.receivedPlaintext) { | ||
msg += CONST.WHITESPACE_TAG // 16 byte tag | ||
|
@@ -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.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to reject here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
|
@@ -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) { | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 alsonotify
should return an rejected promise. Will update the pr accordingly.