Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/server/src/client/internal/open-feature-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,16 @@ export class OpenFeatureClient implements Client {
const hookContextIndex = hooks.length - 1 - index; // reverse index for before hooks
const hookContext = hookContexts[hookContextIndex];

// Update the context on the stable hook context object
Object.assign(hookContext.context, accumulatedContext);

const hookResult = await hook?.before?.(hookContext, Object.freeze(options.hookHints));
if (hookResult) {
accumulatedContext = {
...accumulatedContext,
...hookResult,
};

for (let i = 0; i < hooks.length; i++) {
Object.assign(hookContexts[hookContextIndex].context, accumulatedContext);
}
}

// Update the context on the stable hook context object
Object.assign(hookContext.context, accumulatedContext);
}

// after before hooks, freeze the EvaluationContext.
Expand Down
39 changes: 39 additions & 0 deletions packages/server/test/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,45 @@ describe('Hooks', () => {
expect.anything(),
);
});
it('Should share the same context object reference across all hooks', (done) => {
let hook1Context: EvaluationContext;
let hook2Context: EvaluationContext;
let hook3Context: EvaluationContext;

client.getBooleanValue(FLAG_KEY, false, undefined, {
hooks: [
{
before: (hookContext) => {
hook1Context = hookContext.context;
return { fromHook1: 'value1' };
},
},
{
before: (hookContext) => {
hook2Context = hookContext.context;
return { fromHook2: 'value2' };
},
},
{
before: (hookContext) => {
hook3Context = hookContext.context;
return { fromHook3: 'value3' };
},

after: (hookContext) => {
try {
expect(hookContext.context).toBe(hook1Context);
expect(hookContext.context).toBe(hook2Context);
expect(hookContext.context).toBe(hook3Context);
done();
} catch (err) {
done(err);
}
},
},
],
});
});
});

describe('Requirement 4.3.5', () => {
Expand Down