Skip to content

Commit

Permalink
Merge pull request #3 from srowhani/feat/rollup
Browse files Browse the repository at this point in the history
feat(rollup): fixes build tree duplication, and enables worker imports
  • Loading branch information
srowhani authored May 22, 2020
2 parents afa87a3 + 9fbd556 commit 4a0bccb
Show file tree
Hide file tree
Showing 29 changed files with 5,705 additions and 4,180 deletions.
53 changes: 53 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
plugins: [
'ember'
],
extends: [
'eslint:recommended',
'plugin:ember/recommended'
],
env: {
browser: true
},
rules: {
'ember/no-jquery': 'error'
},
overrides: [
// node files
{
files: [
'.eslintrc.js',
'.template-lintrc.js',
'ember-cli-build.js',
'index.js',
'testem.js',
'blueprints/*/index.js',
'config/**/*.js',
'tests/dummy/config/**/*.js'
],
excludedFiles: [
'addon/**',
'addon-test-support/**',
'app/**',
'tests/dummy/app/**'
],
parserOptions: {
sourceType: 'script'
},
env: {
browser: false,
node: true
},
plugins: ['node'],
rules: Object.assign({}, require('eslint-plugin-node').configs.recommended.rules, {
// add your custom rules and overrides for node files here
})
}
]
};
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/tmp/

# dependencies
/bower_components/
/node_modules/
bower_components/
node_modules/

# misc
/.sass-cache
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v10.12.0
v12
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
cache:
directories:
- node_modules
Expand All @@ -24,4 +24,4 @@ deploy:
local-dir: dist
on:
branch: master
condition: $TRAVIS_NODE_VERSION = "10"
condition: $TRAVIS_NODE_VERSION = "12"
41 changes: 13 additions & 28 deletions addon/index.ts → addon/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import {
Artisan,
JSONRPCResponse,
WorkerProxyStrategy,
} from 'ember-artisans/types';

let WORKER_ID = 0;
let TRANSPORT_ID = 0;

export const timeout = (ms: number) =>
new Promise(resolve => setTimeout(resolve, ms));
export const timeout = (ms) =>
new Promise((resolve) => setTimeout(resolve, ms));

export function createWorker(
workerPath: string,
workerPath,
options = {
timeout: 5000,
},
) {
const resolutionMap: {
[transportId: string]: (value?: {} | PromiseLike<{}> | undefined) => void;
} = {};
const resolutionMap = {};

const workerId = `worker_${WORKER_ID++}`;
const workerInstance = new Worker(workerPath);

const artisanInstance: Artisan = Object.assign(workerInstance, {
const artisanInstance = Object.assign(workerInstance, {
id: workerId,
isRunning: false,
});
Expand All @@ -44,9 +36,7 @@ export function createWorker(
return Reflect.get(target, method, receiver);
}

return async function taskWrapper(
...params: any[]
): Promise<JSONRPCResponse> {
return async function taskWrapper(...params) {
const transportId = `${workerId}-${TRANSPORT_ID++}`;

artisanInstance.isRunning = true;
Expand All @@ -59,7 +49,7 @@ export function createWorker(
},
}));

const workerResolve = new Promise<JSONRPCResponse>(resolve => {
const workerResolve = new Promise((resolve) => {
resolutionMap[transportId] = resolve;
});

Expand All @@ -70,7 +60,7 @@ export function createWorker(
params,
});

const resolvedAction = await Promise.race<JSONRPCResponse>([
const resolvedAction = await Promise.race([
workerTimeout,
workerResolve,
]);
Expand All @@ -84,26 +74,21 @@ export function createWorker(
});
}

function getBestCandidate(pool: Artisan[]): Artisan {
function getBestCandidate(pool) {
return pool.find(({ isRunning }) => !isRunning) || pool[0];
}

export function createWorkerPool(
workerPath: string,
poolSize: number,
): Artisan[] & WorkerProxyStrategy<Promise<JSONRPCResponse>> {
export function createWorkerPool(workerPath, poolSize) {
const availableWorkers = new Array(poolSize)
.fill(null)
.map(() => createWorker(workerPath));

return new Proxy(availableWorkers, {
get(workerPool, method: string) {
return function taskPoolWrapper(
...params: any[]
): Promise<JSONRPCResponse> {
get(workerPool, method) {
return function taskPoolWrapper(...params) {
const candidateWorker = getBestCandidate(workerPool);
return candidateWorker[method](...params);
};
},
}) as any;
});
}
27 changes: 27 additions & 0 deletions addon/services/artisans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Service from '@ember/service';

import { createWorkerPool } from 'ember-artisans/index';

export default class Artisans extends Service {
_pools = [];

poolFor(workerName, poolSize) {
const { _pools } = this;

if (!_pools[workerName]) {
_pools[workerName] = createWorkerPool(workerName, poolSize);
}

return _pools[workerName];
}

/**
* @override
*/
willDestroy() {
Object.values(this._pools).forEach((workerPool) =>
workerPool.forEach((worker) => worker.terminate()),
);
return super.willDestroy(...arguments);
}
}
42 changes: 0 additions & 42 deletions addon/services/artisans.ts

This file was deleted.

25 changes: 0 additions & 25 deletions addon/types/index.ts

This file was deleted.

File renamed without changes.
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
const mergeTrees = require('broccoli-merge-trees')

const rollup = require('broccoli-rollup');
const { buildWorkerTree } = require('./lib/worker-tree')
const fs = require('fs');
const { default: nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');

const boundRollup = workerPath => {
const workerList = fs.readdirSync(workerPath);

return rollup(workerPath, {
rollup: {
input: workerList,
output: [
{
dir: `assets/workers`,
format: 'esm',
},
],
plugins: [
nodeResolve({
extensions: ['.js'],
browser: true,
preferBuiltIns: false
}),
commonjs({
include: [/node_modules/],
}),
],
}
})
}

module.exports = {
name: require('./package').name,
Expand All @@ -9,7 +38,7 @@ module.exports = {
const joinedTrees = [
buildWorkerTree(
this.project,
this.treeGenerator
boundRollup
),
tree
].filter(Boolean)
Expand Down
Loading

0 comments on commit 4a0bccb

Please sign in to comment.