<commit_before>"use strict";

import SerializationPacket = require("modules/serialization/SerializationPacket");
var nullPacket = SerializationPacket.nullPacket;
import $ = require("zepto");
var isArray = $.isArray;
import lf = require("localforage");

var lnks = [];
var objs = {};

var priv = {
    loadLocalForage: function(){
        console.log("fetching from localForage...");
        return lf.getItem<any[]>("links").then(function(linkJson){
            lnks = linkJson || [];
            return lf.getItem("objs");
        }).then(function(objJson){
            objs = objJson || {};
        }).then(function(){console.log("lf fetch finished")});
    },

    dump: function () {
        console.log("writing localForage...");
        return lf.setItem("objs", objs).then(function(){
            return lf.setItem("links", lnks);
        }).then(function(){console.log("lf commit finished")});
    },

    getLinks: function (fromId, type) {
        return lnks.filter(function (l) {
            return l.fromId === fromId && l.type === type;
        });
    },

    indexOfLink: function (fromId, type) {
        var i;
        for (i = 0; i < lnks.length; i += 1) {
            if (lnks[i].fromId === fromId && lnks[i].type === type) {
                return i;
            }
        }
        return -1;
    },


    removeLinks: function (fromId, linkName) {
        var index;
        while ((index = priv.indexOfLink(fromId, linkName)) !== -1) {
            lnks.splice(index, 1);
        }
    },

    addLink: function (fromId, toId, linkName) {
        lnks.push({
            fromId: fromId,
            toId: toId,
            type: linkName
        });
    },

    save: function (data) {
        objs[data.id] = $.extend({}, data);
    },

    getById: function (id) {
        if (!objs[id]) {
            return null;
        }
        return $.extend({}, objs[id]);
    },

    clear: function () {
        lnks = [];
        objs = {};
        return lf.setItem("objs", {}).then(function(){
            return lf.setItem("links", []);
        }).then(function(){console.log("lf erase finished")});
    }
}
var fake = {
    getById: priv.getById,
    save: priv.save,
    addLink: priv.addLink,
    removeLinks: priv.removeLinks,
    getLinks: priv.getLinks,
    clear: priv.clear,
    commit: priv.dump,
    init: priv.loadLocalForage
};

window.fakeDb = fake;

export = fake;
<commit_msg>serialization crap<commit_after>"use strict";

import SerializationPacket = require("modules/serialization/SerializationPacket");
var nullPacket = SerializationPacket.nullPacket;
import $ = require("zepto");
var isArray = $.isArray;
import lf = require("localforage");

var lnks = [];
var objs = {};

var priv = {
    loadLocalForage: function(){
        console.log("fetching from localForage...");
        return lf.getItem<string>("links").then(function(linkJson){
            try{
                lnks = JSON.parse(linkJson) || [];
            }catch(e){
                console.log(e);
                lnks=[];
            }
            return lf.getItem<string>("objs");
        }).then(function(objJson){
            try{
                objs = JSON.parse(objJson) || {};
            }catch(e){
                console.log(e);
                lnks=[];
            }
        }).then(function(){console.log("lf fetch finished")});
    },

    dump: function () {
        console.log("writing localForage...");
        return lf.setItem("objs", JSON.stringify(objs)).then(function(){
            return lf.setItem("links", JSON.stringify(lnks)); // "object cannot be cloned"
        }).then(function(){console.log("lf commit finished")});
    },

    getLinks: function (fromId, type) {
        return lnks.filter(function (l) {
            return l.fromId === fromId && l.type === type;
        });
    },

    indexOfLink: function (fromId, type) {
        var i;
        for (i = 0; i < lnks.length; i += 1) {
            if (lnks[i].fromId === fromId && lnks[i].type === type) {
                return i;
            }
        }
        return -1;
    },


    removeLinks: function (fromId, linkName) {
        var index;
        while ((index = priv.indexOfLink(fromId, linkName)) !== -1) {
            lnks.splice(index, 1);
        }
    },

    addLink: function (fromId, toId, linkName) {
        lnks.push({
            fromId: fromId,
            toId: toId,
            type: linkName
        });
    },

    save: function (data) {
        objs[data.id] = $.extend({}, data);
    },

    getById: function (id) {
        if (!objs[id]) {
            return null;
        }
        return $.extend({}, objs[id]);
    },

    clear: function () {
        lnks = [];
        objs = {};
        return lf.setItem("objs", {}).then(function(){
            return lf.setItem("links", []);
        }).then(function(){console.log("lf erase finished")});
    }
}
var fake = {
    getById: priv.getById,
    save: priv.save,
    addLink: priv.addLink,
    removeLinks: priv.removeLinks,
    getLinks: priv.getLinks,
    clear: priv.clear,
    commit: priv.dump,
    init: priv.loadLocalForage
};

window.fakeDb = fake;

export = fake;

{'fraction_non_alphanumeric': 0.15729497478049692, 'fraction_numerical': 0.0018681113394358303, 'mean_word_length': 1.7755313634007257, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, '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': '3920535', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1596, 'n_words': 336}