Skip to content

Commit

Permalink
workaround for 'Access is denied' for localStorage. #139
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Feb 9, 2024
1 parent 9d3f9fb commit 2c4fdb9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.15.17",
"version": "0.15.18",
"description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
"author": "Endel Dreyer",
"license": "MIT",
Expand Down
28 changes: 18 additions & 10 deletions src/Storage.ts
Expand Up @@ -10,18 +10,26 @@ let storage: any;

function getStorage(): Storage {
if (!storage) {
storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: typeof (window) !== "undefined" && window.localStorage //RN does have window object at this point, but localStorage is not defined
? window.localStorage // regular browser environment
: { // mock localStorage for Node.js or RN environment
cache: {},
setItem: function(key, value) { this.cache[key] = value; },
getItem: function(key) { this.cache[key]; },
removeItem: function(key) { delete this.cache[key]; },
};
try {
storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // RN does have window object at this point, but localStorage is not defined

} catch (e) {
// ignore error
}
}

if (!storage) {
// mock localStorage if not available (Node.js or RN environment)
storage = {
cache: {},
setItem: function (key, value) { this.cache[key] = value; },
getItem: function (key) { this.cache[key]; },
removeItem: function (key) { delete this.cache[key]; },
};
}

return storage;
}

Expand Down

0 comments on commit 2c4fdb9

Please sign in to comment.