Skip to content

Commit

Permalink
Merge pull request #69 from thoov/bower-support
Browse files Browse the repository at this point in the history
add bower support
  • Loading branch information
thoov committed Jan 5, 2016
2 parents 362eb75 + c7f3620 commit ffbd179
Show file tree
Hide file tree
Showing 17 changed files with 12,279 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
node_modules
coverage
tmp
dist

# Individual files to exclude
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mock-socket",
"version": "1.0.5",
"homepage": "https://github.com/thoov/mock-socket",
"main": "./src/main.js",
"main": "./dist/mock-socket.min.js",
"moduleType": "es6",
"authors": [
"Travis Hoover <[email protected]>"
Expand Down
150 changes: 150 additions & 0 deletions dist/event-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _helpersEventObject = require('./helpers/event-object');

var _helpersEventObject2 = _interopRequireDefault(_helpersEventObject);

var _helpersEnvironmentCheck = require('./helpers/environment-check');

var _helpersEnvironmentCheck2 = _interopRequireDefault(_helpersEnvironmentCheck);

/*
* Natively you cannot set or modify the properties: target, srcElement, and currentTarget on Event or
* MessageEvent objects. So in order to set them to the correct values we "overwrite" them to the same
* property but without the restriction of not writable.
*
* @param {object} event - an event object to extend
* @param {object} target - the value that should be set for target, srcElement, and currentTarget
*/
function extendEvent(event, target) {
Object.defineProperties(event, {
target: {
configurable: true,
writable: true
},
srcElement: {
configurable: true,
writable: true
},
currentTarget: {
configurable: true,
writable: true
}
});

if (target) {
event.target = target;
event.srcElement = target;
event.currentTarget = target;
}

return event;
}

/*
* This will return either the native Event/MessageEvent/CloseEvent
* if we are in the browser or it will return the mocked event.
*/
function eventFactory(eventClassName, type, config) {
if (!_helpersEnvironmentCheck2['default'].globalContext[eventClassName]) {
return new _helpersEventObject2['default']({ type: type });
}

if (!config) {
return new _helpersEnvironmentCheck2['default'].globalContext[eventClassName](type);
}

return new _helpersEnvironmentCheck2['default'].globalContext[eventClassName](type, config);
}

/*
* Creates an Event object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config you will need to pass type and optionally target
*/
function createEvent(config) {
var type = config.type;
var target = config.target;

var event = eventFactory('Event', type);

if (!event.path) {
event = JSON.parse(JSON.stringify(event));
}

return extendEvent(event, target);
}

/*
* Creates a MessageEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config you will need to pass type, origin, data and optionally target
*/
function createMessageEvent(config) {
var type = config.type;
var origin = config.origin;
var data = config.data;
var target = config.target;

var messageEvent = eventFactory('MessageEvent', type);

if (!messageEvent.path) {
messageEvent = JSON.parse(JSON.stringify(messageEvent));
}

extendEvent(messageEvent, target);

if (messageEvent.initMessageEvent) {
messageEvent.initMessageEvent(type, false, false, data, origin, '');
} else {
messageEvent.data = data;
messageEvent.origin = origin;
}

return messageEvent;
}

/*
* Creates a CloseEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config you will need to pass type and optionally target, code, and reason
*/
function createCloseEvent(config) {
var code = config.code;
var reason = config.reason;
var type = config.type;
var target = config.target;
var wasClean = config.wasClean;

if (!wasClean) {
wasClean = code === 1000;
}

var closeEvent = eventFactory('CloseEvent', type, {
code: code,
reason: reason,
wasClean: wasClean
});

if (!closeEvent.path || !closeEvent.code) {
closeEvent = JSON.parse(JSON.stringify(closeEvent));
closeEvent.code = code || 0;
closeEvent.reason = reason || '';
closeEvent.wasClean = wasClean;
}

return extendEvent(closeEvent, target);
}

exports.createEvent = createEvent;
exports.createMessageEvent = createMessageEvent;
exports.createCloseEvent = createCloseEvent;
105 changes: 105 additions & 0 deletions dist/event-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var _helpersArrayHelpers = require('./helpers/array-helpers');

/*
* EventTarget is an interface implemented by objects that can
* receive events and may have listeners for them.
*
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
*/

var EventTarget = (function () {
function EventTarget() {
_classCallCheck(this, EventTarget);

this.listeners = {};
}

/*
* Ties a listener function to a event type which can later be invoked via the
* dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - the callback function to invoke whenever a event is dispatched matching the given type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/

_createClass(EventTarget, [{
key: 'addEventListener',
value: function addEventListener(type, listener /* , useCapture */) {
if (typeof listener === 'function') {
if (!Array.isArray(this.listeners[type])) {
this.listeners[type] = [];
}

// Only add the same function once
if ((0, _helpersArrayHelpers.filter)(this.listeners[type], function (item) {
return item === listener;
}).length === 0) {
this.listeners[type].push(listener);
}
}
}

/*
* Removes the listener so it will no longer be invoked via the dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - the callback function to invoke whenever a event is dispatched matching the given type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/
}, {
key: 'removeEventListener',
value: function removeEventListener(type, removingListener /* , useCapture */) {
var arrayOfListeners = this.listeners[type];
this.listeners[type] = (0, _helpersArrayHelpers.reject)(arrayOfListeners, function (listener) {
return listener === removingListener;
});
}

/*
* Invokes all listener functions that are listening to the given event.type property. Each
* listener will be passed the event as the first argument.
*
* @param {object} event - event object which will be passed to all listeners of the event.type property
*/
}, {
key: 'dispatchEvent',
value: function dispatchEvent(event) {
var _this = this;

for (var _len = arguments.length, customArguments = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
customArguments[_key - 1] = arguments[_key];
}

var eventName = event.type;
var listeners = this.listeners[eventName];

if (!Array.isArray(listeners)) {
return false;
}

listeners.forEach(function (listener) {
if (customArguments.length > 0) {
listener.apply(_this, customArguments);
} else {
listener.call(_this, event);
}
});
}
}]);

