Options
All
  • Public
  • Public/Protected
  • All
Menu

Provides wide range of functionality for file loading and event exchange. Throws standard error if failed to perform basic initializations, or system failure that cannot be reported otherwise has occured.

throws

Error

  • loader_failed - Loader did not construct the mandatory properties // TODO: @event module:system.private#events#systemLoad

Hierarchy

Index

Constructors

constructor

  • new System(__namedParameters: object): System
  • The constructor will perform necessary preparations, so that failures can be processed with system events. Up until these preparations are complete, the failure will result in thrown standard Error.

    Parameters

    • __namedParameters: object
      • behaviors: null | Behaviors

        Behaviors to add.

      • onError: null | ErrorCallback

        Callback for error handling during delayed execution after loader has loaded. Takes error string as an argument.

      • options: Options

        System options.

    Returns System

Properties

Private errors

errors: object

Error list.

Type declaration

  • [key: string]: object
    • Optional message?: undefined | string

      Error message.

Private private

Contains system info.

Protected protected

protected: object

Protected entrypoints.

Type declaration

public

public: object

Public entrypoints.

Type declaration

Private shared

shared: object

Contains data shared between the subsystems.

Type declaration

  • role: object
    • [key: string]: string

      Subsystem.

  • subsystem: object

Private subsystems

subsystems: any

Contains subsystem data.

Methods

Private addError

  • addError(code: string, message: string): void
  • Adds an error to the System dynamically

    instance
    fires

    module:system.private#events#errorExists

    example
    Usage code = "no_beakers" message = "Beakers out of stock." var options = { id: "lab_inventory", rootDir: "labs", relativeInitDir: "black_mesa", initFilename: "inventory.yml", logging: "console" }; var labInventory = new System(options);

    labInventory.addError(code, message);

    Parameters

    • code: string

      Error code

    • message: string

      Error description

    Returns void

Private on

  • on(event: string, callback: function): void
  • Adds a behavior bound to "this".

    instance
    example
    Usage var options = { id: "lab_inventory", rootDir: "labs", relativeInitDir: "black_mesa", initFilename: "inventory.yml", logging: "console" };

    var labInventory = new System(options); labInventory.on("system_load_aux", function(that){ console.log("Auxiliary system loaded - " + that.private.id); });

    Parameters

    • event: string

      Behavior name.

    • callback: function

      Behavior.

        • Parameters

          Returns void

    Returns void

Static error

  • error(text: string): void
  • Access stderr

    example
    Usage system.private.error("Not enough resources.");

    Parameters

    • text: string

    Returns void

Static getFile

  • getFile(rootDir: string, relativeDir: string, file: string): Promise<Buffer>
  • Gets file contents.

    Usage

    // Load files
    var grapefruitJuicer = Loader.getFile("c:\machines", "appliances", "grapefruitJuicer.txt");
    
    // Output the result
    grapefruitJuicer.then(function(result){ // grapefruitJuicer - on resolve
      console.log(result);
    }, function(error){ // grapefruitJuicer - on reject
      console.error("Could not load a file.");
    });
    
    // Input - grapefruitJuicer.txt
    // 1000W powerful juicer
    
    // Output
    // 1000W powerful juicer

    Parameters

    • rootDir: string

      Absolute root directory.

    • relativeDir: string

      Directory relative to root.

    • file: string

      Full file name.

    Returns Promise<Buffer>

    File contents.

Static isDir

  • isDir(rootDir: string, relativeDir: string): Promise<boolean>
  • Checks if is a directory.

    Usage

    // Verify directory
    Loader.isDir("c:\machines\appliances","grapefruitJuicer.txt").then(function(result){
      console.log(result);
    });
    
    // Input - grapefruitJuicer.txt
    // 1000W powerful juicer
    
    // Output
    // false

    Parameters

    • rootDir: string

      Absolute root directory.

    • relativeDir: string

      Relative directory to root.

    Returns Promise<boolean>

    Returns true if a directory, false if not.

Static isFile

  • isFile(rawPath: string): Promise<boolean>
  • Checks if is a file

    Usage

    // Verify file
    Loader.isFile("c:\machines\appliances\grapefruitJuicer.txt").then(function(result){
      console.log(result);
    });
    
    // Input - grapefruitJuicer.txt
    // 1000W powerful juicer
    
    // Output
    // true

    Parameters

    • rawPath: string

      Full filepath.

    Returns Promise<boolean>

    Returns true if a file, false if not.

Static join

  • join(...pathArrays: Array<string | Array<string>>): string | Array<string>
  • Join a root directory with a file/folder or an array of files/folders to absolute path.

    Usage

    // Join and log result
    console.log(Loader.join("c:\machines", "appliances"))
    
    // Output
    // c:\machines\appliances

    Parameters

    • Rest ...pathArrays: Array<string | Array<string>>

      File/folder name|names.

    Returns string | Array<string>

    Absolute path|paths.

Static list

  • list(rootDir: string, relativeDir: string): Promise<Array<string>>
  • Returns an array of strings, representing the contents of a folder.

    Usage

    // List directory contents
    Loader.list("c:","machines").then(function(result){
      console.log(result);
    }, function(error){
      console.error("Folder not found.");
    });
    
    // Output
    // ["machines", "appliances"]

    Parameters

    • rootDir: string

      Root directory.

    • relativeDir: string

      Relative directory.

    Returns Promise<Array<string>>

    Array with contents; Rejects with errors from fs.readdir.

Static log

  • log(text: string): void
  • Access stdout

    example
    Usage system.private.log("Resources loaded.");

    Parameters

    • text: string

    Returns void

Static toRelative

  • toRelative(dir: string, target: string | Array<string>): string | Array<string>
  • Extracts relative path from rootDir to target.

    Usage

    // Convert path and output the result
    console.log(Loader.toRelative("c:\machines\refrigerators", "c:\machines\appliances"));
    
    // Output
    // ..\appliances

    Parameters

    • dir: string

      Source folder.

    • target: string | Array<string>

      File/folder name|names.

    Returns string | Array<string>

    Relative path|paths.

Static yamlToObject

  • yamlToObject(string: string): any
  • Converts YAML string to a JS object.

    Usage

    // Ouput conversion of YAML to JSON
    console.log(Loader.yamlToObject("Wine: Red"));
    
    // Output
    // {"Wine": "Red"}

    Parameters

    • string: string

      YAML string.

    Returns any

    Javascript object.

Generated using TypeDoc