UNPKG

@patternplate/client

Version:

Universal javascript client application for patternplate

66 lines (50 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginHub = void 0; var _arson = _interopRequireDefault(require("arson")); var _websocketClient = require("@patternplate/websocket-client"); var uuid = _interopRequireWildcard(require("uuid")); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } const SINGLETON = { nonce: uuid.v4() }; const SCOPE = new WeakMap(); class PluginHub { static create(init) { if (!SCOPE.has(SINGLETON)) { SCOPE.set(SINGLETON, new PluginHub(_extends({}, init, { nonce: SINGLETON }))); } return SCOPE.get(SINGLETON); } constructor(init) { if (init.nonce !== SINGLETON) { throw new TypeError(`Calling new PluginHub() directly is not supported, use PluginHub.create()`); } const ws = new _websocketClient.WebSocketClient({ src: init.src, reconnect: true, interval: 1000 }); ws.open(); ws.onMessage(message => { console.log(message); }); SCOPE.set(this, { ws }); } send(message) { const _SCOPE$get = SCOPE.get(this), ws = _SCOPE$get.ws; message.type = 'plugin'; ws.send(_arson.default.stringify(message)); } } exports.PluginHub = PluginHub; //# sourceMappingURL=plugin-hub.js.map