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

localstorage-watcher-powerful

v1.0.2

Published

A watcher for localStorage changes

Downloads

209

Readme

LocalStorage Watcher

A powerful and efficient localStorage watcher that helps you monitor localStorage changes across browser tabs.

一个强大高效的 localStorage 监听器,帮助你监控跨浏览器标签页的 localStorage 变化。

Features 特性

  • 🔄 Real-time localStorage change detection

    实时检测 localStorage 变化

  • 🌐 Cross-tab synchronization

    跨标签页同步

  • ⚡️ Debounce support for high-frequency updates

    支持高频更新防抖

  • 🛡️ Type-safe with TypeScript

    TypeScript 类型安全

  • 🎯 Single instance pattern

    单例模式

Installation 安装

npm install localstorage-watcher-powerful

Usage 使用方法

Basic Usage 基本用法

import LocalStorageWatcher from 'localstorage-watcher-powerful';

// Initialize the watcher
// 初始化监听器
LocalStorageWatcher.init();

// Watch for changes to a specific key
// 监听特定 key 的变化
LocalStorageWatcher.watch('user-preferences', (newValue, oldValue) => {
  console.log('New value:', newValue);
  console.log('Old value:', oldValue);
});

// Set a value (will trigger watchers)
// 设置值(会触发监听器)
localStorage.setItem('user-preferences', JSON.stringify({ theme: 'dark' }));

Debounced Watching 防抖监听

// Watch with debounce (300ms default)
// 使用防抖监听(默认 300ms)
LocalStorageWatcher.watchWithDebounce('frequently-updated-key', (newValue, oldValue) => {
  console.log('Debounced update:', newValue);
}, 500); // Optional custom delay 可选的自定义延迟

Debug Mode 调试模式

// Enable debug logging
// 启用调试日志
LocalStorageWatcher.enableDebug();

Cleanup 清理

// Remove a specific watcher
// 移除特定的监听器
LocalStorageWatcher.unwatch('user-preferences');

// Clear all watchers
// 清除所有监听器
LocalStorageWatcher.clearWatchers();

// Completely destroy the watcher
// 完全销毁监听器
LocalStorageWatcher.destroy();

API Reference API 参考

Methods 方法

init()

Initializes the localStorage watcher. Must be called before using other methods. 初始化 localStorage 监听器。在使用其他方法前必须调用。

watch(key: string, handler: (newValue: any, oldValue: any) => void)

Watches for changes to a specific localStorage key. 监听特定 localStorage 键的变化。

watchWithDebounce(key: string, handler: (newValue: any, oldValue: any) => void, delay?: number)

Watches for changes with debounce support. 使用防抖方式监听变化。

unwatch(key: string)

Removes the watcher for a specific key. 移除特定键的监听器。

clearWatchers()

Removes all watchers. 移除所有监听器。

destroy()

Completely destroys the watcher instance. 完全销毁监听器实例。

enableDebug()

Enables debug logging. 启用调试日志。

Notes 注意事项

  • Values are automatically serialized/deserialized as JSON

    值会自动进行 JSON 序列化/反序列化

  • Maximum 10 watchers per key by default

    默认每个键最多支持 10 个监听器

  • Works across different browser tabs/windows

    支持跨浏览器标签页/窗口工作

License 许可证

MIT