UNPKG

@oat-sa/tao-core-sdk

Version:
80 lines (74 loc) 2.63 kB
define(['lodash', 'core/communicator/poll'], function (_, pollProvider) { 'use strict'; _ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _; pollProvider = pollProvider && Object.prototype.hasOwnProperty.call(pollProvider, 'default') ? pollProvider['default'] : pollProvider; /** * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; under version 2 * of the License (non-upgradable). * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Copyright (c) 2017-2019 (original work) Open Assessment Technologies SA ; */ /** * 'request' provider for {@link core/communicator} * @extends {core/communicator/poll} never start nor stop the polling */ const requestProvider = _.defaults({ /** * the provider name */ name: 'request', /** * @returns {Promise} */ destroy: function destroy() { this.throttledSend = null; this.messagesQueue = null; return Promise.resolve(); }, /** * @returns {Promise} */ open: function open() { return Promise.resolve(); }, /** * @returns {Promise} */ close: function close() { return Promise.resolve(); }, /** * Sends an messages through the communication implementation * @param {String} channel - The name of the communication channel to use * @param {Object} message - The message to send * @returns {Promise} */ send: function send(channel, message) { // queue the message, it will be sent soon var pending = { channel: channel, message: message }; var promise = new Promise(function (resolve, reject) { pending.promise = { resolve: resolve, reject: reject }; }); this.messagesQueue.push(pending); this.request(); return promise; } }, pollProvider); return requestProvider; });