Skip to main content

Interface: ILoaderStrategy<T>

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:63

An abstracted loader strategy interface.

Type Parameters

Type Parameter
T extends Piece

Methods

filter()

filter(path: string): FilterResult

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:81

Retrieves the name and the extension of the specified file path.

Parameters

ParameterTypeDescription
pathstringThe path of the file to be processed.

Returns

FilterResult

A ModuleData on success, otherwise null to stop the store from processing the path.

Example

// ts-node support
class MyStrategy extends LoaderStrategy {
filter(path) {
const extension = extname(path);
if (!['.js', '.ts'].includes(extension)) return null;
const name = basename(path, extension);
return { extension, name };
}
}

load()

load(store: Store<T>, file: HydratedModuleData): ILoaderResult<T>

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:117

The load hook, use this to override the loader.

Parameters

ParameterType
storeStore<T>
fileHydratedModuleData

Returns

ILoaderResult<T>

Example

class MyStrategy extends LoaderStrategy {
load(store, file) {
// ...
}
}

onError()

onError(error: Error, path: string): void

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:149

Parameters

ParameterTypeDescription
errorErrorThe error that was thrown.
pathstringThe path of the file that caused the error to be thrown.

Returns

void


onLoad()

onLoad(store: Store<T>, piece: T): unknown

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:124

Called after a piece has been loaded, but before Piece.onLoad and Map.set.

Parameters

ParameterTypeDescription
storeStore<T>The store that holds the piece.
pieceTThe piece that was loaded.

Returns

unknown


onLoadAll()

onLoadAll(store: Store<T>): unknown

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:130

Called after all pieces have been loaded.

Parameters

ParameterTypeDescription
storeStore<T>The store that loaded all pieces.

Returns

unknown


onUnload()

onUnload(store: Store<T>, piece: T): unknown

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:137

Called after a piece has been unloaded or overwritten by a newly loaded piece.

Parameters

ParameterTypeDescription
storeStore<T>The store that held the piece.
pieceTThe piece that was unloaded.

Returns

unknown


onUnloadAll()

onUnloadAll(store: Store<T>): unknown

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:143

Called after all pieces have been unloaded.

Parameters

ParameterTypeDescription
storeStore<T>The store that unloaded all pieces.

Returns

unknown


preload()

preload(file: ModuleData): PreloadResult<T>

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:104

The pre-load hook, use this to override the loader.

Parameters

ParameterType
fileModuleData

Returns

PreloadResult<T>

Examples

// CommonJS support:
class MyStrategy extends LoaderStrategy {
preload(path) {
return require(path);
}
}
// ESM support:
class MyStrategy extends LoaderStrategy {
preload(file) {
return import(file.path);
}
}

walk()?

optional walk(store: Store<T>, path: string, logger?: null | StoreLogger): AsyncIterableIterator<string>

Defined in: projects/pieces/src/lib/strategies/ILoaderStrategy.ts:157

Walks the specified path and returns an async iterator of all the files' paths.

Parameters

ParameterTypeDescription
storeStore<T>The store that is walking the path.
pathstringThe path to recursively walk.
logger?null | StoreLoggerThe logger to use when walking the path, if any.

Returns

AsyncIterableIterator<string>