<commit_before>package pman.edb;

import tannus.io.*;
import tannus.ds.*;
import tannus.ds.promises.*;
import tannus.async.*;
import tannus.sys.*;
import tannus.sys.FileSystem as Fs;

import pman.Paths;
import pman.ds.OnceSignal as ReadySignal;
import pman.core.ApplicationState;
import pman.Globals.*;

import edis.libs.nedb.DataStore;

import pman.bg.db.*;
import pman.bg.Dirs;

import Slambda.fn;
import tannus.math.TMath.*;
import haxe.extern.EitherType;

using StringTools;
using tannus.ds.StringUtils;
using Lambda;
using tannus.ds.ArrayTools;
using Slambda;
using tannus.async.VoidAsyncs;

class PManDatabase extends Database {
    /* Constructor Function */
    public function new():Void {
        super();

        //path = (Paths.userData().plusString('pmdb'));
        path = Dirs.dbPath();
        ops = new Operators();

        if (instance == null) {
            instance = this;
        }
    }

/* === Instance Methods === */

    override function init(?done : VoidCb):Void {
        if (!Fs.exists( path )) {
            Fs.createDirectory( path );
        }

        if (done == null)
            done = VoidCb.noop;
        done = done.wrap(function(_done, ?error) {
            //TODO
            _done( error );
        });

        super.init( done );
    }

    /**
      * do the stuff
      */
    override function _build():Void {
        super._build();

        require(function(next) {
            defer(function() {
                mediaStore = media;
                actorStore = actors;

                next();
            });
        });

        require(function(next) {
            defer(function() {
                configInfo = new ConfigInfo();
                preferences = new Preferences();

                next();
            });
        });

        require(function(next) {
            defer(function() {
                appState = new ApplicationState();

                next();
            });
        });
    }

    /**
      * create a TableWrapper
      */
    /*
    private function wrap<T:TableWrapper>(name:String, ?type:Class<T>):T {
        if (type == null) {
            type = untyped TableWrapper;
        }
        if (!name.endsWith('.db')) {
            name += '.db';
        }
        var store:DataStore = new DataStore({
            filename: dsfilename( name ),
            afterSerialization: afterSerialization,
            beforeDeserialization: beforeDeserialization
        });
        return Type.createInstance(type, untyped [store]);
    }
    */

    /**
      * alter the serialization of documents
      */
    private function afterSerialization(data : String):String {
        return data;
    }

    /**
      * alter the deserialization of a document
      */
    private function beforeDeserialization(data : String):String {
        return data;
    }

    /**
      * queue [action] for when [this] database has been declared 'ready'
      */
    //public function onready(action : Void->Void):Void rs.await( action );

    /**
      * get the full path to the DataStore file
      */
    //public function dsfilename(name : String):String {
        //return Std.string(path.plusString( name ).normalize());
    //}

    public static function get(?safeAction: PManDatabase->Void):PManDatabase {
        var res = instance;
        if (res == null) {
            res = new PManDatabase();
        }
        if (safeAction != null) {
            res.onready(safeAction.bind(res));
        }
        return res;
    }

    public static var instance:Null<PManDatabase> = null;

/* === Instance Fields === */

    public var path: Path;
    public var ops : Operators;
    public var mediaStore : MediaStore;
    public var actorStore : ActorStore;

    public var configInfo : ConfigInfo;
    public var preferences : Preferences;
    public var appState : ApplicationState;
    
    //private var rs : ReadySignal;
}
<commit_msg>PManDatabase now actually initializes the AppState<commit_after>package pman.edb;

import tannus.io.*;
import tannus.ds.*;
import tannus.ds.promises.*;
import tannus.async.*;
import tannus.sys.*;
import tannus.sys.FileSystem as Fs;

import pman.Paths;
import pman.ds.OnceSignal as ReadySignal;
import pman.core.ApplicationState;
import pman.Globals.*;

import edis.libs.nedb.DataStore;

import pman.bg.db.*;
import pman.bg.Dirs;

import Slambda.fn;
import tannus.math.TMath.*;
import haxe.extern.EitherType;

using StringTools;
using tannus.ds.StringUtils;
using Lambda;
using tannus.ds.ArrayTools;
using Slambda;
using tannus.async.VoidAsyncs;

class PManDatabase extends Database {
    /* Constructor Function */
    public function new():Void {
        super();

        //path = (Paths.userData().plusString('pmdb'));
        path = Dirs.dbPath();
        ops = new Operators();

        if (instance == null) {
            instance = this;
        }
    }

/* === Instance Methods === */

    override function init(?done : VoidCb):Void {
        if (!Fs.exists( path )) {
            Fs.createDirectory( path );
        }

        if (done == null)
            done = VoidCb.noop;
        done = done.wrap(function(_done, ?error) {
            //TODO
            _done( error );
        });

        super.init( done );
    }

    /**
      * build out [this]'s requirements
      */
    override function _build():Void {
        super._build();

        require(function(next) {
            defer(function() {
                mediaStore = media;
                actorStore = actors;

                next();
            });
        });

        require(function(next) {
            defer(function() {
                configInfo = new ConfigInfo();
                preferences = new Preferences();

                next();
            });
        });

        require(function(next) {
            appState = new ApplicationState();

            appState.initialize(next.wrap(function(_, ?error) {
                if (error != null) {
                    report( error );
                }
                _( error );
            }));
        });
    }

    /**
      * create a TableWrapper
      */
    /*
    private function wrap<T:TableWrapper>(name:String, ?type:Class<T>):T {
        if (type == null) {
            type = untyped TableWrapper;
        }
        if (!name.endsWith('.db')) {
            name += '.db';
        }
        var store:DataStore = new DataStore({
            filename: dsfilename( name ),
            afterSerialization: afterSerialization,
            beforeDeserialization: beforeDeserialization
        });
        return Type.createInstance(type, untyped [store]);
    }
    */

    /**
      * alter the serialization of documents
      */
    private function afterSerialization(data : String):String {
        return data;
    }

    /**
      * alter the deserialization of a document
      */
    private function beforeDeserialization(data : String):String {
        return data;
    }

    /**
      * queue [action] for when [this] database has been declared 'ready'
      */
    //public function onready(action : Void->Void):Void rs.await( action );

    /**
      * get the full path to the DataStore file
      */
    //public function dsfilename(name : String):String {
        //return Std.string(path.plusString( name ).normalize());
    //}

    public static function get(?safeAction: PManDatabase->Void):PManDatabase {
        var res = instance;
        if (res == null) {
            res = new PManDatabase();
        }
        if (safeAction != null) {
            res.onready(safeAction.bind(res));
        }
        return res;
    }

    public static var instance:Null<PManDatabase> = null;

/* === Instance Fields === */

    public var path: Path;
    public var ops : Operators;
    public var mediaStore : MediaStore;
    public var actorStore : ActorStore;

    public var configInfo : ConfigInfo;
    @:deprecated('PManDatabase.preferences is deprecated in favor of ApplicationState')
    public var preferences : Preferences;
    public var appState : ApplicationState;
    
    //private var rs : ReadySignal;
}

{'fraction_non_alphanumeric': 0.12000493339911199, 'fraction_numerical': 0.0, 'mean_word_length': 1.9095801937567276, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '898628', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2251, 'n_words': 561}