@@ -2,7 +2,8 @@ import React, { useCallback, useEffect, useRef } from "react";
2
2
import * as Tone from "tone" ;
3
3
import { observer } from "mobx-react-lite" ;
4
4
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" ;
6
7
import { useAppConfigContext } from "../hooks/use-app-config-context" ;
7
8
import { useRootStore } from "../hooks/use-root-store" ;
8
9
import { useAriaLive } from "../contexts/aria-live-context" ;
@@ -18,7 +19,6 @@ import "./App.scss";
18
19
19
20
const kPluginName = "DAVAI" ;
20
21
const kVersion = "0.1.0" ;
21
- const kPollGraphUpdatesInterval = 5000 ;
22
22
23
23
export const App = observer ( ( ) => {
24
24
const appConfig = useAppConfigContext ( ) ;
@@ -55,7 +55,7 @@ export const App = observer(() => {
55
55
// documentation of the documentChangeNotice object here:
56
56
// https://github.com/concord-consortium/codap/wiki/CODAP-Data-Interactive-Plugin-API#documentchangenotice
57
57
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
59
59
assistantStoreRef . current . transcriptStore . addMessage ( DEBUG_SPEAKER , {
60
60
description : "Document change notice" , content : formatJsonMessage ( notification )
61
61
} ) ;
@@ -77,9 +77,21 @@ export const App = observer(() => {
77
77
}
78
78
} , [ handleDataContextChangeNotice ] ) ;
79
79
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
+
80
91
useEffect ( ( ) => {
81
92
const init = async ( ) => {
82
93
await initializePlugin ( { pluginName : kPluginName , version : kVersion , dimensions} ) ;
94
+ addComponentListener ( handleComponentChangeNotice ) ;
83
95
addDataContextsListListener ( handleDocumentChangeNotice ) ;
84
96
const dataContexts = await getListOfDataContexts ( ) ;
85
97
dataContexts . values . forEach ( ( ctx : Record < string , any > ) => {
@@ -88,22 +100,10 @@ export const App = observer(() => {
88
100
} ) ;
89
101
} ;
90
102
91
- const fetchGraphs = async ( ) => {
92
- const graphDetails = await getGraphDetails ( ) ;
93
- sonificationStore . setGraphs ( graphDetails ) ;
94
- } ;
95
-
96
103
init ( ) ;
97
104
fetchGraphs ( ) ;
98
105
selectSelf ( ) ;
99
106
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
- } ;
107
107
// eslint-disable-next-line react-hooks/exhaustive-deps
108
108
} , [ ] ) ;
109
109
0 commit comments