1v1lol/js/firestore.js
2022-07-09 16:54:09 -05:00

25 lines
738 B
JavaScript

const cache = {};
let callbackId = 0;
// This is being called directly from unity.
function getFirestoreListener(collection, documentId, successCallback, errorCallback) {
callbackId++;
const id = callbackId; // capture value
const unsubscribe = firebase.firestore().collection(collection).doc(documentId).onSnapshot(doc => {
if (doc.exists)
successCallback([id, JSON.stringify(doc.data())]);
}, error => {
errorCallback(error.message);
});
cache[id] = unsubscribe;
return id;
}
// This is being called directly from unity.
function detachFirestoreListener(id) {
if (cache[id] !== undefined) {
cache[id]();
delete cache[id];
}
}