Browse Source

Optimistic message editing

Closes #1586
master
Igor Zhukov 7 years ago
parent
commit
55c661a2c7
  1. 34
      app/js/messages_manager.js

34
app/js/messages_manager.js

@ -21,6 +21,7 @@ angular.module('myApp.services')
var pendingTopMsgs = {} var pendingTopMsgs = {}
var sendFilePromise = $q.when() var sendFilePromise = $q.when()
var tempID = -1 var tempID = -1
var tempFinalizeCallbacks = {}
var dialogsIndex = SearchIndexManager.createIndex() var dialogsIndex = SearchIndexManager.createIndex()
var cachedResults = {query: false} var cachedResults = {query: false}
@ -994,8 +995,7 @@ angular.module('myApp.services')
} }
function canEditMessage(messageID) { function canEditMessage(messageID) {
if (messageID <= 0 || if (!messagesStorage[messageID]) {
!messagesStorage[messageID]) {
return false return false
} }
var message = messagesStorage[messageID] var message = messagesStorage[messageID]
@ -2311,12 +2311,25 @@ angular.module('myApp.services')
delete messagesForHistory[tempID] delete messagesForHistory[tempID]
delete messagesStorage[tempID] delete messagesStorage[tempID]
finalizePendingMessageCallbacks(tempID, finalMessage.mid)
return message return message
} }
return false return false
} }
function finalizePendingMessageCallbacks(tempID, mid) {
var callbacks = tempFinalizeCallbacks[tempID]
console.warn(dT(), callbacks, tempID)
if (callbacks !== undefined) {
angular.forEach(callbacks, function (callback) {
callback(mid)
})
delete tempFinalizeCallbacks[tempID]
}
}
function getInputEntities(entities) { function getInputEntities(entities) {
var sendEntites = angular.copy(entities) var sendEntites = angular.copy(entities)
angular.forEach(sendEntites, function (entity) { angular.forEach(sendEntites, function (entity) {
@ -2333,6 +2346,21 @@ angular.module('myApp.services')
!canEditMessage(messageID)) { !canEditMessage(messageID)) {
return $q.reject() return $q.reject()
} }
if (messageID < 0) {
if (tempFinalizeCallbacks[messageID] === undefined) {
tempFinalizeCallbacks[messageID] = {}
}
var deferred = $q.defer()
tempFinalizeCallbacks[messageID].edit = function (mid) {
console.log('invoke callback', mid)
editMessage(mid, text).then(function (result) {
deferred.resolve(result)
}, function (error) {
deferred.reject(error)
})
}
return deferred.promise
}
var entities = [] var entities = []
text = RichTextProcessor.parseMarkdown(text, entities) text = RichTextProcessor.parseMarkdown(text, entities)
@ -3126,6 +3154,8 @@ angular.module('myApp.services')
msgs[tempID] = true msgs[tempID] = true
$rootScope.$broadcast('history_delete', {peerID: peerID, msgs: msgs}) $rootScope.$broadcast('history_delete', {peerID: peerID, msgs: msgs})
finalizePendingMessageCallbacks(tempID, finalMessage.mid)
} else { } else {
pendingByMessageID[mid] = randomID pendingByMessageID[mid] = randomID
} }

Loading…
Cancel
Save