Skip to content

Commit f364c84

Browse files
committed
Release 0.1.1: Merge branch 'hotfix-0.1.1'
2 parents 7f8e32d + d5fefeb commit f364c84

File tree

10 files changed

+55
-27
lines changed

10 files changed

+55
-27
lines changed

package-lock.json

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

packages/react-together-primereact/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"clean": "rm -rf node_modules dist"
1919
},
2020
"dependencies": {
21-
"react-together": "^0.1.0"
21+
"react-together": "^0.1.1"
2222
},
2323
"peerDependencies": {
2424
"primereact": "^10.6.6"

packages/react-together/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
## 0.1.1
2+
`2024-08-31`
3+
4+
- Fixed bugs on `useStateTogether`
5+
- Fixed bugs on `useStateTogetherWithPerUserValues`
6+
17
## 0.1.0
28
`2024-08-28`
9+
310
- Optimize `useStateTogether` and `useStateTogetherWithPerUserValues` to minimize unnecessary re-rendering
411
- `useConnectedViews` does not return `null`
512
- Allow to override the session model via `sessionParams.model` prop of the `<ReactTogether/>` component

packages/react-together/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-together",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"type": "module",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

packages/react-together/src/hooks/useStateTogether.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,16 @@ export default function useStateTogether<T>(
3434
const modelValue = model?.state.get(rtKey) as T
3535

3636
// This is the local state
37-
const [value, set_value] = useState<T>(
38-
modelValue !== undefined ? modelValue : initial_value
39-
)
37+
const [value, set_value] = useState<T>(() => {
38+
// This function is only executed on the first render
39+
// If there is no value for this key, publish the initial
40+
// value
41+
if (view && model && !model.state.has(rtKey)) {
42+
view.publish(model.id, 'setState', { id: rtKey, newValue: initial_value })
43+
return initial_value
44+
}
45+
return modelValue
46+
})
4047

4148
useEffect(() => {
4249
if (!session || !view || !model) return

packages/react-together/src/hooks/useStateTogetherWithPerUserValues.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ export default function useStateTogetherWithPerUserValues<
2121
T extends NotUndefined
2222
>(
2323
rtKey: string,
24-
initial_value: T
24+
initialValue: T
2525
): [T, Dispatch<SetStateAction<T>>, { [id: string]: T }] {
26+
// Memorize the first initial value, ignore changes to the
27+
// initialValue prop
28+
// https://react.dev/reference/react/useState
29+
const [actualInitialValue] = useState(initialValue)
30+
2631
const context = useContext(CroquetContext)
2732
let model: null | ReactTogetherModel = null
2833
let session: null | Session = null
@@ -35,11 +40,13 @@ export default function useStateTogetherWithPerUserValues<
3540
}
3641
const viewId = view?.viewId || ''
3742
const [allValuesState, setAllValuesState] = useState<{
38-
value: Map<string, T>
43+
value: { [id: string]: T }
3944
hash: string
4045
}>(() => {
41-
const value = (model?.stateTogether.get(rtKey) ||
42-
new Map([[viewId, initial_value]])) as Map<string, T>
46+
const value = mapToObject(
47+
(model?.stateTogether.get(rtKey) ||
48+
new Map([[viewId, actualInitialValue]])) as Map<string, T>
49+
)
4350
const hash = hash_fn(value)
4451
return { value, hash }
4552
})
@@ -50,12 +57,16 @@ export default function useStateTogetherWithPerUserValues<
5057
if (!session || !view || !model || !viewId) return
5158

5259
const handler = () => {
53-
const newValues = model.stateTogether.get(rtKey) as Map<string, T>
60+
const newValues = mapToObject(
61+
model.stateTogether.get(rtKey) as Map<string, T>
62+
)
5463
const newHash = hash_fn(newValues)
5564

56-
setAllValuesState((prev) =>
57-
prev.hash === newHash ? prev : { value: newValues, hash: newHash }
58-
)
65+
setAllValuesState((prev) => {
66+
return prev.hash === newHash
67+
? prev
68+
: { value: newValues, hash: newHash }
69+
})
5970
}
6071

6172
view.subscribe(
@@ -74,7 +85,7 @@ export default function useStateTogetherWithPerUserValues<
7485
view.publish(model.id, 'setStateTogether', {
7586
id: rtKey,
7687
viewId,
77-
newValue: initial_value
88+
newValue: actualInitialValue
7889
})
7990

8091
return () => {
@@ -87,9 +98,9 @@ export default function useStateTogetherWithPerUserValues<
8798

8899
view.unsubscribe(rtKey, 'updated', handler)
89100
}
90-
}, [session, view, viewId, model, rtKey, initial_value])
101+
}, [session, view, viewId, model, rtKey, actualInitialValue])
91102

92-
const localValue = allValues.get(viewId) || initial_value
103+
const localValue = allValues[viewId] || actualInitialValue
93104

94105
const setter = useCallback(
95106
(newValueOrFn: SetStateAction<T>): void => {
@@ -112,12 +123,12 @@ export default function useStateTogetherWithPerUserValues<
112123
// the same state interface
113124
setAllValuesState((prev) => {
114125
const { value, hash } = prev
115-
const newValue = getNewValue(value.get('')!, newValueOrFn)
126+
const newValue = getNewValue(value[viewId]!, newValueOrFn)
116127
const newHash = hash_fn(newValue)
117128
if (hash === newHash) {
118129
return prev
119130
} else {
120-
value.set('', newValue)
131+
value[viewId] = newValue
121132
return { value, hash: newHash }
122133
}
123134
})
@@ -126,5 +137,5 @@ export default function useStateTogetherWithPerUserValues<
126137
[view, viewId, model, rtKey]
127138
)
128139

129-
return [localValue, setter, mapToObject(allValues)]
140+
return [localValue, setter, allValues]
130141
}

packages/react-together/src/models/ReactTogetherModel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export default class ReactTogetherModel extends ReactModel {
4949
}
5050

5151
handleViewExit(viewId: string): void {
52-
this.stateTogether.forEach((st) => st.delete(viewId))
52+
this.stateTogether.forEach((st, key) => {
53+
st.delete(viewId)
54+
this.publish(key, 'updated', {})
55+
})
5356
}
5457
}
5558
ReactTogetherModel.register('ReactTogetherModel')

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"react": "^18.2.0",
2525
"react-dom": "^18.2.0",
2626
"react-router-dom": "^6.26.1",
27-
"react-together": "^0.1.0",
27+
"react-together": "^0.1.1",
2828
"react-together-primereact": "^0.1.0"
2929
},
3030
"devDependencies": {

website/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"react-markdown": "^9.0.1",
4343
"react-router-dom": "^6.25.1",
4444
"react-syntax-highlighter": "^15.5.0",
45-
"react-together": "^0.1.0",
45+
"react-together": "^0.1.1",
4646
"react-together-primereact": "^0.1.0",
4747
"rehype-raw": "^7.0.0",
4848
"remark-gfm": "^4.0.0",

website/src/components/demo/TinyRpg.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default function TinyRpgTogether() {
159159
{Object.entries(everyonesPosition).map(([viewId, position]) => (
160160
<div
161161
className='character absolute bg-blue-500 rounded-full flex items-center justify-center'
162-
id={`position-${viewId}`}
162+
key={`position-${viewId}`}
163163
style={{
164164
width: `${CELL_SIZE - 4}px`,
165165
height: `${CELL_SIZE - 8}px`,

0 commit comments

Comments
 (0)