diff --git a/.gitignore b/.gitignore index 0ed31c6..ad92419 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -example/node_modules +test/node_modules package-lock.json -example/example \ No newline at end of file +test/test \ No newline at end of file diff --git a/example/node.js b/example/node.js deleted file mode 100644 index 3890bce..0000000 --- a/example/node.js +++ /dev/null @@ -1,23 +0,0 @@ -const IPC = require('ipc-node-go') -const ipc = new IPC('./example') -ipc.init() - -ipc.on('log', console.log) -// Log all error from stderr -ipc.on('error', console.error) -ipc.on('ping', ()=> { - console.log(new Date()) -}) - -// -ipc.send('who', {name:'Akuma Nodejs'}) -// send and receive and an acknowledgement -ipc.sendAndReceive('yoo', 'Hello, how are you doing?', (err, d) => { - err ? console.error(err) : console.log(d) -}) -// listen for event and reply to the channel -ipc.onReceiveAnSend('hola', (channel,data)=>{ - console.log(data) - ipc.send(channel, 'cool thanks') -}) - diff --git a/ipc.go b/ipc.go index 71af04f..024aee8 100644 --- a/ipc.go +++ b/ipc.go @@ -130,7 +130,11 @@ func (ipc IPC) Start() { }() for { reader := bufio.NewReader(os.Stdin) - text, _ := reader.ReadString('\n') + text, err := reader.ReadString('\n') + if err != nil { + log.Println(err) + continue + } if text != "" { var payload payloadReceive text = strings.TrimSuffix(text, "\n") @@ -144,16 +148,21 @@ func (ipc IPC) Start() { if payload.Event == "___EXIT___" { os.Exit(0) } - if payload.SR { - for _, handler := range ipc.receiveSendListerners[payload.Event] { - replyChannel := payload.Event + "___RC___" - handler(replyChannel, payload.Data) - } - } else { - for _, handler := range ipc.receiveListerners[payload.Event] { - handler(payload.Data) + // Run the handlers in a goroutine to prevent + // https://github.com/Akumzy/ipc/issues/1 + go func() { + + if payload.SR { + for _, handler := range ipc.receiveSendListerners[payload.Event] { + replyChannel := payload.Event + "___RC___" + handler(replyChannel, payload.Data) + } + } else { + for _, handler := range ipc.receiveListerners[payload.Event] { + handler(payload.Data) + } } - } + }() } } diff --git a/example/main.go b/test/main.go similarity index 100% rename from example/main.go rename to test/main.go diff --git a/test/node.js b/test/node.js new file mode 100644 index 0000000..d93c096 --- /dev/null +++ b/test/node.js @@ -0,0 +1,25 @@ +const IPC = require('ipc-node-go') +const ipc = new IPC('./test') +ipc.init() + +ipc.on('log', console.log) +// Log all error from stderr +ipc.on('error', console.error) +// ipc.on('ping', () => { +// console.log(new Date()) +// }) + +// +ipc.send('who', { name: 'Akuma Nodejs' }) +// send and receive and an acknowledgement + +// listen for event and reply to the channel +ipc.onReceiveAnSend('hola', (channel, data) => { + console.log(data) + ipc.send(channel, 'cool thanks') +}) +// setInterval(() => { +// ipc.sendAndReceive('yoo', 'Hello, how are you doing?', (err, d) => { +// err ? console.error(err) : console.log(d) +// }) +// }, 20000) diff --git a/example/package.json b/test/package.json similarity index 100% rename from example/package.json rename to test/package.json diff --git a/example/yarn.lock b/test/yarn.lock similarity index 100% rename from example/yarn.lock rename to test/yarn.lock