Skip to content

Commit

Permalink
#296: added server to services
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Aug 29, 2024
1 parent 94bb7ab commit 49335b4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
6 changes: 2 additions & 4 deletions packages/services/src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { Request, Response, ExecutionManager, Runner } from '@jitar/execution';
import { MiddlewareManager, ProcedureRunner } from '@jitar/middleware';
import type { Request, Response, ExecutionManager, Runner } from '@jitar/execution';
import { type MiddlewareManager, ProcedureRunner } from '@jitar/middleware';

import RemoteGateway from './gateway/RemoteGateway';

Expand All @@ -27,8 +27,6 @@ export default class Client implements Runner

// TODO: Should be done when constructing the middleware manager
this.#middlewareManager.addMiddleware(new ProcedureRunner(this.#executionManager));

// TODO: Move runtime registration to the starter script
}

run(request: Request): Promise<Response>
Expand Down
76 changes: 76 additions & 0 deletions packages/services/src/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

import type { Request, Response, Runner } from '@jitar/execution';
import type { SourcingManager } from '@jitar/sourcing';

import Service from './Service';
import RunnerService from './RunnerService';

type Configuration =
{
service: Service;
sourcingManager: SourcingManager;
setUpScripts?: string[];
tearDownScripts?: string[];
};

export default class Server implements Runner
{
#service: Service;
#sourcingManager: SourcingManager;
#setUpScripts: string[];
#tearDownScripts: string[];

constructor(configuration: Configuration)
{
this.#service = configuration.service;
this.#sourcingManager = configuration.sourcingManager;
this.#setUpScripts = configuration.setUpScripts ?? [];
this.#tearDownScripts = configuration.tearDownScripts ?? [];
}

get service() { return this.#service; }

async start(): Promise<void>
{
await this.#setUp();

return this.#service.start();
}

async stop(): Promise<void>
{
await this.#service.stop();;

return this.#tearDown();
}

run(request: Request): Promise<Response>
{
if (this.#canNotRun())
{
throw new Error('Cannot run');
}

return (this.#service as RunnerService).run(request);
}

#canNotRun(): boolean
{
return (this.#service as RunnerService).run === undefined;
}

#setUp(): Promise<void>
{
return this.#runScripts(this.#setUpScripts);
}

#tearDown(): Promise<void>
{
return this.#runScripts(this.#tearDownScripts);
}

async #runScripts(scripts: string[]): Promise<void>
{
await Promise.all(scripts.map(script => this.#sourcingManager.import(script)));
}
}
1 change: 1 addition & 0 deletions packages/services/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as LocalWorker } from './worker/LocalWorker';
export { default as RemoteWorker } from './worker/RemoteWorker';

export { default as Client } from './Client';
export { default as Server } from './Server';
export { default as Remote } from './Remote';

export { default as Service } from './Service';
Expand Down
16 changes: 8 additions & 8 deletions packages/sourcing/src/SourcingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { Module, ImportFunction, ModuleNotLoaded } from './modules';

export default class SourceManager
{
#import: ImportFunction;
#fileManager: FileManager;
#import: ImportFunction;

constructor(importFunction: ImportFunction, fileManager: FileManager)
constructor(fileManager: FileManager, importFunction: ImportFunction)
{
this.#import = importFunction;
this.#fileManager = fileManager;
this.#import = importFunction;
}

async read(filename: string): Promise<File>
{
return this.#fileManager.read(filename);
}

async import(specifier: string): Promise<Module>
Expand All @@ -33,9 +38,4 @@ export default class SourceManager
throw new ModuleNotLoaded(specifier, message);
}
}

async read(filename: string): Promise<File>
{
return this.#fileManager.read(filename);
}
}

0 comments on commit 49335b4

Please sign in to comment.