<commit_before>/* Copyright 2015 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

'use strict';

var fs = require('fs');

var debug = require('debug')('wall:library');
var Module = require('./module_defs');

var ModuleDefinitionLibrary = function() {
  // map of path -> promise<string>
  // A key only exists during the loading process.
  this.moduleLoader = {};

  // map of path -> string
  // Only valid modules are stored here.
  this.modules = {};
};
ModuleDefinitionLibrary.prototype.load = function(path) {
  if (path in this.moduleLoader) {
    // In the middle of loading... so join.
    return this.moduleLoader[path];
  }

  var loadModule = () => {
    var rewatch = () => {
      // Clean up in-progress module loading.
      delete this.moduleLoader[path];

      // Watch for future changes.
      var watch = fs.watch(path, {persistent: true}, (event) => {
        debug('Module changed! Reloading', path);
        watch.close();
        loadModule();
      });
    };
    
    var m = Module.loadModuleAtPath(path).then((def) => {
      debug('Loaded', path);
      
      // Save the updated, valid module.
      this.modules[path] = def;
      
      rewatch();
      
      return def;
    }, (e) => {
      debug('Error loading ' + path);
      debug(e);
      
      rewatch();

      return Promise.reject(e);
    });
    
    this.moduleLoader[path] = m;
    return m;
  };

  return loadModule();
};
module.exports = new ModuleDefinitionLibrary;<commit_msg>Convert ModuleDefinitionLibrary to an es6 class.<commit_after>/* Copyright 2015 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

'use strict';

const fs = require('fs');

const debug = require('debug')('wall:library');
const Module = require('server/modules/module_defs');

class ModuleDefinitionLibrary {
  constructor() {
    // map of path -> promise<string>
    // A key only exists during the loading process.
    this.moduleLoader = {};

    // map of path -> string
    // Only valid modules are stored here.
    this.modules = {};
  }
  load(path) {
    if (path in this.moduleLoader) {
      // In the middle of loading... so join.
      return this.moduleLoader[path];
    }

    let loadModule = () => {
      let rewatch = () => {
        // Clean up in-progress module loading.
        delete this.moduleLoader[path];

        // Watch for future changes.
        let watch = fs.watch(path, {persistent: true}, (event) => {
          debug('Module changed! Reloading', path);
          watch.close();
          loadModule();
        });
      };
    
      let m = Module.loadModuleAtPath(path).then((def) => {
        debug('Loaded', path);
      
        // Save the updated, valid module.
        this.modules[path] = def;
      
        rewatch();
      
        return def;
      }, (e) => {
        debug('Error loading ' + path);
        debug(e);
      
        rewatch();

        return Promise.reject(e);
      });
    
      this.moduleLoader[path] = m;
      return m;
    };

    return loadModule();
  }
}

// Export the singleton instance of the library.
module.exports = new ModuleDefinitionLibrary;
{'fraction_non_alphanumeric': 0.14857941313460643, 'fraction_numerical': 0.0039590125756870055, 'mean_word_length': 2.708981001727116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5799576', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1123, 'n_words': 406}