check for parent refresh and parent close
This commit is contained in:
parent
a4d6b7b1d9
commit
b51303fe6f
|
@ -117,37 +117,41 @@ persistStore(store, {}, () => {
|
||||||
SocketClient.connect();
|
SocketClient.connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
(function () {
|
||||||
renderApp(document.getElementById('app'), store);
|
const onLoad = () => {
|
||||||
|
renderApp(document.getElementById('app'), store);
|
||||||
|
|
||||||
const onKeyPress = createKeyPressHandler(store);
|
const onKeyPress = createKeyPressHandler(store);
|
||||||
document.addEventListener('keydown', onKeyPress, false);
|
document.addEventListener('keydown', onKeyPress, false);
|
||||||
|
|
||||||
// garbage collection
|
// garbage collection
|
||||||
function runGC() {
|
function runGC() {
|
||||||
const renderer = getRenderer();
|
const renderer = getRenderer();
|
||||||
|
|
||||||
const chunks = renderer.getAllChunks();
|
const chunks = renderer.getAllChunks();
|
||||||
if (chunks) {
|
if (chunks) {
|
||||||
const curTime = Date.now();
|
const curTime = Date.now();
|
||||||
let cnt = 0;
|
let cnt = 0;
|
||||||
chunks.forEach((value, key) => {
|
chunks.forEach((value, key) => {
|
||||||
if (curTime > value.timestamp + 300000) {
|
if (curTime > value.timestamp + 300000) {
|
||||||
const [zc, xc, yc] = value.cell;
|
const [zc, xc, yc] = value.cell;
|
||||||
if (!renderer.isChunkInView(zc, xc, yc)) {
|
if (!renderer.isChunkInView(zc, xc, yc)) {
|
||||||
cnt++;
|
cnt++;
|
||||||
if (value.isBasechunk) {
|
if (value.isBasechunk) {
|
||||||
SocketClient.deRegisterChunk([xc, yc]);
|
SocketClient.deRegisterChunk([xc, yc]);
|
||||||
|
}
|
||||||
|
chunks.delete(key);
|
||||||
|
value.destructor();
|
||||||
}
|
}
|
||||||
chunks.delete(key);
|
|
||||||
value.destructor();
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
// eslint-disable-next-line no-console
|
||||||
// eslint-disable-next-line no-console
|
console.log('Garbage collection cleaned', cnt, 'chunks');
|
||||||
console.log('Garbage collection cleaned', cnt, 'chunks');
|
}
|
||||||
}
|
}
|
||||||
}
|
setInterval(runGC, 300000);
|
||||||
setInterval(runGC, 300000);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
document.removeEventListener('DOMContentLoaded', onLoad);
|
||||||
|
};
|
||||||
|
document.addEventListener('DOMContentLoaded', onLoad, false);
|
||||||
|
}());
|
||||||
|
|
23
src/popup.js
23
src/popup.js
|
@ -19,16 +19,7 @@ import SocketClient from './socket/SocketClient';
|
||||||
import renderAppPopUp from './components/AppPopUp';
|
import renderAppPopUp from './components/AppPopUp';
|
||||||
|
|
||||||
persistStore(store, {}, () => {
|
persistStore(store, {}, () => {
|
||||||
window.addEventListener('message', (evt) => {
|
window.addEventListener('message', store.dispatch);
|
||||||
if (evt.data.type === 't/UNLOAD') {
|
|
||||||
if (!window.opener || window.opener.closed) {
|
|
||||||
console.log('Parent window closed');
|
|
||||||
SocketClient.connect();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
store.dispatch(evt);
|
|
||||||
});
|
|
||||||
|
|
||||||
store.dispatch({ type: 'HYDRATED' });
|
store.dispatch({ type: 'HYDRATED' });
|
||||||
|
|
||||||
|
@ -81,8 +72,10 @@ persistStore(store, {}, () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
(function () {
|
||||||
// eslint-disable-next-line no-console
|
const onLoad = () => {
|
||||||
console.log('hello');
|
renderAppPopUp(document.getElementById('app'), store);
|
||||||
renderAppPopUp(document.getElementById('app'), store);
|
document.removeEventListener('DOMContentLoaded', onLoad);
|
||||||
});
|
};
|
||||||
|
document.addEventListener('DOMContentLoaded', onLoad, false);
|
||||||
|
}());
|
||||||
|
|
|
@ -12,11 +12,35 @@ window.addEventListener('beforeunload', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
export default () => (next) => (action) => {
|
export default (store) => (next) => (action) => {
|
||||||
if (action instanceof MessageEvent) {
|
if (action instanceof MessageEvent) {
|
||||||
if (action.origin !== origin) {
|
if (action.origin !== origin) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (action.data.type === 't/UNLOAD') {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!window.opener || window.opener.closed) {
|
||||||
|
console.log('Parent window closed');
|
||||||
|
store.dispatch({ type: 't/PARENT_CLOSED' });
|
||||||
|
} else {
|
||||||
|
console.log('Parent window refreshed');
|
||||||
|
/*
|
||||||
|
* hook to event and also send message to catch more
|
||||||
|
* possibilities
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
const sendLoad = () => {
|
||||||
|
window.opener.postMessage({ type: 't/LOAD' }, origin);
|
||||||
|
window.opener.removeEventListener('DOMContentLoaded', sendLoad);
|
||||||
|
};
|
||||||
|
window.opener.addEventListener('DOMContentLoaded', sendLoad, false);
|
||||||
|
} catch {
|
||||||
|
console.log('Could not hook to parent window');
|
||||||
|
}
|
||||||
|
window.opener.postMessage({ type: 't/LOAD' }, origin);
|
||||||
|
}
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
console.log('GOT', action.data);
|
console.log('GOT', action.data);
|
||||||
return next(action.data);
|
return next(action.data);
|
||||||
}
|
}
|
||||||
|
|
37
src/store/middleware/socketClientHookPopUp.js
Normal file
37
src/store/middleware/socketClientHookPopUp.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Hooks for websocket client for popup window
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
import SocketClient from '../../socket/SocketClient';
|
||||||
|
|
||||||
|
export default () => (next) => (action) => {
|
||||||
|
if (SocketClient.readyState === WebSocket.CLOSED) {
|
||||||
|
if (action.type === 't/PARENT_CLOSED') {
|
||||||
|
SocketClient.connect();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (action.type) {
|
||||||
|
case 'SET_NAME':
|
||||||
|
case 'LOGIN':
|
||||||
|
case 'LOGOUT': {
|
||||||
|
SocketClient.reconnect();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 's/REQ_CHAT_MESSAGE': {
|
||||||
|
const {
|
||||||
|
text,
|
||||||
|
channel,
|
||||||
|
} = action;
|
||||||
|
SocketClient.sendChatMessage(text, channel);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(action);
|
||||||
|
};
|
|
@ -20,7 +20,8 @@ import popup from './reducers/popup';
|
||||||
* middleware
|
* middleware
|
||||||
*/
|
*/
|
||||||
import parent from './middleware/parent';
|
import parent from './middleware/parent';
|
||||||
import titlePopUp from './middleware/titlePopUp';
|
import socketClientHook from './middleware/socketClientHookPopUp';
|
||||||
|
import title from './middleware/titlePopUp';
|
||||||
|
|
||||||
const reducers = combineReducers({
|
const reducers = combineReducers({
|
||||||
...sharedReducers,
|
...sharedReducers,
|
||||||
|
@ -33,7 +34,8 @@ const store = createStore(
|
||||||
applyMiddleware(
|
applyMiddleware(
|
||||||
thunk,
|
thunk,
|
||||||
parent,
|
parent,
|
||||||
titlePopUp,
|
socketClientHook,
|
||||||
|
title,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user