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

@sophosoft/nano-state

v0.3.0

Published

NanoState: efficient, effective, and extensible state management

Downloads

15

Readme

nano-state

Efficient, effective, and extensible state management: less is more.

installation

npm i @sophosoft/nano-state
# or
yarn add @sophosoft/nano-state

usage

nano-state is built from the ground-up with TypeScript to leverage strong typing. The target ECMAScript version is es5. While the examples below may be contrived, they should illustrate normal usage.

state

Extend the State type to define interfaces for your various states:

import { State } from '@sophosoft/nano-state'

interface UserState extends State {
    displayName: string
    email: string
    id?: string
    visits: number
}

context

A Context provides exclusive managment of state. Implement getters and mutation methods as necessary in the context.

import { Context } from '@sophosoft/nano-state'

class UserContext extends Context<UserState> {
    public constructor() {
        // initialize default state
        super({
            displayName: 'Guest',
            email: '',
            visits: 0
        })
    }

    public get DisplayName(): string {
        return this.state.displayName
    }

    // ...

    public login(id: string, name: string, email: string, visits: number): void {
        this.state.displayName = name
        this.state.email = email
        this.state.id = id
        this.state.visits = visits + 1
        this.notify('User:LoggedIn', { id: this.state.id })
    }

    public async cache(manager: CacheManager): Promise<void> {
        await manager.write('user', this.state)
        this.notify('User:Cached')
    }

    // ...
}

Note the usage of this.notify(string, payload?). This is the internal event system that is described below.

subscription

The Subscription abstract class allows middleware to react to one or more events. The contract includes one async handle(event: Event<EventPayload>) method, however a strategy pattern may be used to handle multiple events when necessary. By default, a subscription will listen to all events using the wildcard (*) event id. Events are described next.

import { Event, EventPayload, Subscription } from '@sophosoft/nano-state'

class LogSubscription extends Subscription {
    public async handle(event: Event<EventPayload>): Promise<void> {
        console.log(event)
    }
}
import { Subscription } from '@sophosoft/nano-state'

class AuthNSubscription extends Subscription {
    public constructor() {
        super(['AuthN:LogIn', 'AuthN:LogOut'])
    }

    public async handle(event: AuthNEvent): Promise<void> {
        if (event.id === 'AuthN:LogIn') {
            return this.handleLogIn(event)
        } else if (event.id === 'AuthN:LogOut') {
            return this.handleLogOut(event)
        }
    }

    // ...
}

event

Extend the Event and EventPayload types to define specific event payloads.

Events contain an id, store, and an optional payload. The Store is descibed next and may be used by subscriptions to access various contexts and trigger additional events. By default, the payload may be undefined, which will cause syntax errors if payload properties are accessed without proper typing.

import { Event, EventPayload } from '@sophosoft/nano-state'

interface UserLoginPayload extends EventPayload {
    id: string
    name: string
    email: string
    visits: number
}

interface UserLoginEvent extends Event<UserLoginPayload> {
    payload: UserLoginPayload
}

store

The Store class provides access to all of the contexts in an application and dispatches events to all subscriptions. It is intended to be a singleton, globally accessible throughout an application.

Contexts are mapped by aliases, which allow third-party libraries to be integrated with domain-specific names. In order to minimize the inital overhead of registering multiple Contexts, a factory method can be used to lazy-load a Context only when it is requested via Store.getContext(). In this way, a Context is not instanciated until it's needed, and then only once: The resulting instance is stored for subsequent requests.

When an event is triggered, subscriptions are notified in the order in which they were registered.

import { Store, ContextMap, Subscription } from '@sophosoft/nano-state'

// perhaps imported from a config file
const contexts: ContextMap = {
    'authn': new AuthNContext(),
    'user': () => new UserContext(), // lazy-loading
    // ...
}

// perhaps also imported
const subscriptions: Subscription[] = [
    new LogSubscription(),
    new AuthNSubscription(),
    // ...
]

class AppStore extends Store {
    public static readonly Instance: AppStore = new AppStore()

    private constructor() {
        super(contexts, subscriptions)
    }
}

Using the store within an application is then relatively trivial.

import { AppStore } from '@/store/AppStore'
import { UserContext } from '@/user/UserContext'

const store: AppStore = AppStore.Instance

const user: UserContext = store.getContext<UserContext>('user')

console.debug(`${user.DisplayName} is the current user`)

store.trigger('AuthN:LogOut', { id: user.Id })