Skip to content

Commit

Permalink
Adds onCommand support to plugins. (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
skellock committed Aug 23, 2016
1 parent 7302cd5 commit b5b8083
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
8 changes: 7 additions & 1 deletion packages/reactotron-core-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ export class Client {
})

// fires when we receive a command, just forward it off
socket.on('command', command => onCommand && onCommand(command))
socket.on('command', command => {
// trigger our own command handler
onCommand && onCommand(command)

// trigger our plugins onCommand
R.forEach(plugin => plugin.onCommand && plugin.onCommand(command), this.plugins)
})

// assign the socket to the instance
this.socket = socket
Expand Down
32 changes: 13 additions & 19 deletions packages/reactotron-core-client/test/plugin-on-command-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,31 @@ import getFreePort from './_get-free-port'
import socketClient from 'socket.io-client'
import socketServer from 'socket.io'

test.cb('plugins support send', t => {
test.cb('plugins support command', t => {
const mockType = 'type'
const mockPayload = 'payload'

// odd way to hold on to the plugin's send function
let capturedSend

// the plugin to extract the send function
// the plugin to capture the command
const plugin = config => {
capturedSend = config.send
return {}
return {
onCommand: command => {
t.is(command.type, mockType)
t.deepEqual(command.payload, mockPayload)
t.end()
}
}
}

getFreePort(port => {
// the server waits for the command
socketServer(port)
.on('connection', socket => {
// send through the one we recieved in the plugin
capturedSend(mockType, mockPayload)

// fires the server receives a command
socket.on('command', ({type, payload}) => {
t.is(type, mockType)
t.deepEqual(payload, mockPayload)
t.end()
})
socket.emit('command', {type: mockType, payload: mockPayload})
})

// create the client, add the plugin, and connect
createClient({ io: socketClient, port: port })
.use(plugin)
.connect()
const client = createClient({ io: socketClient, port: port })
client.use(plugin)
client.connect()
})
})
40 changes: 40 additions & 0 deletions packages/reactotron-core-client/test/plugin-send-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import test from 'ava'
import { createClient } from '../src'
import getFreePort from './_get-free-port'
import socketClient from 'socket.io-client'
import socketServer from 'socket.io'

test.cb('plugins support send', t => {
const mockType = 'type'
const mockPayload = 'payload'

// odd way to hold on to the plugin's send function
let capturedSend

// the plugin to extract the send function
const plugin = config => {
capturedSend = config.send
return {}
}

getFreePort(port => {
// the server waits for the command
socketServer(port)
.on('connection', socket => {
// send through the one we recieved in the plugin
capturedSend(mockType, mockPayload)

// fires the server receives a command
socket.on('command', ({type, payload}) => {
t.is(type, mockType)
t.deepEqual(payload, mockPayload)
t.end()
})
})

// create the client, add the plugin, and connect
createClient({ io: socketClient, port: port })
.use(plugin)
.connect()
})
})

0 comments on commit b5b8083

Please sign in to comment.