{"_id":"@dhmk/zustand-subscribe-internal","name":"@dhmk/zustand-subscribe-internal","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@dhmk/zustand-subscribe-internal","version":"0.0.1","description":"Internal subscription queue for zustand","keywords":["zustand","middleware","subscription"],"license":"MIT","repository":{"type":"git","url":"https://github.com/dhmk083/dhmk-zustand-subscribe-internal.git"},"main":"lib/index.js","types":"lib/index.d.ts","module":"esm/index.js","sideEffects":false,"scripts":{"preversion":"yarn test && yarn build","version":"git add -A","postversion":"git push origin HEAD && git push origin HEAD --tags","clean":"rm -rf lib esm","build":"yarn clean && tsc && tsc -m esnext --outDir esm","test":"yarn clean && jest"},"peerDependencies":{"zustand":"^4.0.0"},"devDependencies":{"@types/jest":"^29.5.2","immer":"^10.0.2","jest":"^29.5.0","ts-jest":"^29.1.0","typescript":"^5.1.3","zustand":"^4.3.8"},"jest":{"preset":"ts-jest"},"_id":"@dhmk/zustand-subscribe-internal@0.0.1","dist":{"shasum":"b8b34b134c67176148a2e66c2a861b7aa8d9e925","integrity":"sha512-5Hl3ibmwDl0qhJRzCGbJyEUQ/qD3nEGuAOfwu2lMSv/0Q/NV8CSxhEADZgB97PoUy/EGNMH/Q6Ps5SOZxsoFdw==","tarball":"https://registry.npmjs.org/@dhmk/zustand-subscribe-internal/-/zustand-subscribe-internal-0.0.1.tgz","fileCount":3,"unpackedSize":4321,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCsFR0ycMBmcksAoRI8yDnM0SWifuAD7biLMqRalG401gIgBVLAbj0+NZ853dDyg/XbMYejATRFg/Gyo1jgiINLaQo="}]},"_npmUser":{"name":"dhmk083","email":"dhmk083@gmail.com"},"directories":{},"maintainers":[{"name":"dhmk083","email":"dhmk083@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zustand-subscribe-internal_0.0.1_1687797480324_0.952648282606722"},"_hasShrinkwrap":false}},"time":{"created":"2023-06-26T16:38:00.264Z","0.0.1":"2023-06-26T16:38:00.467Z","modified":"2023-06-26T16:38:00.693Z"},"maintainers":[{"name":"dhmk083","email":"dhmk083@gmail.com"}],"description":"Internal subscription queue for zustand","keywords":["zustand","middleware","subscription"],"repository":{"type":"git","url":"https://github.com/dhmk083/dhmk-zustand-subscribe-internal.git"},"license":"MIT","readme":"# zustand-subscribe-internal\n\nInternal subscription queue for zustand\n\n## Installation\n\n```sh\nnpm install @dhmk/zustand-subscribe-internal\n```\n\n## Description\n\nZustand middleware that adds atomic setters and one more notifications queue alongside with `subscribe` queue.\n\nIn a typical zustand app you have the following algorithm when you call setter function:\n\n1. Update state\n2. Notify listeners attached via `subscribe` function\n\nWith this middleware you will have extra steps:\n\n1. Update state\n2. Notify listeners attached via `subscribeInternal` function\n3. If some of listeners call setter, repeat steps 1-2\n4. Notify listeners attached via `subscribe` function\n\nThis might be useful if you want to manage parts of your app state with subscriptions, allowing them to be loosely coupled, while maintaining correct app state at the same time.\n\nHere is an example (although contrived):\n\nLet's say you have a `dayModule`, which manages days of the current month. And also you have a `monthModule`, which manages months. `dayModule` depends on `monthModule` and the latter doesn't know about the former.\n\nLet's say we have an app state such as current day is 31 and current month is January.\n\nNow, `monthModule` has its `setMonth` function called, and the current month is now February. `monthModule` can not change `dayModule`'s current day to any valid day (since 31 February is incorrect), because in our app architecture it doesn't know about that module. So without this middleware `dayModule` would have to use standard `subscribe` function to change its current day appropriately, but that would result into 2 updates visible to \"public\" listeners (which are attached using `subscribe` function): one with incorrect state and one with correct. This can possibly lead to some bugs further down the road.\n\nInstead, if `dayModule` would use `subscribeInternal` function, it could set its current day to a valid value, before any \"public\" listeners would be called. That would result into 1 and only update with correct state.\n\nThe point is, this middleware allows you to have multiple `internal` states of the app, some of which may be intentionally incorrect, before notifying \"public\" listeners that the state has changed.\n\nThis means that every call of a setter function is now atomic for \"public\" listeners, i.e. each listener will be called exactly one time during that function. Also, if you want to achieve atomicity between several setter calls, you can use `atomic` wrapper function.\n\n## API\n\n### `(default export) subscribeInternal()`\n\nEnhances zustand store with several functions:\n\n#### `subscribeInternal(callback: (state, prevState) => void): UnsubscribeFunction`\n\n#### `atomic<T>(fn: () => T): T`\n\nExample:\n\n```ts\nconst store = createStore(\n  subscribeInternal((set, get, api) => {\n    api.subscribeInternal((state, prevState) => {\n      /* your code here */\n    });\n\n    return {\n      value: 0,\n\n      test() {\n        // `subscribe` listeners will be called only once at the end of this function\n        // despite having multiple `set` operations\n        api.atomic(() => {\n          set({ value: 1 });\n          api.atomic(() => {\n            set({ value: 2 });\n          });\n          set({ value: 3 });\n        });\n      },\n    };\n  })\n);\n```\n","readmeFilename":"README.md"}