Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Usage in web workers #12

Open
icidasset opened this issue Jul 27, 2017 · 8 comments
Open

Usage in web workers #12

icidasset opened this issue Jul 27, 2017 · 8 comments

Comments

@icidasset
Copy link

Hey folks 👋

I was wondering if there was a way to use this library (or blockstack.js) in a web worker. The issue I'm having is that each time I store my application state, the browser hangs because the state is so large and the storing process is executed in the same thread. So I tried to do a blockstack.putFile inside a web worker, but that doesn't work because putFile accesses the localStorage.

Thoughts?

@icidasset
Copy link
Author

Ok so I went through the code myself and made it so that the putFile function is able to run inside a web worker.

Here are the changes I made:
master...icidasset:web-worker#diff-d8f0f75d14323216d8e3540b96883ad2

The three important changes are:

1. You can pass in a "dataStore connection" via the options

const dataStoreConnection = (
  opts.ds ? Promise.resolve(opts.ds) : datastoreMountOrCreate()
);

This connection is passed down to all the functions that also need a dataStore connection.

2. Exported additional functions

That is, getSessionBlockchainID is now exported so you can easily get the blockchain id and use it with putFile.

3. Use the dataStoreConnection in more places

// old code
options['headers'] = {'Authorization': `bearer ${getSessionToken()}`}
// new code
options['headers'] = {'Authorization': `bearer ${ds.session_token}`}

Let me know what you think. If these changes are ok then I can make them for all functions and setup a PR.

✌️

@jcnelson
Copy link
Collaborator

Awesome work! Thanks for taking a look at this!

The only concern I have is this:

options['headers'] = {'Authorization': `bearer ${ds.session_token}`}

I need to think more about the implications of loading session_token from the datastore mount context. I don't think this is something that will be supported once multi-reader storage is implemented. I'm not familiar with web workers; are they not allowed to use localStorage?

@icidasset
Copy link
Author

Thanks! Ok cool, let me know if there could be another way to avoid calling getSessionToken() inside that function, maybe the ability to pass in the sessionToken via the options as well.

And no, web workers know next to nothing about the main thread. It's not really convenient, but it's the only way I know to make sure that my UI doesn't block ¯_(ツ)_/¯ (Although it still blocks for a second, sadly 😞 But it's way better than before)

@icidasset
Copy link
Author

How close are you to completing that multiplayer storage @jcnelson ? I was cleaning up the datastore.js file a bit, but I should probably wait until you're done with that multiplayer storage 🤔

@jcnelson
Copy link
Collaborator

jcnelson commented Aug 1, 2017

It's hard to say a precise date (we just wrote up some notes on the near-term storage engineering goals here: https://github.com/blockstack/blockstack/blob/master/meeting-notes/2017-08-01-storage.md).

@icidasset
Copy link
Author

Hmm ok. I'll rebase this work here on that multiplayer branch and I'll continue working on that. Then I can also start using that fileUrl thing you wrote.

I'm also going to split up that datastore.js file, because it's pretty overwhelming 😅

Sound good?

@jcnelson
Copy link
Collaborator

jcnelson commented Aug 2, 2017

I'm also going to split up that datastore.js file, because it's pretty overwhelming 😅

Actually, I'd appreciate it if you didn't at this time. There's a good chance that we're going to be removing a lot of code from it anyway, as part of adding multi-player storage. We can break it apart then :)

@icidasset
Copy link
Author

@jcnelson Aight cool. Let me know ;)

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

No branches or pull requests

2 participants