<commit_before>/*
 * Copyright (c) 2015 <NAME>, Inc.
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *  
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

// using the Adobe asset exporter from https://github.com/adobe-photoshop/generator-assets
// as a base to build off of
(function () {
    "use strict";
    
    var DocumentManager = require("./lib/documentmanager"),
        StateManager = require('./lib/statemanager');

    var PLUGIN_ID = require("./package.json").name;
    
    var _generator = null,
        _currentDocumentId = null,
        _config = null,
        _logger = null,
        _documentManager = null,
        _stateManager = null;

    /*********** INIT ***********/

    function init(generator, config, logger) {
        _generator = generator;
        _config = config;
        _logger = logger;

        console.log("initializing DA-Export generator with config %j", _config);
        
        //the adobe document manager keeps an up to date version of the document DOM and applies documentChanged events as patches to keep up
        _documentManager = new DocumentManager(generator, config, logger);
        _documentManager.on("openDocumentsChanged", handleOpenDocumentsChanged);
        
        _stateManager = new StateManager(generator, config, logger, _documentManager);
        _stateManager.on("enabled", startAssetGeneration);
        _stateManager.on("disabled", pauseAssetGeneration);

        function initLater() {
            
          console.log("NEXT TICK");
          
          // _generator.onPhotoshopEvent("currentDocumentChanged", handleCurrentDocumentChanged);
          // _generator.onPhotoshopEvent("imageChanged", handleImageChanged);
          // _generator.onPhotoshopEvent("toolChanged", handleToolChanged);
          // requestEntireDocument();
            
        }
        
        process.nextTick(initLater);

    }  

    /*********** EVENTS ***********/
    
    //            DocumentManager Events
    //a lot of these handlers are pulled from the Adobe asset exporter, but I'm not down with _functionNames
    function handleOpenDocumentsChanged(all, opened) {
        var open = opened || all;

        open.forEach(function (id) {
            _documentManager.getDocument(id).done(function (document) {
                document.on("generatorSettings", handleDocGeneratorSettingsChange.bind(undefined, id));
            }, function (error) {
                _logger.warning("Error getting document during a document changed event, " +
                    "document was likely closed.", error);
            });
        });
    }
    function handleDocGeneratorSettingsChange(id, change) {
        var curSettings = getChangedSettings(change.current),
            prevSettings = getChangedSettings(change.previous),
            curEnabled = !!(curSettings && curSettings.enabled),
            prevEnabled = !!(prevSettings && prevSettings.enabled);
        
        if (prevEnabled !== curEnabled) {
            if (curEnabled) {
                _stateManager.activate(id);
            } else {
                _stateManager.deactivate(id);
            }
        }
    }
    function getChangedSettings(settings) {
        if (settings && typeof(settings) === "object") {
            return _generator.extractDocumentSettings({generatorSettings: settings}, PLUGIN_ID);
        }
        return null;
    }    
    
    
    //            StateManager Events
    function startAssetGeneration(id) {
        if (_waitingDocuments.hasOwnProperty(id)) {
            return;
        }

        var documentPromise = _documentManager.getDocument(id);
        
        _waitingDocuments[id] = documentPromise;

        documentPromise.done(function (document) {
            delete _waitingDocuments[id];

            if (_canceledDocuments.hasOwnProperty(id)) {
                delete _canceledDocuments[id];
            } else {
                if (!_assetManagers.hasOwnProperty(id)) {
                    _assetManagers[id] = new AssetManager(_generator, _config, _logger, document, _renderManager);

                    document.on("closed", stopAssetGeneration.bind(undefined, id));
                    document.on("end", restartAssetGeneration.bind(undefined, id));
                    document.on("file", handleFileChange.bind(undefined, id));
                }
                _assetManagers[id].start();
            }
        });
    }
