npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@e-square/ark

v16.0.0-next.6

Published

An innovative state management library meticulously crafted for Angular.

Downloads

48

Readme

Ark

An innovative state management library meticulously crafted for Angular.

Installation

Use the package manager npm to install Ark.

npm install @e-square-io/ark@latest

State Management

What is state management? The definition of state management is the handling of the data (state) shared across components in an application.

State management is implemented by stores.

Store

A store is a centralized container where a chunk of the application state resides. Stores can be categorized into two kinds: component stores and global/feature stores. Further discussion will be about feature stores. Feature stores contain data directly related to the application’s or feature's business logic.

Global or feature stores aim to ensure that data can be shared and manipulated across the application predictably and efficiently.

The store in Ark is an injectable Angular service. Extension of the abstract class Ark used to create one. Additionally, the State type and the initial state object must be created. The resulting code will look like the following example.

export interface SessionState {
  user?: User;
}

@Injectable({
  providedIn: 'root',
})
export class SessionStore extends Ark<SessionState> {
  readonly $user = this.select('user');

  constructor() {
    super({}, withStoreConfiguration({ name: 'Session Store' }));
  }
}

Updating the Store

To update the store’s value method update is used. As an argument a partial state object or a callback function that receives the current state and must return a partial state object supposed to be provided.

Besides updating the store’s value, the method update triggers beforeUpdate and afterUpdate interceptors accordingly and the onBeforeUpdate lifecycle hook.

this.sessionStore.update({ user: value });

or

this.sessionStore.update(state => ({ ...state, user: value }))

Getting data from the Store

The store value can be extracted in two ways. First is the instantaneous value that can be achieved with the method getValue. Second, use the method select that returns the computed signal of the store value. Method select can be invoked without arguments, it returns the signal with the whole state object. Or it receives the state object key or projection function that modifies the value of the resulting signal.

$user = this.select('user'); // $user is a readonly signal containing the User object

Customization

Ark constructor receives besides the initial state functional arguments called ArkFeatures. An Ark feature is an object consisting of the feature kind and an object with the feature implementation. Right now, two kinds of features are implemented: interceptors and configuration.

Interceptors

Interceptors are functions being run at specific moments. Right now, there are five types of interceptors are supported:

  1. BeforeInitInterceptor. Runs in the store constructor and can transform the initial value of the store.
  2. AfterInitInterceptor. Runs in the store constructor after the initial state is set.
  3. BeforeUpdateInterceptor. Runs at the beginning of the update method and can transform the state update request.
  4. AfterUpdateInterceptor. Runs at the end of the update process and does additional side actions when the store has been updated.
  5. AfterResetInterceptor. Runs after the store was reset to its initial value.

Interceptors can perform a variety of implicit tasks like validations, logging, adding, or removing data during the store update process.

Write an interceptor

First, decide which moment the interceptor suppose to run. Depending on that, create the function of the appropriate type. Note the types and the number of arguments and return value.

const beforeInit: BeforeInitInterceptor<UpdateSelectState> = initialState => {
  console.log('Before init', initialState);
  initialState.x = '';
  return initialState;
};

const afterInit: AfterInitInterceptor<UpdateSelectState> = initialState => console.log('After init', initialState);

const beforeUpdate: BeforeUpdateInterceptor<UpdateSelectState> = (currState, newState, store) => {
  console.log('%cBefore Update #1.', 'color: lightgreen');
  console.log('Curr state:', currState, 'new state:', newState, 'store', store);
  return newState;
};

const afterUpdate: AfterUpdateInterceptor<UpdateSelectState> = state => console.log('Updated successfully', state);

Now, provide all your interceptor functions to the ArkInterceptors object and supply it with the withInterceptors function to super in the store constructor. Multiple interceptors can intercept the same moment and multiple withInterceptors functions can be used.

Add and remove interceptor at run-time

Every store has the property hooks that contains the instance of the Hooks class. The Hooks class provides methods to add and remove interceptors at run-time.

Store Lifecycle

Ark introduces its lifecycle additionally to the standard Angular. Your application can use lifecycle hooks methods, similar to component lifecycle hooks, to tap into the store update events.

Responding to Store update events

Respond to update events in the lifecycle of the store by implementing one or more of the lifecycle hook interfaces. The hooks allow you to act on the store and the store value at the appropriate moment when the update occurs. Each interface defines the prototype for a single hook method, whose name is the interface name. For example, the OnBeforeUpdate interface has a hook method onBeforeUpdate. If you implement this method in your store class, Ark calls it shortly after the update process will be initiated.

onBeforeUpdate(currentState: UpdateSelectState, newState: Partial<UpdateSelectState>): Partial<UpdateSelectState> {
  console.log('onBeforeUpdate', 'curr', currentState, 'new', newState);

  return newState;
}

onAfterUpdate(): void {
  console.log('onAfterUpdate', this.getValue());
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Support

If you encounter any problems or have any questions, please file an issue on our GitHub page.