Skip to content

Commit

Permalink
Merge pull request #8 from mayank1513/touchup.js
Browse files Browse the repository at this point in the history
Touchup.js
  • Loading branch information
mayank1513 authored Dec 29, 2023
2 parents 3aabcdf + 8c928fe commit d1caed8
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode"]
"recommendations": ["esbenp.prettier-vscode", "mayank1513.trello-kanban-task-board"]
}
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
# PersistAndSync Zustand Store
# Persist-And-Sync Zustand Store

[![test](https://github.com/react18-tools/persist-and-sync/actions/workflows/test.yml/badge.svg)](https://github.com/react18-tools/persist-and-sync/actions/workflows/test.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/5355eb02cfedc9184e3f/maintainability)](https://codeclimate.com/github/mayank1513/persist-and-sync/maintainability) [![codecov](https://codecov.io/gh/mayank1513/persist-and-sync/graph/badge.svg)](https://codecov.io/gh/mayank1513/persist-and-sync) [![Version](https://img.shields.io/npm/v/persist-and-sync.svg?colorB=green)](https://www.npmjs.com/package/persist-and-sync) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/dt/persist-and-sync.svg)](https://www.npmjs.com/package/persist-and-sync) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/persist-and-sync)

> Zustand middleware to easily persist and sync Zustand state between tabs / windows / iframes (Same Origin)
> Zustand middleware to easily persist and sync Zustand state between tabs/windows/iframes (Same Origin)
> Motivation: Recently I got caught up in several issues working with persist middleware and syncing tabs with Zustand. This is a simple light weight middleware to persist and instantly share state between tabs or windows
> Motivation: Recently I got caught up in several issues working with the persist middleware and syncing tabs with Zustand. This is a simple lightweight middleware to persist and instantly share state between tabs or windows
- ✅ 🐙 (642 Bytes gZiped) ~ 0.5 kB size cross-tab state sharing + persistence for zustand
- ✅ 🐙 ~ 1 kB size cross-tab state sharing + persistence for zustand
- ✅ Full TypeScript Support
- ✅ solid reliability in 1 writing and n reading tab-scenarios (with changing writing tab)
- ✅ Fire and forget approach of always using the latest state. Perfect for single user systems
- ✅ solid reliability in 1 writing and n reading tab scenarios (with changing writing tab)
- ✅ Fire and forget approach of always using the latest state. Perfect for single-user systems
- ✅ Share state between multiple browsing contexts
- ✅ Additional control over which fields to persist-and-sync and which to ignore
- ✅ Additional control over which fields to `persist-and-sync` and which to ignore
- ✅ Optimized for performance using memoization and closures.

## Install

```bash
$ pnpm add persist-and-sync
# or
```

**or**

```bash
$ npm install persist-and-sync
# or
```

**or**

```bash
$ yarn add persist-and-sync
```

## Usage

Simply add the middleware while creating the store and the rest will be taken care.
Add the middleware while creating the store and the rest will be taken care.

```ts
import { create } from "zustand";
Expand All @@ -43,7 +51,7 @@ const useStore = create<MyStore>(
count: 0,
set: n => set({ count: n }),
}),
{ name: "my-channel" },
{ name: "my-example" },
),
);
```
Expand Down Expand Up @@ -114,15 +122,15 @@ For more details about regExp check out - [JS RegExp](https://www.w3schools.com/

### Exact match

For exactly matching a parameter/field use `/^your-field-name$/`. `^` forces match from the first caracter and similarly, `$` forces match until last character.
For exactly matching a parameter/field use `/^your-field-name$/`. `^` forces match from the first caracter and similarly, `$` forces match until the last character.

### Ignore multiple fields with exact match

use `regExpToIgnore: /^(field1|field2|field3)$/`

### 🤩 Don't forger to start [this repo](https://github.com/mayank1513/persist-and-sync)!
### 🤩 Don't forget to star [this repo](https://github.com/mayank1513/persist-and-sync)!

Want hands-on course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)
Want a hands-on course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)

## License

Expand Down
7 changes: 7 additions & 0 deletions examples/nextjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# nextjs-example

## 1.0.4

### Patch Changes

- Updated dependencies
- [email protected]

## 1.0.3

### Patch Changes
Expand Down
14 changes: 7 additions & 7 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-example",
"version": "1.0.3",
"version": "1.0.4",
"private": true,
"scripts": {
"dev": "next dev --port 3001",
Expand All @@ -9,19 +9,19 @@
"lint": "next lint"
},
"dependencies": {
"next": "^14.0.3",
"next": "^14.0.4",
"persist-and-sync": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"zustand": "^4.4.7"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.0.3",
"@types/node": "^20.10.2",
"@types/react": "^18.2.41",
"@types/react-dom": "^18.2.17",
"@next/eslint-plugin-next": "^14.0.4",
"@types/node": "^20.10.5",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"eslint-config-custom": "workspace:*",
"tsconfig": "workspace:*",
"typescript": "^5.3.2"
"typescript": "^5.3.3"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"eslint": "^8.55.0",
"prettier": "^3.1.0",
"eslint": "^8.56.0",
"prettier": "^3.1.1",
"tsconfig": "workspace:*",
"turbo": "^1.10.16"
"turbo": "^1.11.2"
},
"packageManager": "[email protected]",
"name": "turbo-template"
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"private": true,
"devDependencies": {
"@vercel/style-guide": "^5.1.0",
"eslint-config-turbo": "^1.10.16"
"eslint-config-turbo": "^1.11.2"
}
}
6 changes: 6 additions & 0 deletions packages/persist-and-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# persist-and-sync

