UNPKG

tgrid

Version:

Grid Computing Framework for TypeScript

253 lines 11.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SharedWorkerAcceptor = void 0; var AcceptorBase_1 = require("../internal/AcceptorBase"); /** * SharedWorker acceptor for client. * * - available only in the Web Browser. * * The `SharedWorkerAcceptor` is a communicator class interacting with the * {@link SharedWorkerConnector} through RFC (Remote Function Call), created by * the {@link SharedWorkerServer} class whenever a client connects to the * `SharedWorker` instance. * * When a remote client connects to the {@link SharedWorkerServer}, * so that a new `SharedworkerAcceptor` instance being created, you can determine * whether to {@link accept} the client's connection or {@link reject not}, * reading the {@link header} property. If you've decided to accept the connection, * call the {@link accept} method with `Provider` instance. Otherwise, reject it * thorugh the {@link reject} method. * * After {@link accept accepting} the connection, don't forget to * {@link close closing} the connection after your business has been completed * to clean up the resources. Otherwise the closing must be performed by the remote * client, you can wait the remote client's closing signal by the {@link join} method. * * Also, when declaring this {@link SharedworkerAcceptor} type, you have to define three * generic arguments; `Header`, `Provider` and `Remote`. Those generic arguments must * be same with the ones defined in the {@link SharedWorkerServer} class. * * For reference, the first `Header` type represents an initial data from the * remote client after the connection. I recommend utilize it as an activation tool * for security enhancement. The second generic argument `Provider` represents a * provider from server to client, and the other `Remote` means a provider from the * remote client to server. * * @template Header Type of the header containing initial data. * @template Provider Type of features provided for the remote client. * @template Remote Type of features provided by remote client. * @author Jeongho Nam - https://github.com/samchon */ var SharedWorkerAcceptor = /** @class */ (function (_super) { __extends(SharedWorkerAcceptor, _super); /** * @hidden */ function SharedWorkerAcceptor(port, header, eraser) { var _this = _super.call(this, header) || this; // ASSIGN MEMBER _this.port_ = port; _this.eraser_ = eraser; return _this; } /* ---------------------------------------------------------------- CONSTRUCTOR ---------------------------------------------------------------- */ /** * @internal */ SharedWorkerAcceptor.create = function (port, header, eraser) { return new SharedWorkerAcceptor(port, header, eraser); }; /** * @inheritDoc */ SharedWorkerAcceptor.prototype.close = function () { return __awaiter(this, void 0, void 0, function () { var error; return __generator(this, function (_a) { switch (_a.label) { case 0: error = this.inspectReady("close"); if (error) throw error; // CLOSE CONNECTION this.state_ = 2 /* SharedWorkerAcceptor.State.CLOSING */; return [4 /*yield*/, this._Close()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @hidden */ SharedWorkerAcceptor.prototype._Close = function (reason) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: // DESTRUCTOR this.eraser_(); return [4 /*yield*/, this.destructor()]; case 1: _a.sent(); // DO CLOSE setTimeout(function () { _this.port_.postMessage(reason === undefined ? 2 /* SharedWorkerAcceptor.State.CLOSING */ : JSON.stringify(reason)); _this.port_.close(); }); // WELL, IT MAY HARD TO SEE SUCH PROPERTIES this.state_ = 3 /* SharedWorkerAcceptor.State.CLOSED */; return [2 /*return*/]; } }); }); }; /* ---------------------------------------------------------------- HANDSHAKES ---------------------------------------------------------------- */ /** * @inheritDoc */ SharedWorkerAcceptor.prototype.accept = function (provider) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { // TEST CONDITION if (this.state_ !== -1 /* SharedWorkerAcceptor.State.NONE */) throw new Error("Error on SharedWorkerAcceptor.accept(): you've already accepted (or rejected) the connection."); //---- // ACCEPT CONNECTION //---- this.state_ = 0 /* 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(1 /* SharedWorkerAcceptor.State.OPEN */); } this.state_ = 1 /* SharedWorkerAcceptor.State.OPEN */; return [2 /*return*/]; }); }); }; /** * Reject connection. * * Reject without acceptance, any interaction. The connection would be closed immediately. * * @param reason Detailed reason of the rejection. Default is "Rejected by server". */ SharedWorkerAcceptor.prototype.reject = function () { return __awaiter(this, arguments, void 0, function (reason) { if (reason === void 0) { reason = "Rejected by server"; } return __generator(this, function (_a) { switch (_a.label) { case 0: // TEST CONDITION if (this.state_ !== -1 /* SharedWorkerAcceptor.State.NONE */) throw new Error("Error on SharedWorkerAcceptor.reject(): you've already accepted (or rejected) the connection."); //---- // REJECT CONNECTION (CLOSE) //---- this.state_ = -2 /* SharedWorkerAcceptor.State.REJECTING */; return [4 /*yield*/, this._Close({ name: "reject", message: reason })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /* ---------------------------------------------------------------- COMMUNICATOR ---------------------------------------------------------------- */ /** * @hidden */ SharedWorkerAcceptor.prototype.sendData = function (invoke) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this.port_.postMessage(JSON.stringify(invoke)); return [2 /*return*/]; }); }); }; /** * @hidden */ SharedWorkerAcceptor.prototype._Handle_message = function (evt) { if (evt.data === 2 /* SharedWorkerAcceptor.State.CLOSING */) this.close().catch(function () { }); else this.replyData(JSON.parse(evt.data)); }; return SharedWorkerAcceptor; }(AcceptorBase_1.AcceptorBase)); exports.SharedWorkerAcceptor = SharedWorkerAcceptor; /** * */ (function (SharedWorkerAcceptor) { })(SharedWorkerAcceptor || (exports.SharedWorkerAcceptor = SharedWorkerAcceptor = {})); //# sourceMappingURL=SharedWorkerAcceptor.js.map