@awcrotwell/motion
Version:
Motion allows you to build reactive, real-time frontend UI components in your Amber application using pure Crystal that are reusable, testable & encapsulated. For brevity, we will call them MotionComponents.
51 lines (50 loc) • 1.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWebSocketURL = void 0;
const Connection_1 = __importDefault(require("./Connection"));
const Subscriptions_1 = __importDefault(require("./Subscriptions"));
function createWebSocketURL(inputUrl) {
const url = typeof inputUrl === 'function' ? inputUrl() : inputUrl;
if (url && !/^wss?:/i.test(url)) {
const a = document.createElement('a');
a.href = url;
// Fix populating Location properties in IE. Otherwise, protocol will be blank.
a.href = a.href; // eslint-disable-line no-self-assign
a.protocol = a.protocol.replace('http', 'ws');
return a.href;
}
return url;
}
exports.createWebSocketURL = createWebSocketURL;
class Consumer {
constructor(url) {
this._url = url;
this.subscriptions = new Subscriptions_1.default(this);
this.connection = new Connection_1.default(this);
}
get url() {
return createWebSocketURL(this._url);
}
send(data) {
return this.connection.send(data);
}
joinChannel(data) {
return this.connection.joinChannel(data);
}
connect() {
return this.connection.open();
}
disconnect() {
return this.connection.close({ allowReconnect: false });
}
ensureActiveConnection() {
if (!this.connection.isActive()) {
return this.connection.open();
}
return false;
}
}
exports.default = Consumer;