Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) =>
{
// TEST CONDITION
if (this.port_ && this.state_ !== SharedWorkerConnector.State.CLOSED)
{
let err: Error;
if (this.state_ === SharedWorkerConnector.State.CONNECTING)
err = new DomainError("On connecting.");
else if (this.state_ === SharedWorkerConnector.State.OPEN)
err = new DomainError("Already connected.");
else
err = new DomainError("Closing.");
reject(err);
return;
}
//----
// CONNECTOR
//----
try
{
// PREPARE MEMBERS
this.state_ = SharedWorkerConnector.State.CONNECTING;
this.args_ = args;
this.connector_ = new Pair(resolve, reject);
// DO CONNECT
protected inspectReady(): Error | null
{
// NO ERROR
if (this.state_ === WorkerServer.State.OPEN)
return null;
// ERROR, ONE OF THEM
else if (this.state_ === WorkerServer.State.NONE)
return new DomainError("Server is not opened yet.");
else if (this.state_ === WorkerServer.State.OPENING)
return new DomainError("Server is on opening; wait for a sec.");
else if (this.state_ === WorkerServer.State.CLOSING)
return new RuntimeError("Server is on closing.");
// MAY NOT BE OCCURED
else if (this.state_ === WorkerServer.State.CLOSED)
return new RuntimeError("The server has been closed.");
else
return new RuntimeError("Unknown error, but not connected.");
}
public async accept(provider: Provider | null = null): Promise
{
// TEST CONDITION
if (this.state_ !== SharedWorkerAcceptor.State.NONE)
throw new DomainError("You've already accepted (or rejected) the connection.");
//----
// ACCEPT CONNECTION
//----
this.state_ = SharedWorkerAcceptor.State.ACCEPTING;
{
// SET PROVIDER
this.provider_ = provider;
// PREPARE PORT
this.port_.onmessage = this._Handle_message.bind(this);
this.port_.start();
// INFORM ACCEPTANCE
this.port_.postMessage(SharedWorkerAcceptor.State.OPEN);
}
public async open(provider: Provider | null = null): Promise
{
// TEST CONDITION
if (is_node() === false)
{
if (self.document !== undefined)
throw new DomainError("This is not Worker.");
}
else if (global.process.send === undefined)
throw new DomainError("This is not Child Process.");
else if (this.state_ !== WorkerServer.State.NONE)
throw new DomainError("Server has opened yet.");
// OPEN WORKER
this.state_ = WorkerServer.State.OPENING;
{
this.provider_ = provider;
g.onmessage = this._Handle_message.bind(this);
g.postMessage(WorkerServer.State.OPEN);
}
this.state_ = WorkerServer.State.OPEN;
}
return new Promise((resolve, reject) =>
{
// TEST CONDITION
if (this.state_ !== WebAcceptor.State.NONE)
{
reject(new DomainError("You've already accepted (or rejected) the connection."));
return;
}
// PREPARE HANDLER
this.request_.on("requestRejected", async () =>
{
await this.destructor();
resolve();
});
// DO REJECT
this.state_ = WebAcceptor.State.REJECTING;
this.request_.reject(status, reason, extraHeaders);
});
}
public async open(provider: Provider | null = null): Promise
{
// TEST CONDITION
if (is_node() === false)
{
if (self.document !== undefined)
throw new DomainError("This is not Worker.");
}
else if (global.process.send === undefined)
throw new DomainError("This is not Child Process.");
else if (this.state_ !== WorkerServer.State.NONE)
throw new DomainError("Server has opened yet.");
// OPEN WORKER
this.state_ = WorkerServer.State.OPENING;
{
this.provider_ = provider;
g.onmessage = this._Handle_message.bind(this);
g.postMessage(WorkerServer.State.OPEN);
}
this.state_ = WorkerServer.State.OPEN;
}
private _Test_connection(): void
{
if (this.worker_ && this.state !== WorkerConnector.State.CLOSED)
{
if (this.state_ === WorkerConnector.State.CONNECTING)
throw new DomainError("On connecting.");
else if (this.state_ === WorkerConnector.State.OPEN)
throw new DomainError("Already connected.");
else
throw new DomainError("Closing.");
}
}
private _Test_connection(): void
{
if (this.worker_ && this.state !== WorkerConnector.State.CLOSED)
{
if (this.state_ === WorkerConnector.State.CONNECTING)
throw new DomainError("On connecting.");
else if (this.state_ === WorkerConnector.State.OPEN)
throw new DomainError("Already connected.");
else
throw new DomainError("Closing.");
}
}