Skip to content
Permalink
Browse files

Update fireweb.js

  • Loading branch information...
Fritsbie committed Jul 21, 2019
1 parent 541d680 commit 650553b134250eebcca202ead7a0400295d7131c
Showing with 128 additions and 107 deletions.
  1. +128 −107 fireweb.js
@@ -1,49 +1,53 @@
// Your web app's Firebase configuration,

//Start deleting from here
var firebaseConfig = {
apiKey: ".......................................",
authDomain: ".......................................",
databaseURL: ".......................................",
projectId: ".......................................",
storageBucket: ".......................................",
messagingSenderId: ".......................................",
appId: "......................................."
};
//To here
//And put your config

var app = firebase.initializeApp(firebaseConfig);
db = firebase.firestore(app);


//Constants
const codeError = -1;
const codeFailed = 0;
const codeSuccess = 1;
const codeChangeServer = 1;
const codeChangeLocal = 0;

//Firestore Database
//Callback codes for the extension
const firestoreCodeError = -1;
const firestoreCodeFailed = 0;
const firestoreCodeSuccess = 1;
const firestoreCodeChangeServer = 1;
const firestoreCodeChangeLocal = 0;
const firestoreCodeChangeAdd = 0;
const firestoreCodeChangeModify = 1;
const firestoreCodeChangeDelete = 2;
const firestoreCodeDocumentAdd = 0;
const firestoreCodeDocumentSet = 1;
const firestoreCodeDocumentUpdate = 2;
const firestoreCodeDocumentRemove = 3;
//Global callback ID
const FIREWEBASYNCID = "6131";


//Vars for other things
var db;
var debugMode = false;
var networkStatus = 'default';

//Result String holder of a document
var resultString = "";

//Query Builder
var queryRef = null;
var dataListener = {};

//Network Online/Offline
var networkStatus = 'default';


//Some Global Variables
var debugMode = false;

//Init Firestore
function firestore_web_init(configMap, im) {
//Conver GML boolean to Javascript boolean

var a = GMS_API.json_encode(configMap);
//var configParsed = JSON.parse( a );
if (im > 0.5) {
var firebaseConfig = JSON.parse(a);
firebase.initializeApp(firebaseConfig);
}

db = firebase.firestore();
}

//Debug Mode
function firestore_web_debug_mode(deb) {
if (deb=="DEBUGIT") {
//Convert GML boolean to Javascript boolean.
if (deb>0.5) {
debugMode = true;
} else {
debugMode = false;
@@ -69,59 +73,52 @@ function firestore_web_default_network() {
}



function firestore_web_document_json() {
return resultString;
}

///>>>

function firestore_web_document_get(documentLocation, callbackFunction) {
function firestore_web_document_get(documentLocation) {

var getOptions = {
source: networkStatus
};

debugLog("Document/Get","Getting the document : " + documentLocation + ". It'll be retrieved to function > " + callbackFunction)
debugLog("Document/Get","Getting the document : " + documentLocation);
db.doc(documentLocation).get(getOptions).then(function(doc) {
if (doc.exists) {
debugLog("Document/Get","Document " + documentLocation + " retrieved.");
transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);
MapDocument("document_get", documentLocation, doc.id, firestoreCodeSuccess, doc.data());

} else {
// doc.data() will be undefined in this case
debugLog("Document/Get","Document " + documentLocation + " does not exist!");
goCallback(callbackFunction, codeFailed);
MapDocument("document_get", documentLocation, "-", firestoreCodeFailed, "");


}
}).catch(function(error) {

debugLog("Document/Get","Error fetching the document. Error code : " + error);
goCallback(callbackFunction, codeError);
MapDocument("document_get", documentLocation, "", firestoreCodeError, "");
});
}




function firestore_web_document_listen(documentLocation, callbackFunction, dataListenerID) {
function firestore_web_document_listen(documentLocation, dataListenerID) {

var listener = db.doc(documentLocation)
.onSnapshot(function(doc) {

var source = "";
var source = -1;
if (doc.metadata.hasPendingWrites=="Local") {
source =="Local DB";
goCallback(callbackFunction,codeChangeLocal);
source == firestoreCodeChangeLocal;
} else {
source =="Server DB";
goCallback(callbackFunction,codeChangeServer);
source == firestoreCodeChangeServer;
}
var source = doc.metadata.hasPendingWrites ? "Local" : "Server";
debugLog("Document/Listen","A "+source+" based change has been detected in " + documentLocation);


MapDocumentListen("document_listen", documentLocation, doc.id, firestoreCodeSuccess, source, doc.data());
debugLog("Document/Listen","Retrieved a document from " + documentLocation);

}).catch(function(error) {
debugLog("Document/Get","Error listening to the document. Error code : " + error);
goCallback(callbackFunction, codeError);
debugLog("Document/Listen","Error listening to the document. Error code : " + error);
MapDocumentListen("document_listen", documentLocation, "", firestoreCodeError, "", "");
});

dataListenerAdd(dataListenerID,listener);
@@ -133,51 +130,64 @@ function firestore_web_listener_detach(listenerID) {
}

///Set a Document
function firestore_web_document_set(documentLocation, jsonStr, callbackFunction) {
function firestore_web_document_set(documentLocation, jsonStr) {

let jsonObject = JSON.parse(jsonStr);
var jsonObject = JSON.parse(jsonStr);

db.doc(documentLocation).set(jsonObject)
.then(function() {
goCallback(callbackFunction,codeSuccess);
MapDocumentGetters("document_set",documentLocation,firestoreCodeSuccess);
debugLog("Document/Set","Successfully SET the document to " + documentLocation);
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error SETting the document. Error code : " + error);
MapDocumentGetters("document_set",documentLocation,firestoreCodeError);
debugLog("Document/Set","Error SETting the document. Error code : " + error);
});
}

///Add a Document
function firestore_web_document_add(collectionLocation, jsonStr, callbackFunction) {
function firestore_web_document_add(collectionLocation, jsonStr) {

let jsonObject = JSON.parse(jsonStr);
var jsonObject = JSON.parse(jsonStr);

db.collection(collectionLocation).add(jsonObject)
.then(function(docRef) {
goCallback(callbackFunction,codeSuccess);
debugLog("Document/Set","Successfully ADDed the document to "+ collectionLocation +". Document ID : " + docRef.id);
MapDocumentGetters("document_add",collectionLocation,firestoreCodeSuccess);
debugLog("Document/Add","Successfully ADDed the document to "+ collectionLocation +". Document ID : " + docRef.id);
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error ADDing the document. Error code : " + error);
MapDocumentGetters("document_add",collectionLocation,firestoreCodeError);
debugLog("Document/Add","Error ADDing the document. Error code : " + error);
});
}

///Update a Document
function firestore_web_document_update(documentLocation, jsonStr, callbackFunction) {
function firestore_web_document_update(documentLocation, jsonStr) {

let jsonObject = JSON.parse(jsonStr);
var jsonObject = JSON.parse(jsonStr);

db.doc(collectionLocation).update(jsonObject)
.then(function(docRef) {
goCallback(callbackFunction,codeSuccess);
debugLog("Document/Set","Successfully written the document to "+ collectionLocation +". Document ID : " + docRef.id);
console.log("Document successfully written!");
MapDocumentGetters("document_update",documentLocation,firestoreCodeSuccess);
debugLog("Document/Update","Successfully written the document to "+ documentLocation +". Document ID : " + docRef.id);
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error SETting the document. Error code : " + error);
MapDocumentGetters("document_update",documentLocation,firestoreCodeError);
debugLog("Document/Update","Error SETting the document. Error code : " + error);
});
}

///Delete a Document
function firestore_web_document_delete(documentLocation) {

db.doc(collectionLocation).delete()
.then(function(docRef) {
MapDocumentGetters("document_delete",documentLocation,firestoreCodeSuccess);
debugLog("Document/Delete","Successfully deleted the document to "+ documentLocation +". Document ID : " + docRef.id);
})
.catch(function(error) {
MapDocumentGetters("document_delete",documentLocation,firestoreCodeError);
debugLog("Document/Delete","Error DELETing the document. Error code : " + error);
});
}

@@ -254,57 +264,51 @@ function firestore_web_query_orderby_ext(key, order) {

}

function firestore_web_query_execute_get(callbackFunction) {
function firestore_web_query_execute_get(queryName) {

queryRef.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {

debugLog("Query/Execute/Get","Document received from a query, being redirected to " + callbackFunction);

transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);
debugLog("Query/Get","Query document is received from a query. Being directed to Social Async Event...");
MapDocument("query_get",queryName,doc.id,firestoreCodeSuccess,doc.data());

});
});

}


function firestore_web_query_execute_listen_all(callbackFunction, datalistenerID) {

function firestore_web_query_execute_listen_all(queryName, dataListenerID) {
var listener = queryRef.onSnapshot(function(querySnapshot) {

querySnapshot.forEach(function(doc) {
debugLog("Query/Execute/Listen","Document received from a query listener, being redirected to " + callbackFunction);

transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);
debugLog("Query/Listen","Query document received from a query listener. Being redirected to Being directed to Social Async Event...");
MapDocument("query_listen_all",queryName,doc.id,firestoreCodeSuccess,doc.data());

});
});
dataListenerAdd(dataListenerID,listener);
}


function firestore_web_query_execute_listen_changes(addedCallbackFunction, modifiedCallbackFunction, removedCallbackFunction, dataListenerID) {
function firestore_web_query_execute_listen_changes(queryName, dataListenerID) {

var listener = queryRef.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
debugLog("Query/Execute/Listen/Changes","A document is added to the Query scope, being redirected to " + addedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(addedCallbackFunction, codeSuccess);

debugLog("Query/Listen/Changes","A Document is added to the Query scope, being redirected to Social Async Event...");
MapDocument("query_listen_changes",queryName,change.doc.id,firestoreCodeChangeAdd,change.doc.data());
}
if (change.type === "modified") {
debugLog("Query/Execute/Listen/Changes","A Document is modified within the Query scope, being redirected to " + modifiedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(modifiedCallbackFunction, codeSuccess);
debugLog("Query/Listen/Changes","A Document is modified within the Query scope, being redirected to being redirected to Social Async Event...");
MapDocument("query_listen_changes",queryName,change.doc.id,firestoreCodeChangeModify,change.doc.data());

}
if (change.type === "removed") {
debugLog("Query/Execute/Listen/Changes","A Document is removed from the Query scope, being redirected to " + removedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(removedCallbackFunction, codeSuccess);
debugLog("Query/Listen/Changes","A Document is removed from the Query scope, being redirected to being redirected to Social Async Event...");
MapDocument("query_listen_changes",queryName,change.doc.id,firestoreCodeChangeDelete,change.doc.data());

}
});
@@ -327,25 +331,42 @@ function dataListenerRemove(key) {
debugLog("Listener/Remove","Stopped listening to the area marked with " + key + " key.");
}

function transferDataToJson(jsonData) {

resultString = JSON.stringify(jsonData);
}

function debugLog (category, message) {
if (debugMode==true) {
console.log("Game Maker Firebase Web ["+category+"] : " + message);
}
}

function MapDocument(docCategory, docLocation, docID, docStatus, docData) {
var map = {};
map["id"] = FIREWEBASYNCID;
map["category"] = docCategory;
map["location"] = docLocation;
map["document_id"] = docID;
map["status"] = docStatus;
map["data"] = JSON.stringify(docData);
GMS_API.send_async_event_social(map);
}


function goCallback(func, status) {
if (func!="")
{
eval("gml_Script_"+func+"(null,null,'"+status+"')");
}
function MapDocumentListen(docCategory, docLocation, docID, docStatus, docSource, docData) {
var map = {};
map["id"] = FIREWEBASYNCID;
map["category"] = docCategory;
map["location"] = docLocation;
map["document_id"] = docID;
map["source"] = docSource;
map["status"] = docStatus;
map["data"] = JSON.stringify(docData);
GMS_API.send_async_event_social(map);
}

function goCallbackExt(func, status, extra) {
eval("gml_Script_"+func+"(null,null,'"+status+"','+"+ extra +")");
function MapDocumentGetters(docCategory, docLocation, docStatus) {
var map = {};
map["id"] = FIREWEBASYNCID;
map["category"] = docCategory;
map["location"] = docLocation;
map["status"] = docStatus;
GMS_API.send_async_event_social(map);
}

0 comments on commit 650553b

Please sign in to comment.
You can’t perform that action at this time.