diff --git a/src/components/poll.ts b/src/components/poll.ts index 2d150726..2ddeb726 100644 --- a/src/components/poll.ts +++ b/src/components/poll.ts @@ -1,13 +1,13 @@ -import appPollsManager, { PollResults, Poll } from "../lib/appManagers/appPollsManager"; -import { RichTextProcessor } from "../lib/richtextprocessor"; -import { findUpClassName, cancelEvent } from "../lib/utils"; -import appSidebarRight from "./sidebarRight"; +import mediaSizes from "../helpers/mediaSizes"; +import { isTouchSupported } from "../helpers/touchSupport"; import appImManager from "../lib/appManagers/appImManager"; +import appPollsManager, { Poll, PollResults } from "../lib/appManagers/appPollsManager"; import serverTimeManager from "../lib/mtproto/serverTimeManager"; -import { ripple } from "./ripple"; -import mediaSizes from "../helpers/mediaSizes"; +import { RichTextProcessor } from "../lib/richtextprocessor"; import $rootScope from "../lib/rootScope"; -import { isTouchSupported } from "../helpers/touchSupport"; +import { cancelEvent, findUpClassName } from "../lib/utils"; +import { ripple } from "./ripple"; +import appSidebarRight from "./sidebarRight"; let lineTotalLength = 0; const tailLength = 9; @@ -380,11 +380,17 @@ export default class PollElement extends HTMLElement { attributeChangedCallback(name: string, oldValue: string, newValue: string) { // вызывается при изменении одного из перечисленных выше атрибутов + console.log('Poll: attributeChangedCallback', name, oldValue, newValue, this.isConnected); if(name == 'poll-id') { this.pollID = newValue; } else if(name == 'message-id') { this.mid = +newValue; } + + if(this.mid > 0 && oldValue !== undefined && +oldValue < 0) { + this.disconnectedCallback(); + connectedPolls.push({id: this.pollID, element: this}); + } } adoptedCallback() { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 7ca5a58e..fbf22115 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -464,7 +464,14 @@ export class AppMessagesManager { public pendingTopMsgs: {[peerID: string]: number} = {}; public sendFilePromise: CancellablePromise = Promise.resolve(); public tempID = -1; - public tempFinalizeCallbacks: any = {}; + public tempFinalizeCallbacks: { + [mid: string]: { + [callbackName: string]: Partial<{ + deferred: CancellablePromise, + callback: (mid: number) => Promise + }> + } + } = {}; public lastSearchFilter: any = {}; public lastSearchResults: any = []; @@ -663,27 +670,28 @@ export class AppMessagesManager { return sendEntites; } + public invokeAfterMessageIsSent(messageID: number, callbackName: string, callback: (mid: number) => Promise) { + const finalize = this.tempFinalizeCallbacks[messageID] ?? (this.tempFinalizeCallbacks[messageID] = {}); + const obj = finalize[callbackName] ?? (finalize[callbackName] = {deferred: deferredPromise()}); + + obj.callback = callback; + + return obj.deferred; + } + public editMessage(messageID: number, text: string, options: Partial<{ noWebPage: true, newMedia: any - }> = {}) { + }> = {}): Promise { /* if(!this.canEditMessage(messageID)) { return Promise.reject({type: 'MESSAGE_EDIT_FORBIDDEN'}); } */ if(messageID < 0) { - if(this.tempFinalizeCallbacks[messageID] === undefined) { - this.tempFinalizeCallbacks[messageID] = {} - } - - const promise = new Promise((resolve, reject) => { - this.tempFinalizeCallbacks[messageID].edit = (mid: number) => { - this.log('invoke callback', mid) - this.editMessage(mid, text).then(resolve, reject); - } + return this.invokeAfterMessageIsSent(messageID, 'edit', (mid) => { + this.log('invoke editMessage callback', mid); + return this.editMessage(mid, text, options); }); - - return promise; } let entities: any[]; @@ -4010,12 +4018,15 @@ export class AppMessagesManager { } public finalizePendingMessageCallbacks(tempID: number, mid: number) { - var callbacks = this.tempFinalizeCallbacks[tempID]; + const callbacks = this.tempFinalizeCallbacks[tempID]; this.log.warn(callbacks, tempID); if(callbacks !== undefined) { - callbacks.forEach((callback: any) => { - callback(mid); - }); + for(const name in callbacks) { + const {deferred, callback} = callbacks[name]; + this.log(`finalizePendingMessageCallbacks: will invoke ${name} callback`); + callback(mid).then(deferred.resolve, deferred.reject); + } + delete this.tempFinalizeCallbacks[tempID]; } diff --git a/src/lib/appManagers/appPollsManager.ts b/src/lib/appManagers/appPollsManager.ts index 1e6eca97..c484fb7c 100644 --- a/src/lib/appManagers/appPollsManager.ts +++ b/src/lib/appManagers/appPollsManager.ts @@ -159,7 +159,7 @@ class AppPollsManager { }; } - public sendVote(mid: number, optionIDs: number[]) { + public sendVote(mid: number, optionIDs: number[]): Promise { const message = appMessagesManager.getMessage(mid); const poll: Poll = message.media.poll; @@ -170,6 +170,13 @@ class AppPollsManager { const inputPeer = appPeersManager.getInputPeerByID(message.peerID); const messageID = message.id; + if(mid < 0) { + return appMessagesManager.invokeAfterMessageIsSent(mid, 'sendVote', (mid) => { + this.log('invoke sendVote callback'); + return this.sendVote(mid, optionIDs); + }); + } + return apiManager.invokeApi('messages.sendVote', { peer: inputPeer, msg_id: messageID,