Home Reference Source Test Repository
import CompileCache from 'electron-compile/src/compile-cache.js'
public class | source

CompileCache

CompileCache manages getting and setting entries for a single compiler; each in-use compiler will have an instance of this class, usually created via createFromCompiler.

You usually will not use this class directly, it is an implementation class for CompileHost.

Static Method Summary

Static Public Methods
public static

createFromCompiler(cachePath: string, compiler: CompilerBase, fileChangeCache: FileChangedCache): CompileCache

Creates a CompileCache from a class compatible with the CompilerBase interface.

public static

Returns whether a file should not be compiled.

Constructor Summary

Public Constructor
public

constructor(cachePath: string, fileChangeCache: FileChangedCache)

Creates an instance, usually used for testing only.

Member Summary

Public Members
public
public

Method Summary

Public Methods
public

get(filePath: string): Object

Returns a file's compiled contents from the cache.

public

getOrFetch(filePath: string, fetcher: Function): Object

Attempts to first get a key via get, then if it fails, call a method to retrieve the contents, then save the result to cache.

public

getOrFetchSync(filePath: string, fetcher: Function): Object

Attempts to first get a key via get, then if it fails, call a method to retrieve the contents, then save the result to cache.

public

getSync(filePath: string): Object

Returns a file's compiled contents from the cache.

public

save(hashInfo: Object, codeOrBinaryData: string / Buffer, mimeType: string, dependentFiles: string[]): Promise

Saves a compiled result to cache

public

saveSync(hashInfo: Object, codeOrBinaryData: string / Buffer, mimeType: string, dependentFiles: string[]): Promise

Saves a compiled result to cache

Static Public Methods

public static createFromCompiler(cachePath: string, compiler: CompilerBase, fileChangeCache: FileChangedCache): CompileCache source

Creates a CompileCache from a class compatible with the CompilerBase interface. This method uses the compiler name / version / options to generate a unique directory name for cached results

Params:

NameTypeAttributeDescription
cachePath string

The root path to use for the cache, a directory representing the hash of the compiler parameters will be created here.

compiler CompilerBase

The compiler to use for version / option information.

fileChangeCache FileChangedCache

A file-change cache that is optionally pre-loaded.

Return:

CompileCache

A configured CompileCache instance.

public static shouldPassthrough(hashInfo: Object): boolean source

Returns whether a file should not be compiled. Note that this doesn't necessarily mean it won't end up in the cache, only that its contents are saved verbatim instead of trying to find an appropriate compiler.

Params:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

Return:

boolean

True if a file should be ignored

Public Constructors

public constructor(cachePath: string, fileChangeCache: FileChangedCache) source

Creates an instance, usually used for testing only.

Params:

NameTypeAttributeDescription
cachePath string

The root directory to use as a cache path

fileChangeCache FileChangedCache

A file-change cache that is optionally pre-loaded.

Public Members

public cachePath: * source

public fileChangeCache: * source

Public Methods

public get(filePath: string): Object source

Returns a file's compiled contents from the cache.

Params:

NameTypeAttributeDescription
filePath string

The path to the file. FileChangedCache will look up the hash and use that as the key in the cache.

Return:

Object

An object with all kinds of information

Return Properties:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

code string

The source code if the file was a text file

binaryData Buffer

The file if it was a binary file

mimeType string

The MIME type saved in the cache.

dependentFiles string[]

The dependent files returned from compiling the file, if any.

public getOrFetch(filePath: string, fetcher: Function): Object source

Attempts to first get a key via get, then if it fails, call a method to retrieve the contents, then save the result to cache.

The fetcher parameter is expected to have the signature:

Object fetcher(filePath : string, hashInfo : Object);

hashInfo is a value returned from getHashForPath The return value of fetcher must be an Object with the properties:

mimeType - the MIME type of the data to save code (optional) - the source code as a string, if file is text binaryData (optional) - the file contents as a Buffer, if file is binary dependentFiles - the dependent files returned by the compiler.

Params:

NameTypeAttributeDescription
filePath string

The path to the file. FileChangedCache will look up the hash and use that as the key in the cache.

fetcher Function

A method which conforms to the description above.

Return:

Object

An Object which has the same fields as the get method return result.

public getOrFetchSync(filePath: string, fetcher: Function): Object source

Attempts to first get a key via get, then if it fails, call a method to retrieve the contents, then save the result to cache.

The fetcher parameter is expected to have the signature:

Object fetcher(filePath : string, hashInfo : Object);

hashInfo is a value returned from getHashForPath The return value of fetcher must be an Object with the properties:

mimeType - the MIME type of the data to save code (optional) - the source code as a string, if file is text binaryData (optional) - the file contents as a Buffer, if file is binary dependentFiles - the dependent files returned by the compiler.

Params:

NameTypeAttributeDescription
filePath string

The path to the file. FileChangedCache will look up the hash and use that as the key in the cache.

fetcher Function

A method which conforms to the description above.

Return:

Object

An Object which has the same fields as the get method return result.

public getSync(filePath: string): Object source

Returns a file's compiled contents from the cache.

Params:

NameTypeAttributeDescription
filePath string

The path to the file. FileChangedCache will look up the hash and use that as the key in the cache.

Return:

Object

An object with all kinds of information

Return Properties:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

code string

The source code if the file was a text file

binaryData Buffer

The file if it was a binary file

mimeType string

The MIME type saved in the cache.

dependentFiles string[]

The dependent files returned from compiling the file, if any.

public save(hashInfo: Object, codeOrBinaryData: string / Buffer, mimeType: string, dependentFiles: string[]): Promise source

Saves a compiled result to cache

Params:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

codeOrBinaryData string / Buffer

The file's contents, either as a string or a Buffer.

mimeType string

The MIME type returned by the compiler.

dependentFiles string[]

The list of dependent files returned by the compiler.

Return:

Promise

Completion.

public saveSync(hashInfo: Object, codeOrBinaryData: string / Buffer, mimeType: string, dependentFiles: string[]): Promise source

Saves a compiled result to cache

Params:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

codeOrBinaryData string / Buffer

The file's contents, either as a string or a Buffer.

mimeType string

The MIME type returned by the compiler.

dependentFiles string[]

The list of dependent files returned by the compiler.

Return:

Promise

Completion.