Skip to content

Commit

Permalink
Replaced uses of $q with Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ldaniels528 committed Oct 23, 2016
1 parent adeaa99 commit aa615dd
Showing 1 changed file with 52 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.scalajs.dom.console
import org.scalajs.nodejs.social.facebook._
import org.scalajs.nodejs.util.ScalaJsHelper._

import scala.concurrent.Promise
import scala.scalajs.js
import scala.scalajs.js.Dynamic.literal
import scala.util.{Failure, Success, Try}
Expand All @@ -14,7 +15,7 @@ import scala.util.{Failure, Success, Try}
* @author [email protected]
* @see [[https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5]]
*/
class FacebookService($q: Q) extends Service {
class FacebookService() extends Service {
type CallbackObject = js.Function1[js.Dynamic, Unit]
type PaginationCallback[T] = js.Function1[FacebookPagination[T], Unit]

Expand All @@ -30,40 +31,40 @@ class FacebookService($q: Q) extends Service {
def facebookID: js.UndefOr[String] = auth.map(_.userID)

def getLoginStatus = {
val deferred = $q.defer[FacebookLoginStatusResponse]()
val promise = Promise[FacebookLoginStatusResponse]()
FB.getLoginStatus((response: js.UndefOr[FacebookLoginStatusResponse]) =>
specialHandling(deferred, response) {
specialHandling(promise, response) {
case resp if resp.status == "connected" =>
auth = resp.authResponse
//console.log(s"facebookID = $facebookID, auth = ${angular.toJson(auth)}")
Success(resp)
case resp =>
Failure(new RuntimeException(s"Facebook is not connected (status: ${resp.status})"))
})
deferred.promise
promise
}

def getUserProfile = {
val deferred = $q.defer[FacebookProfileResponse]()
FB.api(fbURL(), (response: js.UndefOr[FacebookProfileResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookProfileResponse]()
FB.api(fbURL(), (response: js.UndefOr[FacebookProfileResponse]) => handleResponse(promise, response))
promise
}

def login() = {
val deferred = $q.defer[FacebookLoginStatusResponse]()
val promise = Promise[FacebookLoginStatusResponse]()
FB.login((response: js.UndefOr[FacebookLoginStatusResponse]) =>
specialHandling(deferred, response) { resp =>
specialHandling(promise, response) { resp =>
auth = resp.authResponse
//console.log(s"facebookID = $facebookID, auth = ${angular.toJson(auth)}")
Success(resp)
})
deferred.promise
promise
}

def logout() = {
val deferred = $q.defer[FacebookLoginStatusResponse]()
FB.logout((response: js.UndefOr[FacebookLoginStatusResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookLoginStatusResponse]()
FB.logout((response: js.UndefOr[FacebookLoginStatusResponse]) => handleResponse(promise, response))
promise
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -77,23 +78,23 @@ class FacebookService($q: Q) extends Service {
* @see [[https://developers.facebook.com/docs/games/achievements]]
*/
def getAchievement(achievementID: String) = {
val deferred = $q.defer[FacebookAchievementResponse]()
FB.api(fbURL(s"/achievements"), (response: js.UndefOr[FacebookAchievementResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookAchievementResponse]()
FB.api(fbURL(s"/achievements"), (response: js.UndefOr[FacebookAchievementResponse]) => handleResponse(promise, response))
promise
}

///////////////////////////////////////////////////////////////////////////
// Friend-related Functions
///////////////////////////////////////////////////////////////////////////

def createFriendList(friendListId: String) = {
val deferred = $q.defer[FacebookResponse]()
FB.api(fbURL(s"/$friendListId/member"), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.api(fbURL(s"/$friendListId/member"), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

def getFriends = {
val deferred = $q.defer[js.Array[TaggableFriend]]()
val promise = Promise[js.Array[TaggableFriend]]()
val friends = emptyArray[TaggableFriend]
FB.api(fbURL("/friends"), (response: FacebookPagination[TaggableFriend]) => {
//console.log(s"response = ${angular.toJson(response)}")
Expand All @@ -104,27 +105,27 @@ class FacebookService($q: Q) extends Service {
}
()
})
deferred.promise
promise
}

def getFriendList(listType: js.UndefOr[String] = "close_friends") = {
val deferred = $q.defer[FacebookResponse]()
FB.api(fbURL("/friendlists", s"list_type=$listType"), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.api(fbURL("/friendlists", s"list_type=$listType"), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

def getFriendListMembers(friendListId: String) = {
val deferred = $q.defer[FacebookResponse]()
FB.api(fbURL(s"/$friendListId/members"), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.api(fbURL(s"/$friendListId/members"), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

/**
* Retrieves all "taggable" friends for the authenticated user
* @return the array of [[TaggableFriend taggable friends]]
*/
def getTaggableFriends = {
val deferred = $q.defer[js.Array[TaggableFriend]]()
val promise = Promise[js.Array[TaggableFriend]]()
val friends = emptyArray[TaggableFriend]
val callback: PaginationCallback[TaggableFriend] = (response: FacebookPagination[TaggableFriend]) => {
//console.log(s"response = ${angular.toJson(response)}")
Expand All @@ -137,9 +138,9 @@ class FacebookService($q: Q) extends Service {
}
FB.api(fbURL("/taggable_friends"), { (response: TaggableFriendsResponse) =>
handlePaginatedResults(response, callback)
deferred.resolve(friends)
promise.success(friends)
})
deferred.promise
promise
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -153,9 +154,9 @@ class FacebookService($q: Q) extends Service {
* @see [[https://developers.facebook.com/docs/graph-api/reference/user/photos/]]
*/
def getPhotos(`type`: js.UndefOr[String] = js.undefined) = {
val deferred = $q.defer[FacebookPhotosResponse]()
FB.api(fbURL("/photos", `type` map (myType => s"type=$myType")), (response: FacebookPhotosResponse) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookPhotosResponse]()
FB.api(fbURL("/photos", `type` map (myType => s"type=$myType")), (response: FacebookPhotosResponse) => handleResponse(promise, response))
promise
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -171,21 +172,21 @@ class FacebookService($q: Q) extends Service {
///////////////////////////////////////////////////////////////////////////

def feed(appID: String, caption: String, link: String) = {
val deferred = $q.defer[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "feed", link = link, caption = caption), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "feed", link = link, caption = caption), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

def send(appID: String, message: String, link: String) = {
val deferred = $q.defer[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "send", link = link), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "send", link = link), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

def share(appID: String, link: String) = {
val deferred = $q.defer[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "share", href = link), (response: js.UndefOr[FacebookResponse]) => handleResponse(deferred, response))
deferred.promise
val promise = Promise[FacebookResponse]()
FB.ui(FacebookCommand(app_id = appID, method = "share", href = link), (response: js.UndefOr[FacebookResponse]) => handleResponse(promise, response))
promise
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -203,11 +204,11 @@ class FacebookService($q: Q) extends Service {
s"/$fbUserID$path?access_token=$accessToken" + (args map (myArgs => s"&$myArgs") getOrElse "")
}

private def handleResponse[A <: FacebookResponse](deferred: QDefer[A], response: js.UndefOr[A]) = {
private def handleResponse[A <: FacebookResponse](promise: Promise[A], response: js.UndefOr[A]) = {
response.toOption match {
case Some(resp) if resp.error.isEmpty => deferred.resolve(resp)
case Some(resp) => deferred.reject(resp.error)
case None => deferred.reject("No response from Facebook")
case Some(resp) if resp.error.isEmpty => promise.success(resp)
case Some(resp) => promise.failure(new Exception(resp.error getOrElse "Cause unknown"))
case None => promise.failure(new Exception("No response from Facebook"))
}
}

Expand All @@ -229,15 +230,15 @@ class FacebookService($q: Q) extends Service {
})
}

private def specialHandling[A <: FacebookResponse](deferred: QDefer[A], response: js.UndefOr[A])(handler: A => Try[A]) = {
private def specialHandling[A <: FacebookResponse](promise: Promise[A], response: js.UndefOr[A])(handler: A => Try[A]) = {
response.toOption match {
case Some(resp) if resp.error.isEmpty =>
handler(resp) match {
case Success(value) => deferred.resolve(value)
case Failure(e) => deferred.reject(e.getMessage)
case Success(value) => promise.success(value)
case Failure(e) => promise.failure(e)
}
case Some(resp) => deferred.reject(resp.error)
case None => deferred.reject("No response from Facebook")
case Some(resp) => promise.failure(new Exception(resp.error getOrElse "Cause unknown"))
case None => promise.failure(new Exception("No response from Facebook"))
}
}

Expand Down

0 comments on commit aa615dd

Please sign in to comment.