Skip to content

Commit f027e1a

Browse files
authored
Merge pull request #45 from concord-consortium/DAVAI-30-subscribe-to-component-level-change-notifications
feat: Subscribe to component-level change notifications (DAVAI-30)
2 parents 2aa5275 + fe50abd commit f027e1a

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/components/App.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React, { useCallback, useEffect, useRef } from "react";
22
import * as Tone from "tone";
33
import { observer } from "mobx-react-lite";
44
import removeMarkdown from "remove-markdown";
5-
import { addDataContextChangeListener, addDataContextsListListener, ClientNotification, getDataContext, getListOfDataContexts, initializePlugin, selectSelf } from "@concord-consortium/codap-plugin-api";
5+
import { addComponentListener, addDataContextChangeListener, addDataContextsListListener, ClientNotification,
6+
getDataContext, getListOfDataContexts, initializePlugin, selectSelf } from "@concord-consortium/codap-plugin-api";
67
import { useAppConfigContext } from "../hooks/use-app-config-context";
78
import { useRootStore } from "../hooks/use-root-store";
89
import { useAriaLive } from "../contexts/aria-live-context";
@@ -18,7 +19,6 @@ import "./App.scss";
1819

1920
const kPluginName = "DAVAI";
2021
const kVersion = "0.1.0";
21-
const kPollGraphUpdatesInterval = 5000;
2222

2323
export const App = observer(() => {
2424
const appConfig = useAppConfigContext();
@@ -55,7 +55,7 @@ export const App = observer(() => {
5555
// documentation of the documentChangeNotice object here:
5656
// https://github.com/concord-consortium/codap/wiki/CODAP-Data-Interactive-Plugin-API#documentchangenotice
5757
const handleDocumentChangeNotice = useCallback(async (notification: ClientNotification) => {
58-
if (notification.values.operation === "dataContextCountChanged") { // ignore the other notifications -- they are not useful for our purposes
58+
if (notification.values.operation === "dataContextCountChanged") { // ignore the other notifications -- they are not useful for our purposes
5959
assistantStoreRef.current.transcriptStore.addMessage(DEBUG_SPEAKER, {
6060
description: "Document change notice", content: formatJsonMessage(notification)
6161
});
@@ -77,9 +77,21 @@ export const App = observer(() => {
7777
}
7878
}, [handleDataContextChangeNotice]);
7979

80+
const fetchGraphs = useCallback(async () => {
81+
const graphDetails = await getGraphDetails();
82+
sonificationStore.setGraphs(graphDetails);
83+
}, [sonificationStore]);
84+
85+
const handleComponentChangeNotice = useCallback((notification: ClientNotification) => {
86+
if (notification.values.type === "graph") {
87+
fetchGraphs();
88+
}
89+
}, [fetchGraphs]);
90+
8091
useEffect(() => {
8192
const init = async () => {
8293
await initializePlugin({pluginName: kPluginName, version: kVersion, dimensions});
94+
addComponentListener(handleComponentChangeNotice);
8395
addDataContextsListListener(handleDocumentChangeNotice);
8496
const dataContexts = await getListOfDataContexts();
8597
dataContexts.values.forEach((ctx: Record<string, any>) => {
@@ -88,22 +100,10 @@ export const App = observer(() => {
88100
});
89101
};
90102

91-
const fetchGraphs = async () => {
92-
const graphDetails = await getGraphDetails();
93-
sonificationStore.setGraphs(graphDetails);
94-
};
95-
96103
init();
97104
fetchGraphs();
98105
selectSelf();
99106

100-
// since updates to graph components do not generate CODAP notifications, we need to poll to keep the list of graphs up to date
101-
// and to update the sonification store with valid graph items
102-
const interval = setInterval(fetchGraphs, kPollGraphUpdatesInterval);
103-
104-
return () => {
105-
clearInterval(interval);
106-
};
107107
// eslint-disable-next-line react-hooks/exhaustive-deps
108108
}, []);
109109

0 commit comments

Comments
 (0)