mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-18 00:19:49 +01:00
fix: now hydration event is being returned if revision does not exist in cache (#9203)
Now when customer is coming with revision ID that does not exist in cache, we return hydration.
This commit is contained in:
parent
96dac84880
commit
d8a47ce39d
@ -270,3 +270,54 @@ test('should get segment updated and removed events', async () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should return hydration if revision not in cache', async () => {
|
||||||
|
await app.createFeature('base_feature');
|
||||||
|
await syncRevisions();
|
||||||
|
const { body, headers } = await app.request
|
||||||
|
.get('/api/client/delta')
|
||||||
|
.expect(200);
|
||||||
|
const etag = headers.etag;
|
||||||
|
|
||||||
|
expect(body).toMatchObject({
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
type: DELTA_EVENT_TYPES.HYDRATION,
|
||||||
|
features: [
|
||||||
|
{
|
||||||
|
name: 'base_feature',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await app.createFeature('not_important1');
|
||||||
|
await syncRevisions();
|
||||||
|
|
||||||
|
const { body: deltaBody } = await app.request
|
||||||
|
.get('/api/client/delta')
|
||||||
|
.set('If-None-Match', etag)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(deltaBody).toMatchObject({
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
type: DELTA_EVENT_TYPES.FEATURE_UPDATED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const { body: rehydrationBody } = await app.request
|
||||||
|
.get('/api/client/delta')
|
||||||
|
.set('If-None-Match', '1')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(rehydrationBody).toMatchObject({
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
type: DELTA_EVENT_TYPES.HYDRATION,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -154,8 +154,12 @@ export class ClientFeatureToggleDelta {
|
|||||||
if (requiredRevisionId >= this.currentRevisionId) {
|
if (requiredRevisionId >= this.currentRevisionId) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
if (requiredRevisionId === 0) {
|
const delta = this.delta[environment];
|
||||||
const hydrationEvent = this.delta[environment].getHydrationEvent();
|
if (
|
||||||
|
requiredRevisionId === 0 ||
|
||||||
|
delta.isMissingRevision(requiredRevisionId)
|
||||||
|
) {
|
||||||
|
const hydrationEvent = delta.getHydrationEvent();
|
||||||
const filteredEvent = filterHydrationEventByQuery(
|
const filteredEvent = filterHydrationEventByQuery(
|
||||||
hydrationEvent,
|
hydrationEvent,
|
||||||
projects,
|
projects,
|
||||||
@ -168,7 +172,7 @@ export class ClientFeatureToggleDelta {
|
|||||||
|
|
||||||
return Promise.resolve(response);
|
return Promise.resolve(response);
|
||||||
} else {
|
} else {
|
||||||
const environmentEvents = this.delta[environment].getEvents();
|
const environmentEvents = delta.getEvents();
|
||||||
const events = filterEventsByQuery(
|
const events = filterEventsByQuery(
|
||||||
environmentEvents,
|
environmentEvents,
|
||||||
requiredRevisionId,
|
requiredRevisionId,
|
||||||
@ -299,8 +303,7 @@ export class ClientFeatureToggleDelta {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initEnvironmentDelta(environment: string) {
|
private async initEnvironmentDelta(environment: string) {
|
||||||
// Todo: replace with method that gets all features for an environment
|
|
||||||
const baseFeatures = await this.getClientFeatures({
|
const baseFeatures = await this.getClientFeatures({
|
||||||
environment,
|
environment,
|
||||||
});
|
});
|
||||||
|
@ -12,6 +12,22 @@ export class DeltaCache {
|
|||||||
constructor(hydrationEvent: DeltaHydrationEvent, maxLength: number = 20) {
|
constructor(hydrationEvent: DeltaHydrationEvent, maxLength: number = 20) {
|
||||||
this.hydrationEvent = hydrationEvent;
|
this.hydrationEvent = hydrationEvent;
|
||||||
this.maxLength = maxLength;
|
this.maxLength = maxLength;
|
||||||
|
|
||||||
|
this.addBaseEventFromHydration(hydrationEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private addBaseEventFromHydration(
|
||||||
|
hydrationEvent: DeltaHydrationEvent,
|
||||||
|
): void {
|
||||||
|
const lastFeature =
|
||||||
|
hydrationEvent.features[hydrationEvent.features.length - 1];
|
||||||
|
this.addEvents([
|
||||||
|
{
|
||||||
|
eventId: hydrationEvent.eventId,
|
||||||
|
type: 'feature-updated',
|
||||||
|
feature: lastFeature,
|
||||||
|
},
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addEvents(events: DeltaEvent[]): void {
|
public addEvents(events: DeltaEvent[]): void {
|
||||||
@ -27,6 +43,10 @@ export class DeltaCache {
|
|||||||
return this.events;
|
return this.events;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isMissingRevision(revisionId: number): boolean {
|
||||||
|
return !this.events.some((event) => event.eventId === revisionId);
|
||||||
|
}
|
||||||
|
|
||||||
public getHydrationEvent(): DeltaHydrationEvent {
|
public getHydrationEvent(): DeltaHydrationEvent {
|
||||||
return this.hydrationEvent;
|
return this.hydrationEvent;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user