UNPKG

@supabase/realtime-js

Version:
102 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../lib/constants"); class Push { /** * Initializes the Push * * @param channel The Channel * @param event The event, for example `"phx_join"` * @param payload The payload, for example `{user_id: 123}` * @param timeout The push timeout in milliseconds */ constructor(channel, event, payload = {}, timeout = constants_1.DEFAULT_TIMEOUT) { this.channel = channel; this.event = event; this.payload = payload; this.timeout = timeout; this.sent = false; this.timeoutTimer = undefined; this.ref = ''; this.receivedResp = null; this.recHooks = []; this.refEvent = null; } resend(timeout) { this.timeout = timeout; this._cancelRefEvent(); this.ref = ''; this.refEvent = null; this.receivedResp = null; this.sent = false; this.send(); } send() { if (this._hasReceived('timeout')) { return; } this.startTimeout(); this.sent = true; this.channel.socket.push({ topic: this.channel.topic, event: this.event, payload: this.payload, ref: this.ref, join_ref: this.channel._joinRef(), }); } updatePayload(payload) { this.payload = Object.assign(Object.assign({}, this.payload), payload); } receive(status, callback) { var _a; if (this._hasReceived(status)) { callback((_a = this.receivedResp) === null || _a === void 0 ? void 0 : _a.response); } this.recHooks.push({ status, callback }); return this; } startTimeout() { if (this.timeoutTimer) { return; } this.ref = this.channel.socket._makeRef(); this.refEvent = this.channel._replyEventName(this.ref); const callback = (payload) => { this._cancelRefEvent(); this._cancelTimeout(); this.receivedResp = payload; this._matchReceive(payload); }; this.channel._on(this.refEvent, {}, callback); this.timeoutTimer = setTimeout(() => { this.trigger('timeout', {}); }, this.timeout); } trigger(status, response) { if (this.refEvent) this.channel._trigger(this.refEvent, { status, response }); } destroy() { this._cancelRefEvent(); this._cancelTimeout(); } _cancelRefEvent() { if (!this.refEvent) { return; } this.channel._off(this.refEvent, {}); } _cancelTimeout() { clearTimeout(this.timeoutTimer); this.timeoutTimer = undefined; } _matchReceive({ status, response }) { this.recHooks.filter((h) => h.status === status).forEach((h) => h.callback(response)); } _hasReceived(status) { return this.receivedResp && this.receivedResp.status === status; } } exports.default = Push; //# sourceMappingURL=push.js.map