-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add Readme - add description - fixes #8
- Loading branch information
1 parent
1d7c347
commit 6a75572
Showing
9 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,23 +20,41 @@ jobs: | |
with: | ||
node-version: 18 | ||
registry-url: https://registry.npmjs.org | ||
- run: npm i -g pnpm && pnpm i --filter nextjs-themes | ||
- run: npm i -g pnpm && pnpm i | ||
name: Install dependencies | ||
- name: Test | ||
run: npm test | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v3 | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
run: pnpm turbo build --filter persistnsync && npm test | ||
- name: Setup Git | ||
run: | | ||
git config --global user.name "mayank1513" | ||
git config --global user.email "[email protected]" | ||
git fetch | ||
git checkout main | ||
- name: update version and push back to the repo | ||
run: cp -r ../../.changeset . && pnpm changeset version && git push origin main | ||
- name: Publish to npm | ||
run: pnpm changeset version && git push origin main | ||
working-directory: . | ||
|
||
- name: Publish persistNsync to NPM | ||
id: persistNsync | ||
working-directory: ./packages/persistnsync | ||
continue-on-error: true | ||
run: pnpm publish-package && pnpm publish-package2 && pnpm publish-package3 | ||
- uses: actions/setup-node@v3 | ||
if: steps.persistNsync.outcome == 'success' | ||
with: | ||
registry-url: https://npm.pkg.github.com/ | ||
- name: Publish to GitHub Public Repository | ||
if: steps.persistNsync.outcome == 'success' | ||
working-directory: ./packages/persistnsync | ||
run: pnpm publish-gpr && pnpm publish-package2 && pnpm publish-package3 | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} | ||
- uses: actions/setup-node@v3 | ||
if: steps.persistNsync.outcome == 'success' | ||
with: | ||
registry-url: https://registry.npmjs.org | ||
|
||
- name: Publish nextjs-themes to NPM | ||
run: pnpm build && pnpm publish-package && pnpm publish-package2 | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# PersistAndSync Zustand Store [![Version](https://img.shields.io/npm/v/persistnsync.svg?colorB=green)](https://www.npmjs.com/package/persistnsync) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/dt/persistnsync.svg)](https://www.npmjs.com/package/persistnsync) [![Unit Tests](https://github.com/mayank1513/nextjs-themes/actions/workflows/test.yml/badge.svg)](https://github.com/mayank1513/nextjs-themes/actions/workflows/test.yml) | ||
|
||
> Zustand middleware to easily persist and sync Zustand state between tabs / windows / iframes (SameOrigin) | ||
> Motivation: Recently I got cought up in several issues working with persist miggleware and syncing tabs with zustand. This is a simple light weight middleware to persist and instantly share state between tabs or windows | ||
- ✅ 🐙 size cross-tab state sharing 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 | ||
- ✅ Share state between multiple browsing contexts | ||
|
||
## Install | ||
|
||
```bash | ||
$ pnpm add persistnsync | ||
# or | ||
$ npm install persistnsync | ||
# or | ||
$ yarn add persistnsync | ||
``` | ||
|
||
## Usage | ||
|
||
Simply add the middleware while creating the store and the rest will be taken care. | ||
|
||
```ts | ||
import { create } from "zustand"; | ||
import { persistNSync } from "persistnsync"; | ||
|
||
type MyStore = { | ||
count: number; | ||
set: (n: number) => void; | ||
}; | ||
|
||
const useStore = create<MyStore>( | ||
persistNSync( | ||
set => ({ | ||
count: 0, | ||
set: n => set({ count: n }), | ||
}), | ||
{ name: "my-channel" }, | ||
), | ||
); | ||
``` | ||
|
||
⚡🎉Boom! Just a couple of lines and your state perfectly syncs between tabs/windows and it is also persisted using `localStorage`! | ||
|
||
## License | ||
|
||
Licensed as MIT open source. | ||
|
||
<hr /> | ||
|
||
<p align="center" style="text-align:center">with 💖 by <a href="https://mayank-chaudhari.vercel.app" target="_blank">Mayank Kumar Chaudhari</a></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,47 @@ | ||
{ | ||
"name": "persistnsync", | ||
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>", | ||
"version": "0.0.0", | ||
"description": "", | ||
"description": "Zustand middleware to easily persist and sync Zustand state between tabs and windows", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/mayank1513/nextjs-themes#readme", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc && node createPackageJSON.js" | ||
"build": "tsc && node createPackageJSON.js", | ||
"publish-package": "cp README.md dist && cd dist && npm publish && cd ..", | ||
"publish-package2": "node seo.js && cd dist && npm publish && cd ..", | ||
"publish-package3": "node seo1.js && cd dist && npm publish && cd ..", | ||
"publish-gpr": "node createPackageJSON.js && cp README.md dist && node prepGPR.js && cd dist && npm publish && cd .." | ||
}, | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/mayank1513" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/node": "^20.4.5", | ||
"typescript": "^5.1.6", | ||
"zustand": "^4.4.1" | ||
}, | ||
"peerDependencies": { | ||
"zustand": "^3 || ^4" | ||
} | ||
}, | ||
"keywords": [ | ||
"web", | ||
"api", | ||
"broadcast", | ||
"channel", | ||
"broadcast-channel", | ||
"hooks", | ||
"react", | ||
"react 18", | ||
"zustand", | ||
"middleware", | ||
"state", | ||
"optimized", | ||
"tiny", | ||
"typescript", | ||
"javascript" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const packageJsonPath = path.resolve(__dirname, "dist", "package.json"); | ||
const packageJson = require(packageJsonPath); | ||
packageJson.name = `@mayank1513/${packageJson.name}`; | ||
packageJson.publishConfig = { | ||
"@mayank1513:registry": "https://npm.pkg.github.com", | ||
}; | ||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const name = "persist-n-sync"; | ||
const ref = "persistnsync"; | ||
|
||
const packageJsonPath = path.resolve(__dirname, "dist", "package.json"); | ||
const packageJson = require(packageJsonPath); | ||
packageJson.name = packageJson.name.replace(ref, name); | ||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
|
||
const readMePath = path.resolve(__dirname, "dist", "README.md"); | ||
let readMe = fs.readFileSync(readMePath, { encoding: "utf8" }); | ||
readMe = readMe.replace(new RegExp(ref, "g"), name); | ||
fs.writeFileSync(readMePath, readMe); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const name = "persist-and-sync"; | ||
const ref = "persist-n-sync"; | ||
|
||
const packageJsonPath = path.resolve(__dirname, "dist", "package.json"); | ||
const packageJson = require(packageJsonPath); | ||
packageJson.name = packageJson.name.replace(ref, name); | ||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
|
||
const readMePath = path.resolve(__dirname, "dist", "README.md"); | ||
let readMe = fs.readFileSync(readMePath, { encoding: "utf8" }); | ||
readMe = readMe.replace(new RegExp(ref, "g"), name); | ||
fs.writeFileSync(readMePath, readMe); |