Skip to content

Commit

Permalink
🚨 Add more logging to event pipeline, 0.0.177
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzak committed Mar 5, 2019
1 parent 67d1af9 commit 10a30f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/electron/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rosalind",
"productName": "Rosalind",
"version": "0.0.176",
"version": "0.0.177",
"private": true,
"description": "Rosalind",
"author": {
Expand Down
16 changes: 7 additions & 9 deletions app/electron/renderer/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ try {
// Can't just log/stringify the message object as it would just print { isTrusted: true }
// Always need to read .data and .origin directly.
const listener = (messageEvent) => {
// console.log(`preload: ${JSON.stringify(message)} (${JSON.stringify(message.data)}) [(${JSON.stringify(message.origin)})] {${message.source === window}}`)
try {
if (messageEvent.source !== window) {
return
Expand All @@ -94,20 +93,19 @@ try {
return
}

logger.info(`[Preload] Message ${JSON.stringify(messageEvent.data)}`)

const json = messageEvent.data.slice(eventPrefix.length)

const parsed = JSON.parse(json)

if (!((Object.keys(parsed).length === 2) && parsed.name && parsed.payload)) {
throw new Error(`Invalid shape of event data: ${Object.keys(parsed).join(',')}`)
}

if (toNative.indexOf(parsed.name) === -1) {
return
}

logger.info(`[Preload] Message ${JSON.stringify(messageEvent.data)}`)

if (!((Object.keys(parsed).length === 2) && parsed.name && parsed.payload)) {
throw new Error(`Invalid shape of event data: ${Object.keys(parsed).join(',')}`)
}

logger.info(`[Preload] sending to ipc: ${parsed.name}`)

ipcRenderer.send(parsed.name, parsed.payload)
Expand All @@ -127,7 +125,7 @@ try {
eventPrefix,
JSON.stringify(event)
].join('')
logger.info(`[Preload] Posting event ${name} ${eventString}`)
logger.info(`[Preload] Posting event ${name}`)
window.postMessage(eventString, targetOrigin)
} catch (e) {
logger.error(`[Preload] Failed to serialize event ${name}`, e)
Expand Down
15 changes: 13 additions & 2 deletions app/meteor/imports/startup/client/native/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const toWeb = [
'dataTransfer'
]

const DEBUG = true

const eventPrefix = 'rslndNative*'
const events = new EventEmitter()

Expand Down Expand Up @@ -47,7 +49,7 @@ export const onNativeEvent = (event, callback) => {
}

export const toNative = (name, payload = {}) => {
console.log('[Native] Posting message', name, payload)
console.log('[Native] Posting message', name)

if (window.isAndroid) {
console.error('[Native] Android interface not implemented')
Expand All @@ -58,20 +60,28 @@ export const toNative = (name, payload = {}) => {

export const getClientKey = () => clientKey

const debug = msg => DEBUG && console.log(`[Debug] ${msg}`)

const listener = messageEvent => {
debug(`Received message ${messageEvent.origin}`)

if (messageEvent.source !== window) {
debug('Discarding event becuase sources do not match')
return
}

if (!isValidOrigin(messageEvent.origin)) {
debug('Discarding event becuase origin is invalid')
return
}

if (typeof messageEvent.data !== 'string') {
debug('Discarding event because data is not a string')
return
}

if (messageEvent.data.indexOf(eventPrefix) !== 0) {
debug('Discarding event because prefix is missing')
return
}

Expand All @@ -82,10 +92,11 @@ const listener = messageEvent => {
}

if (toWeb.indexOf(parsed.name) === -1) {
debug('Discarding event because name is not whitelisted in toWeb')
return
}

console.log(`[Native] Emitting ${parsed.name} ${JSON.stringify(parsed.payload)}`)
console.log(`[Native] Emitting ${parsed.name}`)
events.emit(parsed.name, parsed.payload)
}

Expand Down

0 comments on commit 10a30f0

Please sign in to comment.