0

I am writing facade for PouchDB Put Method

here is my facde

@JSName("PouchDB")
class PouchDB extends js.Object {
      type CALLBACK = js.Function2[js.UndefOr[js.Dynamic], js.UndefOr[js.Dynamic], Unit]

def this(name: js.UndefOr[String] = js.undefined, options: js.UndefOr[PouchDBOptions] = js.undefined) = this()

def put(doc: js.Object, docId: js.UndefOr[String] = js.undefined, docRev: js.UndefOr[String] = js.undefined, options: js.UndefOr[js.Object] = js.undefined, callback: CALLBACK = ???): Unit = js.native

...

put API takes a callback and also returns Promise , how can i define facade for put API which return promise..

invariant
  • 8,758
  • 9
  • 47
  • 61
  • I do not understand the PouchDB documentation (I don't know PouchDB), and I fail to see the problem because I don't understand what the correct *JS* would look like. Do you have an example in base JavaScript that works? Without that, I have no idea what Scala.js types are supposed to be, here. Note also that `destory` is marked as deprecated in the doc: probably not a good idea to start using it. – sjrd Feb 18 '15 at 12:14
  • static access of destory is deprecated not the instance one :) , leave about destroy method , put API returns Promise instead of using call back http://pouchdb.com/api.html#create_document how can i define for that .. – invariant Feb 18 '15 at 12:42
  • Can you print out the stacktrace of the CustomPouchError? Otherwise it doesn't provide much info. You can do this in the Chrome dev tools by right-clicking the promise, choosing "save to global variable", then typing `temp1.stack`. – nlawson Feb 18 '15 at 13:03
  • @nlawson i used different DB name ,it worked like a charm now , I don't know what exactly i did with old store("dude") in the morning , if i remember correctly i deleted store from static method and some other crazy thing from chrome console :( , sorry about that . – invariant Feb 18 '15 at 13:35
  • @sjrd i modified my question ... – invariant Feb 18 '15 at 13:41

2 Answers2

2

You will need a type for your Promises, just as for everything else. A promise is nothing magical, it's just an object with an interface, which you can type. You will need to know what's the API of PouchDB's promises, but it most likely is something (very) close to Promises/A+. In that case, it would look something like this:

import scala.scalajs.js
import js.annotation.JSName

trait Promise[+A] extends js.Object {
  @JSName("then")
  def andThen[B](onFulfilled: js.Function1[A, B],
      onRejected: js.Function1[Any, B]): Promise[B] = js.native

  @JSName("then")
  def andThen[B](onFulfilled: js.Function1[A, B]): Promise[B] = js.native

  @JSName("then")
  def andThen[B >: A](onFulfilled: Unit = (),
      onRejected: js.Function1[A, B] = ???)): Promise[B] = js.native
}

It's a bit convoluted because the an unspecified handler means that the promise returned by then can contain the same type of value as this promise if the onFulfilled handler is not specified. But I believe it should work like this.

Then you can declare put as returning a Promise[A] for some appropriate type A.

sjrd
  • 21,805
  • 2
  • 61
  • 91
  • Its 3:30 in the morning , my brain cells back to normal state!. i decided to write a wrapper for PouchDB facade ,https://gist.github.com/chandu0101/4d981fe36d654f872e3d ( have to write Class version) . thank you for your answer . – invariant Feb 18 '15 at 22:11
2

Update a couple of years later ;-)

Scalajs contains a build-in type for a JS Promise, just use js.Promise[_].

See: https://www.scala-js.org/doc/interoperability/types.html