@unrevealed/node
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -11,2 +11,3 @@ import { Logger } from './Logger'; | ||
logger?: Logger; | ||
defaults?: Record<string, boolean>; | ||
} | ||
@@ -24,8 +25,9 @@ interface User { | ||
private _featureAccesses; | ||
private _readyState; | ||
private _connectionPromise; | ||
private readonly _defaults; | ||
private readonly _apiKey; | ||
private readonly _apiUrl; | ||
private readonly _trackingUrl; | ||
private _logger; | ||
private _readyState; | ||
private _connectionPromise; | ||
private readonly _logger; | ||
constructor(options: UnrevealedClientOptions); | ||
@@ -48,2 +50,3 @@ get readyState(): ReadyState; | ||
_track(type: 'user' | 'team', body: unknown): Promise<void>; | ||
private get _featureKeys(); | ||
private _isReady; | ||
@@ -50,0 +53,0 @@ private _connectRecursive; |
@@ -31,12 +31,15 @@ "use strict"; | ||
constructor(options) { | ||
var _a; | ||
this._eventSource = null; | ||
this._featureAccesses = {}; | ||
this._featureAccesses = new Map(); | ||
this._readyState = ReadyState.UNINITIALIZED; | ||
this._connectionPromise = null; | ||
const _options = options; | ||
this._apiKey = _options.apiKey; | ||
this._apiUrl = _options.apiUrl || SSE_API_URL; | ||
this._trackingUrl = _options.trackingUrl || TRACKING_API_URL; | ||
this._logger = (_a = options.logger) !== null && _a !== void 0 ? _a : new Logger_1.UnrevealedLogger(); | ||
const { apiKey, apiUrl, trackingUrl, logger, defaults } = options; | ||
this._apiKey = apiKey; | ||
this._apiUrl = apiUrl || SSE_API_URL; | ||
this._trackingUrl = trackingUrl || TRACKING_API_URL; | ||
this._logger = logger !== null && logger !== void 0 ? logger : new Logger_1.UnrevealedLogger(); | ||
const defaultsEntries = defaults | ||
? Object.keys(defaults).map((featureKey) => [featureKey, defaults[featureKey]]) | ||
: []; | ||
this._defaults = new Map(defaultsEntries); | ||
} | ||
@@ -48,3 +51,2 @@ get readyState() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.log('connect'); | ||
if (this._isReady()) { | ||
@@ -65,3 +67,3 @@ return true; | ||
this._readyState = ReadyState.CLOSED; | ||
this._featureAccesses = {}; | ||
this._featureAccesses = new Map(); | ||
} | ||
@@ -71,3 +73,2 @@ isFeatureEnabled(featureKey, { user, team } = {}) { | ||
yield this._connectionPromise; | ||
this.identify({ user, team }).catch(() => { }); | ||
return this._isFeatureEnabledSync(featureKey, { user, team }); | ||
@@ -79,5 +80,3 @@ }); | ||
yield this._connectionPromise; | ||
this.identify({ user, team }).catch(() => { }); | ||
const featureKeys = Object.keys(this._featureAccesses); | ||
return featureKeys.filter((featureKey) => this._isFeatureEnabledSync(featureKey, { user, team })); | ||
return this._featureKeys.filter((featureKey) => this._isFeatureEnabledSync(featureKey, { user, team })); | ||
}); | ||
@@ -111,2 +110,7 @@ } | ||
} | ||
get _featureKeys() { | ||
return [ | ||
...new Set([...this._featureAccesses.keys(), ...this._defaults.keys()]), | ||
]; | ||
} | ||
_isReady() { | ||
@@ -145,4 +149,3 @@ return this._readyState === ReadyState.READY; | ||
const eventSource = this._createEventSource(); | ||
eventSource.addEventListener('error', (event, ...args) => { | ||
console.log('error', event, args); | ||
eventSource.addEventListener('error', (event) => { | ||
if (this._readyState === ReadyState.CONNECTING) { | ||
@@ -163,3 +166,2 @@ if ('status' in event && event.status === 401) { | ||
eventSource.addEventListener('put', (event) => __awaiter(this, void 0, void 0, function* () { | ||
console.log('put', event); | ||
try { | ||
@@ -174,3 +176,2 @@ this._handlePut(event); | ||
eventSource.addEventListener('patch', (event) => { | ||
console.log('patch', event); | ||
this._handlePatch(event); | ||
@@ -187,5 +188,6 @@ }); | ||
_isFeatureEnabledSync(featureKey, { user, team } = {}) { | ||
const featureAccess = this._featureAccesses[featureKey]; | ||
var _a; | ||
const featureAccess = this._featureAccesses.get(featureKey); | ||
if (!featureAccess) { | ||
return false; | ||
return (_a = this._defaults.get(featureKey)) !== null && _a !== void 0 ? _a : false; | ||
} | ||
@@ -226,3 +228,8 @@ if (featureAccess.fullAccess) { | ||
try { | ||
this._featureAccesses = JSON.parse(event.data); | ||
const featureAccessesData = JSON.parse(event.data); | ||
const entries = Object.keys(featureAccessesData).map((featureKey) => [ | ||
featureKey, | ||
featureAccessesData[featureKey], | ||
]); | ||
this._featureAccesses = new Map(entries); | ||
} | ||
@@ -238,11 +245,14 @@ catch (err) { | ||
try { | ||
const featureAccesses = JSON.parse(event.data); | ||
Object.keys(featureAccesses).map((featureId) => { | ||
if (featureAccesses[featureId] === null) { | ||
const newFeatureAccessesData = JSON.parse(event.data); | ||
const newFeatureKeys = Object.keys(newFeatureAccessesData); | ||
newFeatureKeys.forEach((featureId) => { | ||
const featureAccess = newFeatureAccessesData[featureId]; | ||
if (featureAccess === null) { | ||
if (featureId in this._featureAccesses) { | ||
delete this._featureAccesses[featureId]; | ||
this._featureAccesses.delete(featureId); | ||
} | ||
return; | ||
} | ||
this._featureAccesses[featureId] = featureAccesses[featureId]; | ||
else { | ||
this._featureAccesses.set(featureId, featureAccess); | ||
} | ||
}); | ||
@@ -249,0 +259,0 @@ } |
{ | ||
"name": "@unrevealed/node", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Unrevealed SDK for Node", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
16438
347