Skip to content

Commit

Permalink
feat(Yorkie): Add yorkie subscribe & doc store
Browse files Browse the repository at this point in the history
  • Loading branch information
hughesgoon committed Aug 19, 2024
1 parent 1126af7 commit 8e63ce9
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/pages/Map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import EditDesignPanel from '../../components/map/BaseMap/EditDesignPanel';
import GlobalNavigationBar from '../../components/global/GlobalNavigationBar';
import { MapMode } from '../../types/enum/MapMode';
import { YorkieDocType } from '../../types/map/object/ObjectInfo';
import useMapInfoStore from '../../stores/mapInfoStore';

//"editor"
//마이페이지 > 편집 가능한 지도에서의 접근이므로, 이미 로그인 된 상태일 것.
Expand All @@ -35,7 +36,13 @@ const Map = () => {
useRegisterStore();
const [dimmed, setDimmed] = useState<boolean>(false);
const [client, setClient] = useState<Client>();
const [doc, setDoc] = useState<Document<any, any>>();
const {
doc,
setDoc: setYorkieDoc,
setInformationAttributes,
setObjects,
setInnerData,
} = useMapInfoStore();

useEffect(() => {
if (mapMode === MapMode.UNVALID) {
Expand Down Expand Up @@ -98,10 +105,11 @@ const Map = () => {
doc.update((root) => {
if (!root.informationAttributes) root.informationAttributes = [];
if (!root.objects) root.objects = [];
setInnerData(root);
}, 'initialize document');

setClient(client);
setDoc(doc);
setYorkieDoc(doc);
};

initializeYorkie();
Expand All @@ -113,6 +121,22 @@ const Map = () => {
};
}, [mapName]);

if (!client || !doc) {
return <div>Loading...</div>;
}

if (mapMode === MapMode.EDIT) {
doc.subscribe('$.informationAttributes', (event) => {
console.log('informationAttributes updated', event);
setInformationAttributes(doc.getRoot().informationAttributes);
});

doc.subscribe('$.objects', (event) => {
console.log('objects updated', event);
setObjects(doc.getRoot().objects);
});
}

// map
return (
<div className={styles.map}>
Expand Down

0 comments on commit 8e63ce9

Please sign in to comment.