From 77720873e5017cacdae2dfeafbc75cfc873c5f39 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 20 Apr 2018 09:50:33 +0100 Subject: [PATCH] replaced xhttp with Promise --- firebase.coffee | 69 ++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/firebase.coffee b/firebase.coffee index b46ea64..2f83955 100644 --- a/firebase.coffee +++ b/firebase.coffee @@ -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" @@ -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 @@ -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) -> @@ -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