Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Improve Thread pool invokation syntax #1691

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class Application extends events.EventEmitter {
return this.static.get(fileName);
}

async invoke({ method, args, exclusive = false }) {
async invoke({ method, exclusive = false, args }) {
const { port1: port, port2 } = new MessageChannel();
const data = { method, args };
const msg = { type: 'invoke', name: 'request', exclusive, data, port };
Expand Down
1 change: 1 addition & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fsp = require('fs').promises;
class Cache {
constructor(place, application) {
this.path = application.absolute(place);
this.place = place;
this.application = application;
}

Expand Down
30 changes: 27 additions & 3 deletions lib/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class Modules extends Cache {
}, timeout);
}

set(relPath, iface) {
set(relPath, exports) {
const names = parsePath(relPath);
const method = [this.place, ...names].join('.');
const iface = this.wrapIface(method, exports);
let level = this.tree;
const last = names.length - 1;
for (let depth = 0; depth <= last; depth++) {
Expand All @@ -44,8 +46,8 @@ class Modules extends Cache {
if (next === undefined) next = { parent: level };
level[name] = next;
if (depth === 1 && name === 'start') {
if (iface.constructor.name === 'AsyncFunction') {
this.application.starts.push(iface);
if (exports.constructor.name === 'AsyncFunction') {
this.application.starts.push(exports);
} else {
const msg = `${relPath} expected to be async function`;
this.application.console.error(msg);
Expand Down Expand Up @@ -73,6 +75,28 @@ class Modules extends Cache {
}
}
}

wrapIface(method, iface) {
if (iface.constructor.name === 'AsyncFunction') {
const { invoke } = this.application;
return (args) => ({
handler: iface,
args,
then(resolve, reject) {
return this.handler(this.args).then(resolve, reject);
},
catch(reject) {
return this.handler(this.args).then(null, reject);
},
inThread({ exclusive = false }) {
this.handler = invoke;
this.args = { method, exclusive, args };
return this;
},
});
}
return iface;
}
}

module.exports = { Modules };
1 change: 1 addition & 0 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const application = require('./application.js');
home,
});
const { console } = logger;
logger.addListener('error', (err) => console.error(err));
Object.assign(application, { config, logger, console });

const logError = async (err) => {
Expand Down
Loading