Skip to content

Instantly share code, notes, and snippets.

@pmcalabrese
Created April 9, 2017 21:39
Show Gist options
  • Save pmcalabrese/babf1e9142159f0455b1709f6fe38e63 to your computer and use it in GitHub Desktop.
Save pmcalabrese/babf1e9142159f0455b1709f6fe38e63 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/sihulec
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
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 GEvent = (function () {
function GEvent() {
_classCallCheck(this, GEvent);
}
_createClass(GEvent, null, [{
key: "dispatch",
value: function dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
}, {
key: "listen",
value: function listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
}, {
key: "createEvent",
value: function createEvent(topic) {
var _this = this;
return {
listen: function listen(cb) {
_this.listen(topic, cb);
},
dispatch: function dispatch(data) {
_this.dispatch(topic, data);
}
};
}
}]);
return GEvent;
})();
var CEvent = (function () {
_createClass(CEvent, [{
key: "dispatch",
value: function dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
}, {
key: "listen",
value: function listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
}]);
function CEvent(topic) {
var _this2 = this;
_classCallCheck(this, CEvent);
this.topic = topic;
return {
listen: function listen(cb) {
_this2.listen(topic, cb);
},
dispatch: function dispatch(data) {
_this2.dispatch(topic, data);
}
};
}
//var event1 = GEvent.createEvent('test');
return CEvent;
})();
var event1 = new CEvent('test');
event1.listen(function (data) {
console.log("test first listener", data);
});
event1.listen(function (data) {
console.log("test second listener", data);
});
event1.dispatch({ data: 'first data' });
setTimeout(function () {
event1.dispatch({ data: 'timeout data' });
}, 500);
</script>
<script id="jsbin-source-javascript" type="text/javascript">class GEvent {
static dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
static listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
static createEvent(topic) {
return {
listen: (cb) => {
this.listen(topic, cb)
},
dispatch: (data) => {
this.dispatch(topic, data)
}
}
}
}
class CEvent {
dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
constructor(topic) {
this.topic = topic
return {
listen: (cb) => {
this.listen(topic, cb)
},
dispatch: (data) => {
this.dispatch(topic, data)
}
}
}
}
//var event1 = GEvent.createEvent('test');
let event1 = new CEvent('test');
event1.listen(function(data) {
console.log("test first listener", data);
})
event1.listen(function(data) {
console.log("test second listener", data);
})
event1.dispatch({ data: 'first data'});
setTimeout(function() {
event1.dispatch({ data: 'timeout data'});
}, 500)
</script></body>
</html>
"use strict";
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 GEvent = (function () {
function GEvent() {
_classCallCheck(this, GEvent);
}
_createClass(GEvent, null, [{
key: "dispatch",
value: function dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
}, {
key: "listen",
value: function listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
}, {
key: "createEvent",
value: function createEvent(topic) {
var _this = this;
return {
listen: function listen(cb) {
_this.listen(topic, cb);
},
dispatch: function dispatch(data) {
_this.dispatch(topic, data);
}
};
}
}]);
return GEvent;
})();
var CEvent = (function () {
_createClass(CEvent, [{
key: "dispatch",
value: function dispatch(topic, data) {
document.dispatchEvent(new CustomEvent(topic, { detail: data }));
}
}, {
key: "listen",
value: function listen(topic, cb) {
document.addEventListener(topic, function (data) {
cb(data.detail);
}, true);
}
}]);
function CEvent(topic) {
var _this2 = this;
_classCallCheck(this, CEvent);
this.topic = topic;
return {
listen: function listen(cb) {
_this2.listen(topic, cb);
},
dispatch: function dispatch(data) {
_this2.dispatch(topic, data);
}
};
}
//var event1 = GEvent.createEvent('test');
return CEvent;
})();
var event1 = new CEvent('test');
event1.listen(function (data) {
console.log("test first listener", data);
});
event1.listen(function (data) {
console.log("test second listener", data);
});
event1.dispatch({ data: 'first data' });
setTimeout(function () {
event1.dispatch({ data: 'timeout data' });
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment