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

mobx-observable-history

v2.0.3

Published

History.js wrapper with observable location and reactive URLSearchParams

Downloads

3,561

Readme

MobX & History.js make some ♡

History.js wrapper with observable location and reactive URLSearchParams

Install

  • NPM npm i mobx-observable-history
  • Yarn yarn add mobx-observable-history

Dependencies:

  • mobx: "^6.0"
  • history: "^4.0"

Why

When work on projects mobx it feels natural to use reactivity everywhere.

Benefits

  • convenient api to manage current location's state
  • observable history.location and history.action
  • observable history.searchParams is URLSearchParams object with extra goodies (see below)
  • compatible with react-router

Examples

import { autorun, reaction, comparer } from "mobx"
import { createBrowserHistory } from "history";
import { createObservableHistory } from "mobx-observable-history"

const history = createBrowserHistory();
const navigation = createObservableHistory(history);

// Reacting to any location change
autorun(() => {
  const {pathname, search, hash} = navigation.location
  console.log("LOCATION", {pathname, search, hash})
})

// Reacting to partial location change
reaction(() => navigation.location.pathname, page => {
  console.log("PAGE", page)
})

// Reacting to multiple values of one search param, e.g. ?y=1&y=2
reaction(() => navigation.searchParams.getAll("y"), params => {
  console.log("Y", params) // params is ["1", "2"]
}, {
  fireImmediately: true,
  equals: comparer.shallow,
})

// Partial updates
navigation.location.pathname = "/path"  // push history to new location, same as navigation.merge("/path")
navigation.location.search = "?x=1" // `?` can be omitted
navigation.location.hash = "#y" // `#` can be omitted
navigation.merge({pathname: "/path", search: "z=3"}) // push history to new location 
navigation.searchParams.delete("x") // remove all ?x=1&x=.. from search params
navigation.searchParams.set("y", "2") // remove previous all ?y=1&y=2&y=etc. and set to single value

API

history.toString(): string

Get observable location (pathname + search + hash)

Examples:

autorun(() => console.log("LOCATION", history.toString()))

history.merge(location: object | Partial, replace?: boolean): void

Merge partial location (pathname, search, hash)

Examples:

history.merge({pathname: "/test"})       // history.push + merge
history.merge("/test?x=1&x2#tag")
history.merge({pathname: "/test"}, true) // history.replace + merge

history.normalize(location: string | LocationDescriptor, opts?: { skipEmpty = false }): string

Normalize location and return new object {pathname, search, hash}

history.destroy(): History

Stops internal observations and return native history.js object

history.searchParams is observable URLSearchParams() with extra goodies:

  • history.searchParams.merge(search: string | object | URLSearchParams)

    Merge new search params with existing.

  • history.searchParams.replace(search: string | object | URLSearchParams)

    Fully replace current search params.

  • history.searchParams.deleteAll()

    Clear all current search params.

  • history.searchParams.toString(opts?: { withPrefix = false })

    Observable search-params string representation.

    • {withPrefix: true} adds ? prefix to output (default: false)

License

MIT