Skip to content

Commit

Permalink
replaced xhttp with Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
steveruizok committed Apr 20, 2018
1 parent 4a07194 commit 7772087
Showing 1 changed file with 28 additions and 41 deletions.
69 changes: 28 additions & 41 deletions firebase.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class exports.Firebase extends Framer.BaseClass
url = "https://#{project}.firebaseio.com#{path}.json#{secret}"


unless parameters is undefined
if parameters?
if parameters.shallow then url += "&shallow=true"
if parameters.format is "export" then url += "&format=export"

Expand All @@ -47,35 +47,24 @@ class exports.Firebase extends Framer.BaseClass
url += "&startAt=#{parameters.startAt}" if typeof parameters.startAt is "number"
url += "&endAt=#{parameters.endAt}" if typeof parameters.endAt is "number"
url += "&equalTo=#{parameters.equalTo}" if typeof parameters.equalTo is "number"

xhttp = new XMLHttpRequest

console.log "Firebase: New '#{method}'-request with data: '#{JSON.stringify(data)}' \n URL: '#{url}'" if debug
xhttp.onreadystatechange = =>

unless parameters is undefined
if parameters.print is "silent" or typeof parameters.download is "string" then return # ugh

switch xhttp.readyState
when 0 then console.log "Firebase: Request not initialized \n URL: '#{url}'" if debug
when 1 then console.log "Firebase: Server connection established \n URL: '#{url}'" if debug
when 2 then console.log "Firebase: Request received \n URL: '#{url}'" if debug
when 3 then console.log "Firebase: Processing request \n URL: '#{url}'" if debug
when 4
if xhttp.responseText?
callback(JSON.parse(xhttp.responseText)) if callback?
console.log "Firebase: Request finished, response: '#{JSON.parse(xhttp.responseText)}' \n URL: '#{url}'" if debug
else
console.log "Lost connection to Firebase." if debug


if xhttp.status is "404"
console.warn "Firebase: Invalid request, page not found \n URL: '#{url}'" if debug


xhttp.open(method, url, true)
xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8")
xhttp.send(data = "#{JSON.stringify(data)}")


options = if data?
body: JSON.stringify(data)
method: method
headers:
'content-type': 'application/json; charset=utf-8'

r = fetch url, (options ? {})
.then (res) ->
if !res.ok then throw Error(res.statusText)
json = res.json()
json.then callback
return json
.catch (error) => console.warn(error)

return r


# Available methods
Expand All @@ -87,7 +76,6 @@ class exports.Firebase extends Framer.BaseClass
delete: (path, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "DELETE", null, parameters, @debug)



onChange: (path, callback) ->


Expand All @@ -111,17 +99,16 @@ class exports.Firebase extends Framer.BaseClass
console.warn "Firebase: Connection to Firebase Project '#{@projectID}' closed" if @debug
currentStatus = "disconnected"

return

else

url = "https://#{@projectID}.firebaseio.com#{path}.json#{@secretEndPoint}"
source = new EventSource(url)
console.log "Firebase: Listening to changes made to '#{path}' \n URL: '#{url}'" if @debug
url = "https://#{@projectID}.firebaseio.com#{path}.json#{@secretEndPoint}"
source = new EventSource(url)
console.log "Firebase: Listening to changes made to '#{path}' \n URL: '#{url}'" if @debug

source.addEventListener "put", (ev) =>
callback(JSON.parse(ev.data).data, "put", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PUT': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug
source.addEventListener "put", (ev) =>
callback(JSON.parse(ev.data).data, "put", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PUT': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug

source.addEventListener "patch", (ev) =>
callback(JSON.parse(ev.data).data, "patch", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PATCH': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug
source.addEventListener "patch", (ev) =>
callback(JSON.parse(ev.data).data, "patch", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PATCH': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug

0 comments on commit 7772087

Please sign in to comment.