Compare commits

...

15 Commits

Author SHA1 Message Date
a0189921a5 2.0.19 2024-10-02 17:49:56 +02:00
b409f1aa55 fix(dependencies): Update dependencies to latest versions 2024-10-02 17:49:56 +02:00
57553aa7c8 2.0.18 2024-10-02 15:59:41 +02:00
c302cbdae4 fix(core): Fix type errors and typos in Smartstate class 2024-10-02 15:59:41 +02:00
fd6e3ba8b1 update description 2024-05-29 14:16:33 +02:00
8c3a7aa637 update tsconfig 2024-04-14 18:24:08 +02:00
8c0125a8c0 update tsconfig 2024-04-01 21:41:21 +02:00
40f3ffbdfb update npmextra.json: githost 2024-04-01 19:59:48 +02:00
d4e01a73d3 update npmextra.json: githost 2024-03-30 21:48:48 +01:00
641e77892a 2.0.17 2023-10-07 12:23:03 +02:00
0c1211778b fix(core): update 2023-10-07 12:23:03 +02:00
024f7f4f8f 2.0.16 2023-10-04 07:59:13 +02:00
8f1cba5078 fix(core): update 2023-10-04 07:59:12 +02:00
188f8057bf 2.0.15 2023-10-03 19:19:54 +02:00
99cb86258e fix(core): update 2023-10-03 19:19:54 +02:00
9 changed files with 5369 additions and 3882 deletions

149
changelog.md Normal file
View File

@ -0,0 +1,149 @@
# Changelog
## 2024-10-02 - 2.0.19 - fix(dependencies)
Update dependencies to latest versions
- Updated @git.zone/tsbuild to version ^2.1.84
- Updated @git.zone/tsbundle to version ^2.0.15
- Updated @git.zone/tsrun to version ^1.2.49
- Updated @git.zone/tstest to version ^1.0.90
- Updated @push.rocks/tapbundle to version ^5.3.0
- Updated @types/node to version ^22.7.4
- Updated @push.rocks/lik to version ^6.0.15
- Updated @push.rocks/smartjson to version ^5.0.20
- Updated @push.rocks/smartpromise to version ^4.0.4
- Updated @push.rocks/smartrx to version ^3.0.7
- Updated @push.rocks/webstore to version ^2.0.20
## 2024-10-02 - 2.0.18 - fix(core)
Fix type errors and typos in Smartstate class
- Updated type annotation in Smartstate class to ensure StatePartNameType extends string.
- Fixed a typo in the JSDoc comment: 'existing' instead of 'exiting'.
- Corrected improper type casting in the Smartstate class.
## 2024-05-29 - 2.0.17 - Maintenance
General updates and improvements.
- Updated project description
- Multiple updates to `tsconfig`
- Updated `npmextra.json` to include `githost`
## 2023-10-07 - 2.0.16 - Maintenance
General updates and improvements.
- Core update
## 2023-10-04 - 2.0.15 - Maintenance
General updates and improvements.
- Core update
## 2023-10-03 - 2.0.14 to 2.0.10 - Maintenance
General updates and improvements.
- Core updates
## 2023-09-11 - 2.0.9 - Maintenance
General updates and improvements.
- Core update
## 2023-09-11 - 2.0.8 - Maintenance
General updates and improvements.
- Core update
## 2023-07-27 - 2.0.7 - Maintenance
General updates and improvements.
- Core update
## 2023-07-27 - 2.0.6 - Maintenance
General updates and improvements.
- Core update
## 2023-04-13 - 2.0.5 - Maintenance
General updates and improvements.
- Core update
## 2023-04-12 - 2.0.4 - Maintenance
General updates and improvements.
- Core update
## 2023-04-04 - 2.0.3 to 2.0.1 - Maintenance
General updates and improvements.
- Core updates
## 2023-03-15 - 2.0.0 - Major Update
Core update with significant changes.
## 2022-03-25 - 1.0.23 - Major Update
Breaking changes and major updates.
- SWITCH TO ESM
## 2022-01-24 - 1.0.22 - Maintenance
General updates and improvements.
- Core updates
## 2020-11-30 - 1.0.21 to 1.0.20 - Maintenance
General updates and improvements.
- Core updates
## 2020-11-30 - 1.0.19 to 1.0.18 - Maintenance
General updates and improvements.
- Core updates
## 2020-07-27 - 1.0.17 to 1.0.16 - Maintenance
General updates and improvements.
- Core updates
## 2020-05-27 - 1.0.15 - Maintenance
General updates and improvements.
- Core update
## 2020-05-27 - 1.0.14 - Maintenance
General updates and improvements.
- Core update
## 2019-09-25 - 1.0.13 - Maintenance
General updates and improvements.
- Core update
## 2019-09-25 - 1.0.12 - Maintenance
General updates and improvements.
- Core updates
## 2019-04-30 - 1.0.11 to 1.0.10 - Maintenance
General updates and improvements.
- Core updates
## 2019-03-22 - 1.0.9 - Maintenance
General updates and improvements.
- Core update
## 2019-02-27 - 1.0.8 - Minor Update
Minor updates and improvements.
- Updated action generation
- Core update
## 2019-02-21 - 1.0.7 - Initial Release
Initial release of the project.
- Initial core implementation

View File

@ -6,12 +6,27 @@
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartstate",
"description": "a package that handles state in a good way",
"description": "A package for handling and managing state in applications.",
"npmPackagename": "@push.rocks/smartstate",
"license": "MIT"
"license": "MIT",
"keywords": [
"state management",
"reactive programming",
"TypeScript",
"observables",
"web storage",
"state action",
"state selection",
"state notification",
"asynchronous state",
"cumulative notification"
]
}
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}

View File

@ -1,8 +1,8 @@
{
"name": "@push.rocks/smartstate",
"version": "2.0.14",
"version": "2.0.19",
"private": false,
"description": "a package that handles state in a good way",
"description": "A package for handling and managing state in applications.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
@ -14,20 +14,20 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.70",
"@git.zone/tsbundle": "^2.0.8",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.81",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.0"
"@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsbundle": "^2.0.15",
"@git.zone/tsrun": "^1.2.49",
"@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.3.0",
"@types/node": "^22.7.4"
},
"dependencies": {
"@push.rocks/isohash": "^2.0.1",
"@push.rocks/lik": "^6.0.5",
"@push.rocks/smartjson": "^5.0.10",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/smartrx": "^3.0.6",
"@push.rocks/webstore": "^2.0.13"
"@push.rocks/lik": "^6.0.15",
"@push.rocks/smartjson": "^5.0.20",
"@push.rocks/smartpromise": "^4.0.4",
"@push.rocks/smartrx": "^3.0.7",
"@push.rocks/webstore": "^2.0.20"
},
"files": [
"ts/**/*",
@ -43,5 +43,22 @@
],
"browserslist": [
"last 1 chrome versions"
]
],
"keywords": [
"state management",
"reactive programming",
"TypeScript",
"observables",
"web storage",
"state action",
"state selection",
"state notification",
"asynchronous state",
"cumulative notification"
],
"homepage": "https://code.foss.global/push.rocks/smartstate",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartstate.git"
}
}

8838
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

150
readme.md
View File

@ -1,37 +1,137 @@
# @push.rocks/smartstate
a package that handles state in a good way
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartstate)
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartstate)
* [github.com (source mirror)](https://github.com/push.rocks/smartstate)
* [docs (typedoc)](https://push.rocks.gitlab.io/smartstate/)
## Install
## Status for master
To install `@push.rocks/smartstate`, you can use npm (Node Package Manager). Run the following command in your terminal:
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartstate/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartstate/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartstate)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartstate)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartstate)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartstate)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartstate)](https://lossless.cloud)
```bash
npm install @push.rocks/smartstate --save
```
This will add `@push.rocks/smartstate` to your project's dependencies.
## Usage
Use TypeScript for best in class intellisense.
The `@push.rocks/smartstate` library provides an elegant way to handle state within your JavaScript or TypeScript projects, leveraging the power of Reactive Extensions (RxJS) and a structured state management strategy. In the following sections, we will explore the comprehensive capabilities of this package and how to effectively use them in various scenarios, ensuring a robust state management pattern in your applications.
## Contribution
### Getting Started
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
First, let's import the necessary components from the library:
For further information read the linked docs at the top of this readme.
```typescript
import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate';
```
## Legal
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
### Creating a SmartState Instance
`Smartstate` acts as the container for your state parts. You can consider it as the root of your state management structure.
```typescript
const myAppSmartState = new Smartstate<YourStatePartNamesEnum>();
```
### Defining State Parts
State parts represent separable sections of your state, making it easier to manage and modularize. For example, you may have a state part for user data and another for application settings.
Define an enum for state part names for better management:
```typescript
enum AppStateParts {
UserState,
SettingsState
}
```
Now, let's create a state part within our `myAppSmartState` instance:
```typescript
interface IUserState {
isLoggedIn: boolean;
username?: string;
}
const userStatePart = await myAppSmartState.getStatePart<IUserState>(
AppStateParts.UserState,
{ isLoggedIn: false } // Initial state
);
```
### Subscribing to State Changes
You can subscribe to changes in a state part to perform actions accordingly:
```typescript
userStatePart.select().subscribe((currentState) => {
console.log(`User Logged In: ${currentState.isLoggedIn}`);
});
```
If you need to select a specific part of your state, you can pass a selector function:
```typescript
userStatePart.select(state => state.username).subscribe((username) => {
if (username) {
console.log(`Current user: ${username}`);
}
});
```
### Modifying State with Actions
Create actions to modify the state in a controlled manner:
```typescript
interface ILoginPayload {
username: string;
}
const loginUserAction = userStatePart.createAction<ILoginPayload>(async (statePart, payload) => {
return { ...statePart.getState(), isLoggedIn: true, username: payload.username };
});
// Dispatch the action to update the state
loginUserAction.trigger({ username: 'johnDoe' });
```
### Persistent State
`Smartstate` supports the concept of persistent states, where you can maintain state across sessions. To utilize this, specify a persistent mode when getting a state part:
```typescript
const settingsStatePart = await myAppSmartState.getStatePart<AppStateParts, ISettingsState>(
AppStateParts.SettingsState,
{ theme: 'light' }, // Initial state
'persistent' // Mode
);
```
This mode ensures that the state is saved and can be reloaded even after the application restarts, providing a seamless user experience.
### Comprehensive Usage
Putting it all together, `@push.rocks/smartstate` offers a flexible and powerful pattern for managing application state. By modularizing state parts, subscribing to state changes, and controlling state modifications through actions, developers can maintain a clean and scalable architecture. Combining these strategies with persistent states unlocks the full potential for creating dynamic and user-friendly applications.
Remember to leverage TypeScript for its excellent support for types and interfaces, enhancing your development experience with type checking and IntelliSense, ensuring a more reliable and maintainable codebase.
For more complex scenarios, consider combining multiple state parts, creating hierarchical state structures, and integrating with other state management solutions as needed. With `@push.rocks/smartstate`, the possibilities are vast, empowering you to tailor the state management approach to fit the unique requirements of your project.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartstate',
version: '2.0.14',
description: 'a package that handles state in a good way'
version: '2.0.19',
description: 'A package for handling and managing state in applications.'
}

