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

@ash9g/redu

v1.0.1

Published

Strongly typed typescript helpers for redux

Downloads

7

Readme

Some examples:

import {createActionFactory} from "@ash9g/redu";
import {LocDim, DskState, WndState} from "./DskState";
import {AppDescriptor, AppHook} from "./App"

export interface XY {
  x: number
  y: number
}

export const DskActions = {
  SetState: createActionFactory("SetState").payload<{ state: DskState, }>(),

  DskMouseMove: createActionFactory("DskMouseMove").payload<{ xy: XY, }>(),
  DskMouseUp: createActionFactory("DskMouseUp").payload(),

  WndTitleDown: createActionFactory("WndTitleDown").payload<{ wndId: number, xy: XY, }>(),

  WndBorderDown: createActionFactory("WndBorderDown").payload<{ wndId: number, xy: XY, direction: string, }>(),
  WndMouseDown: createActionFactory("WndMouseDown").payload<{ wndId: number }>(),

  WndClose: createActionFactory("WndClose").payload<{ wndId: number }>(),
  WndMax: createActionFactory("WndMax").payload<{ wndId: number }>(),
  WndMin: createActionFactory("WndMin").payload<{ wndId: number }>(),

  IcnRun: createActionFactory("IcnRun").payload<{ appId: string, appProps: any }>(),

  AppStart: createActionFactory("AppStart").payload<{ wnd: WndState,  hook: AppHook}>(),
  AppStop: createActionFactory("AppStop").payload<{ wnd: WndState,  hook: AppHook}>(),
  SetAppProps: createActionFactory("SetAppProps").payload<{wndId: number, appProps: any}>(),

  //TODO ADD TYPE TO ONE OF BELOW
}

export type DskAction =
  typeof DskActions.DskMouseMove.at |
  typeof DskActions.DskMouseUp.at |
  typeof DskActions.WndTitleDown.at |
  typeof DskActions.WndBorderDown.at |
  typeof DskActions.WndMouseDown.at |
  typeof DskActions.WndClose.at |
  // typeof DskActions.WndOpen.at |
  typeof DskActions.WndMax.at |
  typeof DskActions.WndMin.at |
  typeof DskActions.IcnRun.at |
  typeof DskActions.SetState.at |
  typeof DskActions.AppStart.at |
  typeof DskActions.AppStop.at |
  typeof DskActions.SetAppProps.at
import {DskAction, DskActions} from "./DskActions";
import {DskState, DskStateHelp, WndState, LocDim, XY} from "./DskState";
import {createReducerSet} from "ash9g/redu";


