<commit_before>/**
 * Data model provider.
 *
 * @package appmaker
 * @author  <NAME> <<EMAIL>>
 */

// Temp dev solution
var MAKEDRIVE_URL = 'ws://makedrive.alicoding.com?username=wmobiledev';

var MakeDrive = require('makedrive');
var watch = require('watchjs').watch;
var utils = require('./utils');

/**
 * Constructor
 */
function Model (options) {
    var self = this;
    console.log('hi');

    if (!(self instanceof Model)) return new Model();

    // Options
    // memory: (false) Use memory provider for Makedrive
    // noConnect: () if true, will not connect to remove server
    options = options || {};

    // Internal
    self._ns = '/_model';
    self._ready = false;
    self._logger = function (prefix, msg) {
        if (typeof msg === 'undefined') {
            msg = prefix;
            prefix = 'Model';
        }

        if (msg) console.log('[' + prefix + '] ' + msg);
    };

    // Makedrive
    self._fs = MakeDrive.fs({
        memory: options.memory,
        interval: 20000
    });
    self._sync = self._fs.sync;
    if (!options.noConnect) {
        self._sync.connect(MAKEDRIVE_URL);
    }
    self._sync.on('connected', function() {
        self._logger('Makedrive connected');
    });
    self._sync.on('error', self._logger);
    self._sync.on('completed', function() {
        self._logger('Sync Completed');
    });
    self._sync.on('syncing', function() {
        self._logger('Syncing');
    });

    // Public
    self.history = {
        ftu: true,
        path: '/ftu'
    };
    self.locale = null;
    self.user = {
        name: null,
        location: null,
        avatar: null
    };
    self.apps = [
        {
            id: '000d1745-5d3c-4997-ac0c-15df68bbbecz',
            name: 'Sample App',
            icon: '/images/placeholder_puppy.png',
            author: {
                name: 'Andrew',
                location: 'Portland',
                avatar: '/images/avatar_puppy.png'
            },
            blocks: []
        }
    ];
}

/**
 * Restores the model from localforage.
 *
 * @param  {Function} callback
 *
 * @return {void}
 */
Model.prototype.restore = function (callback) {
    var self = this;
    self._fs.readFile(self._ns, function (err, item) {
        if (err && err.code === 'ENOENT') {
            self._logger('No saved file found.');
            self.observe();
            return callback(null);
        }

        if (!item) {
            self._logger('File was empty');
            return callback(null);
        }
        item = JSON.parse(item);

        if (typeof item.history !== 'undefined') self.history = item.history;
        if (typeof item.user !== 'undefined') self.user = item.user;
        if (typeof item.apps !== 'undefined') self.apps = item.apps;
        if (typeof item.locale !== 'undefined') self.locale = item.locale;

        self.observe();
        self._logger('Data restored');

        callback(null);
    });
};

/**
 * Saves the current model state to localforage.
 *
 * @param  {Function} callback
 *
 * @return {void}
 */
Model.prototype.save = function (callback) {
    var self = this;
    callback = callback || function (){};

    var item = {
        history: self.history,
        user: self.user,
        apps: self.apps,
        locale: self.locale,
    };
    putItem = JSON.stringify(item);
    self._fs.writeFile(self._ns, putItem, function (item) {
        self._logger('Data saved');
        callback(null);
    });
};

/**
 * Starts observing ("watch"-ing) for object changes.
 *
 * @return {void}
 */
Model.prototype.observe = function () {
    var self = this;

    watch(self.history, function () {
        self.save(self._logger);
    }, 1);

    watch(self.user, function () {
        self.save(self._logger);
    }, 1);

    watch(self.locale, function () {
        self.save(self._logger);
    }, 1);

    watch(self.apps, function (path) {
        self.save(self._logger);
    }, 10);

    self._ready = true;
};

var model;

function instantiateModel (options) {
    // If the instance doesn't exist, create it.
    if (!model) model = new Model(options);
    return model;
}

module.exports = instantiateModel;
<commit_msg>console.log<commit_after>/**
 * Data model provider.
 *
 * @package appmaker
 * @author  <NAME> <<EMAIL>>
 */

