<commit_before>package client.ext.firebase

import client.appNameForFilesystem
import client.util.MapInLocalStorage
import client.util.handleError
import client.util.handlingErrors
import firebase.database.Database
import firebase.database.Reference
import kotlin.browser.window
import kotlin.js.Json

/**
 * A [firebase.database.Database] backed by browser localStorage.
 * @author <NAME> (<EMAIL>)
 * Date: 4/19/18
 * Time: 5:05 AM
 */
open class FirebaseDatabaseWithLocalStorage(private val firebaseDatabase: Database) {
    init {
        syncRemainingToFirebaseAsynchronously()
    }

    fun push(parentReference: Reference, value: Any, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }): Reference {
        val reference = parentReference.push()
        set(reference, value, onComplete)
        return reference
    }

    fun set(reference: Reference, value: Any, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }) {
        markAsNotSynced(reference, value)
        setWithoutMarkingAsNotSynced(reference, value, onComplete)
    }

    fun remove(reference: Reference, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }) {
        markAsNotSynced(reference, null)
        removeWithoutMarkingAsNotSynced(reference, onComplete)
    }

    private fun markAsNotSynced(ref: Reference, value: Any?) {
        valuesToSync.put(ref.path, value)
    }

    private fun markAsSynced(reference: Reference) {
        valuesToSync.remove(reference.path)
    }

    private fun setWithoutMarkingAsNotSynced(reference: Reference, entity: Any, onComplete: (Error?) -> Any) {
        val jsonValue = JSON.parse<Any>(JSON.stringify(entity))
        reference.set(jsonValue, onComplete = { error ->
            handlingErrors("firebase set") {
                if (error == null) {
                    markAsSynced(reference)
                }
                onComplete.invoke(error)
            }
        })
    }

    private fun removeWithoutMarkingAsNotSynced(reference: Reference, onComplete: (a: Error?) -> Any) {
        reference.remove(onComplete = { error ->
            handlingErrors("firebase remove onComplete") {<commit_msg>Add some logging to FirebaseDatabaseWithLocalStorage<commit_after>package client.ext.firebase

import client.appNameForFilesystem
import client.util.MapInLocalStorage
import client.util.handleError
import client.util.handlingErrors
import firebase.database.Database
import firebase.database.Reference
import kotlin.browser.window
import kotlin.js.Json

/**
 * A [firebase.database.Database] backed by browser localStorage.
 * @author <NAME> (<EMAIL>)
 * Date: 4/19/18
 * Time: 5:05 AM
 */
open class FirebaseDatabaseWithLocalStorage(private val firebaseDatabase: Database) {
    init {
        syncRemainingToFirebaseAsynchronously()
    }

    fun push(parentReference: Reference, value: Any, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }): Reference {
        val reference = parentReference.push()
        set(reference, value, onComplete)
        return reference
    }

    fun set(reference: Reference, value: Any, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }) {
        markAsNotSynced(reference, value)
        setWithoutMarkingAsNotSynced(reference, value, onComplete)
    }

    fun remove(reference: Reference, onComplete: ((Error?) -> Any) = { if (it != null) handleError(it) }) {
        markAsNotSynced(reference, null)
        removeWithoutMarkingAsNotSynced(reference, onComplete)
    }

    private fun markAsNotSynced(ref: Reference, value: Any?) {
        valuesToSync.put(ref.path, value)
    }

    private fun markAsSynced(reference: Reference) {
        valuesToSync.remove(reference.path)
    }

    private fun setWithoutMarkingAsNotSynced(reference: Reference, entity: Any, onComplete: (Error?) -> Any) {
        val jsonString = JSON.stringify(entity)
        val jsonValue = JSON.parse<Any>(jsonString)
        println("FirebaseDatabaseWithLocalStorage: setting ${reference.path} to $jsonString")
        reference.set(jsonValue, onComplete = { error ->
            handlingErrors("firebase set") {
                if (error == null) {
                    markAsSynced(reference)
                }
                onComplete.invoke(error)
            }
        })
    }

    private fun removeWithoutMarkingAsNotSynced(reference: Reference, onComplete: (a: Error?) -> Any) {
        println("FirebaseDatabaseWithLocalStorage: removing ${reference.path}")
        reference.remove(onComplete = { error ->
            handlingErrors("firebase remove onComplete") {
{'fraction_non_alphanumeric': 0.10950010820168794, 'fraction_numerical': 0.0034624540142826226, 'mean_word_length': 3.2364802933088908, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5939670', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1270, 'n_words': 295}