Skip to content

Commit efb1cf6

Browse files
committed
(fix): resolve AsyncLocalStorage Illegal Invocation errors
- I (and apparently most mst-persist users) don't use localStorage, so this went undetected for a while until someone reported a bug - and I finally hit upon it myself when adding tests shortly after, which added a solid reproduction as well as a blocker on it - it made localStorage unusable as all of AsyncLocalStorage's calls would give Illegal Invocation errors - see https://stackoverflow.com/q/41126149/3431180 for more details on this error - using .call did not work, not on callWithPromise.call(window, ...) nor func.call(window, ...args) - similarly window.localStorage.func.bind(window) did not work - so instead used wrapper functions as an alternative that didn't feel too convoluted - using window.localStorage[funcKey] in callWithPromise did not quite feel right, wrapper functions felt better
1 parent 6b1dd3b commit efb1cf6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/asyncLocalStorage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ interface IAsyncLocalStorage {
66
}
77

88
export const AsyncLocalStorage: IAsyncLocalStorage = {
9+
// must use wrapper functions when passing localStorage functions (https://github.com/agilgur5/mst-persist/issues/18)
910
clear () {
10-
return callWithPromise(window.localStorage.clear)
11+
return callWithPromise(() => window.localStorage.clear())
1112
},
1213
getItem (key) {
13-
return callWithPromise(window.localStorage.getItem, key)
14+
return callWithPromise(() => window.localStorage.getItem(key))
1415
},
1516
removeItem (key) {
16-
return callWithPromise(window.localStorage.removeItem, key)
17+
return callWithPromise(() => window.localStorage.removeItem(key))
1718
},
1819
setItem (key, value) {
19-
return callWithPromise(window.localStorage.setItem, key, value)
20+
return callWithPromise(() => window.localStorage.setItem(key, value))
2021
}
2122
}
2223

0 commit comments

Comments
 (0)