Last active
October 15, 2019 10:21
-
-
Save shsunmoonlee/9c86be7fe75cf0d3d36e80aa86a88332 to your computer and use it in GitHub Desktop.
Spark AR Firebase sharing the state tutorial.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -- Firebase | |
| const Firebase = (function () { | |
| // -- Modules | |
| const D = require("Diagnostics"); | |
| const N = require("Networking"); | |
| // -- Public | |
| /** | |
| * Firebase class. | |
| * | |
| * @constructor | |
| * @param {String} url - The url of your realtime database. | |
| */ | |
| function Firebase(url) { | |
| this._url = url; | |
| this._isObserving = false; | |
| this._onUpdate = null; | |
| } | |
| /** | |
| * Update the realtime database and set the data as current value. | |
| */ | |
| Firebase.prototype.set = function(data) { | |
| let options = { | |
| method: 'PUT', | |
| body: JSON.stringify(data) | |
| } | |
| N.fetch(this._url, options).then(function(result) { | |
| if (!((result.status >= 200) && (result.status < 300))) { | |
| D.log("Failed to post data!"); | |
| } | |
| }); | |
| } | |
| /** | |
| * Update the realtime database and set the data as current value. | |
| */ | |
| Firebase.prototype.subscribe = function(onUpdate) { | |
| this._onUpdate = onUpdate; | |
| } | |
| /** | |
| * Start observing changes in your realtime database. | |
| */ | |
| Firebase.prototype.start = function() { | |
| this._isObserving = true; | |
| fetch.call(this); | |
| } | |
| /** | |
| * Stop observing changes in your realtime database. | |
| */ | |
| Firebase.prototype.stop = function() { | |
| this._isObserving = false; | |
| } | |
| /** | |
| * Get a description for debugging purposes. | |
| * | |
| * @return {String} | |
| */ | |
| Firebase.prototype.toString = function() { | |
| return 'Firebase("' + this._url + '")'; | |
| } | |
| // -- Private | |
| /** | |
| * Fetch realtime database once. | |
| */ | |
| function fetch() { | |
| const self = this; | |
| N.fetch(self._url).then(function(result) { | |
| if (self._isObserving) { | |
| fetch.call(self); | |
| } | |
| if ((result.status >= 200) && (result.status < 300)) { | |
| return result.json(); | |
| } | |
| throw new Error("HTTP status code " + result.status); | |
| }).then(function(json) { | |
| if (self._onUpdate != null) { | |
| self._onUpdate(json); | |
| } | |
| }).catch(function(error) { | |
| D.log("There was an issue with fetch operation: " + error.message); | |
| }); | |
| } | |
| // -- Export | |
| return Firebase; | |
| })(); | |
| // -- Modules | |
| const S = require("Scene"); | |
| const TG = require("TouchGestures"); | |
| // -- Constants | |
| const URL = "YOUR OWN FIREBASE ADDRESS i.e) https://YOURPROJECTNAME.firebaseio.com/.json"; | |
| // -- Scene | |
| const counterText = S.root.find("Counter Text"); | |
| const decrementButton = S.root.find("Decrement Button"); | |
| const incrementButton = S.root.find("Increment Button"); | |
| // -- Example | |
| var data = null; | |
| var firebase = new Firebase(URL); | |
| firebase.subscribe(function(e) { | |
| data = e; | |
| counterText.text = `${data.counter}`; | |
| }); | |
| firebase.start(); | |
| TG.onTap(decrementButton).subscribe(function(e) { | |
| data.counter = data.counter - 1; | |
| firebase.set(data); | |
| }); | |
| TG.onTap(incrementButton).subscribe(function(e) { | |
| data.counter = data.counter + 1; | |
| firebase.set(data); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment