@push.rocks/smartstate
TypeScript icon, indicating that this package has built-in type declarations

2.0.19 • Public • Published

@push.rocks/smartstate

a package that handles state in a good way

Install

To install @push.rocks/smartstate, you can use npm (Node Package Manager). Run the following command in your terminal:

npm install @push.rocks/smartstate --save

This will add @push.rocks/smartstate to your project's dependencies.

Usage

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.

Getting Started

First, let's import the necessary components from the library:

import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate';

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.

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:

enum AppStateParts {
  UserState,
  SettingsState
}

Now, let's create a state part within our myAppSmartState instance:

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:

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:

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:

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:

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 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.

Package Sidebar

Install

npm i @push.rocks/smartstate

Weekly Downloads

93

Version

2.0.19

License

MIT

Unpacked Size

1.08 MB

Total Files

29

Last publish

Collaborators

  • lossless