Giter VIP home page Giter VIP logo

ngx-simple-store's Introduction

Angular Application Store

Simple store for application based on Subject service and your enum for describing application state. There are three ways to use it. Subscribe for latest changes, get value once or subscribe via async pipe in template directly.

Example

#1 Installing to project

npm i ngx-simple-store --save

#2 Available methods:

select(name); // here you can subscribe by name

next(name, value); // setting a new value

getOnce(name); // get what you want without subscription

#3 Initialization of module and making your app state as enum:
//Your ./app.module.ts:
import { NgxSimpleStore } from 'ngx-simple-store';

export enum AppState {
  isSideBarOpened = 'isSideBarOpened',
  isDarkTheme = 'isDarkTheme',
  refreshTableState = 'refreshTableState'
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgxSimpleStoreModule.forRoot(AppState) //our module
  ],
  providers: [],
  bootstrap: [AppComponent]
})

#4.1 Adding to component "A":
@Component({
  selector: 'app-component',
  template:
  `<button (click)="toggleSideBar()">Toggle Sidebar!</button>`
})
import { NgxSimpleStore } from 'ngx-simple-store';
...
export class AppComponent {
  constructor(private appStore: NgxSimpleStoreService) { }
  
  toggleSideBar() {
    this.appStore.next(AppState.isSideBarOpened, !this.appStore.getOnce(AppState.isSideBarOpened));
  }
}
#4.2 Using in component "B":

Three ways to use this service: by subscription, getting once, or using pipe 'async' directly in template (my favorite one, for this you need to do this variable public).

import { NgxSimpleStore } from 'ngx-simple-store';
@Component({
  selector: 'second-component',
  template:
  `<div>
    One more way to use your service!
    {{ appStore.select(AppState.isSideBarOpened) | async }}
  </div>`
})
export class SecondComponent {
  isSideBarOpened;
  constructor(public appStore: NgxSimpleStoreService) {
  
    // subscribe for getting the latest changes
    this.appStore.select(AppState.isSideBarOpened)
        .subscribe(value => console.log(value));
        
    //get value once
    this.isSideBarOpened = this.appStore.getOnce(AppState.isSideBarOpened);
        
  }
}

Problem

Sometimes making special services for managing state of sidebar, dark theme of template, width of window, some events between components that are far from each of other and so on is a headache for everyone.

In cases when you need to manage some app states that are used in many places we're doing special service with Subject variable, writing methods for subscribing, adding a new value, getting value once. But when we have a lot of such places it becomes difficult and your code becomes too complicated.

Solution

It's just a first version of universal service which you can inject to your component "A", inject to component "B" and use it as common service with common state (like a singleton if you want). I have added an utility function that will convert your enum to individual Subjects by names so it works just as a Subject service that was made for convenient way to work with some common states.

Use cases!

Use distinctUntilChanged for not getting same values (useful in some cases). Use debounceTime for getting latest value in a period of time

this.store
    .select('isOpenedSidebar')
    .pipe(distinctUntilChanged(), debounceTime(500))
    .subscribe(value => {
      this.isSidebarOpen = value;
    });

ngx-simple-store's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.