Skip to content

Commit

Permalink
fix: fix export of module in browser environment
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Dec 22, 2016
1 parent 2cc08f0 commit 5a0594c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
83 changes: 43 additions & 40 deletions examples/jquery/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
/* global $ circuitBreaker */

(function appInitialization () {
$(() => {
$('#flakey').click(handleClick('/flakeyService', '#flakeyResponse'));
$('.clear').click(function () { $(this).siblings('p').remove(); });
});
const route = '/flakeyService';
const element = '#flakeyResponse';

const circuitBreakerOptions = {
timeout: 500,
Expand All @@ -14,55 +12,60 @@
Promise: Promise
};

function handleClick (route, element) {
const circuit = circuitBreaker((route, element) => {
circuit.fallback(() => ({ body: `${route} unavailable right now. Try later.` }));
const circuit = circuitBreaker((route, element) => {
circuit.fallback(() => ({ body: `${route} unavailable right now. Try later.` }));

// Return a promise to the circuit
return new Promise((resolve, reject) => {
$.get(route)
.done((data) => resolve(data))
.fail((err) => {
reject(err);
console.error(err);
});
});
}, circuitBreakerOptions);
// Return a promise to the circuit
return new Promise((resolve, reject) => {
$.get(route)
.done((data) => resolve(data))
.fail((err) => {
reject(err);
console.error(err);
});
});
}, circuitBreakerOptions);

circuit.on('success',
(data) => $(element).append(makeNode(`SUCCESS: ${JSON.stringify(data)}`)));
circuit.on('success',
(data) => $(element).append(makeNode(`SUCCESS: ${JSON.stringify(data)}`)));

circuit.on('timeout',
() => $(element).append(
makeNode(`TIMEOUT: ${route} is taking too long to respond.`)));
circuit.on('timeout',
() => $(element).append(
makeNode(`TIMEOUT: ${route} is taking too long to respond.`)));

circuit.on('reject',
() => $(element).append(
makeNode(`REJECTED: The breaker for ${route} is open. Failing fast.`)));
circuit.on('reject',
() => $(element).append(
makeNode(`REJECTED: The breaker for ${route} is open. Failing fast.`)));

circuit.on('open',
() => $(element).append(
makeNode(`OPEN: The breaker for ${route} just opened.`)));
circuit.on('open',
() => $(element).append(
makeNode(`OPEN: The breaker for ${route} just opened.`)));

circuit.on('halfOpen',
() => $(element).append(
makeNode(`HALF_OPEN: The breaker for ${route} is half open.`)));
circuit.on('halfOpen',
() => $(element).append(
makeNode(`HALF_OPEN: The breaker for ${route} is half open.`)));

circuit.on('close',
() => $(element).append(
makeNode(`CLOSE: The breaker for ${route} has closed. Service OK.`)));
circuit.on('close',
() => $(element).append(
makeNode(`CLOSE: The breaker for ${route} has closed. Service OK.`)));

circuit.on('fallback',
(data) => $(element).append(
makeNode(`FALLBACK: ${JSON.stringify(data)}`)));

return () => circuit.fire(route, element).catch((e) => console.error(e));
}
circuit.on('fallback',
(data) => $(element).append(
makeNode(`FALLBACK: ${JSON.stringify(data)}`)));

function makeNode (body) {
const response = document.createElement('p');
$(response).addClass(body.substring(0, body.indexOf(':')).toLowerCase());
response.append(body);
return response;
}

function callService () {
circuit.fire(route, element).catch((e) => console.error(e));
}

$(() => {
$('#flakey').click(callService);
$('.clear').click(function () { $(this).siblings('p').remove(); });
});
})();
8 changes: 1 addition & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,10 @@ function circuitBreaker (action, options) {
circuitBreaker.promisify = require('./lib/promisify');

function exportModule (exported) {
console.log('Exporting', exported);
console.log('Module', module);
if (typeof document === 'object') {
// in a browser environment
root[exported.name] = exported;
} else if (typeof module === 'object' && module.exports) {
if (typeof module === 'object' && module.exports) {
// we're in a node.js environment
module.exports = exports = exported;
} else {
// ??
root[exported.name] = exported;
}
}
Expand Down

0 comments on commit 5a0594c

Please sign in to comment.