Contract execution refactor. (#153)

This commit is contained in:
Ravin Perera
2020-11-18 07:16:17 +05:30
committed by GitHub
parent 0098c3ddab
commit 1608e9fc49
6 changed files with 190 additions and 193 deletions

View File

@@ -21,13 +21,19 @@ const echoContract = (ctx) => {
else {
await user.send("Echoing: " + msg);
}
});
});
// Broadcast message to all connected users.
// ctx.users.get().forEach(u => u.send("Hello"));
// Get list of all users who are connected.
// ctx.users.get();
// Send message to specific user (identified by public key).
// await ctx.users.find(<PubkeyHex>).send("Hello");
// Get the user identified by public key.
// ctx.users.find("<PubkeyHex>");
// Get list of all peers in the cluster.
// ctx.peers.get();
// Get the peer identified by public key.
// ctx.peers.find("<PubkeyHex>");
// Peer messages example.
// if (!ctx.readonly) {

View File

@@ -29,9 +29,9 @@ class HotPocketContract {
const executionContext = new ContractExecutionContext(hpargs, users, peers);
this.events.emit("session_start");
invokeCallback(contractFunc, executionContext).then(() => {
invokeCallback(contractFunc, executionContext).catch(errHandler).finally(() => {
// Wait for any pending tasks added during execution.
Promise.all(pendingTasks).then(() => {
Promise.all(pendingTasks).catch(errHandler).finally(() => {
this.events.emit("session_end");
this.#terminate();
});
@@ -121,7 +121,7 @@ class UsersCollection {
if (pendingUserCount == 0) {
// All user message events has been emitted.
// Now start waiting for queued up user message callback completion.
Promise.all(userMessageTasks).then(allUsersCompletionResolver)
Promise.all(userMessageTasks).catch(errHandler).finally(allUsersCompletionResolver)
}
}
@@ -368,13 +368,15 @@ const invokeCallback = async (callback, ...args) => {
return;
if (callback.constructor.name === 'AsyncFunction') {
await callback(...args);
await callback(...args).catch(errHandler);
}
else {
callback(...args);
}
}
const errHandler = (err) => console.log(err);
module.exports = {
HotPocketContract
}