Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Es modules #641

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

17 changes: 0 additions & 17 deletions .eslintrc

This file was deleted.

7 changes: 3 additions & 4 deletions lib/entry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
import transportList from './transport-list.js';
import main from './main.js';

var transportList = require('./transport-list');

module.exports = require('./main')(transportList);
export default main(transportList);

// TODO can't get rid of this until all servers do
if ('_sockjs_onload' in global) {
Expand Down
26 changes: 11 additions & 15 deletions lib/event/close.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
'use strict';

var inherits = require('inherits')
, Event = require('./event')
;

function CloseEvent() {
Event.call(this);
this.initEvent('close', false, false);
this.wasClean = false;
this.code = 0;
this.reason = '';
import Event from './event.js';

class CloseEvent extends Event {
constructor() {
super('close');
this.initEvent(false, false);
this.wasClean = false;
this.code = 0;
this.reason = '';
}
}

inherits(CloseEvent, Event);

module.exports = CloseEvent;
export default CloseEvent;
82 changes: 38 additions & 44 deletions lib/event/emitter.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,51 @@
'use strict';
import EventTarget from './eventtarget.js';

class EventEmitter extends EventTarget {
removeAllListeners(type) {
if (type) {
delete this._listeners[type];
} else {
this._listeners = {};
}
}

var inherits = require('inherits')
, EventTarget = require('./eventtarget')
;
once(type, listener) {
let fired = false;

function EventEmitter() {
EventTarget.call(this);
}
const g = function (...args) {
this.removeListener(type, g);

inherits(EventEmitter, EventTarget);
if (!fired) {
fired = true;
Reflect.apply(listener, this, args);
}
}.bind(this);

EventEmitter.prototype.removeAllListeners = function(type) {
if (type) {
delete this._listeners[type];
} else {
this._listeners = {};
this.on(type, g);
}
};

EventEmitter.prototype.once = function(type, listener) {
var self = this
, fired = false;

function g() {
self.removeListener(type, g);

if (!fired) {
fired = true;
listener.apply(this, arguments);
emit(...args) {
const type = args[0];
const listeners = this._listeners[type];
if (!listeners) {
return;
}
}

this.on(type, g);
};
// Equivalent of Array.prototype.slice.call(arguments, 1);
const l = args.length;
const copyArgs = Array.from({length: l - 1});
for (let ai = 1; ai < l; ai++) {
copyArgs[ai - 1] = args[ai];
}

EventEmitter.prototype.emit = function() {
var type = arguments[0];
var listeners = this._listeners[type];
if (!listeners) {
return;
}
// equivalent of Array.prototype.slice.call(arguments, 1);
var l = arguments.length;
var args = new Array(l - 1);
for (var ai = 1; ai < l; ai++) {
args[ai - 1] = arguments[ai];
}
for (var i = 0; i < listeners.length; i++) {
listeners[i].apply(this, args);
for (const listener of listeners) {
listener.apply(this, copyArgs);
}
}
};
}

EventEmitter.prototype.on = EventEmitter.prototype.addListener = EventTarget.prototype.addEventListener;
EventEmitter.prototype.addListener = EventTarget.prototype.addEventListener;
EventEmitter.prototype.on = EventTarget.prototype.addEventListener;
EventEmitter.prototype.removeListener = EventTarget.prototype.removeEventListener;

module.exports.EventEmitter = EventEmitter;
export {EventEmitter};
35 changes: 17 additions & 18 deletions lib/event/event.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
'use strict';
class Event {
constructor(eventType) {
this.type = eventType;
}

function Event(eventType) {
this.type = eventType;
}

Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
this.type = eventType;
this.bubbles = canBubble;
this.cancelable = cancelable;
this.timeStamp = +new Date();
return this;
};
static CAPTURING_PHASE = 1;
static AT_TARGET = 2;
static BUBBLING_PHASE = 3;

Event.prototype.stopPropagation = function() {};
Event.prototype.preventDefault = function() {};
initEvent(canBubble, cancelable) {
this.bubbles = canBubble;
this.cancelable = cancelable;
this.timeStamp = Date.now();
return this;
}

Event.CAPTURING_PHASE = 1;
Event.AT_TARGET = 2;
Event.BUBBLING_PHASE = 3;
stopPropagation() {}
preventDefault() {}
}

module.exports = Event;
export default Event;
98 changes: 49 additions & 49 deletions lib/event/eventtarget.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
'use strict';

/* Simplified implementation of DOM2 EventTarget.
* http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
*/

function EventTarget() {
this._listeners = {};
}

EventTarget.prototype.addEventListener = function(eventType, listener) {
if (!(eventType in this._listeners)) {
this._listeners[eventType] = [];
}
var arr = this._listeners[eventType];
// #4
if (arr.indexOf(listener) === -1) {
// Make a copy so as not to interfere with a current dispatchEvent.
arr = arr.concat([listener]);
class EventTarget {
constructor() {
this._listeners = {};
}
this._listeners[eventType] = arr;
};

EventTarget.prototype.removeEventListener = function(eventType, listener) {
var arr = this._listeners[eventType];
if (!arr) {
return;
}
var idx = arr.indexOf(listener);
if (idx !== -1) {
if (arr.length > 1) {
addEventListener(eventType, listener) {
if (!(eventType in this._listeners)) {
this._listeners[eventType] = [];
}

let array = this._listeners[eventType];
// #4
if (!array.includes(listener)) {
// Make a copy so as not to interfere with a current dispatchEvent.
this._listeners[eventType] = arr.slice(0, idx).concat(arr.slice(idx + 1));
} else {
delete this._listeners[eventType];
array = [...array, listener];
}
return;

this._listeners[eventType] = array;
}
};

EventTarget.prototype.dispatchEvent = function() {
var event = arguments[0];
var t = event.type;
// equivalent of Array.prototype.slice.call(arguments, 0);
var args = arguments.length === 1 ? [event] : Array.apply(null, arguments);
// TODO: This doesn't match the real behavior; per spec, onfoo get
// their place in line from the /first/ time they're set from
// non-null. Although WebKit bumps it to the end every time it's
// set.
if (this['on' + t]) {
this['on' + t].apply(this, args);

removeEventListener(eventType, listener) {
const array = this._listeners[eventType];
if (!array) {
return;
}

const idx = array.indexOf(listener);
if (idx !== -1) {
if (array.length > 1) {
// Make a copy so as not to interfere with a current dispatchEvent.
this._listeners[eventType] = [...array.slice(0, idx), ...array.slice(idx + 1)];
} else {
delete this._listeners[eventType];
}
}
}
if (t in this._listeners) {
// Grab a reference to the listeners list. removeEventListener may alter the list.
var listeners = this._listeners[t];
for (var i = 0; i < listeners.length; i++) {
listeners[i].apply(this, args);

dispatchEvent(...args) {
const t = args[0].type;
// TODO: This doesn't match the real behavior; per spec, onfoo get
// their place in line from the /first/ time they're set from
// non-null. Although WebKit bumps it to the end every time it's
// set.
if (this['on' + t]) {
this['on' + t](...args);
}

if (t in this._listeners) {
// Grab a reference to the listeners list. removeEventListener may alter the list.
const listeners = this._listeners[t];
for (const listener of listeners) {
listener.apply(this, args);
}
}
}
};
}

module.exports = EventTarget;
export default EventTarget;
22 changes: 9 additions & 13 deletions lib/event/trans-message.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'use strict';

var inherits = require('inherits')
, Event = require('./event')
;

function TransportMessageEvent(data) {
Event.call(this);
this.initEvent('message', false, false);
this.data = data;
import Event from './event.js';

class TransportMessageEvent extends Event {
constructor(data) {
super('message');
this.initEvent(false, false);
this.data = data;
}
}

inherits(TransportMessageEvent, Event);

module.exports = TransportMessageEvent;
export default TransportMessageEvent;
46 changes: 24 additions & 22 deletions lib/facade.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
'use strict';
import {postMessage} from './utils/iframe.js';

var iframeUtils = require('./utils/iframe')
;
class FacadeJS {
constructor(transport) {
this._transport = transport;
transport.on('message', this._transportMessage.bind(this));
transport.on('close', this._transportClose.bind(this));
}

function FacadeJS(transport) {
this._transport = transport;
transport.on('message', this._transportMessage.bind(this));
transport.on('close', this._transportClose.bind(this));
}
_transportClose(code, reason) {
postMessage('c', JSON.stringify([code, reason]));
}

_transportMessage(frame) {
postMessage('t', frame);
}

FacadeJS.prototype._transportClose = function(code, reason) {
iframeUtils.postMessage('c', JSON.stringify([code, reason]));
};
FacadeJS.prototype._transportMessage = function(frame) {
iframeUtils.postMessage('t', frame);
};
FacadeJS.prototype._send = function(data) {
this._transport.send(data);
};
FacadeJS.prototype._close = function() {
this._transport.close();
this._transport.removeAllListeners();
};
_send(data) {
this._transport.send(data);
}

_close() {
this._transport.close();
this._transport.removeAllListeners();
}
}

module.exports = FacadeJS;
export default FacadeJS;
Loading