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

@solid-hooks/persist

v0.1.1

Published

persist signal or store to storage for solid-js

Downloads

10

Readme

@solid-hooks/persist

Install

npm i @solid-hooks/persist
yarn add @solid-hooks/persist
pnpm add @solid-hooks/persist

Usage

import { usePersist } from '@solid-hooks/persist'
import { createSignal } from 'solid-js'
import { createStore } from 'solid-js/store'

const [signal, setSignal] = usePersist(createSignal('initial'), 'foo', { storage: sessionStorage })

const [store, setStore] = usePersist(
  createStore({ bar: 'initial', test: { foo: 'initial' } }),
  'testing',
  { paths: ['test.foo'] }
)

standalone usage:

import { usePersistSignal, usePersistStore } from '@solid-hooks/persist'
import { createSignal } from 'solid-js'
import { createStore } from 'solid-js/store'

const [signal, setSignal] = usePersistSignal(createSignal('initial'), 'foo', { storage: sessionStorage })

const [store, setStore] = usePersistStore(
  createStore({ bar: 'initial', test: { foo: 'initial' } }),
  'testing',
  { paths: ['test.foo'] }
)

types:

export type PersistStoreOptions<T extends object, Paths extends Path<T>[] = []> = {
  /**
   * localStorage like api, support async
   * @default localStorage
   */
  storage?: AnyStorage
  /**
   * serializer for persist state
   * @default { read: JSON.parse, write: JSON.stringify }
   */
  serializer?: Serializer<FlattenType<PartialObject<T, Paths>>>
  /**
   * object paths to persist
   * @example ['test.deep.data', 'idList[0]']
   */
  paths?: Paths | undefined
  /**
   * sync persisted data,
   * built-in: {@link storageSync}, {@link messageSync}, {@link wsSync}, {@link multiSync}
   */
  sync?: PersistenceSyncAPI
}

export type PersistSignalOptions<T> = Pick<PersistStoreOptions<any>, 'storage' | 'sync'> & {
  /**
   * serializer for persist state
   * @default { read: JSON.parse, write: JSON.stringify }
   */
  serializer?: Serializer<T>
}

IndexedDB

use IndexedDB storage with idb-keyval (as peerDependencies)

import { createIdbStorage, usePersist } from '@solid-hooks/persist'
import { createSignal } from 'solid-js'

const idbStorage = createIdbStorage('custom-store-name')
const [time, setTime] = usePersist(createSignal(Date.now()), 'time', {
  storage: idbStorage,
}),

Sync API

The storage API has an interesting functionality: if you set an item in one instance of the same page, other instances are notified of the change via the storage event so they can elect to automatically update.

Same as @solid-primitives/storage

storageSync

With storageSync, you can use exactly this API in order to sync to external updates to the same storage.

const [state, setState] = usePersist(createSignal(), { sync: storageSync })

messageSync

With messageSync, you can recreate the same functionality for other storages within the client using either the post message API or broadcast channel API. If no argument is given, it is using post message, otherwise provide the broadcast channel as argument

const [state, setState] = usePersist(createSignal(), {
  storage: customStorage,
  sync: messageSync(),
})

wsSync

With wsSync, you can create your synchronization API based on a web socket connection (either created yourself or by our @solid-primitives/websocket package); this allows synchronization between client and server.

const [state, setState] = usePersist(createSignal(), { sync: wsSync(makeWs(/**/)) })

multiplexSync

You can also multiplex different synchronization APIs using multiplexSync:

const [state, setState] = usePersist(createSignal(), {
  sync: multiplexSync(storageSync, wsSync(ws)),
})

Custom synchronization API

If you want to create your own sync API, you can use the following pattern:

export type PersistenceSyncData = {
  key: string
  newValue: string | null | undefined
  timeStamp: number
  url?: string
}

export type PersistenceSyncCallback = (data: PersistenceSyncData) => void

export type PersistenceSyncAPI = [
  /** subscribes to sync */
  subscribe: (subscriber: PersistenceSyncCallback) => void,
  update: (key: string, value: string | null | undefined) => void,
]

You can use APIs like Pusher or a WebRTC data connection to synchronize your state.