Interface: ILoaderStrategy<T>
An abstracted loader strategy interface.
Type Parameters
Type Parameter |
---|
T extends Piece |
Methods
filter()
filter(
path
:string
):FilterResult
Retrieves the name and the extension of the specified file path.
Parameters
Parameter | Type | Description |
---|---|---|
path | string | The path of the file to be processed. |
Returns
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 };
}
}
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:81
load()
load(
store
:Store
<T
,never
>,file
:HydratedModuleData
):ILoaderResult
<T
>
The load hook, use this to override the loader.
Parameters
Parameter | Type |
---|---|
store | Store <T , never > |
file | HydratedModuleData |
Returns
Example
class MyStrategy extends LoaderStrategy {
load(store, file) {
// ...
}
}
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:117
onError()
onError(
error
:Error
,path
:string
):void
Parameters
Parameter | Type | Description |
---|---|---|
error | Error | The error that was thrown. |
path | string | The path of the file that caused the error to be thrown. |
Returns
void
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:149
onLoad()
onLoad(
store
:Store
<T
,never
>,piece
:T
):unknown
Called after a piece has been loaded, but before Piece.onLoad and Store.set.
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store that holds the piece. |
piece | T | The piece that was loaded. |
Returns
unknown
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:124
onLoadAll()
onLoadAll(
store
:Store
<T
,never
>):unknown
Called after all pieces have been loaded.
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store that loaded all pieces. |
Returns
unknown
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:130
onUnload()
onUnload(
store
:Store
<T
,never
>,piece
:T
):unknown
Called after a piece has been unloaded or overwritten by a newly loaded piece.
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store that held the piece. |
piece | T | The piece that was unloaded. |
Returns
unknown
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:137
onUnloadAll()
onUnloadAll(
store
:Store
<T
,never
>):unknown
Called after all pieces have been unloaded.
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store that unloaded all pieces. |
Returns
unknown
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:143
preload()
preload(
file
:ModuleData
):PreloadResult
<T
>
The pre-load hook, use this to override the loader.
Parameters
Parameter | Type |
---|---|
file | ModuleData |
Returns
Examples
// CommonJS support:
class MyStrategy extends LoaderStrategy {
preload(path) {
return require(path);
}
}
// ESM support:
class MyStrategy extends LoaderStrategy {
preload(file) {
return import(file.path);
}
}
Defined in
projects/pieces/src/lib/strategies/ILoaderStrategy.ts:104
walk()?
optional
walk(store
:Store
<T
,never
>,path
:string
,logger
?:null
|StoreLogger
):AsyncIterableIterator
<string
>
Walks the specified path and returns an async iterator of all the files' paths.
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store that is walking the path. |
path | string | The path to recursively walk. |
logger ? | null | StoreLogger | The logger to use when walking the path, if any. |
Returns
AsyncIterableIterator
<string
>