hjs-message
Version:
Messaging API
317 lines (298 loc) • 10.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Message = exports.FLAGS_TO_CLEAR_ON_COPY_FROM = exports.FLAG_ASYNCHRONOUS = exports.FLAG_IN_USE = undefined;
var _stringify = require("babel-runtime/core-js/json/stringify");
var _stringify2 = _interopRequireDefault(_stringify);
var _map = require("babel-runtime/core-js/map");
var _map2 = _interopRequireDefault(_map);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @babel */
var FLAG_IN_USE = exports.FLAG_IN_USE = 1 << 0;
var FLAG_ASYNCHRONOUS = exports.FLAG_ASYNCHRONOUS = 1 << 1;
var FLAGS_TO_CLEAR_ON_COPY_FROM = exports.FLAGS_TO_CLEAR_ON_COPY_FROM = FLAG_IN_USE;
var MAX_POOL_SIZE = 50;
var sPoolSize = 0;
var sPool = null;
var Message = exports.Message = function () {
function Message() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$when = _ref.when,
when = _ref$when === undefined ? 0 : _ref$when,
_ref$state = _ref.state,
state = _ref$state === undefined ? 0 : _ref$state,
_ref$what = _ref.what,
what = _ref$what === undefined ? 0 : _ref$what,
_ref$arg = _ref.arg1,
arg1 = _ref$arg === undefined ? 0 : _ref$arg,
_ref$arg2 = _ref.arg2,
arg2 = _ref$arg2 === undefined ? 0 : _ref$arg2,
_ref$data = _ref.data,
data = _ref$data === undefined ? new _map2.default() : _ref$data,
_ref$obj = _ref.obj,
obj = _ref$obj === undefined ? null : _ref$obj,
_ref$replyTo = _ref.replyTo,
replyTo = _ref$replyTo === undefined ? null : _ref$replyTo,
_ref$target = _ref.target,
target = _ref$target === undefined ? null : _ref$target,
_ref$callback = _ref.callback,
callback = _ref$callback === undefined ? null : _ref$callback;
(0, _classCallCheck3.default)(this, Message);
this.next = null;
this.update({ when: when, state: state, what: what, arg1: arg1, arg2: arg2, data: data, obj: obj, replyTo: replyTo, target: target, callback: callback });
}
(0, _createClass3.default)(Message, [{
key: "clearForRecycle",
value: function clearForRecycle() {
this.state = 0;
this.when = 0;
this.what = 0;
this.arg1 = 0;
this.arg2 = 0;
this.callback = null;
this.replyTo = null;
this.target = null;
this.data = null;
this.obj = null;
}
}, {
key: "copyFrom",
value: function copyFrom(message) {
if (message instanceof Message) {
this.update({
state: message.state & ~FLAGS_TO_CLEAR_ON_COPY_FROM,
what: message.what,
arg1: message.arg1,
arg2: message.arg2,
obj: message.obj,
data: message.data,
replyTo: message.replyTo
});
}
}
}, {
key: "deserialize",
value: function deserialize(message) {
if (typeof message === 'string') {
this.update(JSON.parse(message));
}
}
}, {
key: "getCallback",
value: function getCallback() {
return this.callback;
}
}, {
key: "getData",
value: function getData() {
return this.data;
}
}, {
key: "getObj",
value: function getObj() {
return this.obj;
}
}, {
key: "getTarget",
value: function getTarget() {
return this.target;
}
}, {
key: "getWhat",
value: function getWhat() {
return this.what;
}
}, {
key: "getWhen",
value: function getWhen() {
return this.when;
}
}, {
key: "isAsynchronous",
value: function isAsynchronous() {
return (this.state & FLAG_ASYNCHRONOUS) !== 0;
}
}, {
key: "isInUse",
value: function isInUse() {
return (this.state & FLAG_IN_USE) === FLAG_IN_USE;
}
}, {
key: "markInUse",
value: function markInUse() {
this.state |= FLAG_IN_USE;
}
}, {
key: "peekData",
value: function peekData() {
return this.data;
}
}, {
key: "recycle",
value: function recycle() {
this.clearForRecycle();
if (sPoolSize < MAX_POOL_SIZE) {
this.next = sPool;
sPool = this;
sPoolSize++;
}
}
}, {
key: "sendToTarget",
value: function sendToTarget() {
if (this.target) {
this.target.sendMessage(this);
}
}
}, {
key: "serialize",
value: function serialize() {
return (0, _stringify2.default)({
state: this.state,
what: this.what,
arg1: this.arg1,
arg2: this.arg2,
obj: this.obj,
data: this.data,
when: this.when
});
}
}, {
key: "setAsynchronous",
value: function setAsynchronous(asynchronous) {
if (asynchronous) {
this.state |= FLAG_ASYNCHRONOUS;
} else {
this.state &= ~FLAG_ASYNCHRONOUS;
}
}
}, {
key: "setCallback",
value: function setCallback(callback) {
this.callback = callback;
}
}, {
key: "setData",
value: function setData(data) {
this.data = data;
}
}, {
key: "setTarget",
value: function setTarget(target) {
this.target = target;
}
}, {
key: "toString",
value: function toString() {
var now = Date.now();
var duration = this.when - now;
var str = "Message{\n";
str += "state:" + this.state + ",\n";
str += "when:" + duration + ",\n";
if (this.target !== null) {
if (this.callback !== null) {
str += "callback:" + this.callback.constructor.name + ",\n";
} else {
str += "what:" + this.what + ",\n";
}
if (this.arg1 !== 0) {
str += "arg1:" + this.arg1 + ",\n";
}
if (this.arg2 !== 0) {
str += "arg2:" + this.arg2 + ",\n";
}
if (this.obj !== null) {
str += "obj:" + this.obj + ",\n";
}
str += "target:" + this.target.constructor.name + ",\n";
if (this.replyTo !== null) {
str += "replyTo:" + this.replyTo.constructor.name + ",\n";
}
} else {
str += "barrier:" + this.arg1 + ",\n";
}
if (this.data !== null) {
str += "data:" + this.data.toString();
}
str += "}";
return str;
}
}, {
key: "update",
value: function update() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$when = _ref2.when,
when = _ref2$when === undefined ? 0 : _ref2$when,
_ref2$state = _ref2.state,
state = _ref2$state === undefined ? 0 : _ref2$state,
_ref2$what = _ref2.what,
what = _ref2$what === undefined ? 0 : _ref2$what,
_ref2$arg = _ref2.arg1,
arg1 = _ref2$arg === undefined ? 0 : _ref2$arg,
_ref2$arg2 = _ref2.arg2,
arg2 = _ref2$arg2 === undefined ? 0 : _ref2$arg2,
_ref2$data = _ref2.data,
data = _ref2$data === undefined ? null : _ref2$data,
_ref2$obj = _ref2.obj,
obj = _ref2$obj === undefined ? null : _ref2$obj,
_ref2$replyTo = _ref2.replyTo,
replyTo = _ref2$replyTo === undefined ? null : _ref2$replyTo,
_ref2$target = _ref2.target,
target = _ref2$target === undefined ? null : _ref2$target,
_ref2$callback = _ref2.callback,
callback = _ref2$callback === undefined ? null : _ref2$callback;
this.when = when;
this.state = state;
this.what = what;
this.arg1 = arg1;
this.arg2 = arg2;
this.obj = obj;
this.replyTo = replyTo;
this.target = target;
this.callback = callback;
this.data = data;
}
}], [{
key: "obtain",
value: function obtain() {
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var m = null;
if (!message) {
if (sPool !== null) {
m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
return new Message();
} else {
var orig = message;
m = Message.obtain();
if (orig instanceof Message) {
m.what = orig.what;
m.arg1 = orig.arg1;
m.arg2 = orig.arg2;
m.obj = orig.obj;
m.replyTo = orig.replyTo;
if (orig.data !== null) {
m.data = new _map2.default(orig.data);
}
m.target = orig.target;
m.callback = orig.callback;
return m;
} else {
m.update(orig);
}
return m;
}
}
}]);
return Message;
}();