Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With indexeddb, retrieving data from a node after a browser refresh returns undefined #42

Open
dfreire opened this issue Jan 6, 2018 · 4 comments

Comments

@dfreire
Copy link
Contributor

dfreire commented Jan 6, 2018

I detected a problem when trying to use gun in the browser with indexeddb.
Apparently, I cannot get data from a node after a browser refresh.

This is the localStorage implementation, which works fine:

// setup with localStorage
const gun = Gun();

// run
gun.get('something').val() // undefined as expected
gun.get('something').put({ timestamp: Date.now() })
gun.get('something').val() // timestamp as expected

// refresh
gun.get('something').val() // timestamp as expected

This is the indexeddb implementation, which has the problem:
(same thing with level-js and fruitdown)

// setup with indexedDB
const Gun = require('gun-level');
const levelup = require('levelup');
const leveldown = require('level-js');
const db = leveldown('my-big-db');
db.status = 'unknown'; // see https://github.com/Level/level.js/issues/59
const gun = Gun({
  localStorage: false,
  level: levelup(db),
});

// run
gun.get('something').val() // undefined as expected
gun.get('something').put({ timestamp: Date.now() })
gun.get('something').val() // timestamp as expected

// refresh
gun.get('something').val() // undefined !!
// wait a lot of time
gun.get('something').val() // still undefined !!
// write again, then read
gun.get('something').put({ timestamp: Date.now() })
gun.get('something').val() // timestamp as expected
@dfreire
Copy link
Contributor Author

dfreire commented Jan 6, 2018

Actually, my bad!!
After reading more docs I finally realized the problem was in not using encoding-down

Fixed my setup:

// setup with indexedDB
const Gun = require('gun-level');
const levelup = require('levelup');
const leveldown = require('level-js');
const encode = require('encoding-down');
const db = leveldown('my-big-db');
const gun = Gun({
    localStorage: false,
    level: levelup(encode(db, { valueEncoding: 'json' })),
});

Furthermore, the db.status = 'unknown'; hack is no longer necessary 👍

@amark
Copy link
Collaborator

amark commented Jan 6, 2018

Is this working @dfreire ?

@dfreire
Copy link
Contributor Author

dfreire commented Jan 6, 2018

Yes, the issue can be closed (as it was not an issue).

However, maybe the snippet above could placed somewhere to document the explicit case of using indexeddb in the browser?

@amark
Copy link
Collaborator

amark commented Jan 7, 2018

That is a great idea, could you make a PR to the README of this repo? Then I'll accept it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants