Skip to content

Commit

Permalink
refactor(structure): move ipc to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb committed Mar 6, 2018
1 parent 163c6f7 commit 8aeb184
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/backend/exports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './compiler/host'
export * from './compiler/store'
export * from './transformers/paths'
export * from './ipc'

export * from './configuration'
export * from './service'
Expand Down
10 changes: 5 additions & 5 deletions src/ipc/client.ts → src/backend/ipc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ export type Client<RQ, RS, K extends Keys<RQ, RS> = Keys<RQ, RS>> = {
[P in K]: (data: RQ[P]) => Promise<RS[P]>
}

export function Client<RQ, RS, K extends Keys<RQ, RS> = Keys<RQ, RS>>(name: string, keys: K[]): Client<RQ, RS, K> {
export function Client<RQ, RS>(name: string, keys: Array<Keys<RQ, RS>>): Client<RQ, RS, Keys<RQ, RS>> {
const object: Partial<Client<RQ, RS>> = {}

keys.forEach(key => object[key] = data => request<RQ, RS>(name, key, data))

return object as Client<RQ, RS>
}

async function request<RQ, RS, K extends Keys<RQ, RS> = Keys<RQ, RS>>(
name: string, endpoint: K, data: RQ[K]
): Promise<RS[K]> {
return new Promise<RS[K]>((resolve, reject) => {
async function request<RQ, RS>(
name: string, endpoint: Keys<RQ, RS>, data: RQ[Keys<RQ, RS>]
) {
return new Promise<RS[Keys<RQ, RS>]>((resolve, reject) => {
const serialized = JSON.stringify({data})
const req = http.request({
socketPath: getSocketPath(name),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/ipc/worker/index.ts → src/backend/ipc/worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ChildProcess, fork} from 'child_process'

import {Emitter} from '../../utils/emitter'
import {Emitter} from '../../../utils/emitter'
import {Keys} from '../handler'

export interface RequestMessage<R, K extends keyof R = keyof R> {
Expand All @@ -23,9 +23,9 @@ export interface ResponseMessage<R, K extends keyof R = keyof R> {
}
}

type Message<RQ, RS, K extends Keys<RQ, RS> = Keys<RQ, RS>> = RequestMessage<RQ, K> | ResponseMessage<RS, K>
type Message<RQ, RS> = RequestMessage<RQ, Keys<RQ, RS>> | ResponseMessage<RS, Keys<RQ, RS>>

export class Worker<RQ = {}, RS = {}, K extends Keys<RQ, RS> = Keys<RQ, RS>> {
export class Worker<RQ = {}, RS = {}> {
private readonly onMessage = new Emitter<Message<RQ, RS>>()
private readonly child: ChildProcess

Expand All @@ -37,7 +37,7 @@ export class Worker<RQ = {}, RS = {}, K extends Keys<RQ, RS> = Keys<RQ, RS>> {
this.child.on('message', message => this.onMessage.emit(message))
}

public async request<M extends K>(method: M, data: RQ[M]): Promise<RS[M]> {
public async request<M extends Keys<RQ, RS>>(method: M, data: RQ[M]): Promise<RS[M]> {
const id = Math.random().toString(36)
const promise = this.onMessage.once(({type, id: messageId}) => type === 'response' && messageId === id)
const message: RequestMessage<RQ, M> = {id, method, data, type: 'request'}
Expand All @@ -51,7 +51,7 @@ export class Worker<RQ = {}, RS = {}, K extends Keys<RQ, RS> = Keys<RQ, RS>> {
}

if(result.data.error === null && result.data.result !== null) {
return result.data.result
return result.data.result as RS[M]
}

throw new Error(result.data.error || 'Unknown error')
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/backend/worker/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Request, Response} from '../../interfaces'
import {Client} from '../../ipc'
import {Client} from '../ipc'

// This is when I dream about type introspection
export const IPCClient = Client<Request, Response>('typescript', [
Expand Down
2 changes: 1 addition & 1 deletion src/backend/worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CompileRequest, CompileResult, Request, Response, TypeCheckResult} from '../../interfaces'
import {HandlerMethod, Server, setSocketPath, Worker} from '../../ipc'
import {HandlerMethod, Server, setSocketPath, Worker} from '../ipc'

export class TypeScriptWorker extends Worker<Request, Response> {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/worker/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Request, Response} from '../../interfaces'
import {Handler} from '../../ipc'
import {Handler} from '../ipc'

import {TypeScriptCompiler} from '../compiler/tsc'
import {loadConfiguration} from '../configuration'
Expand Down
1 change: 0 additions & 1 deletion src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './backend/exports'
export * from './frontend/assets/js-asset'
export * from './frontend/assets/classes/transpile'
export * from './ipc'
export * from './utils/fs'

0 comments on commit 8aeb184

Please sign in to comment.