## 1.1.2

### Patch Changes

- Update dependencies

## 1.1.1

### Patch Changes
Expand Down
16 changes: 16 additions & 0 deletions packages/persist-and-sync/legacy-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

const fs = require("fs");
const path = require("path");
const packageJson = require(path.resolve(__dirname, "package.json"));

delete packageJson.scripts;
packageJson.main = packageJson.main.split("/")[1];
packageJson.types = packageJson.types.split("/")[1];
packageJson.name = "persistnsync";

fs.writeFileSync(
path.resolve(__dirname, "dist", "package.json"),
JSON.stringify(packageJson, null, 2),
);

22 changes: 11 additions & 11 deletions packages/persist-and-sync/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "persist-and-sync",
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
"version": "1.1.1",
"version": "1.1.2",
"description": "Zustand middleware to easily persist and sync Zustand state between tabs and windows",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/mayank1513/persist-and-sync.git"
"url": "git+https://github.com/react18-tools/persist-and-sync.git"
},
"bugs": {
"url": "https://github.com/mayank1513/persist-and-sync/issues"
"url": "https://github.com/react18-tools/persist-and-sync/issues"
},
"homepage": "https://github.com/mayank1513/persist-and-sync#readme",
"homepage": "https://github.com/react18-tools/persist-and-sync#readme",
"sideEffects": false,
"license": "MIT",
"scripts": {
"build": "tsc && node createPackageJSON.js",
"publish-package": "cp ../../README.md dist && cd dist && npm publish",
"build": "tsc && node touchup.js",
"publish-package": "cd dist && npm publish && node ../legacy-support.js && npm publish",
"test": "vitest run --coverage"
},
"funding": {
Expand All @@ -26,13 +26,13 @@
},
"devDependencies": {
"@testing-library/react": "^14.1.2",
"@types/node": "^20.10.2",
"@vitejs/plugin-react": "^4.2.0",
"@vitest/coverage-v8": "^0.34.6",
"@types/node": "^20.10.5",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-v8": "^1.1.0",
"jsdom": "^23.0.1",
"octokit": "^3.1.2",
"typescript": "^5.3.2",
"vitest": "^0.34.6",
"typescript": "^5.3.3",
"vitest": "^1.1.0",
"zustand": "^4.4.7"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ fs.writeFileSync(
path.resolve(__dirname, "dist", "package.json"),
JSON.stringify(packageJson, null, 2),
);

fs.copyFileSync(
path.resolve(__dirname, "..", "..", "README.md"),
path.resolve(__dirname, "dist", "README.md"),
);
2 changes: 1 addition & 1 deletion packages/persist-and-sync/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default defineConfig({
setupFiles: ["vitest.setup.ts"],
coverage: {
reporter: ["text", "json", "clover", "html"],
include: ["src/**/*.ts"],
},
threads: true,
mockReset: false,
},
});

0 comments on commit d1caed8

Please sign in to comment.