Skip to content

Commit

Permalink
do not assign storage immediatelly when module is required. closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jul 17, 2018
1 parent 922f270 commit e7f5f2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.9.10",
"version": "0.9.11",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down
18 changes: 13 additions & 5 deletions src/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const storage: Storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // regular browser environment
/**
* We do not assign 'storage' to window.localStorage immediatelly for React
* Native compatibility. window.localStorage is not present when this module is
* loaded.
*/

function getStorage () {
return (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // regular browser environment
}

export function setItem(key: string, value: string) {
storage.setItem(key, value);
getStorage().setItem(key, value);
}

export function getItem(key: string, callback: Function) {
const value: any = storage.getItem(key);
const value: any = getStorage().getItem(key);

if (
typeof (Promise) === 'undefined' || // old browsers
Expand Down

0 comments on commit e7f5f2d

Please sign in to comment.