Skip to content

Commit

Permalink
dist and lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Jan 14, 2024
1 parent d2b2809 commit aa306aa
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 216 deletions.
178 changes: 71 additions & 107 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions lib/event-engine/core/retryPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ var RetryPolicy = /** @class */ (function () {
maximumRetry: configuration.maximumRetry,
shouldRetry: function (error, attempt) {
var _a;
if (RetryPolicy.excludedErrorCodes.includes((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode)) {
if (((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode) === 403) {
return false;
}
return this.maximumRetry > attempt;
},
getDelay: function (_) {
getDelay: function (_, reason) {
if (reason === null || reason === void 0 ? void 0 : reason.retryAfter) {
return reason.retryAfter * 1000;
}
return (this.delay + Math.random()) * 1000;
},
getGiveupReason: function (error, attempt) {
var _a;
if (this.maximumRetry <= attempt) {
return 'retry attempts exhaused.';
}
if (RetryPolicy.excludedErrorCodes.includes((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode)) {
return 'forbidden or too many requests.';
if (((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode) === 403) {
return 'forbidden operation.';
}
return 'unknown error';
},
Expand All @@ -42,7 +45,10 @@ var RetryPolicy = /** @class */ (function () {
}
return this.maximumRetry > attempt;
},
getDelay: function (attempt) {
getDelay: function (attempt, reason) {
if (reason === null || reason === void 0 ? void 0 : reason.retryAfter) {
return reason.retryAfter * 1000;
}
var calculatedDelay = (Math.pow(2, attempt) + Math.random()) * 1000;
if (calculatedDelay > this.maximumDelay) {
return this.maximumDelay;
Expand All @@ -56,14 +62,13 @@ var RetryPolicy = /** @class */ (function () {
if (this.maximumRetry <= attempt) {
return 'retry attempts exhaused.';
}
if (RetryPolicy.excludedErrorCodes.includes((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode)) {
return 'forbidden or too many requests.';
if (((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode) === 403) {
return 'forbidden operation.';
}
return 'unknown error';
},
};
};
RetryPolicy.excludedErrorCodes = [403, 429];
return RetryPolicy;
}());
exports.RetryPolicy = RetryPolicy;
39 changes: 17 additions & 22 deletions lib/event-engine/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
Expand Down Expand Up @@ -86,23 +97,15 @@ var EventEngineDispatcher = /** @class */ (function (_super) {
_this.on(effects.handshake.type, (0, core_1.asyncHandler)(function (payload, abortSignal, _a) {
var handshake = _a.handshake, presenceState = _a.presenceState, config = _a.config;
return __awaiter(_this, void 0, void 0, function () {
var handshakeParams, result, e_1;
var result, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
abortSignal.throwIfAborted();
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
handshakeParams = {
abortSignal: abortSignal,
channels: payload.channels,
channelGroups: payload.groups,
filterExpression: config.filterExpression,
};
if (config.maintainPresenceState)
handshakeParams.state = presenceState;
return [4 /*yield*/, handshake(handshakeParams)];
return [4 /*yield*/, handshake(__assign({ abortSignal: abortSignal, channels: payload.channels, channelGroups: payload.groups, filterExpression: config.filterExpression }, (config.maintainPresenceState && { state: presenceState })))];
case 2:
result = _b.sent();
return [2 /*return*/, engine.transition(events.handshakeSuccess(result))];
Expand Down Expand Up @@ -186,7 +189,7 @@ var EventEngineDispatcher = /** @class */ (function (_super) {
case 0:
if (!(config.retryConfiguration && config.retryConfiguration.shouldRetry(payload.reason, payload.attempts))) return [3 /*break*/, 6];
abortSignal.throwIfAborted();
return [4 /*yield*/, delay(config.retryConfiguration.getDelay(payload.attempts))];
return [4 /*yield*/, delay(config.retryConfiguration.getDelay(payload.attempts, payload.reason))];
case 1:
_b.sent();
abortSignal.throwIfAborted();
Expand Down Expand Up @@ -223,28 +226,20 @@ var EventEngineDispatcher = /** @class */ (function (_super) {
_this.on(effects.handshakeReconnect.type, (0, core_1.asyncHandler)(function (payload, abortSignal, _a) {
var handshake = _a.handshake, delay = _a.delay, presenceState = _a.presenceState, config = _a.config;
return __awaiter(_this, void 0, void 0, function () {
var handshakeParams, result, error_3;
var result, error_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(config.retryConfiguration && config.retryConfiguration.shouldRetry(payload.reason, payload.attempts))) return [3 /*break*/, 6];
abortSignal.throwIfAborted();
return [4 /*yield*/, delay(config.retryConfiguration.getDelay(payload.attempts))];
return [4 /*yield*/, delay(config.retryConfiguration.getDelay(payload.attempts, payload.reason))];
case 1:
_b.sent();
abortSignal.throwIfAborted();
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
handshakeParams = {
abortSignal: abortSignal,
channels: payload.channels,
channelGroups: payload.groups,
filterExpression: config.filterExpression,
};
if (config.maintainPresenceState)
handshakeParams.state = presenceState;
return [4 /*yield*/, handshake(handshakeParams)];
return [4 /*yield*/, handshake(__assign({ abortSignal: abortSignal, channels: payload.channels, channelGroups: payload.groups, filterExpression: config.filterExpression }, (config.maintainPresenceState && { state: presenceState })))];
case 3:
result = _b.sent();
return [2 /*return*/, engine.transition(events.handshakeReconnectSuccess(result))];
Expand Down
12 changes: 8 additions & 4 deletions lib/event-engine/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ exports.subscriptionChange = (0, core_1.createEvent)('SUBSCRIPTION_CHANGED', fun
exports.restore = (0, core_1.createEvent)('SUBSCRIPTION_RESTORED', function (channels, groups, timetoken, region) { return ({
channels: channels,
groups: groups,
timetoken: timetoken,
region: region,
cursor: {
timetoken: timetoken,
region: region !== null && region !== void 0 ? region : 0,
},
}); });
exports.handshakeSuccess = (0, core_1.createEvent)('HANDSHAKE_SUCCESS', function (cursor) { return cursor; });
exports.handshakeFailure = (0, core_1.createEvent)('HANDSHAKE_FAILURE', function (error) { return error; });
Expand All @@ -32,7 +34,9 @@ exports.receiveReconnectFailure = (0, core_1.createEvent)('RECEIVE_RECONNECT_FAI
exports.receiveReconnectGiveup = (0, core_1.createEvent)('RECEIVING_RECONNECT_GIVEUP', function (error) { return error; });
exports.disconnect = (0, core_1.createEvent)('DISCONNECT', function () { return ({}); });
exports.reconnect = (0, core_1.createEvent)('RECONNECT', function (timetoken, region) { return ({
timetoken: timetoken,
region: region,
cursor: {
timetoken: timetoken !== null && timetoken !== void 0 ? timetoken : '',
region: region !== null && region !== void 0 ? region : 0,
},
}); });
exports.unsubscribeAll = (0, core_1.createEvent)('UNSUBSCRIBE_ALL', function () { return ({}); });
22 changes: 5 additions & 17 deletions lib/event-engine/presence/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,12 @@ var PresenceEventEngineDispatcher = /** @class */ (function (_super) {
_this.on(effects.heartbeat.type, (0, core_1.asyncHandler)(function (payload, _, _a) {
var heartbeat = _a.heartbeat, presenceState = _a.presenceState, config = _a.config;
return __awaiter(_this, void 0, void 0, function () {
var heartbeatParams, result, e_1;
var result, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
heartbeatParams = {
channels: payload.channels,
channelGroups: payload.groups,
};
if (config.maintainPresenceState)
heartbeatParams.state = presenceState;
return [4 /*yield*/, heartbeat(heartbeatParams)];
return [4 /*yield*/, heartbeat(__assign({ channels: payload.channels, channelGroups: payload.groups }, (config.maintainPresenceState && { state: presenceState })))];
case 1:
result = _b.sent();
engine.transition(events.heartbeatSuccess(200));
Expand Down Expand Up @@ -173,26 +167,20 @@ var PresenceEventEngineDispatcher = /** @class */ (function (_super) {
_this.on(effects.delayedHeartbeat.type, (0, core_1.asyncHandler)(function (payload, abortSignal, _a) {
var heartbeat = _a.heartbeat, retryDelay = _a.retryDelay, presenceState = _a.presenceState, config = _a.config;
return __awaiter(_this, void 0, void 0, function () {
var heartbeatParams, result, e_3;
var result, e_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(config.retryConfiguration && config.retryConfiguration.shouldRetry(payload.reason, payload.attempts))) return [3 /*break*/, 6];
abortSignal.throwIfAborted();
return [4 /*yield*/, retryDelay(config.retryConfiguration.getDelay(payload.attempts))];
return [4 /*yield*/, retryDelay(config.retryConfiguration.getDelay(payload.attempts, payload.reason))];
case 1:
_b.sent();
abortSignal.throwIfAborted();
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
heartbeatParams = {
channels: payload.channels,
channelGroups: payload.groups,
};
if (config.maintainPresenceState)
heartbeatParams.state = presenceState;
return [4 /*yield*/, heartbeat(heartbeatParams)];
return [4 /*yield*/, heartbeat(__assign({ channels: payload.channels, channelGroups: payload.groups }, (config.maintainPresenceState && { state: presenceState })))];
case 3:
result = _b.sent();
return [2 /*return*/, engine.transition(events.heartbeatSuccess(200))];
Expand Down
12 changes: 4 additions & 8 deletions lib/event-engine/states/handshake_failed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ exports.HandshakeFailedState.on(events_1.subscriptionChange.type, function (_, e
return handshaking_1.HandshakingState.with({ channels: event.payload.channels, groups: event.payload.groups });
});
exports.HandshakeFailedState.on(events_1.reconnect.type, function (context, event) {
var _a, _b, _c, _d, _e;
return handshaking_1.HandshakingState.with({
channels: context.channels,
groups: context.groups,
cursor: {
timetoken: (_c = (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.timetoken) !== null && _b !== void 0 ? _b : context.timetoken) !== null && _c !== void 0 ? _c : '0',
region: (_e = (_d = event.payload) === null || _d === void 0 ? void 0 : _d.region) !== null && _e !== void 0 ? _e : 0,
},
cursor: context.cursor,
});
});
exports.HandshakeFailedState.on(events_1.restore.type, function (_, event) {
exports.HandshakeFailedState.on(events_1.restore.type, function (context, event) {
var _a, _b;
return handshaking_1.HandshakingState.with({
channels: event.payload.channels,
groups: event.payload.groups,
cursor: {
timetoken: event.payload.timetoken,
region: (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
timetoken: event.payload.cursor.timetoken,
region: event.payload.cursor.region ? event.payload.cursor.region : (_b = (_a = context === null || context === void 0 ? void 0 : context.cursor) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
},
});
});
Expand Down
18 changes: 7 additions & 11 deletions lib/event-engine/states/handshake_reconnecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ exports.HandshakeReconnectingState = new state_1.State('HANDSHAKE_RECONNECTING')
exports.HandshakeReconnectingState.onEnter(function (context) { return (0, effects_1.handshakeReconnect)(context); });
exports.HandshakeReconnectingState.onExit(function () { return effects_1.handshakeReconnect.cancel; });
exports.HandshakeReconnectingState.on(events_1.handshakeReconnectSuccess.type, function (context, event) {
var _a;
var _a, _b;
var cursor = {
timetoken: (_a = context === null || context === void 0 ? void 0 : context.timetoken) !== null && _a !== void 0 ? _a : event.payload.cursor.timetoken,
timetoken: !!((_a = context.cursor) === null || _a === void 0 ? void 0 : _a.timetoken) ? (_b = context.cursor) === null || _b === void 0 ? void 0 : _b.timetoken : event.payload.cursor.timetoken,
region: event.payload.cursor.region,
};
return receiving_1.ReceivingState.with({
Expand All @@ -47,32 +47,28 @@ exports.HandshakeReconnectingState.on(events_1.handshakeReconnectGiveup.type, fu
return handshake_failed_1.HandshakeFailedState.with({
groups: context.groups,
channels: context.channels,
timetoken: context.timetoken,
cursor: context.cursor,
reason: event.payload,
}, [(0, effects_1.emitStatus)({ category: categories_1.default.PNConnectionErrorCategory, error: (_a = event.payload) === null || _a === void 0 ? void 0 : _a.message })]);
});
exports.HandshakeReconnectingState.on(events_1.disconnect.type, function (context) {
var _a;
return handshake_stopped_1.HandshakeStoppedState.with({
channels: context.channels,
groups: context.groups,
cursor: {
timetoken: (_a = context.timetoken) !== null && _a !== void 0 ? _a : '0',
region: 0,
},
cursor: context.cursor,
});
});
exports.HandshakeReconnectingState.on(events_1.subscriptionChange.type, function (_, event) {
return handshaking_1.HandshakingState.with({ channels: event.payload.channels, groups: event.payload.groups });
});
exports.HandshakeReconnectingState.on(events_1.restore.type, function (_, event) {
exports.HandshakeReconnectingState.on(events_1.restore.type, function (context, event) {
var _a, _b;
return handshaking_1.HandshakingState.with({
channels: event.payload.channels,
groups: event.payload.groups,
cursor: {
timetoken: event.payload.timetoken,
region: (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
timetoken: event.payload.cursor.timetoken,
region: event.payload.cursor.region ? event.payload.cursor.region : (_b = (_a = context === null || context === void 0 ? void 0 : context.cursor) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
},
});
});
Expand Down
12 changes: 4 additions & 8 deletions lib/event-engine/states/handshake_stopped.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ exports.HandshakeStoppedState.on(events_1.subscriptionChange.type, function (con
});
});
exports.HandshakeStoppedState.on(events_1.reconnect.type, function (context, event) {
var _a, _b, _c, _d, _e, _f;
return handshaking_1.HandshakingState.with(__assign(__assign({}, context), { cursor: {
timetoken: (_d = (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.timetoken) !== null && _b !== void 0 ? _b : (_c = context.cursor) === null || _c === void 0 ? void 0 : _c.timetoken) !== null && _d !== void 0 ? _d : '0',
region: (_f = (_e = event.payload) === null || _e === void 0 ? void 0 : _e.region) !== null && _f !== void 0 ? _f : 0,
} }));
return handshaking_1.HandshakingState.with(__assign(__assign({}, context), { cursor: event.payload.cursor }));
});
exports.HandshakeStoppedState.on(events_1.restore.type, function (_, event) {
exports.HandshakeStoppedState.on(events_1.restore.type, function (context, event) {
var _a, _b;
return exports.HandshakeStoppedState.with({
channels: event.payload.channels,
groups: event.payload.groups,
cursor: {
timetoken: event.payload.timetoken,
region: (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
timetoken: event.payload.cursor.timetoken,
region: event.payload.cursor.region ? event.payload.cursor.region : (_b = (_a = context === null || context === void 0 ? void 0 : context.cursor) === null || _a === void 0 ? void 0 : _a.region) !== null && _b !== void 0 ? _b : 0,
},
});
});
Expand Down
Loading

0 comments on commit aa306aa

Please sign in to comment.