[w3c/ServiceWorker] Service Worker local variables disappear for no reason (Issue #1656)

I have a strange problem. I pass a value from the client to the service worker as a postmessage, and the service worker records the value. But when the service worker gets this value internally, it is empty. I can't reproduce this situation locally, but my log records this situation. What could be the reason for this, or any ideas to help me troubleshoot this problem?

The pseudo-code
```js
// client
navigator.serviceWorker.controller.postMessage({ type: 'SET_AUTH_KEY', data });
```

```js
// Service Worker
class SWEvent {
    eventData
    constructor() {
        this.eventData = {}
        self.addEventListener('message', (event) => {
            if (event.data.type === 'SET_AUTH_KEY') {
                this.eventData = event.data.data
                swLogMessage({ level: 'info', desc: 'successfully set auth_key to sw' }, clientId);
            }
        });
    }
    getEventData() {
        return  this.eventData
    }
}
const sWEvent= new SWEvent()

// Execute after a period of time
setTimeout(() => {
    const data = sWEvent.getEventData()
    if (!data) {
        swLogMessage(
            { level: 'warn', desc: 'url not auth_key' },
            clientId
        );
    }
}, 5000);
```


![image](https://user-images.githubusercontent.com/15622519/183829673-84622861-9682-4273-9078-8f7b8ec430fd.png)

It can be seen from the log time that the value has been set to sw normally, but cannot be obtained

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1656
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/ServiceWorker/issues/1656@github.com>

Received on Wednesday, 10 August 2022 06:25:50 UTC