Skip to content

Commit

Permalink
(fix): resolve AsyncLocalStorage Illegal Invocation errors
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 committed Nov 19, 2019
1 parent 10d4cd7 commit 81b0927
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/asyncLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ interface IAsyncLocalStorage {
}

export const AsyncLocalStorage: IAsyncLocalStorage = {
// must use wrapper functions when passing localStorage functions (https://github.com/agilgur5/mst-persist/issues/18)
clear () {
return callWithPromise(window.localStorage.clear)
return callWithPromise(() => window.localStorage.clear())
},
getItem (key) {
return callWithPromise(window.localStorage.getItem, key)
return callWithPromise(() => window.localStorage.getItem(key))
},
removeItem (key) {
return callWithPromise(window.localStorage.removeItem, key)
return callWithPromise(() => window.localStorage.removeItem(key))
},
setItem (key, value) {
return callWithPromise(window.localStorage.setItem, key, value)
return callWithPromise(() => window.localStorage.setItem(key, value))
}
}

Expand Down

0 comments on commit 81b0927

Please sign in to comment.