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

local-sync-state

v2.0.1

Published

LocalSyncState is a synchronous state in one object

Downloads

8

Readme

LocalSyncState is a synchronous state in one object.

Why?

:blue_heart: TypeScript + Defensive Types

You remove the default state — now it is: type | undefined (like React's useState).

:cyclone: Synchronous

Your subscribers (via onUpdate) get the new state really instantly. See example.

Excellent for high-load operations.

:gift: All-in-one object

You can send it to any depth and always get up-to-date state.

Excellent for integration.

:fire: Immutability

...always up-to-date state and in full control.

import localSyncState from 'local-sync-state'

const appleInfo = localSyncState({ worms: 0 })

const oldAppleInfo = appleInfo.get() // { worms: 0 } forever

appleInfo.set({ worms: 1 })

const newAppleInfo = appleInfo.get() // { worms: 1 } forever

console.log(oldAppleInfo === newAppleInfo) // false

:zap: Fully Tested

Feel free to use new versions. Nothing will fall.

:high_brightness: Any value in state

Not only an object. Number, string — any value! See Usage.

Types will be taken automatically.

Usage

get

import localSyncState from 'local-sync-state'

const numberState = localSyncState(0)

const currentNumber = numberState.get() // 0

Without default value:

import localSyncState from 'local-sync-state'

const numberState = localSyncState<number>()

const currentNumber = numberState.get() // undefined

set

import localSyncState from 'local-sync-state'

const numberState = localSyncState(0)

numberState.set(1)
// OR
numberState.set((oldNumber) => oldNumber + 1)

onUpdate

Add your subscriber.

import localSyncState from 'local-sync-state'

const numberState = localSyncState(0)

const destroyer = numberState.onUpdate((state, prevState) => {
  // Your subscriber
})

// Delete this subscriber
destroyer()

update

Run all subscribers now without set.

import localSyncState from 'local-sync-state'

const numberState = localSyncState(0)

numberState.update()

destroy

Delete all subscribers and block the addition of new subscribers.

import localSyncState from 'local-sync-state'

const numberState = localSyncState(0)

numberState.destroy()

Synchronous Example

No Promises.

import localSyncState from 'local-sync-state'

const state = localSyncState('wolf')

state.onUpdate(() => console.log(1))

state.set('fox')

console.log(2)

// Console output:
// 1
// 2

React

Hook for new state

import { useLocalSyncState } from 'local-sync-state/react'

export default function AppleFarm() {
  const numberOfApples = useLocalSyncState(0)
  numberOfApples.get() // 0
}

Usage of external state

Just use useSyncExternalStore of React 18.

import appleState from './appleState'

export default function AppleFarm() {
  const apple = useSyncExternalStore(appleState.onUpdate, appleState.get)
}

Usage of external state with Selector

Just modify the second argument in useSyncExternalStore of React 18.

import appleState from './appleState'

export default function AppleFarm() {
  const appleFarmId = useSyncExternalStore(appleState.onUpdate, () => appleState.get().farmId)

  // Component will rerender only if you change the farmId
}

Browser

Or move the file to your libs directory.

<script src="/node_modules/local-sync-state/dist/browser/local-sync-state.browser.min.js"></script>
<script>
  // localSyncState is available globally now
  var appleName = localSyncState('Bob')
</script>

My Links