@@ -8,34 +8,38 @@ class MainContainer extends Component {
88 constructor ( ) {
99 super ( ) ;
1010 this . state = {
11- snapshots : [ { state : 'snapshot1' , state2 : 'othercomp snapshot1' } , { state : 'snapshot2' } , { state : 'snapshot3' } ] ,
11+ snapshots : [ ] ,
1212 snapshotIndex : 0 ,
13+ port : null ,
1314 } ;
1415
1516 this . handleChangeSnapshot = this . handleChangeSnapshot . bind ( this ) ;
17+ this . handleSendSnapshot = this . handleSendSnapshot . bind ( this ) ;
1618 }
1719
1820 componentDidMount ( ) {
19- // add a listener to capture messages from our backend
20- // this should be in the inject script
21- // window.addEventListener(
22- // 'message',
23- // (event) => {
24- // // We only accept messages from ourselves
25- // if (event.source !== window) return;
26- // console.log(`Message received from backend: ${event.payload}`);
27- // },
28- // false,
29- // );
21+ const port = chrome . runtime . connect ( ) ;
22+ port . onMessage . addListener ( ( snapshots ) => {
23+ console . log ( 'message from background script' , snapshots ) ;
24+ this . setState ( { snapshots } ) ;
25+ } ) ;
26+ port . onDisconnect . addListener ( ( obj ) => {
27+ console . log ( 'disconnected port' , obj ) ;
28+ } ) ;
29+ this . setState ( { port } ) ;
3030 }
3131
32+ // this method changes the snapshotIndex state
33+ // snapshotIndex could be changed from either the ActionContainer or the TravelContainer
3234 handleChangeSnapshot ( snapshotIndex ) {
33- console . log ( 'handleChangeSnapshot' , snapshotIndex ) ;
34- // change snapshotIndex
35- // snapshotIndex could be changed from either the ActionContainer or the TravelContainer
3635 this . setState ( { snapshotIndex } ) ;
3736 }
3837
38+ handleSendSnapshot ( snapshotIndex ) {
39+ const { snapshots, port } = this . state ;
40+ port . postMessage ( { action : 'jumpToSnap' , payload : snapshots [ snapshotIndex ] } ) ;
41+ }
42+
3943 render ( ) {
4044 const { snapshots, snapshotIndex } = this . state ;
4145
@@ -48,13 +52,14 @@ class MainContainer extends Component {
4852 snapshots = { snapshots }
4953 snapshotIndex = { snapshotIndex }
5054 handleChangeSnapshot = { this . handleChangeSnapshot }
55+ handleSendSnapshot = { this . handleSendSnapshot }
5156 />
5257 < StateContainer snapshot = { snapshots [ snapshotIndex ] } />
5358 </ div >
5459 < TravelContainer
55- snapshotsLength = { snapshots . length }
56- handleChangeSnapshot = { this . handleChangeSnapshot }
57- snapshotIndex = { snapshotIndex }
60+ snapshotsLength = { snapshots . length }
61+ handleChangeSnapshot = { this . handleChangeSnapshot }
62+ snapshotIndex = { snapshotIndex }
5863 />
5964 </ div >
6065 ) ;
0 commit comments