return EventTarget;
})();

exports['default'] = EventTarget;
module.exports = exports['default'];
29 changes: 29 additions & 0 deletions dist/helpers/array-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reject = reject;
exports.filter = filter;

function reject(array, callback) {
var results = [];
array.forEach(function (itemInArray) {
if (!callback(itemInArray)) {
results.push(itemInArray);
}
});

return results;
}

function filter(array, callback) {
var results = [];
array.forEach(function (itemInArray) {
if (callback(itemInArray)) {
results.push(itemInArray);
}
});

return results;
}
20 changes: 20 additions & 0 deletions dist/helpers/close-codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
*/
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var codes = {
CLOSE_NORMAL: 1000,
CLOSE_GOING_AWAY: 1001,
CLOSE_PROTOCOL_ERROR: 1002,
CLOSE_UNSUPPORTED: 1003,
CLOSE_NO_STATUS: 1005,
CLOSE_ABNORMAL: 1006,
CLOSE_TOO_LARGE: 1009
};

exports["default"] = codes;
module.exports = exports["default"];
21 changes: 21 additions & 0 deletions dist/helpers/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This delay allows the thread to finish assigning its on* methods
* before invoking the delay callback. This is purely a timing hack.
* http://geekabyte.blogspot.com/2014/01/javascript-effect-of-setting-settimeout.html
*
* @param {callback: function} the callback which will be invoked after the timeout
* @parma {context: object} the context in which to invoke the function
*/
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
function delay(callback, context) {
setTimeout(function timeout(timeoutContext) {
callback.call(timeoutContext);
}, 4, context);
}

exports["default"] = delay;
module.exports = exports["default"];
18 changes: 18 additions & 0 deletions dist/helpers/environment-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
function isNode() {
if (typeof window === 'undefined') {
return true;
}

return false;
}

exports['default'] = {
globalContext: isNode() ? global : window,
isNode: isNode
};
module.exports = exports['default'];
Loading

0 comments on commit ffbd179

Please sign in to comment.