RN packager: wrap-worker-fn.js: syncify

Reviewed By: davidaurelio

Differential Revision: D5803796

fbshipit-source-id: d6b7d169a2c17864e8ce68d82df8e898d966e404
This commit is contained in:
Jean Lauliac 2017-09-11 07:38:41 -07:00 committed by Facebook Github Bot
parent 69eb3430b6
commit 36b2f8d67f
3 changed files with 41 additions and 72 deletions

View File

@ -10,20 +10,15 @@
*/ */
'use strict'; 'use strict';
const asyncify: Asyncify = require('async/asyncify');
const optimizeModule = require('./worker/optimize-module'); const optimizeModule = require('./worker/optimize-module');
const transformModule = require('./worker/transform-module'); const transformModule = require('./worker/transform-module');
const wrapWorkerFn = require('./worker/wrap-worker-fn'); const wrapWorkerFn = require('./worker/wrap-worker-fn');
import type {Callback} from './types.flow';
import type {OptimizationOptions} from './worker/optimize-module'; import type {OptimizationOptions} from './worker/optimize-module';
import type {TransformOptions} from './worker/transform-module'; import type {TransformOptions} from './worker/transform-module';
import type {WorkerFnWithIO} from './worker/wrap-worker-fn'; import type {WorkerFnWithIO} from './worker/wrap-worker-fn';
type Asyncify = <A, B, C>((A, B) => C) => (A, B, Callback<C>) => void;
exports.optimizeModule = exports.optimizeModule =
(wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO<OptimizationOptions>); (wrapWorkerFn(optimizeModule): WorkerFnWithIO<OptimizationOptions>);
exports.transformModule = exports.transformModule =
(wrapWorkerFn(asyncify(transformModule)): WorkerFnWithIO<TransformOptions>); (wrapWorkerFn(transformModule): WorkerFnWithIO<TransformOptions>);

View File

@ -27,72 +27,71 @@ describe('wrapWorkerFn:', () => {
let workerFn, wrapped; let workerFn, wrapped;
beforeEach(() => { beforeEach(() => {
workerFn = fn(); workerFn = fn();
workerFn.stub.yields();
wrapped = wrapWorkerFn(workerFn); wrapped = wrapWorkerFn(workerFn);
}); });
const fs = require('fs'); const fs = require('fs');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
it('reads the passed-in file synchronously as buffer', done => { it('reads the passed-in file synchronously as buffer', () => {
wrapped(infile, outfile, {}, () => { wrapped(infile, outfile, {});
expect(fs.readFileSync).toBeCalledWith(infile); expect(fs.readFileSync).toBeCalledWith(infile);
done();
});
}); });
it('calls the worker function with file contents and options', done => { it('calls the worker function with file contents and options', () => {
const contents = 'arbitrary(contents);'; const contents = 'arbitrary(contents);';
const options = {arbitrary: 'options'}; const options = {arbitrary: 'options'};
fs.readFileSync.mockReturnValue(contents); fs.readFileSync.mockReturnValue(contents);
wrapped(infile, outfile, options, () => { wrapped(infile, outfile, options);
expect(workerFn).toBeCalledWith(contents, options, any(Function)); expect(workerFn).toBeCalledWith(contents, options);
done();
});
}); });
it('passes through any error that the worker function calls back with', done => { it('passes through any error that the worker function calls back with', () => {
const error = new Error(); const error = new Error();
workerFn.stub.yields(error); workerFn.stub.throws(error);
wrapped(infile, outfile, {}, e => { try {
wrapped(infile, outfile, {});
throw new Error('should not reach');
} catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
done(); }
});
}); });
it('writes the result to disk', done => { it('writes the result to disk', () => {
const result = {arbitrary: 'result'}; const result = {arbitrary: 'result'};
workerFn.stub.yields(null, result); workerFn.stub.returns(result);
wrapped(infile, outfile, {}, () => { wrapped(infile, outfile, {});
expect(mkdirp.sync).toBeCalledWith(dirname(outfile)); expect(mkdirp.sync).toBeCalledWith(dirname(outfile));
expect(fs.writeFileSync).toBeCalledWith( expect(fs.writeFileSync).toBeCalledWith(
outfile, outfile,
JSON.stringify(result), JSON.stringify(result),
'utf8', 'utf8',
); );
done();
});
}); });
it('calls back with any error thrown by `mkdirp.sync`', done => { it('calls back with any error thrown by `mkdirp.sync`', () => {
const error = new Error(); const error = new Error();
mkdirp.sync.mockImplementationOnce(() => { mkdirp.sync.mockImplementationOnce(() => {
throw error; throw error;
}); });
wrapped(infile, outfile, {}, e => { try {
wrapped(infile, outfile, {});
throw new Error('should not reach');
} catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
done(); }
});
}); });
it('calls back with any error thrown by `fs.writeFileSync`', done => { it('calls back with any error thrown by `fs.writeFileSync`', () => {
const error = new Error(); const error = new Error();
fs.writeFileSync.mockImplementationOnce(() => { fs.writeFileSync.mockImplementationOnce(() => {
throw error; throw error;
}); });
wrapped(infile, outfile, {}, e => { try {
wrapped(infile, outfile, {});
throw new Error('should not reach');
} catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
done(); }
});
}); });
}); });

View File

@ -17,47 +17,22 @@ const mkdirp = require('mkdirp');
const {dirname} = require('path'); const {dirname} = require('path');
import type {Callback} from '../types.flow';
type Path = string; type Path = string;
type WorkerFn<Options> = ( type WorkerFn<Options> = (fileContents: Buffer, options: Options) => mixed;
fileContents: Buffer,
options: Options,
callback: Callback<Object>,
) => void;
export type WorkerFnWithIO<Options> = ( export type WorkerFnWithIO<Options> = (
infile: Path, infile: Path,
outfile: Path, outfile: Path,
options: Options, options: Options,
callback: Callback<>,
) => void; ) => void;
function wrapWorkerFn<Options>( function wrapWorkerFn<Options>(
workerFunction: WorkerFn<Options>, workerFunction: WorkerFn<Options>,
): WorkerFnWithIO<Options> { ): WorkerFnWithIO<Options> {
return ( return (infile: Path, outfile: Path, options: Options) => {
infile: Path,
outfile: Path,
options: Options,
callback: Callback<>,
) => {
const contents = fs.readFileSync(infile); const contents = fs.readFileSync(infile);
workerFunction(contents, options, (error, result) => { const result = workerFunction(contents, options);
if (error) {
callback(error);
return;
}
try {
mkdirp.sync(dirname(outfile)); mkdirp.sync(dirname(outfile));
fs.writeFileSync(outfile, JSON.stringify(result), 'utf8'); fs.writeFileSync(outfile, JSON.stringify(result), 'utf8');
} catch (writeError) {
callback(writeError);
return;
}
callback(null);
});
}; };
} }