dart-archive / polymer_elements

https://pub.dartlang.org/packages/polymer_elements
BSD 3-Clause "New" or "Revised" License
24 stars 17 forks source link

Firebase_auth.dart should allow optional arguments #136

Open pjjjv opened 7 years ago

pjjjv commented 7 years ago

Firebase_auth. dart should allow no argument to be passed in to signInWithPopup, just like the JS version. Currently, this is required:


  signInWithPopup(provider) =>
      jsElement.callMethod('signInWithPopup', [provider]);

Also, I ran into https://github.com/firebase/polymerfire/issues/44 with rc9, so it would be a good idea to release a new version.

Finally, please make these things more dart-y by providing extra conversions in the box. Dart's learning curve is already steep enough without every developer having to figure out javascript and ecmascript6. Add the following bits and bytes:


  Future signInWithPopup() {
    var c = new Completer();
    Promise promise = new Promise(jsElement.callMethod('signInWithPopup'));

    promise.then(([response]) {c.complete(decodeAuthData(response));});
    promise.catchError(([error]) {c.completeError(error);});
    return c.future;
  }

  /**
   * Converts authData response from JsObject to native Dart object.
   */
  dynamic decodeAuthData(JsObject authData) {
    var json = js.context['JSON'].callMethod('stringify', [authData]);
    return JSON.decode(json);
  }

class Promise {
  static Promise all(List input) {
    return js.context["Promise"].callMethod("all", [input]);
  }

  final JsObject obj;

  Promise(this.obj);

  void then(Function func) {
    obj.callMethod("then", [func]);
  }

  void catchError(Function func) {
    obj.callMethod("catch", [func]);
  }
}