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

@statekit/core

v0.5.0-alpha

Published

Check out `@statekit/react`!

Downloads

4

Readme

statekit PoC WIP

Check out @statekit/react!

import { anAsyncFn, testLog } from "../../__test__/utils"
import { createDfaConfig, createDfa } from "./dfa"

const initialData = { count: 0, foo: "bar" }
type Data = typeof initialData

const config = createDfaConfig({
   states: ["idle", "running", "paused"],
   data: initialData,
   events: {
      noop: () => {},
      data: ({ draft }) => {
         //console.log(draft)
         draft
      },
      payload: ({ draft }, p: Partial<Data>) => {
         Object.assign(draft, p)
      },
      meta: (meta) => {
         meta.draft.foo = meta.state.toString()
      },
      return: ({ isStateOneOf, S }) => {
         if (isStateOneOf([S.idle, S.paused])) return
      },
      cases: ({ cases, S, draft }) =>
         cases({
            [S.idle]: () => S.running,
            [S.paused]: () => {
               draft.count = 123
            },
         }),
      async: async ({ draft, S }) => {
         draft.foo = "loading"

         draft.foo = await anAsyncFn("success")

         return S.running
      },
   },
   helpers: {
      isCountZero: ({ data }) => data.count === 0,
      isCountEqualTo: ({ data }, to: number) => data.count === to,
      isDataSameAs: ({ data }, payload: Data) =>
         data.count === payload.count && data.foo == payload.foo,
   },
   transitionRules: {
      only: {
         paused: ["running"],
      },
      deny: {
         running: ["paused"],
      },
   },
})

/* TS Tests below */
const _tsEmptyConfig = createDfaConfig({})
const _tsConfig = createDfaConfig({ states: ["asd", "qwe"] })
const _ts1 = createDfa(_tsEmptyConfig)
const _ts2 = createDfa(_tsConfig)
const _ts3 = createDfa(config)
const _ts4 = createDfa({
   states: ["idle", "running", "paused"],
   data: initialData,
   events: {
      noop: () => {},
      data: ({ draft }) => {
         console.log(draft)
      },
      payload: ({ draft }, p: Partial<Data>) => {
         Object.assign(draft, p)
      },
      meta: (meta) => {
         meta.draft.foo = meta.state.toString()
      },
      return: ({ isStateOneOf, S }) => {
         if (isStateOneOf([S.idle, S.paused])) return
      },
      cases: ({ cases, S, draft }) =>
         cases({
            [S.idle]: () => S.running,
            [S.paused]: () => {
               draft.count = 123
            },
         }),
   },
   transitionRules: {
      only: {
         paused: ["running"],
      },
      deny: {
         running: ["paused"],
      },
   },
})
// just using
_ts1
_ts2
_ts3
_ts4
/* */

beforeAll(() => jest.useFakeTimers())
afterAll(() => jest.useRealTimers())

describe("DFA", () => {
   it("should create a machine", () => {
      const m = createDfa(config)

      // also checks TS type inference
      const initialState: 0 = m.STATE[config.states[0]]
      expect(m.state).toEqual(initialState)

      expect(m.STATE).toEqual({ idle: 0, running: 1, paused: 2 })

      expect(m.data).toEqual(config.data)
   })

   it("should emit a state", () => {
      const m = createDfa(config)

      const prevState = m.setState(m.STATE.running)
      expect(prevState).toEqual(m.STATE.idle)
      expect(m.state).toEqual(m.STATE.running)

      const prevState2 = m.setState(m.state)
      expect(prevState2).toEqual(false)
      expect(m.state).toEqual(m.STATE.running)
   })

   it("should emit events", () => {
      const m = createDfa(config)
      const { event } = m

      event.noop()
      expect(m.data).toEqual(initialData)

      event.data()
      expect(m.data).toEqual(initialData)

      event.payload({ count: 1 })
      expect(m.data).toEqual({ ...initialData, count: 1 })

      event.meta()
      expect(m.data.foo).toEqual(m.state.toString())
   })

   it("async events work", async () => {
      const m = createDfa(config)
      const { event, STATE } = m

      expect(m.state).toBe(STATE.idle)
      expect(m.data.foo).toBe(initialData.foo)

      const promise = event.async()

      expect(m.state).toBe(STATE.idle)
      expect(m.data.foo).toBe("loading") // consider

      jest.advanceTimersToNextTimer()
      await promise

      expect(m.state).toBe(STATE.running)
      expect(m.data.foo).toBe("success")
   })

   it("helpers work", async () => {
      const m = createDfa(config)
      const { helper } = m

      expect(helper.isCountZero()).toBe(true)

      m.setData((draft) => {
         draft.count = 1
      })

      expect(helper.isCountZero()).toBe(false)
   })

   it("helpers with params work", async () => {
      const m = createDfa(config)
      const { helper } = m

      // single arg
      expect(helper.isCountEqualTo(1)).toBe(false)
      expect(helper.isCountEqualTo(0)).toBe(true)

      // multiple args
      expect(helper.isDataSameAs({ count: 0, foo: "qwe" })).toBe(false)
      expect(helper.isDataSameAs({ count: 0, foo: "bar" })).toBe(true)
   })
})