Skip to content

Commit 23df709

Browse files
authoredOct 7, 2023
feat: add a mechanism to retrieve all context (with defaults) (#1751)
Previously, all users of constructs would have to know beforehand the keys in the context to expect, and use either `tryGetContext` or `getContext` to retrieve a specific key from context. This provides a mechanism to get the full context of a node in the construct tree, with overridable defaults. The `getAllContext` method allows users to retrieve all context. The interface for `getContext` and `tryGetContext` remains unchanged. I have also fixed a small typo in the library.
1 parent 80a94b5 commit 23df709

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
 

‎API.md

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/construct.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Node {
4444
/**
4545
* The id of this construct within the current scope.
4646
*
47-
* This is a a scope-unique id. To obtain an app-unique id for this construct, use `addr`.
47+
* This is a scope-unique id. To obtain an app-unique id for this construct, use `addr`.
4848
*/
4949
public readonly id: string;
5050

@@ -236,6 +236,25 @@ export class Node {
236236
return this.scope && this.scope.node.getContext(key);
237237
}
238238

239+
/**
240+
* Retrieves the all context of a node from tree context.
241+
*
242+
* Context is usually initialized at the root, but can be overridden at any point in the tree.
243+
*
244+
* @param defaults Any keys to override the retrieved context
245+
* @returns The context object or an empty object if there is discovered context
246+
*/
247+
public getAllContext(defaults?: object): any {
248+
if (typeof defaults === 'undefined') {
249+
defaults = {};
250+
}
251+
252+
if (this.scope === undefined) { return defaults; }
253+
254+
const value = { ...this._context, ...defaults };
255+
return this.scope && this.scope.node.getAllContext(value);
256+
}
257+
239258
/**
240259
* Retrieves a value from tree context.
241260
*

‎test/construct.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ test('construct.getContext(key) throws if context is not defined', () => {
150150
}).toThrowError(`No context value present for ${key} key`);
151151
});
152152

153+
test('construct.getAllContext can be used to read the full context of a node', () => {
154+
// GIVEN
155+
const context = {
156+
ctx1: 12,
157+
ctx2: 'hello',
158+
};
159+
160+
// WHEN
161+
const t = createTree(context);
162+
t.child1_1_1.node.setContext('ctx1', 13);
163+
164+
// THEN
165+
expect(t.child1_2.node.getAllContext()).toStrictEqual(context);
166+
expect(t.child1_1_1.node.getAllContext()).toStrictEqual({ ctx1: 13, ctx2: 'hello' });
167+
});
168+
153169
test('construct.tryGetContext(key) can be used to read a value from context defined at the root level', () => {
154170
const context = {
155171
ctx1: 12,

0 commit comments

Comments
 (0)
Failed to load comments.