A practical example of the State Pattern in TypeScript
Tags: typescript, howto, tutorial, solidprinciples
Published: 2023-11-14 15:00:44

Are you struggling with your business logic and keeping classes seperated? Do you need a way to split some logic into seperate classes and keep them SOLID? This short guide might really help you out!

In TypeScript, managing global state efficiently across components or services is a common challenge. In this guide, we'll explore a simple yet effective way to create a global state service using the observer pattern. This approach is especially useful when you want to keep your codebase lightweight and avoid unnecessary dependencies.

So for example, instead of your sessiontimer.ts depending on a sessionwarning.ts and a session-logout.ts we can have all these files depend on only one service called a globalstate.ts. 

<img width="100%" style="width:100%" src="https://media0.giphy.com/media/v1.Y2lkPTc5MGI3NjExY2Q0eG5tenQ1djl4M2pjNjltaHB2YTM1enVkMTYxODZqaWZ3MG5rNCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/d3mlE7uhX8KFgEmY/giphy.gif">


## The Global State Service
Let's start by creating a GlobalStateService class that follows the observer pattern:

```typescript
class GlobalStateService {
  private static instance: GlobalStateService;

  private eventListeners: Map<string, ((data?: any) => void)[]> = new Map();
 
   //Ensure a singleton
  static getInstance(): GlobalStateService {
    if (!GlobalStateService.instance) {
      GlobalStateService.instance = new GlobalStateService();
    }
    return GlobalStateService.instance;
  }

  private constructor() {  }

  on(eventName: string, listener: (data?: any) => void): void {
    if (!this.eventListeners.has(eventName)) {
      this.eventListeners.set(eventName, []);
    }
    this.eventListeners.get(eventName)?.push(listener);
  }

  off(eventName: string, listener: (data?: any) => void): void {
    const listeners = this.eventListeners.get(eventName);
    if (listeners) {
      const index = listeners.indexOf(listener);
      if (index !== -1) {
        listeners.splice(index, 1);
      }
    }
  }

  emit(eventName: string, data?: any): void {
    const listeners = this.eventListeners.get(eventName);
    if (listeners) {
      listeners.forEach((listener) => listener(data));
    }
  }
}


```
This class has 3 functions, on, off and emit. We implement this class into other classes. For example a SessionPopup will use a GlobalStateService and the 'on' function. When we use this 'on' function we can read the data submitted by that 'on' function. We can also 'emit' an event with data to every class which also uses the GlobalStateService.


## Usage in Components
Now, let's see how to use this GlobalStateService in a component. In this example we trigger a popup.

```typescript
export class SessionPopup {
// Example usage:
const globalStateService = GlobalStateService.getInstance();

private constructor(){
  this.globalStateService.on('sessionWarning', (remainingSeconds) => {
    console.log(`Session warning! open popup`);
    this.open();
    // Trigger your session warning logic here
  });

}

private open(): void{
   //open the popup
}

// Don't forget to unsubscribe when components are destroyed
sessionWarningSubscription();
sessionExpiredSubscription();
}

```

When the SessionPopup is created it subscribes to all events called 'sessionWarning'. Now we can open the popup when another component triggers this event. 

#All Done!#
With this change your other components can subscribe and wait until a trigger needs to be executed.

<img width="100%" style="width:100%" src="https://media0.giphy.com/media/v1.Y2lkPTc5MGI3NjExM3pqY25zM2RnazlteHZ5MTFneHVxbW43dmdudGx5ZzZveXJ2ZXBmMyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/bTF4iiCmxK5qkH6rKF/giphy.gif">



## Conclusion

This lightweight global state service provides a straightforward mechanism for managing events and state across your TypeScript application using the subscriber pattern. It allows you to avoid unnecessary dependencies, keeping your codebase clean and easy to maintain. Feel free to customize and extend the service according to your specific requirements.