Skip to content

Commit 9cf8ea1

Browse files
committed
wip 24.07.2024
1 parent ce2a95a commit 9cf8ea1

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"react-dom": "18.2.0",
2121
"react-router-dom": "6.16.0",
2222
"remeda": "^1.29.0",
23-
"zustand": "4.4.2"
23+
"rxjs": "^7.8.1",
24+
"zustand": "4.4.2",
25+
"zustand-rx": "^2.0.1"
2426
},
2527
"devDependencies": {
2628
"@iconify-json/lucide": "1.1.131",

pnpm-lock.yaml

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/game/model/subscriptions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { selectCollectedCards } from './selectors'
22
import { useGameState } from './state'
3+
import { GameState } from '.'
4+
import { subscribeWithSelector } from 'zustand/middleware'
5+
import { toStream, StateValueOf } from 'zustand-rx'
6+
import { StoreApi } from 'zustand'
37

48
type GameStateListener<T> = (selectedState: T, previousSelectedState: T) => void
59

@@ -12,5 +16,12 @@ export const listenToAllCardsCollected = (
1216
return cards.length === collectedCards.length * 2
1317
}, listener)
1418

19+
type Test = typeof useGameState;
20+
1521
export const listenToTurnTimer = (listener: GameStateListener<number>) =>
1622
useGameState.subscribe((state) => state.turn.timer, listener)
23+
24+
export const gameEnded$ = toStream<StoreApi<GameState>>(useGameState, state => {
25+
26+
return state.
27+
})

src/shared/model/toStream.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Observable } from 'rxjs'
2+
import { StoreApi } from 'zustand'
3+
4+
/**
5+
* This is a copy-pasted type from
6+
* https://github.com/pmndrs/zustand/blob/a836356b5e4062abde852af3ba5ee6fc3f1a41fe/src/middleware/subscribeWithSelector.ts#L28-L40
7+
*
8+
*
9+
*/
10+
type StoreSubscribeWithSelector<T> = {
11+
subscribe: {
12+
(listener: (selectedState: T, previousSelectedState: T) => void): () => void
13+
<U>(
14+
selector: (state: T) => U,
15+
listener: (selectedState: U, previousSelectedState: U) => void,
16+
options?: {
17+
equalityFn?: (a: U, b: U) => boolean
18+
fireImmediately?: boolean
19+
},
20+
): () => void
21+
}
22+
}
23+
24+
type ToStreamOptions = {
25+
fireImmediately?: boolean
26+
}
27+
28+
/**
29+
* This file is a copy-pasted and modified version of
30+
* https://github.com/patdx/zustand-rx/blob/main/libs/zustand-rx/src/lib/zustand-rx.ts
31+
*
32+
* TODO: Move to zustand-rx package after the following issue will be resolved:
33+
* https://github.com/patdx/zustand-rx/issues/944
34+
*/
35+
export const toStream = <TState, TValue>(
36+
store: StoreApi<TState> & StoreSubscribeWithSelector<TState>,
37+
selector: (value: TState) => TValue,
38+
{ fireImmediately }: ToStreamOptions = {},
39+
): Observable<TValue> => {
40+
return new Observable<TValue>((subscriber) => {
41+
if (fireImmediately) {
42+
subscriber.next(selector(store.getState()))
43+
}
44+
const unsubscribe = store.subscribe(selector, (value) => {
45+
subscriber.next(value)
46+
})
47+
return () => unsubscribe()
48+
})
49+
}

0 commit comments

Comments
 (0)