@devcycle/js-client-sdk
Advanced tools
Comparing version
{ | ||
"name": "@devcycle/js-client-sdk", | ||
"version": "1.13.2", | ||
"version": "1.13.3", | ||
"description": "The Javascript Client SDK for DevCycle", | ||
@@ -20,6 +20,6 @@ "author": "", | ||
"uuid": "^8.3.2", | ||
"@devcycle/types": "1.1.9", | ||
"@devcycle/types": "1.1.10", | ||
"@nestjs/class-validator": "0.13.4", | ||
"class-transformer": "0.5.1", | ||
"iso-639-1": "2.1.15", | ||
"iso-639-1": "2.1.13", | ||
"reflect-metadata": "0.1.13" | ||
@@ -26,0 +26,0 @@ }, |
@@ -17,2 +17,5 @@ import { DevCycleClient } from './Client'; | ||
private flushInterval; | ||
private flushEventQueueSize; | ||
private maxEventQueueSize; | ||
private eventQueueBatchSize; | ||
constructor(sdkKey: string, dvcClient: DevCycleClient, options: DevCycleOptions); | ||
@@ -29,2 +32,3 @@ flushEvents(): Promise<void>; | ||
queueAggregateEvent(event: AggregateEvent): void; | ||
private checkEventQueueSize; | ||
/** | ||
@@ -31,0 +35,0 @@ * Turn the Aggregate Event Map into an Array of DVCAPIEvent objects for publishing. |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,2 +9,3 @@ exports.EventQueue = exports.EventTypes = void 0; | ||
const utils_1 = require("./utils"); | ||
const chunk_1 = __importDefault(require("lodash/chunk")); | ||
exports.EventTypes = { | ||
@@ -13,2 +17,4 @@ variableEvaluated: 'variableEvaluated', | ||
constructor(sdkKey, dvcClient, options) { | ||
var _a, _b; | ||
this.eventQueueBatchSize = 100; | ||
this.sdkKey = sdkKey; | ||
@@ -29,2 +35,16 @@ this.client = dvcClient; | ||
this.flushInterval = setInterval(this.flushEvents.bind(this), eventFlushIntervalMS); | ||
this.flushEventQueueSize = (_a = options === null || options === void 0 ? void 0 : options.flushEventQueueSize) !== null && _a !== void 0 ? _a : 100; | ||
this.maxEventQueueSize = (_b = options === null || options === void 0 ? void 0 : options.maxEventQueueSize) !== null && _b !== void 0 ? _b : 1000; | ||
if (this.flushEventQueueSize >= this.maxEventQueueSize) { | ||
throw new Error(`flushEventQueueSize: ${this.flushEventQueueSize} must be smaller than ` + | ||
`maxEventQueueSize: ${this.maxEventQueueSize}`); | ||
} | ||
else if (this.flushEventQueueSize < 10 || | ||
this.flushEventQueueSize > 1000) { | ||
throw new Error(`flushEventQueueSize: ${this.flushEventQueueSize} must be between 10 and 1000`); | ||
} | ||
else if (this.maxEventQueueSize < 100 || | ||
this.maxEventQueueSize > 5000) { | ||
throw new Error(`maxEventQueueSize: ${this.maxEventQueueSize} must be between 100 and 5000`); | ||
} | ||
} | ||
@@ -43,18 +63,28 @@ async flushEvents() { | ||
} | ||
this.client.logger.info(`DVC Flush ${eventsToFlush.length} Events`); | ||
this.client.logger.info(`Flush ${eventsToFlush.length} Events`); | ||
this.eventQueue = []; | ||
this.aggregateEventMap = {}; | ||
try { | ||
const res = await (0, Request_1.publishEvents)(this.sdkKey, this.client.config || null, user, eventsToFlush, this.client.logger); | ||
if (res.status !== 201) { | ||
this.eventQueue.push(...eventsToFlush); | ||
const eventRequests = (0, chunk_1.default)(eventsToFlush, this.eventQueueBatchSize); | ||
for (const eventRequest of eventRequests) { | ||
try { | ||
const res = await (0, Request_1.publishEvents)(this.sdkKey, this.client.config || null, user, eventRequest, this.client.logger); | ||
if (res.status === 201) { | ||
this.client.logger.info(`DevCycle Flushed ${eventRequest.length} Events.`); | ||
} | ||
else if (res.status >= 500 || res.status === 408) { | ||
this.client.logger.warn('failed to flush events, retrying events. ' + | ||
`Response status: ${res.status}, message: ${res.statusText}`); | ||
this.eventQueue.push(...eventRequest); | ||
} | ||
else { | ||
this.client.logger.error('failed to flush events, dropping events. ' + | ||
`Response status: ${res.status}, message: ${res.statusText}`); | ||
} | ||
} | ||
else { | ||
this.client.logger.info(`DVC Flushed ${eventsToFlush.length} Events.`); | ||
catch (ex) { | ||
this.client.eventEmitter.emitError(ex); | ||
this.client.logger.error('failed to flush events due to error, dropping events. ' + | ||
`Error message: ${ex === null || ex === void 0 ? void 0 : ex.message}`); | ||
} | ||
} | ||
catch (ex) { | ||
this.client.eventEmitter.emitError(ex); | ||
this.eventQueue.push(...eventsToFlush); | ||
} | ||
} | ||
@@ -65,2 +95,6 @@ /** | ||
queueEvent(event) { | ||
if (this.checkEventQueueSize()) { | ||
this.client.logger.warn(`DevCycle: Max event queue size (${this.maxEventQueueSize}) reached, dropping event: ${event}`); | ||
return; | ||
} | ||
this.eventQueue.push(event); | ||
@@ -73,2 +107,6 @@ } | ||
queueAggregateEvent(event) { | ||
if (this.checkEventQueueSize()) { | ||
this.client.logger.warn(`DevCycle: Max event queue size (${this.maxEventQueueSize}) reached, dropping event: ${event}`); | ||
return; | ||
} | ||
(0, utils_1.checkParamDefined)('type', event.type); | ||
@@ -89,2 +127,10 @@ (0, utils_1.checkParamDefined)('target', event.target); | ||
} | ||
checkEventQueueSize() { | ||
const aggCount = Object.values(this.aggregateEventMap).reduce((acc, v) => acc + Object.keys(v).length, 0); | ||
const queueSize = this.eventQueue.length + aggCount; | ||
if (queueSize >= this.flushEventQueueSize) { | ||
this.flushEvents(); | ||
} | ||
return queueSize >= this.maxEventQueueSize; | ||
} | ||
/** | ||
@@ -91,0 +137,0 @@ * Turn the Aggregate Event Map into an Array of DVCAPIEvent objects for publishing. |
@@ -80,2 +80,10 @@ import { DVCLogger, DVCDefaultLogLevel, VariableTypeAlias, VariableValue, DVCJSON, DVCCustomDataJSON } from '@devcycle/types'; | ||
disableCustomEventLogging?: boolean; | ||
/** | ||
* Controls the maximum size the event queue can grow to until a flush is forced. Defaults to `100`. | ||
*/ | ||
flushEventQueueSize?: number; | ||
/** | ||
* Controls the maximum size the event queue can grow to until events are dropped. Defaults to `1000`. | ||
*/ | ||
maxEventQueueSize?: number; | ||
} | ||
@@ -82,0 +90,0 @@ export interface DevCycleUser { |
@@ -17,2 +17,5 @@ import { DevCycleClient } from './Client'; | ||
private flushInterval; | ||
private flushEventQueueSize; | ||
private maxEventQueueSize; | ||
private eventQueueBatchSize; | ||
constructor(sdkKey: string, dvcClient: DevCycleClient, options: DevCycleOptions); | ||
@@ -29,2 +32,3 @@ flushEvents(): Promise<void>; | ||
queueAggregateEvent(event: AggregateEvent): void; | ||
private checkEventQueueSize; | ||
/** | ||
@@ -31,0 +35,0 @@ * Turn the Aggregate Event Map into an Array of DVCAPIEvent objects for publishing. |
@@ -80,2 +80,10 @@ import { DVCLogger, DVCDefaultLogLevel, VariableTypeAlias, VariableValue, DVCJSON, DVCCustomDataJSON } from '@devcycle/types'; | ||
disableCustomEventLogging?: boolean; | ||
/** | ||
* Controls the maximum size the event queue can grow to until a flush is forced. Defaults to `100`. | ||
*/ | ||
flushEventQueueSize?: number; | ||
/** | ||
* Controls the maximum size the event queue can grow to until events are dropped. Defaults to `1000`. | ||
*/ | ||
maxEventQueueSize?: number; | ||
} | ||
@@ -82,0 +90,0 @@ export interface DevCycleUser { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
235203
3.88%2781
2.58%+ Added
+ Added
- Removed
- Removed
Updated
Updated