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

chrome.storage.Storage.sync.set and get not working #47

Open
dportabella opened this issue Nov 28, 2019 · 4 comments
Open

chrome.storage.Storage.sync.set and get not working #47

dportabella opened this issue Nov 28, 2019 · 4 comments

Comments

@dportabella
Copy link

After executing this scalajs code:

chrome.storage.Storage.sync.set(Map("key1" -> "value1"))

this javascript code

chrome.storage.sync.get(undefined, function(result) { console.log(JSON.stringify(result)); });

shows {"value1$5":"value1"} instead of {"key1":"value1"}
is this a bug?

also,

chrome.storage.Storage.sync.get(js.undefined).foreach(println)

throws an Error. why?

@raquo
Copy link

raquo commented Nov 28, 2019

The facade was incorrect, it was fixed in 9518752

I don't know why the latest version is not published in Maven though.

@dportabella
Copy link
Author

@raquo , I don't manage to build and use a version of scala-js-chrome with the fix you mention. See #48
Any idea?

@raquo
Copy link

raquo commented Dec 4, 2019

@dportabella Not super sure, but looks like you need to upgrade the versions of sbt plugins in this project, at the very least scalajs and scalafmt. Watch out for any breaking changes though. Also you might need to drop support for scala 2.10 in build.sbt to achieve that, that's a very old version.

Oh and better start from the master branch, it's one year newer than the diff I linked to.

Really that's a question for the maintainer, I was only passing by, don't know the details of this project.

@dportabella
Copy link
Author

Thanks @raquo .

I just gave up on building scala-js-chrome after spending two days on this.
Here it is my workaround in case someone is interested:

import chrome.utils.ErrorHandling.lastErrorOrValue

import scala.concurrent.{Future, Promise}
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

object ChromeStorageSyncWrapper {
  def set(items: js.Dynamic): Future[Unit] = {
    val promise = Promise[Unit]()
    ChromeStorageSyncNative.set(items, js.Any.fromFunction0(() => {
      promise.complete(lastErrorOrValue(()))
    }))
    promise.future
  }

  def get(keys: js.UndefOr[js.Any] = js.undefined): Future[js.Dynamic] = {
    val promise = Promise[js.Dynamic]()
    ChromeStorageSyncNative.get(keys, (results: js.Dynamic) => {
      promise.complete(lastErrorOrValue(results))
    })
    promise.future
  }
}

@js.native
@JSGlobal("chrome.storage.sync")
object ChromeStorageSyncNative extends js.Object {
  def set(items: js.Dynamic, callback: js.UndefOr[js.Function0[_]] = js.undefined): Unit = js.native
  def get(keys: js.UndefOr[js.Any] = js.undefined, callback: js.Function1[js.Dynamic, _]): Unit = js.native
}

// Use:
ChromeStorageSyncWrapper.set(js.Dynamic.literal("key1" -> "value1", "key2" -> "value2"))
ChromeStorageSyncWrapper.get(js.Array("key1", "key2"))

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