// Temp dev solution
var MAKEDRIVE_URL = 'ws://makedrive.alicoding.com?username=wmobiledev';

var MakeDrive = require('makedrive');
var watch = require('watchjs').watch;
var utils = require('./utils');

/**
 * Constructor
 */
function Model (options) {
    var self = this;

    if (!(self instanceof Model)) return new Model();

    // Options
    // memory: (false) Use memory provider for Makedrive
    // noConnect: () if true, will not connect to remove server
    options = options || {};

    // Internal
    self._ns = '/_model';
    self._ready = false;
    self._logger = function (prefix, msg) {
        if (typeof msg === 'undefined') {
            msg = prefix;
            prefix = 'Model';
        }

        if (msg) console.log('[' + prefix + '] ' + msg);
    };

    // Makedrive
    self._fs = MakeDrive.fs({
        memory: options.memory,
        interval: 20000
    });
    self._sync = self._fs.sync;
    if (!options.noConnect) {
        self._sync.connect(MAKEDRIVE_URL);
    }
    self._sync.on('connected', function() {
        self._logger('Makedrive connected');
    });
    self._sync.on('error', self._logger);
    self._sync.on('completed', function() {
        self._logger('Sync Completed');
    });
    self._sync.on('syncing', function() {
        self._logger('Syncing');
    });

    // Public
    self.history = {
        ftu: true,
        path: '/ftu'
    };
    self.locale = null;
    self.user = {
        name: null,
        location: null,
        avatar: null
    };
    self.apps = [
        {
            id: '000d1745-5d3c-4997-ac0c-15df68bbbecz',
            name: 'Sample App',
            icon: '/images/placeholder_puppy.png',
            author: {
                name: 'Andrew',
                location: 'Portland',
                avatar: '/images/avatar_puppy.png'
            },
            blocks: []
        }
    ];
}

/**
 * Restores the model from localforage.
 *
 * @param  {Function} callback
 *
 * @return {void}
 */
Model.prototype.restore = function (callback) {
    var self = this;
    self._fs.readFile(self._ns, function (err, item) {
        if (err && err.code === 'ENOENT') {
            self._logger('No saved file found.');
            self.observe();
            return callback(null);
        }

        if (!item) {
            self._logger('File was empty');
            return callback(null);
        }
        item = JSON.parse(item);

        if (typeof item.history !== 'undefined') self.history = item.history;
        if (typeof item.user !== 'undefined') self.user = item.user;
        if (typeof item.apps !== 'undefined') self.apps = item.apps;
        if (typeof item.locale !== 'undefined') self.locale = item.locale;

        self.observe();
        self._logger('Data restored');

        callback(null);
    });
};

/**
 * Saves the current model state to localforage.
 *
 * @param  {Function} callback
 *
 * @return {void}
 */
Model.prototype.save = function (callback) {
    var self = this;
    callback = callback || function (){};

    var item = {
        history: self.history,
        user: self.user,
        apps: self.apps,
        locale: self.locale,
    };
    putItem = JSON.stringify(item);
    self._fs.writeFile(self._ns, putItem, function (item) {
        self._logger('Data saved');
        callback(null);
    });
};

/**
 * Starts observing ("watch"-ing) for object changes.
 *
 * @return {void}
 */
Model.prototype.observe = function () {
    var self = this;

    watch(self.history, function () {
        self.save(self._logger);
    }, 1);

    watch(self.user, function () {
        self.save(self._logger);
    }, 1);

    watch(self.locale, function () {
        self.save(self._logger);
    }, 1);

    watch(self.apps, function (path) {
        self.save(self._logger);
    }, 10);

    self._ready = true;
};

var model;

function instantiateModel (options) {
    // If the instance doesn't exist, create it.
    if (!model) model = new Model(options);
    return model;
}

module.exports = instantiateModel;

{'fraction_non_alphanumeric': 0.1551123393007329, 'fraction_numerical': 0.00672834314550042, 'mean_word_length': 2.0761271249076128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5469427', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2528, 'n_words': 593}