-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Remove message-angular to use subscribers from core
- Loading branch information
1 parent
c2bf35e
commit 4d2d0be
Showing
9 changed files
with
122 additions
and
232 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
class AvMessage { | ||
subscribers = {}; | ||
|
||
constructor() { | ||
this.isEnabled = true; | ||
this.DEFAULT_EVENT = 'avMessage'; | ||
this.DOMAIN = /https?:\/\/([\w\d-]+\.)?availity\.(com|net)/; | ||
window.addEventListener('message', this.getEventData); | ||
} | ||
|
||
enabled(value) { | ||
if (arguments.length) { | ||
this.isEnabled = !!value; | ||
} | ||
return this.isEnabled; | ||
} | ||
|
||
getEventData = event => { | ||
if ( | ||
!this.isEnabled || // do nothing if not enabled | ||
!event || | ||
!event.data || | ||
!event.origin || | ||
!event.source || // check event exists and has necesary properties | ||
event.source === window || // don't process messages emitted from the same window | ||
!this.isDomain(event.origin) | ||
) { | ||
// check origin as trusted domain | ||
return; | ||
} | ||
|
||
let { data } = event; | ||
|
||
if (typeof data === 'string') { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
// warn about error | ||
} | ||
} | ||
|
||
if (typeof data === 'string') { | ||
event = data; | ||
data = undefined; | ||
} else { | ||
event = (data && data.event) || this.DEFAULT_EVENT; | ||
} | ||
|
||
this.onMessage(event, data); | ||
}; | ||
|
||
subscribe(event, fn) { | ||
if (!this.subscribers[event]) { | ||
this.subscribers[event] = []; | ||
} | ||
this.subscribers[event].push(fn); | ||
return () => { | ||
this.subscribers[event] = this.subscribers[event].filter( | ||
val => val !== fn | ||
); | ||
}; | ||
} | ||
|
||
// remove all subscribers for this event | ||
unsubscribe(event) { | ||
delete this.subscribers[event]; | ||
} | ||
|
||
unsubscribeAll() { | ||
this.subscribers = {}; | ||
} | ||
|
||
onMessage(event, data) { | ||
if (this.subscribers[event]) { | ||
this.subscribers[event].forEach(fn => { | ||
fn(data); | ||
}); | ||
} | ||
} | ||
|
||
// if current domain doesn't match regex DOMAIN, return true. | ||
isDomain(url) { | ||
return !this.DOMAIN.test(this.domain()) || this.DOMAIN.test(url); | ||
} | ||
|
||
domain() { | ||
if (window.location.origin) { | ||
return window.location.origin; | ||
} | ||
|
||
if (window.location.hostname) { | ||
return `${window.location.protocol}//${window.location.hostname}${ | ||
window.location.port ? `:${window.location.port}` : '' | ||
}`; | ||
} | ||
|
||
return '*'; | ||
} | ||
|
||
send(payload, target = window.top) { | ||
if (!this.isEnabled || !payload) { | ||
// ingore send calls if not enabled | ||
return; | ||
} | ||
try { | ||
const message = | ||
typeof payload === 'string' ? payload : JSON.stringify(payload); | ||
target.postMessage(message, this.domain()); | ||
} catch (err) { | ||
console.warn('AvMessage.send() ', err); // eslint-disable-line | ||
} | ||
} | ||
} | ||
|
||
export default AvMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,3 @@ | ||
class AvMessage { | ||
subscribers = {}; | ||
import AvMessage from './AvMessage'; | ||
|
||
constructor() { | ||
this.isEnabled = true; | ||
this.DEFAULT_EVENT = 'avMessage'; | ||
this.DOMAIN = /https?:\/\/([\w\d-]+\.)?availity\.(com|net)/; | ||
window.addEventListener('message', ::this.getEventData); | ||
} | ||
|
||
enabled(value) { | ||
if (arguments.length) { | ||
this.isEnabled = !!value; | ||
} | ||
return this.isEnabled; | ||
} | ||
|
||
getEventData(event) { | ||
if ( | ||
!this.isEnabled || // do nothing if not enabled | ||
!event || | ||
!event.data || | ||
!event.origin || | ||
!event.source || // check event exists and has necesary properties | ||
event.source === window || // don't process messages emitted from the same window | ||
!this.isDomain(event.origin) | ||
) { | ||
// check origin as trusted domain | ||
return; | ||
} | ||
|
||
let { data } = event; | ||
|
||
if (typeof data === 'string') { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
// warn about error | ||
} | ||
} | ||
|
||
if (typeof data === 'string') { | ||
event = data; | ||
data = undefined; | ||
} else { | ||
event = (data && data.event) || this.DEFAULT_EVENT; | ||
} | ||
|
||
this.onMessage(event, data); | ||
} | ||
|
||
subscribe(event, fn) { | ||
if (!this.subscribers[event]) { | ||
this.subscribers[event] = []; | ||
} | ||
this.subscribers[event].push(fn); | ||
return () => { | ||
this.subscribers[event] = this.subscribers[event].filter( | ||
val => val !== fn | ||
); | ||
}; | ||
} | ||
|
||
// remove all subscribers for this event | ||
unsubscribe(event) { | ||
delete this.subscribers[event]; | ||
} | ||
|
||
unsubscribeAll() { | ||
this.subscribers = {}; | ||
} | ||
|
||
onMessage(event, data) { | ||
if (this.subscribers[event]) { | ||
this.subscribers[event].forEach(fn => { | ||
fn(data); | ||
}); | ||
} | ||
} | ||
|
||
// if current domain doesn't match regex DOMAIN, return true. | ||
isDomain(url) { | ||
return !this.DOMAIN.test(this.domain()) || this.DOMAIN.test(url); | ||
} | ||
|
||
domain() { | ||
if (window.location.origin) { | ||
return window.location.origin; | ||
} | ||
|
||
if (window.location.hostname) { | ||
return `${window.location.protocol}//${window.location.hostname}${ | ||
window.location.port ? `:${window.location.port}` : '' | ||
}`; | ||
} | ||
|
||
return '*'; | ||
} | ||
|
||
send(payload, target) { | ||
if (!this.isEnabled || !payload) { | ||
// ingore send calls if not enabled | ||
return; | ||
} | ||
try { | ||
const message = | ||
typeof payload === 'string' ? payload : JSON.stringify(payload); | ||
target = target || window.parent; | ||
target.postMessage(message, this.domain()); | ||
} catch (err) { | ||
console.warn('AvMessage.send() ', err); // eslint-disable-line | ||
} | ||
} | ||
} | ||
|
||
export default AvMessage; | ||
export default new AvMessage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters