Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a mechanism to retrieve all context (with defaults) #1751

Merged
merged 3 commits into from
Oct 7, 2023
Merged
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
15 changes: 15 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Node {
/**
* The id of this construct within the current scope.
*
* This is a a scope-unique id. To obtain an app-unique id for this construct, use `addr`.
* This is a scope-unique id. To obtain an app-unique id for this construct, use `addr`.
*/
public readonly id: string;

Expand Down Expand Up @@ -236,6 +236,25 @@ export class Node {
return this.scope && this.scope.node.getContext(key);
}

/**
* Retrieves the all context of a node from tree context.
*
* Context is usually initialized at the root, but can be overridden at any point in the tree.
*
* @param defaults Any keys to override the retrieved context
* @returns The context object or an empty object if there is discovered context
*/
public getAllContext(defaults?: object): any {
if (typeof defaults === 'undefined') {
defaults = {};
}

if (this.scope === undefined) { return defaults; }

const value = { ...this._context, ...defaults };
return this.scope && this.scope.node.getAllContext(value);
}

/**
* Retrieves a value from tree context.
*
Expand Down
16 changes: 16 additions & 0 deletions test/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ test('construct.getContext(key) throws if context is not defined', () => {
}).toThrowError(`No context value present for ${key} key`);
});

test('construct.getAllContext can be used to read the full context of a node', () => {
// GIVEN
const context = {
ctx1: 12,
ctx2: 'hello',
};

// WHEN
const t = createTree(context);
t.child1_1_1.node.setContext('ctx1', 13);

// THEN
expect(t.child1_2.node.getAllContext()).toStrictEqual(context);
expect(t.child1_1_1.node.getAllContext()).toStrictEqual({ ctx1: 13, ctx2: 'hello' });
});

test('construct.tryGetContext(key) can be used to read a value from context defined at the root level', () => {
const context = {
ctx1: 12,
Expand Down