-
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.
Merge pull request #26 from poly-state/feat/batch
transaction support
- Loading branch information
Showing
22 changed files
with
2,257 additions
and
1,615 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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/config@2.0.1/schema.json", | ||
"changelog": ["@changesets/changelog-github", { "repo": "poly-state/poly-state" }], | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "public", | ||
"baseBranch": "master", | ||
"updateInternalDependencies": "minor", | ||
"ignore": [] | ||
"$schema": "https://unpkg.com/@changesets/config@2.0.1/schema.json", | ||
"changelog": [ | ||
"@changesets/changelog-github", | ||
{ "repo": "poly-state/poly-state" } | ||
], | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "public", | ||
"baseBranch": "master", | ||
"updateInternalDependencies": "patch", | ||
"ignore": [] | ||
} |
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,27 @@ | ||
--- | ||
'@poly-state/core': minor | ||
'@poly-state/react': patch | ||
--- | ||
|
||
## @poly-state/core: | ||
|
||
- Global transactions support | ||
|
||
```js | ||
import { transact } from '@poly-state/core'; | ||
|
||
transact(() => { | ||
store1.setState({ count: 1 }); | ||
store2.setState({ name: test }); | ||
store2.setState({ address: 'London' }); | ||
}); | ||
``` | ||
|
||
> store 1 and store 2 will be updated only once! | ||
## @poly-state/react: | ||
|
||
- fixed a peer dependency issue for react | ||
- use `useSyncExternalStore` | ||
- use `shallowCompare` | ||
- upgrade packages and bundling |
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 @@ | ||
{ | ||
"mode": "pre", | ||
"tag": "next", | ||
"initialVersions": { | ||
"preact-example": "0.0.1", | ||
"react-example": "0.0.1", | ||
"@poly-state/core": "1.3.0", | ||
"@poly-state/preact": "3.0.0", | ||
"@poly-state/react": "3.0.0" | ||
}, | ||
"changesets": [] | ||
} |
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
{ | ||
"name": "react-example", | ||
"private": true, | ||
"version": "0.0.1", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@poly-state/core": "*", | ||
"@poly-state/react": "*", | ||
"@types/react": "^18.0.0", | ||
"@types/react-dom": "^18.0.0", | ||
"@vitejs/plugin-react": "^1.3.0", | ||
"typescript": "^4.6.3", | ||
"vite": "^2.9.9" | ||
} | ||
} | ||
"name": "react-example", | ||
"private": true, | ||
"version": "0.0.1", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@poly-state/core": "*", | ||
"@poly-state/react": "*", | ||
"@types/react": "^18.0.0", | ||
"@types/react-dom": "^18.0.0", | ||
"@vitejs/plugin-react": "^1.3.0", | ||
"typescript": "^5.2.2", | ||
"vite": "^2.9.9" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -24,6 +24,6 @@ | |
}, | ||
"devDependencies": { | ||
"@changesets/changelog-github": "^0.4.5" | ||
} | ||
}, | ||
"packageManager": "pnpm@8.6.1" | ||
} | ||
|
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
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,45 @@ | ||
import { createStore, transact } from '..'; | ||
|
||
describe('Poly Stat Transact functionality', () => { | ||
it('should perform batch updates', () => { | ||
const store = createStore({ count: 0, name: 'test', other: {} }); | ||
|
||
const listener = jest.fn(); | ||
|
||
store.subscribe(listener); | ||
|
||
transact(() => { | ||
store.setCount(1); | ||
store.setName('test2'); | ||
store.setOther({ test: true }); | ||
}); | ||
|
||
expect(listener).toBeCalledTimes(1); | ||
expect(listener).toHaveBeenCalledWith({ count: 1, name: 'test2', other: { test: true } }); | ||
}); | ||
|
||
it('should perform batched updates across multiple stores', () => { | ||
const store1 = createStore({ count: 0, name: 'test' }); | ||
const store2 = createStore({ count: 0, name: 'test' }); | ||
|
||
const listener1 = jest.fn(); | ||
const listener2 = jest.fn(); | ||
|
||
store1.subscribe(listener1); | ||
store2.subscribe(listener2); | ||
|
||
transact(() => { | ||
store1.setCount(1); | ||
store1.setName('test2'); | ||
|
||
store2.setCount(1); | ||
store2.setName('test2'); | ||
}); | ||
|
||
expect(listener1).toBeCalledTimes(1); | ||
expect(listener1).toHaveBeenCalledWith({ count: 1, name: 'test2' }); | ||
|
||
expect(listener2).toBeCalledTimes(1); | ||
expect(listener2).toHaveBeenCalledWith({ count: 1, name: 'test2' }); | ||
}); | ||
}); |
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,11 @@ | ||
export const GLOBAL_TRANSACT = { | ||
callBacks: new Map<string, () => void>(), | ||
running: false, | ||
}; | ||
|
||
export const transact = (cb: () => void) => { | ||
GLOBAL_TRANSACT.running = true; | ||
cb(); | ||
GLOBAL_TRANSACT.callBacks.forEach((cb) => cb()); | ||
GLOBAL_TRANSACT.running = false; | ||
}; |
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
Oops, something went wrong.