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 🙏

© 2025 – Pkg Stats / Ryan Hefner

general-form-storage

v1.0.3

Published

A form storage solution with cross-tab synchronization

Readme

FormStorage - 跨标签页表单同步解决方案

npm license

FormStorage 是一个专为 Vue 3 设计的表单状态管理库,提供跨标签页的实时数据同步和自动保存功能。

✨ 特性

  • 跨标签页同步:使用 BroadcastChannel 实现不同标签页间的实时数据同步
  • 自动保存:在页面隐藏或关闭时自动保存表单数据
  • 防抖保存:内置防抖机制,避免频繁保存
  • 数据隔离:通过 formId 区分不同表单的数据
  • 轻量无依赖:无需依赖任何其他模块

📦 安装

npm install general-form-storage
# 或
yarn add general-form-storage

🚀 快速开始

注:以下例子中仅是在 Vue3 环境下的使用教程,实际可脱离 Vue 环境

import { ref } from "vue";
import FormStorage from "general-form-storage";

const formData = ref({
  username: "",
  email: "",
  preferences: {},
});

// 创建 FormStorage 实例
const formStorage = new FormStorage("userForm", formData);

// 初始化
formStorage.init();

// 在组件中使用
watch(
  formData,
  () => {
    formStorage.debouncedSaveData();
  },
  { deep: true }
);

📚 API 文档

FormStorage

构造函数

new FormStorage(formId: string, formData: Ref<object>)
  • formId: 表单唯一标识符
  • formData: Vue 3 的 ref 响应式对象

方法

| 方法名 | 参数 | 返回值 | 描述 | | :----------------------- | :----------------- | :----- | :------------------------------- | | init() | - | void | 初始化,从 localStorage 加载数据 | | saveData() | - | void | 立即保存数据 | | debouncedSaveData() | - | void | 防抖保存(延迟 3000ms) | | loadData() | - | void | 从 localStorage 加载数据 | | clearData() | - | void | 清除当前表单数据 | | syncData(data: object) | data: 要同步的数据 | void | 手动同步数据 | | static clearAll() | - | void | 清除所有表单数据 |

生命周期集成

FormStorage 会自动:

  1. 在页面隐藏时保存数据
  2. 在页面关闭前保存数据
  3. 在跨标签页修改时同步数据

🌟 高级用法

自定义防抖时间

// 在构造函数后覆盖默认防抖函数
formStorage.debouncedSaveData = formStorage.debounce(
  formStorage.saveData,
  5000
);

路由切换时保存

const router = useRouter();

router.beforeEach((to, from) => {
  formStorage.saveData();
  return true;
});

⚠️ 注意事项

  1. 每个表单应有唯一的 formId
  2. 数据大小受 localStorage 限制(通常 5MB)
  3. 不支持 IE 浏览器(依赖 BroadcastChannel)

🤝 贡献

欢迎提交 issue 或 pull request。

📜 许可证

MIT © [empty_reason]