Skip to content

Commit

Permalink
Added fallback for unsupported SharedWorker. Relates to #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Wildhoney committed Mar 2, 2017
1 parent 1709e60 commit 671c85d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 31 deletions.
90 changes: 62 additions & 28 deletions dist/freelancer.js
@@ -1,4 +1,4 @@
module.exports =
var F =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports =


Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
/**
* @method createUrl
Expand All @@ -85,54 +85,88 @@ Object.defineProperty(exports, "__esModule", {
*/
const createUrl = (fn, ...options) => {

if (typeof fn !== 'function') {
if (typeof fn !== 'function') {

// Ensure the passed parameter is actually a function.
throw new Error('Freelancer: Passed parameter must be a function.');
}
// Ensure the passed parameter is actually a function.
throw new Error('Freelancer: Passed parameter must be a function.');
}

// Map each of the passed options through the JSON stringify process.
const params = options.map(option => JSON.stringify(option));
// Map each of the passed options through the JSON stringify process.
const params = options.map(JSON.stringify);

// Transform the passed function into an IIFE and then create a blob.
const blob = new Blob([`(${fn.toString()})(${params})`], { type: 'application/javascript' });
// Transform the passed function into an IIFE and then create a blob.
const blob = new Blob([`(${fn.toString()})(${params})`], { type: 'application/javascript' });

// Create a URL from the aforementioned blob that handles the worker logic.
return URL.createObjectURL(blob);
// Create a URL from the aforementioned blob that handles the worker logic.
return URL.createObjectURL(blob);
};

/**
* @class Freelancer
* @extends Worker
* @method createFallback
* @param {String} name
* @return {Object}
*/
class Freelancer extends Worker {
const createFallback = name => {

return class {

/**
* @constructor
* @param {Array} args
* @return {Worker}
* @return {NullWorker}
*/
constructor(...args) {
return super(createUrl(...args));
constructor() {

// Raise an error when a worker isn't supported.
throw new Error(`Freelancer: Unfortunately ${name} is not supported by the current browser.`);
}

};
};

/**
* @constant WorkerExtend
* @type {Worker|NullWorker}
*/
const WorkerExtend = window.Worker || createFallback('Worker');

/**
* @constant SharedWorkerExtend
* @type {Worker|NullWorker}
*/
const SharedWorkerExtend = window.SharedWorker || createFallback('SharedWorker');

/**
* @class Freelancer
* @extends Worker
*/
class Freelancer extends WorkerExtend {

/**
* @constructor
* @param {Array} args
* @return {Worker}
*/
constructor(...args) {
return super(createUrl(...args));
}

}

exports.Freelancer = Freelancer; /**
* @class SharedFreelancer
* @extends SharedWorker
*/

class SharedFreelancer extends SharedWorker {
class SharedFreelancer extends SharedWorkerExtend {

/**
* @constructor
* @param {Array} args
* @return {Worker}
*/
constructor(...args) {
return super(createUrl(...args));
}
/**
* @constructor
* @param {Array} args
* @return {Worker}
*/
constructor(...args) {
return super(createUrl(...args));
}

}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -73,4 +73,4 @@
"no-duplicate-imports": "off"
}
}
}
}
40 changes: 38 additions & 2 deletions src/freelancer.js
Expand Up @@ -24,11 +24,47 @@ const createUrl = (fn, ...options) => {

};

/**
* @method createFallback
* @param {String} name
* @return {Object}
*/
const createFallback = name => {

return class {

/**
* @constructor
* @return {Object}
*/
constructor() {

// Raise an error when a worker isn't supported.
throw new Error(`Freelancer: Unfortunately the ${name} is not supported by the current browser.`);

}

}

};

/**
* @constant WorkerExtend
* @type {Worker|Object}
*/
const WorkerExtend = window.Worker || createFallback('Worker');

/**
* @constant SharedWorkerExtend
* @type {Worker|Object}
*/
const SharedWorkerExtend = window.SharedWorker || createFallback('SharedWorker');

/**
* @class Freelancer
* @extends Worker
*/
export class Freelancer extends Worker {
export class Freelancer extends WorkerExtend {

/**
* @constructor
Expand All @@ -45,7 +81,7 @@ export class Freelancer extends Worker {
* @class SharedFreelancer
* @extends SharedWorker
*/
export class SharedFreelancer extends SharedWorker {
export class SharedFreelancer extends SharedWorkerExtend {

/**
* @constructor
Expand Down

0 comments on commit 671c85d

Please sign in to comment.