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

@jacobtipp/hydrated-bloc

v4.0.3

Published

A package that provides mixins to persist and restore bloc and cubit states. Built to work with `@jacobtipp/bloc`

Downloads

68

Readme

@jacobtipp/hydrated-bloc

A package that provides mixins to persist and restore bloc and cubit states. Built to work with @jacobtipp/bloc

Installation

npm install @jacobtipp/hydrated-bloc

Setup

import { HydratedStorage, HydratedLocalStorage } from "@jacobtipp/hydrated-bloc"

HydratedStorage.storage = new HydratedLocalStorage()

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

HydratedCubit

import { Cubit } from "@jacobtipp/bloc"
import { WithHydratedCubit } from "@jacobtipp/hydrated-bloc"

// Create a base cubit that extends from Cubit
class CounterCubitBase extends Cubit<number> {}
// Wrap your Cubit with WithHydratedCubit mixin 
class CounterCubit extends WithHydratedCubit(CounterCubitBase) {
  constructor(state: number) {
    super(state);

    // populate the internal state storage with latest state
    // must be called in constructor when using mixin
    this.hydrate();
  }

  increment = () => this.emit(this.state + 1);

  // optionally serialize your state to JSON by overriding toJson
  override toJson(state: number): string {
    return super.toJson(state)
  }

  // optionally deserialize your JSON to state by overriding fromJson
  override fromJson(json: string): number {
    return super.fromJson(json)
  }
}

HydratedBloc

import { Bloc } from "@jacobtipp/bloc"
import { WithHydratedBloc } from "@jacobtipp/hydrated-bloc"

abstract class CounterEvent {}

class Increment extends CounterEvent {}

// Create a base bloc that extends from Bloc
class CounterBlocBase extends Bloc<CounterEvent, number> {}
// Wrap your Bloc with WithHydratedBloc mixin 
class CounterBloc extends WithHydratedBloc(CounterBlocBase) {
  constructor(state: number) {
    super(state);

    // populate the internal state storage with latest state
    // must be called in constructor when using mixin
    this.hydrate();

    this.on(Increment, (_event, emit) => {
      emit(this.state + 1);
    });
  }

  // optionally serialize your state to JSON by overriding toJson
  override toJson(state: number): string {
    return super.toJson(state)
  }

  // optionally deserialize your JSON to state by overriding fromJson
  override fromJson(json: string): number {
    return super.fromJson(json)
  }
}

Now the CounterCubit and CounterBloc will automatically persist/restore their state. We can increment the counter value, refresh the browser, and the previous state will be retained.

HydratedMixin

blocs that extend a WithHydratedCubit or WithHydratedBloc inherit the the following properties and methods

interface HydratedMixin<State> {
  id: string;
  storagePrefix: string;
  storageToken: string;
  clear(): Promise<void>;
  fromJson(json: string): State;
  toJson(state: State): string;
  hydrate(): void;
}

Custom Hydrated Storage

If the default HydratedLocalStorage doesn't meet your needs, you can always implement a custom Storage by simply implementing the Storage interface and initializing HydratedStorage with the custom `Storage``.

import { Storage } from "@jacobtipp/hydrated-bloc"

class MyHydratedStorage implements Storage {
  override read(key: string) {
    throw new Error('Method not implemented.');
  }
  override write(key: string, value: any): Promise<void> {
    throw new Error('Method not implemented.');
  }
  override delete(key: string): Promise<void> {
    throw new Error('Method not implemented.');
  }
  override clear(): Promise<void> {
    throw new Error('Method not implemented.');
  }
  override close(): Promise<void> {
    throw new Error('Method not implemented.');
  }
}
HydratedStorage.storage = new MyHydratedStorage()