feat(core): HttpServerEventStream via Context API
MODIFY
packages/core/src/index.ts
packages/core/src/index.ts
@@ -17,5 +17,6 @@ export * from './context/context.factory';
export * from './context/context.token.factory';
export * from './server/server.event';
export * from './server/server.interface';
+export * from './server/server.tokens';
export { createServer } from './server/server.factory';
export { HttpListenerConfig, httpListener } from './listener/http.listener';

MODIFY
packages/core/src/server/server.factory.ts
packages/core/src/server/server.factory.ts
@@ -4,27 +4,29 @@ import { Subject } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
import { isCloseEvent, AllServerEvents } from './server.event';
import { subscribeServerEvents } from './server.event.subscriber';
-import { createContext, lookup, registerAll } from '../context/context.factory';
+import { createContext, lookup, registerAll, bindTo } from '../context/context.factory';
import { createEffectMetadata } from '../effects/effectsMetadata.factory';
import { CreateServerConfig, Server } from './server.interface';
+import { serverEvent$ } from './server.tokens';
const DEFAULT_HOSTNAME = '127.0.0.1';
export const createServer = (config: CreateServerConfig): Server => {
const { httpListener, event$, port, hostname, dependencies = [], options = {} } = config;
- const serverEvent$ = new Subject<AllServerEvents>();
+ const serverEventSubject = new Subject<AllServerEvents>();
+ const boundServerEvent$ = bindTo(serverEvent$)(() => serverEventSubject.asObservable());
+ const context = registerAll([ boundServerEvent$, ...dependencies ])(createContext());
- const context = registerAll(dependencies)(createContext());
const httpListenerWithContext = httpListener.run(context);
const server = options.httpsOptions
? https.createServer(options.httpsOptions, httpListenerWithContext)
: http.createServer(httpListenerWithContext);
- subscribeServerEvents(hostname || DEFAULT_HOSTNAME)(serverEvent$)(server);
+ subscribeServerEvents(hostname || DEFAULT_HOSTNAME)(serverEventSubject)(server);
if (event$) {
const metadata = createEffectMetadata({ ask: lookup(context) });
- event$(serverEvent$.pipe(takeWhile(e => !isCloseEvent(e))), server, metadata).subscribe();
+ event$(serverEventSubject.pipe(takeWhile(e => !isCloseEvent(e))), server, metadata).subscribe();
}
return {

ADD
packages/core/src/server/server.tokens.ts
+import { Observable } from 'rxjs';
+import { AllServerEvents } from './server.event';
+import { createContextToken } from '../context/context.token.factory';
+
+export const serverEvent$ = createContextToken<Observable<AllServerEvents>>();

feat(core): HttpServerEventStream via Context API