diff --git a/examples/todo/client/client.ts b/examples/todo/client/client.ts index 5ea84e8..9b0b6ea 100644 --- a/examples/todo/client/client.ts +++ b/examples/todo/client/client.ts @@ -8,15 +8,10 @@ import './base.css' const todoList: Record = {} const peer = new Peer({ url: `ws://localhost:8081/` - // log: { - // logCallbacks: [console.log], - // logName: 'Peer 2', - // logLevel: LogLevel.socket - // } }) const addTodo = (title: string) => { - peer.call('todo/add', [title]) + peer.call('todo/add', [title]).catch(() => console.log('Failed to add')) } const removeTodo = (id: string) => { diff --git a/examples/todo/server/index.ts b/examples/todo/server/index.ts index 45711b5..e7f3f1a 100644 --- a/examples/todo/server/index.ts +++ b/examples/todo/server/index.ts @@ -1,4 +1,4 @@ -import { Daemon, LogLevel, Method, Peer, State, ValueType } from '../../../lib' +import { Daemon, Method, Peer, State, ValueType } from '../../../lib' import { Todo } from './Todo' var port = parseInt(process.argv[2]) || 8081 @@ -12,11 +12,6 @@ var daemon = new Daemon({ batches: true, asNotification: true } - // log: { - // logCallbacks: [console.log], - // logName: 'Daemon', - // logLevel: LogLevel.socket - // } }) daemon.listen({ @@ -28,16 +23,7 @@ console.log('listening on port', port) // Create Jet Peer var peer = new Peer({ url: `ws://localhost:8081/` - // log: { - // logCallbacks: [console.log], - // logName: 'Peer 1', - // logLevel: LogLevel.socket - // } }) -// const peer2 = new jet.Peer({ -// port: internalPort,log:{logCallbacks:[console.log],logname:"Peer 2",loglevel:jet.LogLevel.debug} - -// }) var todoStates: Record> = {} @@ -56,8 +42,13 @@ addTodo.on('call', (args) => { todoState.on('set', (requestedTodo) => { todo.merge(requestedTodo) }) + if (todo.id in todoStates) { + throw 'already existent' + } todoStates[todo.id] = todoState - peer.add(todoState) + peer.add(todoState).catch(() => { + console.log('State already existent') + }) }) // Provide a "todo/remove" method to delete a certain todo diff --git a/src/3_jet/daemon/index.ts b/src/3_jet/daemon/index.ts index b53d147..966b5ed 100644 --- a/src/3_jet/daemon/index.ts +++ b/src/3_jet/daemon/index.ts @@ -336,6 +336,7 @@ export class Daemon extends EventEmitter { this.forward('set', peer.user, params) .then((res) => newPeer.respond(id, res, true)) .catch((err) => newPeer.respond(id, err, false)) + .finally(() => newPeer.send()) ) newPeer.addListener( 'call', @@ -343,6 +344,7 @@ export class Daemon extends EventEmitter { this.forward('call', peer.user, params) .then((res) => newPeer.respond(id, res, true)) .catch((err) => newPeer.respond(id, err, false)) + .finally(() => newPeer.send()) ) }) this.jsonRPCServer.addListener('disconnect', (peer: JsonRPC) => { diff --git a/src/3_jet/peer/index.ts b/src/3_jet/peer/index.ts index 863db34..8365120 100644 --- a/src/3_jet/peer/index.ts +++ b/src/3_jet/peer/index.ts @@ -158,8 +158,20 @@ export class Peer extends EventEmitter { this.#jsonrpc.respond(id, error, false) return } - method.call(m.args) - this.#jsonrpc.respond(id, {}, true) + try { + method.call(m.args) + this.#jsonrpc.respond(id, {}, true) + } catch (err) { + this.#jsonrpc.respond( + id, + new InvalidParamError( + 'InvalidParam', + 'Failed to call method', + err && typeof err == 'object' ? err.toString() : undefined + ), + false + ) + } } else { const error = new NotFound(m.path) this.#log.error(error.toString())