antfu-collective / vitesse-webext

⚡️ WebExtension Vite Starter Template
MIT License
2.93k stars 225 forks source link

fix: replace watchWithFilter with pausableWatch #183

Closed gxr404 closed 5 months ago

gxr404 commented 5 months ago

Description

Infinite loop unexpectedly fixed by await storageInterface.getItem(key) in the previous PR

This approach is unreasonable. Relevant discussion

A better approach would be to pause the watch when the localstroage listen is triggered

View the commit history, the current code for useWebExtensionStorage is based on reference useStorageAsync

useStorageAsync here also did not use pausableWatch because browser mechanism is different from extension

// Event not triggered
window.addEventListener('storage', e=>{console.log(e)})
localStorage.setItem('test', 111)

// Event triggered
chrome.storage.onChanged.addListener(function() {console.log('trigger')})
chrome.storage.local.set({'test': 123})

Linked Issues

Why does extension need to customize useStorageAsync

Why use pausableWatch to replace watchWithFilter