refactor(core): ListenerServer common interface
MODIFY
packages/core/src/http/server/http.server.factory.ts
packages/core/src/http/server/http.server.factory.ts
@@ -27,6 +27,8 @@ export const createServer = (config: CreateServerConfig): Server => {
...dependencies,
])(createContext());
+ const listener = httpListener(context);
+
if (event$) {
const ctx = createEffectContext({ ask: lookup(context), client: server });
event$(serverEvent$, ctx).subscribe();
@@ -34,14 +36,13 @@ export const createServer = (config: CreateServerConfig): Server => {
const listen = () => new Promise<https.Server | http.Server>((resolve, reject) => {
const runningServer = server.listen(port, hostname);
- const httpListenerWithContext = httpListener(context);
// @TODO: bind Routing
runningServer.once('listening', () => resolve(runningServer));
runningServer.once('error', error => reject(error));
runningServer.once('close', runningServer.removeAllListeners);
- runningServer.on('request', httpListenerWithContext);
+ runningServer.on('request', listener);
});
listen.context = context;

MODIFY
packages/core/src/http/server/http.server.interface.ts
packages/core/src/http/server/http.server.interface.ts
-import * as http from 'http';
import * as https from 'https';
import { HttpServerEffect } from '../effects/http.effects.interface';
-import { BoundDependency, Context } from '../../context/context.factory';
+import { BoundDependency } from '../../context/context.factory';
+import { ListenerServer } from '../../listener/listener.interface';
+import { HttpServer } from '../http.interface';
import { httpListener } from './http.server.listener';
export interface CreateServerConfig {
@@ -13,10 +14,7 @@ export interface CreateServerConfig {
dependencies?: BoundDependency<any>[];
}
-export interface Server {
- (): Promise<https.Server | http.Server>;
- context: Context;
-}
+export interface Server extends ListenerServer<HttpServer> {}
export interface ServerOptions {
httpsOptions?: https.ServerOptions;

DELETE
packages/core/src/http/server/server.factory.ts
-import * as http from 'http';
-import * as https from 'https';
-import { createContext, lookup, registerAll, bindTo } from '../../context/context.factory';
-import { createEffectContext } from '../../effects/effectsContext.factory';
-import { insertIf, isTestingMetadataOn } from '../../+internal';
-import { subscribeServerEvents } from './http.server.event.subscriber';
-import { CreateServerConfig, Server } from './http.server.interface';
-import { ServerClientToken, ServerEventStreamToken, ServerRequestMetadataStorageToken } from './http.server.tokens';
-import { serverRequestMetadataStorage } from './http.server.metadata.storage';
-
-const DEFAULT_HOSTNAME = '127.0.0.1';
-
-export const createServer = (config: CreateServerConfig): Server => {
- const { httpListener, event$, port, hostname, dependencies = [], options = {} } = config;
-
- const server = options.httpsOptions ? https.createServer(options.httpsOptions) : http.createServer();
- const serverEvent$ = subscribeServerEvents(hostname || DEFAULT_HOSTNAME)(server);
-
- const boundServerEvent$ = bindTo(ServerEventStreamToken)(() => serverEvent$);
- const boundServer = bindTo(ServerClientToken)(() => server);
- const boundServerRequestMetadataStorage = bindTo(ServerRequestMetadataStorageToken)(serverRequestMetadataStorage);
-
- const context = registerAll([
- boundServer,
- boundServerEvent$,
- ...insertIf(isTestingMetadataOn(), boundServerRequestMetadataStorage),
- ...dependencies,
- ])(createContext());
-
- if (event$) {
- const ctx = createEffectContext({ ask: lookup(context), client: server });
- event$(serverEvent$, ctx).subscribe();
- }
-
- const listen = () => new Promise<https.Server | http.Server>((resolve, reject) => {
- const runningServer = server.listen(port, hostname);
- const httpListenerWithContext = httpListener(context);
-
- // @TODO: bind Routing
-
- runningServer.once('listening', () => resolve(runningServer));
- runningServer.once('error', error => reject(error));
- runningServer.once('close', runningServer.removeAllListeners);
- runningServer.on('request', httpListenerWithContext);
- });
-
- listen.context = context;
-
- return listen;
-};

MODIFY
packages/core/src/listener/listener.factory.ts
packages/core/src/listener/listener.factory.ts
import { flow } from 'fp-ts/lib/function';
import { createReader, ReaderHandler } from '../context/context.reader.factory';
-import { ListenerConfig, Listener } from './listener.interface';
+import { ListenerConfig, Listener, ListenerHandler } from './listener.interface';
-export type ListenerHandler<T extends ListenerConfig, U> =
- (config?: T) => ReaderHandler<U>;
-
-export const createListener = <T extends ListenerConfig, U>(
- handler: ListenerHandler<T, U>
-): Listener<T, U> => flow(handler, createReader);
+export const createListener = <T extends ListenerConfig, U extends ListenerHandler>(
+ fn: (config?: T) => ReaderHandler<U>
+): Listener<T, U> => flow(fn, createReader);

MODIFY
packages/core/src/listener/listener.interface.ts
packages/core/src/listener/listener.interface.ts
@@ -9,6 +9,13 @@ export interface ListenerConfig<T = any> {
output$?: Effect<any, any, T>;
}
-export interface Listener<T extends ListenerConfig, U> {
+export type ListenerHandler = (...args: any[]) => void;
+
+export interface Listener<T extends ListenerConfig, U extends ListenerHandler> {
(config?: T): Reader<Context, U>;
}
+
+export interface ListenerServer<T> {
+ (): Promise<T>;
+ context: Context;
+}

refactor(core): ListenerServer common interface