team113 / messenger

Open-source front-end part of messenger by team113
GNU Affero General Public License v3.0
14 stars 13 forks source link

Replace `localStorage` usage with `drift` subscriptions #1045

Open SleepySquash opened 1 week ago

SleepySquash commented 1 week ago

Background

Currently WebUtils use a lot of localStorage communication for PopupCalls and AuthService.

Problem to solve

Should replace localStorage with drift, as it allows to synchronize data properly between tabs.

SleepySquash commented 4 days ago

localStorage is used for: 1) call_* is used to monitor calls being in popup windows opened and closed. For example, the main application creates a new popup call and it shouldn't create any more new. This is where call_* keys come in handy. This usage may be replaced with a separate table in drift. 2) prefs_call_* is used to position calls in popup windows. This also may be replaced with drift, as it seems. 3) credentials_* may already be removed, as this is already synchronized by drift.

call_*

Local storage is handy to use there, as we can keep track of opened popups this way. We need to track popups being opened and closed, as we don't know whether there's any opened at all, web's API won't allow window to query this kind of thing. To track popup being opened, we simply add a new entry to the local storage. To track popup being closed, we can use the web's event handlers to detect this and remove the call from local storage.

In order to implement the same behaviour using drift, we must have an open database connection to remove the popup window entry. We may use Dart to bind to JavaScript event handlers, however Dart will be available only when the Dart engine is initialized, and user may close the window before that happens. Using JavaScript directly here is a much more strict and bug-free approach.