Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'one-time' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const id = yeast();
/**
* Prevents execution of resolve and reject due to race conditions.
*
* @type {Function}
* @param {Error} err Optional error.
* @param {String} data The response.
* @returns {Undefined} Nothing useful.
* @private
*/
const done = once((err, data) => {
this.active.delete(xhr);
this.timers.clear(id);
if (!err) {
debug(`(${id}) successfully completed http request`);
return resolve(data);
}
//
// In case of error we want to be sure that the connection with the
// server/socket was closed, so we're going to forcefuly abort.
//
try {
xhr.abort();
} catch (e) {
debug('aborting xhr failed', e);
return;
}
debug('restoring previous replaced handler');
ErrorUtils.setGlobalHandler(old);
});
/**
* Our custom uncaughtException handler, we want to store this reference so
* when we attempt to restore the error handler, we could check if the
* current handler this function so we don't accidentally override a new handler.
*
* @type {Function}
* @private
*/
const handler = once(function uncaughtException(...args) {
debug('captured uncaught exception', args);
//
// We only want to call our own error handler as this exception happened
// while running the test suite we don't accidenlty want to trigger any
// error reporting that shouldn't be triggered.
//
fn(...args);
});
ErrorUtils.setGlobalHandler(handler);
return restore;
}
async function intercept({ send }) {
const ocm = ['log', 'info', 'warn', 'error'];
const oc = {};
ocm.forEach(function each(method) {
oc[method] = console[method];
console[method] = send.bind(send, `console.${method}`);
});
return once(async function after() {
ocm.forEach(function each(method) {
console[method] = oc[method];
delete oc[method];
});
});
}
module.exports = controller => {
if (!controller.confs.icheques.hosts.includes(document.location.hostname)) return;
const failAlert = () => harlan.alert({
subtitle: 'Não foi possível carregar um módulo da iCheques.',
paragraph: 'Verifique se o endereço cdn.jsdelivr.net é liberado na sua rede interna.',
});
const refinCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-refin/index.js').fail(failAlert));
const veiculosCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-veiculos/index.js').fail(failAlert));
const followCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-follow-document/index.js').fail(failAlert));
controller.registerBootstrap('icheques::init::plataform', callback => $.getScript('/js/icheques.js').done(() => {
callback();
const tags = (controller.confs.user || {}).tags || [];
if (tags.indexOf('no-follow') === -1) followCall();
if (tags.indexOf('no-refin') === -1) refinCall();
if (tags.indexOf('no-veiculos') === -1) veiculosCall();
}).fail(() => {
callback();
failAlert();
}));
};
namespace: 'ekke',
...options
};
const url = `ws://${this.hostname}:${this.port}/${namespace}`;
const socket = new WebSocket(url);
/**
* The full clean-up pass that we need to do when a connection is closed.
*
* @type {Function}
* @param {Boolean} [alive] Should we check if CLI comes back to life?
* @public
*/
const cleanup = once(function cleaner(alive = false) {
try {
socket.close();
} catch (e) {
debug('closing the socket failed, but at least we tried', e);
}
this.timers.clear('socket');
this.socket = null;
if (alive) this.alive();
}.bind(this));
/**
* Handle incoming messages from the socket.
*
* @param {MessageEvent} event The WebSocket Message Event.
module.exports = controller => {
if (!controller.confs.icheques.hosts.includes(document.location.hostname)) return;
const failAlert = () => harlan.alert({
subtitle: 'Não foi possível carregar um módulo da iCheques.',
paragraph: 'Verifique se o endereço cdn.jsdelivr.net é liberado na sua rede interna.',
});
const refinCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-refin/index.js').fail(failAlert));
const veiculosCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-veiculos/index.js').fail(failAlert));
const followCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-follow-document/index.js').fail(failAlert));
controller.registerBootstrap('icheques::init::plataform', callback => $.getScript('/js/icheques.js').done(() => {
callback();
const tags = (controller.confs.user || {}).tags || [];
if (tags.indexOf('no-follow') === -1) followCall();
if (tags.indexOf('no-refin') === -1) refinCall();
if (tags.indexOf('no-veiculos') === -1) veiculosCall();
}).fail(() => {
callback();
failAlert();
}));
};
function capture(fn) {
const old = ErrorUtils.getGlobalHandler();
/**
* A function that will restore the error handler to it's original state
* as we've found it, we only want to restore it once or we could accidentally
* override another error handler.
*
* @type {Function}
* @public
*/
const restore = once(function previous() {
if (ErrorUtils.getGlobalHandler() !== handler) {
debug('unable to restore old handler, as our current got replaced');
return;
}
debug('restoring previous replaced handler');
ErrorUtils.setGlobalHandler(old);
});
/**
* Our custom uncaughtException handler, we want to store this reference so
* when we attempt to restore the error handler, we could check if the
* current handler this function so we don't accidentally override a new handler.
*
* @type {Function}
* @private
module.exports = controller => {
if (!controller.confs.icheques.hosts.includes(document.location.hostname)) return;
const failAlert = () => harlan.alert({
subtitle: 'Não foi possível carregar um módulo da iCheques.',
paragraph: 'Verifique se o endereço cdn.jsdelivr.net é liberado na sua rede interna.',
});
const refinCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-refin/index.js').fail(failAlert));
const veiculosCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-icheques-veiculos/index.js').fail(failAlert));
const followCall = oneTime(() => $.getScript('https://cdn.jsdelivr.net/npm/harlan-follow-document/index.js').fail(failAlert));
controller.registerBootstrap('icheques::init::plataform', callback => $.getScript('/js/icheques.js').done(() => {
callback();
const tags = (controller.confs.user || {}).tags || [];
if (tags.indexOf('no-follow') === -1) followCall();
if (tags.indexOf('no-refin') === -1) refinCall();
if (tags.indexOf('no-veiculos') === -1) veiculosCall();
}).fail(() => {
callback();
failAlert();
}));
};
complete() {
return once(async function completed(err) {
debug('completed the run', err);
if (err) {
if (typeof err === 'number') {
err = new Error(`Test are failing with exit code ${err}`);
}
this.subway.send('complete', failure(err));
} else {
this.subway.send('complete');
}
await this.teardown();
}.bind(this));
}