pubnub
Version:
Publish & Subscribe Real-time Messaging with PubNub
76 lines (61 loc) • 2.17 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _config = _interopRequireDefault(require("../components/config"));
var _flow_interfaces = require("../flow_interfaces");
var hashCode = function hashCode(payload) {
var hash = 0;
if (payload.length === 0) return hash;
for (var i = 0; i < payload.length; i += 1) {
var character = payload.charCodeAt(i);
hash = (hash << 5) - hash + character;
hash = hash & hash;
}
return hash;
};
var _default = function () {
function _default(_ref) {
var config = _ref.config;
(0, _classCallCheck2["default"])(this, _default);
(0, _defineProperty2["default"])(this, "_config", void 0);
(0, _defineProperty2["default"])(this, "hashHistory", void 0);
this.hashHistory = [];
this._config = config;
}
(0, _createClass2["default"])(_default, [{
key: "getKey",
value: function getKey(message) {
var hashedPayload = hashCode(JSON.stringify(message.payload)).toString();
var timetoken = message.publishMetaData.publishTimetoken;
return "".concat(timetoken, "-").concat(hashedPayload);
}
}, {
key: "isDuplicate",
value: function isDuplicate(message) {
return this.hashHistory.includes(this.getKey(message));
}
}, {
key: "addEntry",
value: function addEntry(message) {
if (this.hashHistory.length >= this._config.maximumCacheSize) {
this.hashHistory.shift();
}
this.hashHistory.push(this.getKey(message));
}
}, {
key: "clearHistory",
value: function clearHistory() {
this.hashHistory = [];
}
}]);
return _default;
}();
exports["default"] = _default;
module.exports = exports.default;
//# sourceMappingURL=deduping_manager.js.map