Skip to content

Commit

Permalink
fix: Check worker event data has iterator (#10755)
Browse files Browse the repository at this point in the history
<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on improving the handling of messages received in web
workers by adding a check to ensure the data received is an array before
destructuring it. This change enhances the robustness of the code by
preventing potential errors when processing unexpected message formats.

### Detailed summary
- Added a check to verify if `e?.data` is an array before destructuring
it into `[eId, data]`.
- Maintained the existing logic for resolving or rejecting the promise
based on the `data.success` property.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Sep 30, 2024
1 parent 8a9a54d commit ced738e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 9 additions & 7 deletions apps/gamification/utils/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class WorkerProxy {
this.id = id
const promise = new Promise<T>((resolve, reject) => {
const handler = (e: any) => {
const [eId, data] = e.data
if (id === eId) {
this.worker.removeEventListener('message', handler)
if (data.success === false) {
reject(data.error)
} else {
resolve(data.result)
if (Array.isArray(e?.data)) {
const [eId, data] = e.data
if (id === eId) {
this.worker.removeEventListener('message', handler)
if (data.success === false) {
reject(data.error)
} else {
resolve(data.result)
}
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions apps/web/src/utils/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class WorkerProxy {
this.id = id
const promise = new Promise<T>((resolve, reject) => {
const handler = (e: any) => {
const [eId, data] = e.data
if (id === eId) {
this.worker.removeEventListener('message', handler)
if (data.success === false) {
reject(data.error)
} else {
resolve(data.result)
if (Array.isArray(e?.data)) {
const [eId, data] = e.data
if (id === eId) {
this.worker.removeEventListener('message', handler)
if (data.success === false) {
reject(data.error)
} else {
resolve(data.result)
}
}
}
}
Expand Down

0 comments on commit ced738e

Please sign in to comment.