add basic functionality
This commit is contained in:
parent
e3e7626abd
commit
218deb9947
1
dist/index.d.ts
vendored
1
dist/index.d.ts
vendored
@ -1,2 +1,3 @@
|
||||
export * from './smartipc.classes.thread';
|
||||
export * from './smartipc.classes.threadfunction';
|
||||
export * from './smartipc.classes.pool';
|
||||
|
3
dist/index.js
vendored
3
dist/index.js
vendored
@ -4,4 +4,5 @@ function __export(m) {
|
||||
}
|
||||
__export(require("./smartipc.classes.thread"));
|
||||
__export(require("./smartipc.classes.threadfunction"));
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBQUEsK0NBQXlDO0FBQ3pDLHVEQUFpRCJ9
|
||||
__export(require("./smartipc.classes.pool"));
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBQUEsK0NBQXlDO0FBQ3pDLHVEQUFpRDtBQUNqRCw2Q0FBdUMifQ==
|
5
dist/smartipc.classes.pool.d.ts
vendored
Normal file
5
dist/smartipc.classes.pool.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
export declare class Pool {
|
||||
pool: any;
|
||||
constructor();
|
||||
run(workerPathArg: string): any;
|
||||
}
|
12
dist/smartipc.classes.pool.js
vendored
Normal file
12
dist/smartipc.classes.pool.js
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
const plugins = require("./smartipc.plugins");
|
||||
class Pool {
|
||||
constructor() {
|
||||
this.pool = new plugins.threads.Pool();
|
||||
}
|
||||
run(workerPathArg) {
|
||||
return this.pool.run(workerPathArg);
|
||||
}
|
||||
}
|
||||
exports.Pool = Pool;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMuY2xhc3Nlcy5wb29sLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRpcGMuY2xhc3Nlcy5wb29sLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw4Q0FBNkM7QUFFN0M7SUFFSTtRQUNJLElBQUksQ0FBQyxJQUFJLEdBQUcsSUFBSSxPQUFPLENBQUMsT0FBTyxDQUFDLElBQUksRUFBRSxDQUFBO0lBQzFDLENBQUM7SUFDRCxHQUFHLENBQUMsYUFBcUI7UUFDckIsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLGFBQWEsQ0FBQyxDQUFBO0lBQ3ZDLENBQUM7Q0FDSjtBQVJELG9CQVFDIn0=
|
6
dist/smartipc.classes.thread.d.ts
vendored
6
dist/smartipc.classes.thread.d.ts
vendored
@ -1,10 +1,16 @@
|
||||
import { Pool } from './smartipc.classes.pool';
|
||||
export declare let setWorkerBasePath: (basePathArg: string) => void;
|
||||
export declare class Thread {
|
||||
thread: any;
|
||||
workerPath: string;
|
||||
running: boolean;
|
||||
assignedPool: Pool;
|
||||
constructor(filePathArg: string);
|
||||
/**
|
||||
* sends a message to the spawned process
|
||||
*/
|
||||
send<T>(message: any): Promise<T>;
|
||||
kill(): void;
|
||||
assignToPool(poolArg: Pool): void;
|
||||
private checkSpawn();
|
||||
}
|
||||
|
30
dist/smartipc.classes.thread.js
vendored
30
dist/smartipc.classes.thread.js
vendored
@ -10,23 +10,45 @@ exports.setWorkerBasePath = (basePathArg) => {
|
||||
};
|
||||
class Thread {
|
||||
constructor(filePathArg) {
|
||||
this.thread = plugins.threads.spawn(filePathArg);
|
||||
this.running = false;
|
||||
this.assignedPool = null;
|
||||
this.workerPath = filePathArg;
|
||||
}
|
||||
/**
|
||||
* sends a message to the spawned process
|
||||
*/
|
||||
send(message) {
|
||||
let done = q.defer();
|
||||
this.thread.send(message).on('message', (message) => {
|
||||
this.checkSpawn();
|
||||
this.thread.send(message);
|
||||
this.thread.on('message', (message) => {
|
||||
done.resolve(message);
|
||||
}).on('error', err => {
|
||||
});
|
||||
this.thread.on('done', (job, message) => {
|
||||
done.resolve(message);
|
||||
});
|
||||
this.thread.on('error', err => {
|
||||
done.reject(err);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
kill() {
|
||||
this.thread.kill();
|
||||
this.running = false;
|
||||
}
|
||||
assignToPool(poolArg) {
|
||||
this.assignedPool = poolArg;
|
||||
}
|
||||
checkSpawn() {
|
||||
if (!this.running && !this.assignedPool) {
|
||||
this.running = true;
|
||||
this.thread = plugins.threads.spawn(this.workerPath);
|
||||
}
|
||||
else if (!this.running && this.assignedPool) {
|
||||
this.running = true;
|
||||
this.thread = this.assignedPool.run(this.workerPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.Thread = Thread;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMuY2xhc3Nlcy50aHJlYWQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGlwYy5jbGFzc2VzLnRocmVhZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsOENBQTZDO0FBQzdDLDRCQUEyQjtBQUVoQixRQUFBLGlCQUFpQixHQUFHLENBQUMsV0FBbUI7SUFDL0MsT0FBTyxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDO1FBQ3ZCLFFBQVEsRUFBRTtZQUNOLElBQUksRUFBRSxXQUFXO1NBQ3BCO0tBQ0osQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFBO0FBRUQ7SUFFSSxZQUFZLFdBQW1CO1FBQzNCLElBQUksQ0FBQyxNQUFNLEdB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EVBQUUsQ0FBQTtRQUNsQixJQUFJLENBQUMsT0FBTyxHQUFHLEtBQUssQ0FBQTtJQUN4QixDQUFDO0lBRUQsWUFBWSxDQUFDLE9BQWE7UUFDdEIsSUFBSSxDQUFDLFlBQVksR0FBRyxPQUFPLENBQUE7SUFDL0IsQ0FBQztJQUVPLFVBQVU7UUFDZCxFQUFFLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQyxPQUFPLElBQUksQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQztZQUN0QyxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksQ0FBQTtZQUNuQixJQUFJLENBQUMsTUFBTSxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUN4RCxDQUFDO1FBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLE9BQU8sSUFBSSxJQUFJLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQztZQUM1QyxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksQ0FBQTtZQUNuQixJQUFJLENBQUMsTUFBTSxHQUFHLElBQUksQ0FBQyxZQUFZLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUN4RCxDQUFDO0lBQ0wsQ0FBQztDQUNKO0FBOUNELHdCQThDQyJ9
|
0
dist/smartipc.pool.d.ts
vendored
0
dist/smartipc.pool.d.ts
vendored
1
dist/smartipc.pool.js
vendored
1
dist/smartipc.pool.js
vendored
@ -1 +0,0 @@
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMucG9vbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0aXBjLnBvb2wudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9
|
@ -2,6 +2,6 @@
|
||||
require("typings-global");
|
||||
require("smartq");
|
||||
module.exports = (input, done) => {
|
||||
done('hi');
|
||||
done(input);
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hpbGQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJjaGlsZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLGtCQUFlO0FBQ2YsTUFBTSxDQUFDLE9BQU8sR0FBRyxDQUFDLEtBQUssRUFBRSxJQUFJO0lBQ3pCLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQTtBQUNkLENBQUMsQ0FBQSJ9
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hpbGQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJjaGlsZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLGtCQUFlO0FBQ2YsTUFBTSxDQUFDLE9BQU8sR0FBRyxDQUFDLEtBQUssRUFBRSxJQUFJO0lBQ3pCLElBQUksQ0FBQyxLQUFLLENBQUMsQ0FBQTtBQUNmLENBQUMsQ0FBQSJ9
|
@ -1,5 +1,5 @@
|
||||
import 'typings-global'
|
||||
import 'smartq'
|
||||
module.exports = (input, done) => {
|
||||
done('hi')
|
||||
done(input)
|
||||
}
|
||||
|
19
test/test.js
19
test/test.js
@ -3,6 +3,7 @@ require("typings-test");
|
||||
const smartipc = require("../dist/index");
|
||||
let testThreadFunction;
|
||||
let testThread;
|
||||
let testPool;
|
||||
describe('smartipc', function () {
|
||||
it('should create an instance of ThreadFunction', function () {
|
||||
testThreadFunction = new smartipc.ThreadFunction((input, done) => {
|
||||
@ -22,5 +23,21 @@ describe('smartipc', function () {
|
||||
testThread.kill();
|
||||
});
|
||||
});
|
||||
it('should not spawn when nothing is sent', function () {
|
||||
smartipc.setWorkerBasePath(__dirname);
|
||||
let testThread = new smartipc.Thread('child.js');
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiwwQ0FBeUM7QUFFekMsSUFBSSxrQkFBMkMsQ0FBQTtBQUMvQyxJQUFJLFVBQTJCLENBQUE7QUFFL0IsUUFBUSxDQUFDLFVBQVUsRUFBQztJQUNoQixFQUFFLENBQUMsNkNBQTZDLEVBQUM7UUFDN0Msa0JBQWtCLEdBQUcsSUFBSSxRQUFRLENBQUMsY0FBYyxDQUFDLENBQUMsS0FBSyxFQUFFLElBQUk7WUFDekQsSUFBSSxHQUFHLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFBO1lBQ3hCLElBQUksQ0FBQyxHQUFHLENBQUMsS0FBSyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUE7UUFDMUIsQ0FBQyxDQUFDLENBQUE7UUFDRixrQkFBa0IsQ0FBQyxJQUFJLENBQUMsb0JBQW9CLENBQUMsQ0FBQyxJQUFJLENBQUMsT0FBTztZQUN0RCxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFBO1lBQ3BCLGtCQUFrQixDQUFDLElBQUksRUFBRSxDQUFBO1FBQzdCLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7SUFDRixFQUFFLENBQUMscUNBQXFDLEVBQUM7UUFDckMsUUFBUSxDQUFDLGlCQUFpQixDQUFDLFNBQVMsQ0FBQyxDQUFBO1FBQ3JDLFVBQVUsR0FBRyxJQUFJLFFBQVEsQ0FBQyxNQUFNLENBQUMsVUFBVSxDQUFDLENBQUE7UUFDNUMsVUFBVSxDQUFDLElBQUksQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDLElBQUksQ0FBQyxPQUFPO1lBQzlDLE9BQU8sQ0FBQyxHQUFHLENBQUMsT0FBTyxDQUFDLENBQUE7WUFDcEIsVUFBVSxDQUFDLElBQUksRUFBRSxDQUFBO1FBQ3JCLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9
|
||||
it('should run in a Pool', function () {
|
||||
let testPool = new smartipc.Pool();
|
||||
let testThread = new smartipc.Thread('child.js');
|
||||
testThread.assignToPool(testPool);
|
||||
testThread.send('what').then(message => {
|
||||
console.log(message);
|
||||
testThread.send('another').then(message => {
|
||||
console.log(message);
|
||||
testThread.assignedPool.pool.killAll();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiwwQ0FBeUM7QUFFekMsSUFBSSxrQkFBMkMsQ0FBQTtBQUMvQyxJQUFJLFVBQTJCLENBQUE7QUFDL0IsSUFBSSxRQUF1QixDQUFBO0FBRTNCLFFBQVEsQ0FBQyxVQUFVLEVBQUM7SUFDaEIsRUFBRSxDQUFDLDZDQUE2QyxFQUFDO1FBQzdDLGtCQUFrQixHQUFHLElBQUksUUFBUSxDQUFDLGNBQWMsQ0FBQyxDQUFDLEtBQUssRUFBRSxJQUFJO1lBQ3pELElBQUksR0FBRyxHQUFHLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQTtZQUN4QixJQUFJLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFBO1FBQzFCLENBQUMsQ0FBQ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BO1FBQ2hELFVBQVUsQ0FBQyxZQUFZLENBQUMsUUFBUSxDQUFDLENBQUE7UUFDakMsVUFBVSxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsQ0FBQyxJQUFJLENBQUMsT0FBTztZQUNoQyxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFBO1lBQ3BCLFVBQVUsQ0FBQyxJQUFJLENBQUMsU0FBUyxDQUFDLENBQUMsSUFBSSxDQUFDLE9BQU87Z0JBQ25DLE9BQU8sQ0FBQyxHQUFHLENBQUMsT0FBTyxDQUFDLENBQUE7Z0JBQ3BCLFVBQVUsQ0FBQyxZQUFZLENBQUMsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1lBQzFDLENBQUMsQ0FBQyxDQUFBO1FBQ04sQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
19
test/test.ts
19
test/test.ts
@ -4,6 +4,7 @@ import * as smartipc from '../dist/index'
|
||||
|
||||
let testThreadFunction: smartipc.ThreadFunction
|
||||
let testThread: smartipc.Thread
|
||||
let testPool: smartipc.Pool
|
||||
|
||||
describe('smartipc',function(){
|
||||
it('should create an instance of ThreadFunction',function(){
|
||||
@ -24,4 +25,22 @@ describe('smartipc',function(){
|
||||
testThread.kill()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not spawn when nothing is sent',function(){
|
||||
smartipc.setWorkerBasePath(__dirname)
|
||||
let testThread = new smartipc.Thread('child.js')
|
||||
})
|
||||
|
||||
it('should run in a Pool', function() {
|
||||
let testPool = new smartipc.Pool()
|
||||
let testThread = new smartipc.Thread('child.js')
|
||||
testThread.assignToPool(testPool)
|
||||
testThread.send('what').then(message => {
|
||||
console.log(message)
|
||||
testThread.send('another').then(message => {
|
||||
console.log(message)
|
||||
testThread.assignedPool.pool.killAll()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -1,2 +1,3 @@
|
||||
export * from './smartipc.classes.thread'
|
||||
export * from './smartipc.classes.threadfunction'
|
||||
export * from './smartipc.classes.pool'
|
||||
|
11
ts/smartipc.classes.pool.ts
Normal file
11
ts/smartipc.classes.pool.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import * as plugins from './smartipc.plugins'
|
||||
|
||||
export class Pool {
|
||||
pool
|
||||
constructor() {
|
||||
this.pool = new plugins.threads.Pool()
|
||||
}
|
||||
run(workerPathArg: string) {
|
||||
return this.pool.run(workerPathArg)
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
import * as plugins from './smartipc.plugins'
|
||||
import * as q from 'smartq'
|
||||
|
||||
import { Pool } from './smartipc.classes.pool'
|
||||
|
||||
export let setWorkerBasePath = (basePathArg: string) => {
|
||||
plugins.threads.config.set({
|
||||
basepath: {
|
||||
@ -11,8 +13,11 @@ export let setWorkerBasePath = (basePathArg: string) => {
|
||||
|
||||
export class Thread {
|
||||
thread
|
||||
workerPath: string
|
||||
running: boolean = false
|
||||
assignedPool: Pool = null
|
||||
constructor(filePathArg: string) {
|
||||
this.thread = plugins.threads.spawn(filePathArg)
|
||||
this.workerPath = filePathArg
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,9 +25,15 @@ export class Thread {
|
||||
*/
|
||||
send<T>(message: any): Promise<T> {
|
||||
let done = q.defer<T>()
|
||||
this.thread.send(message).on('message', (message: T) => {
|
||||
this.checkSpawn()
|
||||
this.thread.send(message)
|
||||
this.thread.on('message', (message: T) => {
|
||||
done.resolve(message)
|
||||
}).on('error', err => {
|
||||
})
|
||||
this.thread.on('done', (job, message: T) => {
|
||||
done.resolve(message)
|
||||
})
|
||||
this.thread.on('error', err => {
|
||||
done.reject(err)
|
||||
})
|
||||
return done.promise
|
||||
@ -30,5 +41,20 @@ export class Thread {
|
||||
|
||||
kill() {
|
||||
this.thread.kill()
|
||||
this.running = false
|
||||
}
|
||||
|
||||
assignToPool(poolArg: Pool) {
|
||||
this.assignedPool = poolArg
|
||||
}
|
||||
|
||||
private checkSpawn() {
|
||||
if (!this.running && !this.assignedPool) {
|
||||
this.running = true
|
||||
this.thread = plugins.threads.spawn(this.workerPath)
|
||||
} else if (!this.running && this.assignedPool) {
|
||||
this.running = true
|
||||
this.thread = this.assignedPool.run(this.workerPath)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user