-
Notifications
You must be signed in to change notification settings - Fork 13.3k
fix: Intermittent error "Cannot read properties of undefined" when editing messages #36745
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
04f5029
fix
tiagoevanp 6a51923
Create shy-dolphins-share.md
tiagoevanp 8cc861a
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/…
tiagoevanp b71a3ca
mutex
tiagoevanp eb6f76f
Merge branch 'fix/update-message-weird-error' of github.com:RocketCha…
tiagoevanp d5edc86
typecheck
tiagoevanp a11c67a
fix
tiagoevanp c5bec0f
Revert promise chain bailout
tassoevan 436dcaa
tests
tiagoevanp 256e5f7
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/…
tiagoevanp 1c3c88a
test updates
tiagoevanp aeca3df
fix unecessary new fragment function
tiagoevanp a001bae
update test
tiagoevanp c72cf5f
refinement refactor
tiagoevanp 44b69f3
test adjustments
tiagoevanp fc81cbb
fix promise not fullfiled by conditional state in e2e test
tiagoevanp 17eb9cb
Merge branch 'develop' into fix/update-message-weird-error
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@rocket.chat/meteor": patch | ||
| --- | ||
|
|
||
| Fixes intermittent error "Cannot read properties of undefined" when editing messages |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import type { ChatAPI } from '../../../../client/lib/chats/ChatAPI'; | ||
| import { clearHighlightMessage } from '../../../../client/views/room/MessageList/providers/messageHighlightSubscription'; | ||
|
|
||
| export class CurrentEditingMessage { | ||
| private lock = false; | ||
|
|
||
| private mid?: string; | ||
|
|
||
| private queue: { | ||
| resolve: (release: () => void) => void; | ||
| }[] = []; | ||
|
|
||
| private chat: ChatAPI; | ||
|
|
||
| constructor(chat: ChatAPI) { | ||
| this.chat = chat; | ||
| } | ||
|
|
||
| public reset = async () => { | ||
| return this.runExclusive(async () => { | ||
| if (!this.chat.composer || !this.mid) { | ||
| return false; | ||
| } | ||
|
|
||
| const message = await this.chat.data.findMessageByID(this.mid); | ||
|
|
||
| if (this.chat.composer.text !== message?.msg) { | ||
| this.chat.composer.setText(message?.msg ?? ''); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }); | ||
| }; | ||
|
|
||
| public stop = async () => { | ||
| await this.runExclusive(async () => { | ||
| if (!this.chat.composer || !this.mid) { | ||
| return; | ||
| } | ||
|
|
||
| const message = await this.chat.data.findMessageByID(this.mid); | ||
| const draft = this.chat.composer.text; | ||
|
|
||
| if (draft === message?.msg) { | ||
| await this.chat.data.discardDraft(this.mid); | ||
| } else { | ||
| await this.chat.data.saveDraft(this.mid, (await this.chat.data.getDraft(this.mid)) || draft); | ||
| } | ||
|
|
||
| this.chat.composer.setEditingMode(false); | ||
| this.mid = undefined; | ||
| clearHighlightMessage(); | ||
| }); | ||
| }; | ||
|
|
||
| public cancel = async () => { | ||
| await this.runExclusive(async () => { | ||
| if (!this.mid) { | ||
| return; | ||
| } | ||
|
|
||
| await this.chat.data.discardDraft(this.mid); | ||
| this.chat.composer?.setText((await this.chat.data.getDraft(undefined)) ?? ''); | ||
| }); | ||
| }; | ||
|
|
||
| private acquire = async () => { | ||
| return new Promise<() => void>((resolve) => { | ||
| this.queue.push({ resolve }); | ||
| this.dispatch(); | ||
| }); | ||
| }; | ||
|
|
||
| private dispatch() { | ||
| if (this.lock) { | ||
| return; | ||
| } | ||
|
|
||
| const next = this.queue.shift(); | ||
|
|
||
| if (!next) { | ||
| return; | ||
| } | ||
|
|
||
| this.lock = true; | ||
| next.resolve(this.buildRelease()); | ||
| } | ||
|
|
||
| private buildRelease = () => { | ||
| return async () => { | ||
| this.lock = false; | ||
| this.dispatch(); | ||
| }; | ||
| }; | ||
|
|
||
| public getMID() { | ||
| return this.mid; | ||
| } | ||
|
|
||
| public setMID(mid: string) { | ||
| this.mid = mid; | ||
| } | ||
|
|
||
| private async runExclusive<T>(callback: () => Promise<T>) { | ||
| const release = await this.acquire(); | ||
|
|
||
| try { | ||
| return await callback(); | ||
| } finally { | ||
| release(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.