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

refactor: optimize overall performance #581

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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 .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"max-len": ["error", 80],
"arrow-parens": 0,
"comma-dangle": [2, "only-multiline"],
"prefer-reflect": 2,
"global-require": 0,
"operator-assignment": 0,
"class-methods-use-this": 0,
"no-restricted-syntax": [
"error",
Expand Down
6 changes: 3 additions & 3 deletions bin/lux
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function commandNotFound(cmd) {

function setEnvVar(key, val, def) {
if (val) {
Reflect.set(process.env, key, val);
} else if (!Reflect.has(process.env, key)) {
Reflect.set(process.env, key, def);
process.env[key] = val;
} else if (!process.env[key]) {
process.env[key] = def;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export const LUX_CONSOLE = ENV.LUX_CONSOLE || false;
export const PLATFORM = platform();
export const CIRCLECI = ENV.CIRCLECI;
export const APPVEYOR = ENV.APPVEYOR;
export const IS_PRODUCTION = NODE_ENV === 'production';
20 changes: 20 additions & 0 deletions src/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ export interface Chain<T> {
value(): T;
construct<U, V: Class<U>>(constructor: V): Chain<U>;
}

export type ObjectMap<K, V> = {
[key: K]: V;
};

export interface Thenable<+R> {
constructor(callback: (
resolve: (result: Promise<R> | R) => void,
reject: (error: any) => void
) => mixed): void;

then<U>(
onFulfill?: (value: R) => Promise<U> | U,
onReject?: (error: any) => Promise<U> | U
): Promise<U>;

catch<U>(
onReject?: (error: any) => ?Promise<U> | U
): Promise<U>;
}
2 changes: 1 addition & 1 deletion src/packages/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Application {
* @type {Map}
* @private
*/
controllers: FreezeableMap<string, Controller>;
controllers: FreezeableMap<string, Controller<*>>;

/**
* A map containing each `Serializer` instance.
Expand Down
4 changes: 2 additions & 2 deletions src/packages/application/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function initialize<T: Application>(app: T, {
);

models.forEach(model => {
Reflect.defineProperty(model, 'serializer', {
Object.defineProperty(model, 'serializer', {
value: closestChild(serializers, model.resourceName),
writable: false,
enumerable: false,
Expand All @@ -67,7 +67,7 @@ export default async function initialize<T: Application>(app: T, {
);

controllers.forEach(controller => {
Reflect.defineProperty(controller, 'controllers', {
Object.defineProperty(controller, 'controllers', {
value: controllers,
writable: true,
enumerable: false,
Expand Down
2 changes: 1 addition & 1 deletion src/packages/application/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type Application$opts = Config & {
database: Database$config;
};

export type Application$factoryOpts<T: Controller | Serializer<*>> = {
export type Application$factoryOpts<T: Controller<*> | Serializer<*>> = {
key: string;
store: Database;
parent: ?T;
Expand Down
10 changes: 5 additions & 5 deletions src/packages/application/utils/create-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import type Controller from '../../controller';
import type Serializer from '../../serializer';
import type { Bundle$Namespace } from '../../loader'; // eslint-disable-line max-len, no-duplicate-imports

export default function createController<T: Controller>(
export default function createController<T: Controller<*>>(
constructor: Class<T>,
opts: {
key: string;
store: Database;
parent: ?Controller;
parent: ?Controller<*>;
serializers: Bundle$Namespace<Serializer<*>>;
}
): T {
Expand All @@ -36,11 +36,11 @@ export default function createController<T: Controller>(
serializer = closestAncestor(serializers, key);
}

const instance: T = Reflect.construct(constructor, [{
const instance: T = new constructor({
model,
namespace,
serializer
}]);
});

if (serializer) {
if (!instance.filter.length) {
Expand All @@ -64,7 +64,7 @@ export default function createController<T: Controller>(
];
}

Reflect.defineProperty(instance, 'parent', {
Object.defineProperty(instance, 'parent', {
value: parent,
writable: false,
enumerable: true,
Expand Down
6 changes: 3 additions & 3 deletions src/packages/application/utils/create-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export default function createSerializer<T: Serializer<*>>(
parent = null;
}

const instance: T = Reflect.construct(constructor, [{
const instance: T = new constructor({
model,
parent,
namespace
}]);
});

Reflect.defineProperty(instance, 'parent', {
Object.defineProperty(instance, 'parent', {
value: parent,
writable: false,
enumerable: true,
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/commands/dbcreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createLoader } from '../../loader';
*/
export function dbcreate() {
const load = createLoader(CWD);
const config = Reflect.get(load('config').database, NODE_ENV);
const config = load('config').database[NODE_ENV];

if (!config) {
throw new DatabaseConfigMissingError(NODE_ENV);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/commands/dbdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createLoader } from '../../loader';
*/
export function dbdrop() {
const load = createLoader(CWD);
const config = Reflect.get(load('config').database, NODE_ENV);
const config = load('config').database[NODE_ENV];

if (!config) {
throw new DatabaseConfigMissingError(NODE_ENV);
Expand Down
5 changes: 2 additions & 3 deletions src/packages/cli/commands/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import type Application from '../../application';

export function repl(): Promise<void> {
return new Promise(async (resolve) => {
const app: Application = await Reflect.apply(require, null, [
path.join(CWD, 'dist', 'boot')
]);
// $FlowIgnore
const app: Application = require(path.join(CWD, 'dist', 'boot'));

const instance = startRepl({
prompt: '> '
Expand Down
9 changes: 7 additions & 2 deletions src/packages/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function serve({
const load = createLoader(CWD);
const { logging } = load('config');
const logger = new Logger(logging);
let maxWorkers;

if (hot) {
const watcher = await watch(CWD);
Expand All @@ -34,11 +35,15 @@ export async function serve({
});
}

if (!cluster) {
maxWorkers = 1;
}

createCluster({
logger,
maxWorkers,
path: CWD,
port: PORT,
maxWorkers: cluster ? undefined : 1
port: PORT
}).once('ready', () => {
logger.info(`Lux Server listening on port: ${cyan(`${PORT}`)}`);
});
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/generator/utils/generator-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as generators from './generate-type';

export default function generatorFor(type: string): Generator {
const normalized = type.toLowerCase();
const generator: void | Generator = Reflect.get(generators, normalized);
const generator: void | Generator = generators[normalized];

if (!generator) {
throw new Error(`Could not find a generator for '${type}'.`);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/templates/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default (name: string, attrs: Array<string>) => {
.pipe(str => camelize(str, true))
.value();

const value = Reflect.get(types, key);
const value = types[key];

if (value) {
const inverse = camelize(normalized, true);
Expand Down
32 changes: 32 additions & 0 deletions src/packages/compiler/utils/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @flow
import merge from '../../../utils/merge';

const IGNORE_WARNING = /^treating '.+' as external dependency$/i;

export function createRollupConfig<T: Object>(options: T): T {
return merge({
acorn: {
sourceType: 'module',
ecmaVersion: 2017,
allowReserved: true,
preserveParens: true
},
entry: '',
onwarn: warning => {
if (IGNORE_WARNING.test(warning)) {
return;
}

process.stderr.write(`${warning}\n`);
},
preferConst: true
}, options);
}

export function createBundleConfig<T: Object>(options: T): T {
return merge({
format: 'cjs',
sourceMap: true,
useStrict: false
}, options);
}
2 changes: 1 addition & 1 deletion src/packages/compiler/utils/create-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default async function createManifest(
Array
.from(assets)
.map(([key, value]) => {
const write = Reflect.get(writer, key);
const write = writer[key];

if (write) {
return write(value);
Expand Down
Loading