View File

@ -6,26 +6,26 @@ export type TInitMode = 'soft' | 'mandatory' | 'force' | 'persistent';
/**
* Smartstate takes care of providing state
*/
export class Smartstate<StatePartNameType> {
public statePartMap: { [key: string]: StatePart<StatePartNameType, any> } = {};
export class Smartstate<StatePartNameType extends string> {
public statePartMap: { [key in StatePartNameType]?: StatePart<StatePartNameType, any> } = {};
constructor() {}
/**
* Allows getting and initializing a new statepart
* initMode === 'soft' it will allow existing stateparts
* initMode === 'mandatory' will fail if there is an exiting statepart
* initMode === 'mandatory' will fail if there is an existing statepart
* initMode === 'force' will overwrite any existing statepart
* @param statePartNameArg
* @param initialArg
* @param initMode
*/
public async getStatePart<PayloadType>(
statePartNameArg: string & StatePartNameType,
statePartNameArg: StatePartNameType,
initialArg?: PayloadType,
initMode?: TInitMode
): Promise<StatePart<StatePartNameType, PayloadType>> {
if (this.statePartMap[statePartNameArg as any]) {
if (this.statePartMap[statePartNameArg]) {
if (initialArg && (!initMode || initMode !== 'soft')) {
throw new Error(
`${statePartNameArg} already exists, yet you try to set an initial state again`
@ -43,7 +43,7 @@ export class Smartstate<StatePartNameType> {
}
/**
* creates a statepart
* Creates a statepart
* @param statePartName
* @param initialPayloadArg
*/
@ -54,17 +54,20 @@ export class Smartstate<StatePartNameType> {
): Promise<StatePart<StatePartNameType, PayloadType>> {
const newState = new StatePart<StatePartNameType, PayloadType>(
statePartName,
initMode === 'persistent' ? {
dbName: 'smartstate',
storeName: statePartName as any,
} : null
initMode === 'persistent'
? {
dbName: 'smartstate',
storeName: statePartName,
}
: null
);
await newState.init();
const currentState = newState.getState();
await newState.setState({
...initialPayloadArg,
...currentState,
});
this.statePartMap[statePartName as any] = newState;
this.statePartMap[statePartName] = newState;
return newState;
}
}
}

View File

@ -7,23 +7,24 @@ export class StatePart<TStatePartName, TStatePayload> {
public stateStore: TStatePayload;
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
private webStoreOptions: plugins.webstore.IWebStoreOptions;
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance
constructor(nameArg: TStatePartName, webStoreOptions?: plugins.webstore.IWebStoreOptions) {
constructor(nameArg: TStatePartName, webStoreOptionsArg?: plugins.webstore.IWebStoreOptions) {
this.name = nameArg;
// Initialize WebStore if webStoreOptions are provided
if (webStoreOptions) {
this.webStore = new plugins.webstore.WebStore<TStatePayload>(webStoreOptions);
this.initWebStore();
if (webStoreOptionsArg) {
this.webStoreOptions = webStoreOptionsArg;
}
}
/**
* initializes the webstore
*/
private async initWebStore() {
if (this.webStore) {
public async init() {
if (this.webStoreOptions) {
this.webStore = new plugins.webstore.WebStore<TStatePayload>(this.webStoreOptions);
await this.webStore.init();
const storedState = await this.webStore.get(String(this.name));
if (storedState) {
@ -52,6 +53,7 @@ export class StatePart<TStatePartName, TStatePayload> {
if (this.webStore) {
await this.webStore.set(String(this.name), newStateArg);
}
return this.stateStore;
}
/**