-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {type AlwatrLogger, createLogger} from '@alwatr/logger'; | ||
|
||
/** | ||
* AlpineStore Options. | ||
* | ||
* @template T - The type of the store value. | ||
* @param {string} name - The name of the store. | ||
* @param {T} defaultValue - The default value of the store. | ||
*/ | ||
export type AlpineStoreOptions<T extends DictionaryReq> = { | ||
name: string; | ||
defaultValue: T; | ||
}; | ||
|
||
/** | ||
* Provides a Alpine.js pure store implementation with logger. | ||
*/ | ||
export class AlpineStore<T extends DictionaryReq> { | ||
/** | ||
* The store's data. | ||
*/ | ||
store: T; | ||
|
||
protected logger_: AlwatrLogger; | ||
|
||
/** | ||
* Provides a Alpine.js pure store implementation with logger. | ||
* | ||
* @param {AlpineStoreOptions} config - Configuration object. | ||
* | ||
* @example | ||
* import {AlpineStore} from '@nexim/alpine'; | ||
* | ||
* const {store} = new AlpineStore({ | ||
* name: 'myStore', | ||
* defaultValue: {data: 'root'}, | ||
* }); | ||
* console.log(store.data); // Output: { data: 'root' } | ||
* | ||
* store.data = 'user'; | ||
* console.log(store.data); // Output: { data: 'user' } | ||
*/ | ||
constructor(config: AlpineStoreOptions<T>) { | ||
this.logger_ = createLogger(`${__package_name__}:${config.name}`); | ||
this.logger_.logMethodArgs?.('constructor', config); | ||
|
||
this.store = config.defaultValue; | ||
} | ||
} |