Skip to content

Commit d995bd1

Browse files
committed
NETPYNE-209 hooked events on notebook load
1 parent fbc4834 commit d995bd1

2 files changed

Lines changed: 40 additions & 30 deletions

File tree

webapp/components/NetPyNE.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
LaunchDialog,
1313
TutorialObserver,
1414
} from 'netpyne/components';
15-
import { loadModel, openDialog } from '../redux/actions/general';
15+
import { openDialog } from '../redux/actions/general';
1616
// import { execPythonMessage } from './general/GeppettoJupyterUtils';
1717
import { replayAll } from './general/CommandRecorder';
1818

@@ -47,7 +47,6 @@ class NetPyNE extends React.Component {
4747
constructor (props) {
4848
super(props);
4949
this.openPythonCallDialog = this.openPythonCallDialog.bind(this);
50-
this.loaded = false;
5150
this.kernelRestartState = {
5251
state: "idle",
5352
kernelID: undefined,
@@ -64,33 +63,7 @@ class NetPyNE extends React.Component {
6463

6564
setDefaultWidgets();
6665

67-
// Listen messages
68-
const loadFromEvent = (event) => {
69-
// Here we would expect some cross-origin check, but we don't do anything more than load a model here
70-
switch (event.data.type) {
71-
case 'INIT_INSTANCE':
72-
if (this.loaded) {
73-
return;
74-
}
75-
this.loaded = true;
76-
console.log('NetPyNE-UI component is ready');
77-
if (window !== window.parent) {
78-
window.parent.postMessage({
79-
type: 'APP_READY',
80-
}, '*');
81-
}
82-
break;
83-
case 'LOAD_RESOURCE':
84-
// eslint-disable-next-line no-case-declarations
85-
const resource = event.data.payload;
86-
this.props.dispatchAction(loadModel(resource));
87-
break;
88-
default:
89-
break;
90-
}
91-
};
92-
// A message from the parent frame can specify the file to load
93-
window.addEventListener('message', loadFromEvent);
66+
9467

9568
// Logic for kernel reinit
9669
const handleKernelRestart = ({ detail: { kernel, type } }) => {

webapp/redux/middleware/middleware.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import {
2626
addInstancesToCanvas,
2727
openConfirmationDialog,
2828
registerModelPath,
29+
loadModel,
2930
LOAD_MODEL
3031
} from '../actions/general';
3132
import { OPEN_BACKEND_ERROR_DIALOG, CLOSE_BACKEND_ERROR_DIALOG, openBackendErrorDialog } from '../actions/errors';
3233
import { closeDrawerDialogBox } from '../actions/drawer';
3334
import Utils from '../../Utils';
34-
import { downloadJsonResponse, downloadPythonResponse } from './utils';
35+
import { downloadJsonResponse, downloadPythonResponse, initParentFrameMessaging } from './utils';
3536
import * as Constants from '../../constants';
3637
import { ADD_EXPERIMENT, REMOVE_EXPERIMENT } from '../actions/experiments';
3738

@@ -249,6 +250,36 @@ export default (store) => (next) => (action) => {
249250

250251
}
251252

253+
254+
/**
255+
* Notify parent frame that the app is ready
256+
* Used in Open Source Brain integration
257+
*/
258+
function initParentFrameMessaging () {
259+
if (window !== window.parent) {
260+
window.parent.postMessage({
261+
type: 'APP_READY',
262+
}, '*');
263+
264+
265+
// Listen messages from the parent frame (Open Source Brain integration)
266+
const loadFromEvent = (event) => {
267+
// Here we would expect some cross-origin check, but we don't do anything more than load a model here
268+
switch (event.data.type) {
269+
case 'LOAD_RESOURCE':
270+
// eslint-disable-next-line no-case-declarations
271+
const resource = event.data.payload;
272+
store.dispatch(loadModel(resource));
273+
break;
274+
default:
275+
break;
276+
}
277+
};
278+
// A message from the parent frame can specify the file to load
279+
window.addEventListener('message', loadFromEvent);
280+
}
281+
}
282+
252283
switch (action.type) {
253284
case GeppettoActions.layoutActions.SET_WIDGETS: {
254285
if (Object.values(action.data).length == 1) {
@@ -291,6 +322,9 @@ export default (store) => (next) => (action) => {
291322
getExperiments()
292323
});
293324
Utils.execPythonMessage(`netpyne_geppetto.saveToIndexFile(None, "${Constants.KERNEL_HANDLING.tmpModelPath}", True, True)`, ()=>{}, false);
325+
326+
327+
initParentFrameMessaging();
294328

295329
setTimeout(() => {
296330
if (!responded) {
@@ -299,6 +333,8 @@ export default (store) => (next) => (action) => {
299333
.then(() => window.location.reload());
300334
}
301335
}, TIMEOUT);
336+
337+
302338
break;
303339
}
304340
case OPEN_BACKEND_ERROR_DIALOG:
@@ -529,3 +565,4 @@ export default (store) => (next) => (action) => {
529565
}
530566
}
531567
};
568+

0 commit comments

Comments
 (0)