broadcast messages from one popup over parent to others

This commit is contained in:
HF 2022-09-05 11:10:22 +02:00
parent ec2907c0a6
commit 47ac68c34a
3 changed files with 16 additions and 4 deletions

View File

@ -24,7 +24,11 @@ class PopUps {
if (~pos) this.wins.splice(pos, 1); if (~pos) this.wins.splice(pos, 1);
} }
dispatch(msg) { /*
* send message to all popups
* except the ignore one
*/
dispatch(msg, ignore = null) {
const { wins } = this; const { wins } = this;
try { try {
for (let i = 0; i < wins.length; i += 1) { for (let i = 0; i < wins.length; i += 1) {
@ -34,7 +38,9 @@ class PopUps {
i -= 1; i -= 1;
continue; continue;
} }
win.postMessage(msg, this.origin); if (win !== ignore) {
win.postMessage(msg, this.origin);
}
} }
} catch { } catch {
return false; return false;

View File

@ -17,7 +17,9 @@ window.addEventListener('beforeunload', () => {
export default (store) => (next) => (action) => { export default (store) => (next) => (action) => {
if (action instanceof MessageEvent) { if (action instanceof MessageEvent) {
if (action.origin !== origin) { if (action.origin !== origin
|| !action.data.type
) {
return null; return null;
} }
if (action.data.type === 't/UNLOAD') { if (action.data.type === 't/UNLOAD') {

View File

@ -9,7 +9,9 @@ import popUps from '../../core/popUps';
export default (store) => (next) => (action) => { export default (store) => (next) => (action) => {
if (action instanceof MessageEvent) { if (action instanceof MessageEvent) {
if (action.origin !== window.location.origin) { if (action.origin !== window.location.origin
|| !action.data.type
) {
return null; return null;
} }
if (action.data.type === 't/UNLOAD') { if (action.data.type === 't/UNLOAD') {
@ -23,6 +25,8 @@ export default (store) => (next) => (action) => {
); );
popUps.add(action.source); popUps.add(action.source);
console.log('popup added'); console.log('popup added');
} else if (action.data.type.startsWith('s/')) {
popUps.dispatch(action.data, action.source);
} }
return next(action.data); return next(action.data);
} }