const DskReducers = {

  SetStateReducer: DskActions.SetState.reducer<DskState>((s, a) => {
    return a.state
  }),

  DskMouseMoveReducer: DskActions.DskMouseMove.reducer<DskState>((s, a) => {
    if (s.ops.moveeWid != -1) {
      const dx = a.xy.x - s.ops.startPageXY.x
      const dy = a.xy.y - s.ops.startPageXY.y

      const wid = s.ops.moveeWid
      const wnd = s.wnds.wnds.get(wid)
      s = s.setIn(["wnds", "wnds"], s.wnds.wnds.updateIn([wid, "nextLoc"], nl => {
        const x = Math.max(wnd.loc.x + dx, 0)
        const y = Math.max(wnd.loc.y + dy, 0)
        return nl.merge({x, y})
      }))

      return s
    }

    if (s.ops.resizeeWid != -1) {
      const dir = s.ops.resizeDir
      const wid = s.ops.resizeeWid
      const wnd = DskStateHelp.getWndOrNull(s, wid)

      if (dir != "") {
        let mdx = 0
        if (dir != "N" && dir != "S") {
          mdx = a.xy.x - s.ops.startPageXY.x
        }

        let mdy = 0
        if (dir != "E" && dir != "W") {
          mdy = a.xy.y - s.ops.startPageXY.y
        }

        let width: any
        let height: any
        let top: any
        let left: any

        if (dir == "N" || dir == "NE" || dir == "NW") {
          top = (mdy + wnd.loc.y)
          if (top < 0) {
            //If the top goes off the top of screen while resizing 
            //just cap it & height
            top = 0
            height = wnd.loc.y + wnd.loc.height
          } else {
            height = (-mdy + wnd.loc.height)
          }
        } else if (dir == "S" || dir == "SE" || dir == "SW") {
          height = (mdy + wnd.loc.height)
        }

        if (dir == "E" || dir == "NE" || dir == "SE") {
          width = (mdx + wnd.loc.width)
        } else if (dir == "W" || dir == "NW" || dir == "SW") {
          left = (mdx + wnd.loc.x)
          if (left < 0) {
            left = 0
            width = wnd.loc.x + wnd.loc.width
          } else {
            width = (-mdx + wnd.loc.width)
          }

        }

        const heightOk = height != null && !isNaN(height) && height >= 0
        const widthOk = width != null && !isNaN(width) && width >= 0
        const topOk = top != null && !isNaN(top)
        const leftOk = left != null && !isNaN(left)

        let nextLoc = wnd.nextLoc

        if (heightOk) {
          nextLoc = nextLoc.set("height", height)
        }
        if (widthOk) {
          nextLoc = nextLoc.set("width", width)
        }
        if (topOk) {
          nextLoc = nextLoc.set("y", top)
        }
        if (leftOk) {
          nextLoc = nextLoc.set("x", left)
        }

        s = s.setIn(["wnds", "wnds"], s.wnds.wnds.set(wid, wnd.set("nextLoc", nextLoc)))

        return s
      }
    }
    return null
  }),


  DskMouseUpReducer: DskActions.DskMouseUp.reducer<DskState>((s, a) => {

    let ops = s.ops.set("allowSelect", true)

    if (s.ops.moveeWid != -1) {
      const wid = s.ops.moveeWid
      const wnd = DskStateHelp.getWndOrNull(s, wid)

      s = s.setIn(["wnds", "wnds"], s.wnds.wnds.set(wid, wnd.merge({
        moving: false,
        loc: wnd.nextLoc || wnd.loc,
        nextLoc: null,
      })))

      s = s.set("ops", ops.merge({
        moveeWid: -1,
        startPageXY: null,
      }))
    }

    if (s.ops.resizeeWid != -1) {
      const wid = s.ops.resizeeWid
      const wnd = DskStateHelp.getWndOrNull(s, wid)

      s = s.setIn(["wnds", "wnds"], s.wnds.wnds.set(wid, wnd.merge({
        loc: wnd.nextLoc || wnd.loc,
        nextLoc: null,
      })))

      s = s.set("ops", ops.merge({
        resizeeWid: -1,
        resizeDir: "",
        startPageXY: null,
      }))

    }


    return s
  }),

  WndTitleDownReducer: DskActions.WndTitleDown.reducer<DskState>((s, a) => {
    const wid = a.wndId
    const wnd = DskStateHelp.getWndOrNull(s, wid)
    if (wnd != null) {
      let ops = s.ops.set("allowSelect", false)
      s = s.set("ops", ops)

      s = DskStateHelp.raiseWnd(s, wid)

      s = s.setIn(["wnds", "wnds"], s.wnds.wnds.update(wid, w => w.merge({
        moving: true,
        nextLoc: wnd.loc,
      })))

      s = s.set("ops", s.ops.merge({
        moveeWid: wid,
        startPageXY: new XY(a.xy),
      }))
    }
    return s
  }),

  WndBorderDownReducer: DskActions.WndBorderDown.reducer<DskState>((s, a) => {
    const wid = a.wndId
    const wnd = DskStateHelp.getWndOrNull(s, wid)
    if (wnd != null) {
      let ops = s.ops.set("allowSelect", false)
      s = s.set("ops", ops)

      s = DskStateHelp.raiseWnd(s, wid)

      s = s.setIn(["wnds", "wnds"], s.wnds.wnds.update(wid, w => w.merge({
        nextLoc: wnd.loc,
      })))

      s = s.set("ops", s.ops.merge({
        resizeeWid: wid,
        startPageXY: new XY(a.xy),
        resizeDir: a.direction,
      }))
    }
    return s
  }),

  WndDownReducer: DskActions.WndMouseDown.reducer<DskState>((s, a) => {
    return DskStateHelp.raiseWnd(s, a.wndId)
  }),

  WndCloseReducer: DskActions.WndClose.reducer<DskState>((s, a) => {
    return s.set("wnds", s.wnds.set("wnds", s.wnds.wnds.remove(a.wndId)))
  }),

  // WndOpenReducer: DskActions.WndOpen.reducer<DskState>((s, a) => {
  //   console.error("Wndopen no longer supported")
  //   return s
  // }),


  IcnRunReducer: DskActions.IcnRun.reducer<DskState>((s, a) => {
    const app = s.appRegistry.get(a.appId)
    if (app) {
      const appDescriptor = app
      const appProps = a.appProps
      const wndId = s.ops.nextWndId
      const x = 100 + (wndId * 20) % 200
      const y = x
      s = s.setIn(["ops", "nextWndId"], wndId + 1)

      const wnd = new WndState({
        wndId,
        appDescriptor,
        appProps,
        wndTitle: appDescriptor.title,
        loc: new LocDim({
          x, y,
          height: 480,
          width: 640,
        }),
      })
      s = s.setIn(["wnds", "wnds", wndId], wnd)

      s = DskStateHelp.raiseWnd(s, wndId)
    } else {
      console.error(`Unable to locate cmd[${a.appId}] for icn click`)
    }
    return s;
  }),

  SetAppPropsReducer: DskActions.SetAppProps.reducer<DskState>((s, a) => {
    s = DskStateHelp.updateWnd(s, a.wndId, wnd => wnd.set("appProps", a.appProps))
    return s
  })
}


export const DskReducerSet = createReducerSet<DskState, DskAction>(DskReducers)