<commit_msg>commented out a bit, now it works and saves enabled state<commit_after>/*
 * Copyright (c) 2015 <NAME>, Inc.
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *  
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

// using the Adobe asset exporter from https://github.com/adobe-photoshop/generator-assets
// as a base to build off of
(function () {
    "use strict";
    
    var DocumentManager = require("./lib/documentmanager"),
        StateManager = require('./lib/statemanager');

    var PLUGIN_ID = require("./package.json").name;
    
    var _generator = null,
        _currentDocumentId = null,
        _config = null,
        _logger = null,
        _documentManager = null,
        _stateManager = null;

    var _assetManagers = {};
    
    var _waitingDocuments = {},
        _canceledDocuments = {};

    /*********** INIT ***********/

    function init(generator, config, logger) {
        _generator = generator;
        _config = config;
        _logger = logger;

        console.log("initializing DA-Export generator with config %j", _config);
        
        //the adobe document manager keeps an up to date version of the document DOM and applies documentChanged events as patches to keep up
        _documentManager = new DocumentManager(generator, config, logger);
        _documentManager.on("openDocumentsChanged", handleOpenDocumentsChanged);
        
        _stateManager = new StateManager(generator, config, logger, _documentManager);
        _stateManager.on("enabled", startAssetGeneration);
        _stateManager.on("disabled", pauseAssetGeneration);

        function initLater() {
            
          console.log("NEXT TICK");
          
          // _generator.onPhotoshopEvent("currentDocumentChanged", handleCurrentDocumentChanged);
          // _generator.onPhotoshopEvent("imageChanged", handleImageChanged);
          // _generator.onPhotoshopEvent("toolChanged", handleToolChanged);
          // requestEntireDocument();
            
        }
        
        process.nextTick(initLater);

    }  

    /*********** EVENTS ***********/
    
    //            DocumentManager Events
    //a lot of these handlers are pulled from the Adobe asset exporter, but I'm not down with _functionNames
    function handleOpenDocumentsChanged(all, opened) {
        var open = opened || all;

        open.forEach(function (id) {
            _documentManager.getDocument(id).done(function (document) {
                document.on("generatorSettings", handleDocGeneratorSettingsChange.bind(undefined, id));
            }, function (error) {
                _logger.warning("Error getting document during a document changed event, " +
                    "document was likely closed.", error);
            });
        });
    }
    function handleDocGeneratorSettingsChange(id, change) {
        var curSettings = getChangedSettings(change.current),
            prevSettings = getChangedSettings(change.previous),
            curEnabled = !!(curSettings && curSettings.enabled),
            prevEnabled = !!(prevSettings && prevSettings.enabled);
        
        if (prevEnabled !== curEnabled) {
            if (curEnabled) {
                _stateManager.activate(id);
            } else {
                _stateManager.deactivate(id);
            }
        }
    }
    function getChangedSettings(settings) {
        if (settings && typeof(settings) === "object") {
            return _generator.extractDocumentSettings({generatorSettings: settings}, PLUGIN_ID);
        }
        return null;
    }    
    
    
    //            StateManager Events
    function startAssetGeneration(id) {
        if (_waitingDocuments.hasOwnProperty(id)) {
            return;
        }

        var documentPromise = _documentManager.getDocument(id);
        
        _waitingDocuments[id] = documentPromise;

        documentPromise.done(function (document) {
            delete _waitingDocuments[id];

            if (_canceledDocuments.hasOwnProperty(id)) {
                delete _canceledDocuments[id];
            } else {
                console.log("TODO: ASSET MANAGER");
                return;
                if (!_assetManagers.hasOwnProperty(id)) {
                    _assetManagers[id] = new AssetManager(_generator, _config, _logger, document, _renderManager);

                    document.on("closed", stopAssetGeneration.bind(undefined, id));
                    document.on("end", restartAssetGeneration.bind(undefined, id));
                    document.on("file", handleFileChange.bind(undefined, id));
                }
                _assetManagers[id].start();
            }
        });
    }

{'fraction_non_alphanumeric': 0.09607736804049169, 'fraction_numerical': 0.0007230657989877079, 'mean_word_length': 2.4246363354998453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3880182', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2632, 'n_words': 819}