Skip to content

Commit a85c235

Browse files
Luuk PetersLuuk Peters
authored andcommitted
Fix typos and improve clarity in Context API documentation
1 parent d042f6d commit a85c235

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

16/umbraco-cms/customizing/foundation/context-api/consume-a-context.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ description: >-
44
with the rest of the application
55
---
66
# Consume a Context
7-
There are two ways to consume a context: get a __one-time reference__ to the context, or get a __subscription__ for handling context changes. The Context API is a flexible system where contexts can get disonnected or replaced. A subscription allows for the handling of these changes. However, subscriptions use more resources and are typically consumed in the constructor, a time when the computer is already processing a lot. Which way to go depends on your use case.
7+
There are two ways to consume a context: get a __one-time reference__ to the context, or get a __subscription__ for handling context changes. The Context API is a flexible system where contexts can get disconnected or replaced. A subscription allows for the handling of these changes. However, subscriptions use more resources. They are typically consumed in the constructor, a time when the computer is already processing a lot. Which way to go depends on your use case.
88

9-
If you need a context from the moment your element loads and you hold on to the context by putting it into a variable, you should always use the subscription. Otherwise you risk holding on to a context that could be disconnected or replaced without you knowing. Also if you're observing properties of a context, you want know if the context is still available and a subscription also makes the best sense.
9+
If you need a context from the moment your element loads and you hold on to the context by putting it into a variable, you should always use the subscription. Otherwise you risk holding on to a context that could be disconnected or replaced without you knowing. If you're observing properties of a context, you want to know if the context is still available. In this case, a subscription makes the best sense.
1010

11-
A one-time reference is great for fire-and-forget events in your element. Those include events that occur after user interaction. The key here is that the context is not needed when the element is initialized, but only needed then a specific criteria is met. We just need to get a context to do something, but after that, there is no reason to keep the context.
11+
A one-time reference is great for fire-and-forget events in your element. Those include events that occur after user interaction. The key here is that the context is not needed when the element is initialized. It's only needed when a specific criteria is met. We just need to get a context to do something. After that, there is no reason to keep the context.
1212

1313
When you're dealing with a subscription, it's good practice to consume the context in the constructor for the following reasons:
1414

15-
* The constructor runs once when the element is created, ensuring your context subscription is set up before the element connects to the DOM. This guarantees you won't miss any context updates that occur during the element's initialization.
16-
* Context consumers created in the constructor are automatically connected when the element enters the DOM (`connectedCallback`) and disconnected when it's removed (`disconnectedCallback`). You don't need to manually manage this lifecycle—Umbraco's controller system handles it for you.
15+
* The constructor runs once when the element is created. This ensures your context subscription is set up before the element connects to the DOM. This guarantees you won't miss any context updates that occur during the element's initialization.
16+
* Context consumers created in the constructor are automatically connected when the element enters the DOM (`connectedCallback`). They are disconnected when it's removed (`disconnectedCallback`). You don't need to manually manage this lifecycle—Umbraco's controller system handles it for you.
1717
* By establishing context subscriptions in the constructor, your element's state is consistent from the moment it's created. This prevents race conditions where the element might render or perform actions before its required contexts are available.
18-
* Creating context consumers in the constructor is more efficient than creating them in lifecycle methods that can be called multiple times (like `connectedCallback`, which fires every time the element is added to the DOM).
18+
* Creating context consumers in the constructor is more efficient. This is more efficient than creating them in lifecycle methods that can be called multiple times. For example, `connectedCallback` fires every time the element is added to the DOM.
1919

2020
## Consuming contexts in an element
21-
An [Umbraco Element](../umbraco-element/) is **any web component** that extends `UmbLitElement` or uses `UmbElementMixin` to wrap its base class. Whether you're building with Lit, vanilla JavaScript, or any other web component framework, you can make it an Umbraco Element with full access to the Context API.
21+
An [Umbraco Element](../umbraco-element/) is **any web component** that extends `UmbLitElement` or uses `UmbElementMixin` to wrap its base class. Whether you're building with Lit, vanilla JavaScript, or any other web component framework, you can make it an Umbraco Element. This gives it full access to the Context API.
2222

2323
Umbraco Elements provide two methods for consuming contexts:
2424

@@ -28,7 +28,7 @@ Umbraco Elements provide two methods for consuming contexts:
2828
Both methods accept a Context Token (or string alias) to identify which context to consume.
2929

3030
### Get as one-time reference
31-
The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also an example using vanilla Javascript. Both examples don't have any TypeScript specific code, so you either use them in a JavaScript or TypeScript file.
31+
The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also an example using vanilla Javascript. Both examples don't have any TypeScript specific code. You can use them in either a JavaScript or TypeScript file.
3232

3333
{% tabs %}
3434
{% tab title="Lit element" %}
@@ -42,7 +42,7 @@ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
4242
export default class ExampleElement extends UmbLitElement {
4343

4444
/** Notification handler for the notification button */
45-
async #notificationButtonClick {
45+
async #notificationButtonClick() {
4646
//We try to get an instance of the context
4747
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
4848
if (!notificationContext) {
@@ -106,7 +106,7 @@ export default class ExampleElement extends UmbElementMixin(HTMLElement) {
106106
}
107107

108108
/** Notification handler for the notification button */
109-
async #notificationButtonClick {
109+
async #notificationButtonClick() {
110110

111111
//We try to get an instance of the context
112112
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
@@ -132,7 +132,7 @@ customElements.define('example-element', ExampleElement);
132132
{% endtabs %}
133133
134134
### Get as a subscription
135-
The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also a Lit-less example. Both examples don't have any TypeScript specific code, so you either use them in a JavaScript or TypeScript file.
135+
The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also an HTML element example. Both examples don't have any TypeScript specific code. You can use them in either a JavaScript or TypeScript file.
136136
137137
{% tabs %}
138138
{% tab title="Lit element" %}
@@ -204,9 +204,9 @@ customElements.define('example-element', ExampleElement);
204204
{% endtabs %}
205205
206206
## Consuming contexts in non-UI elements
207-
Not all code that needs contexts lives in UI elements (web components). Services, managers, repositories, and helper classes often need access to contexts like notifications, workspaces, or application state, but they don't exist as elements in the DOM.
207+
Not all code that needs contexts lives in UI elements (web components). Services, managers, repositories, and helper classes often need access to contexts. These may include notifications, workspaces, or application state. However, they don't exist as elements in the DOM.
208208
209-
For these non-UI classes, extend `UmbControllerBase` to gain the same context consumption capabilities as elements. This base class provides `getContext()` and `consumeContext()` methods, allowing any class with a controller host to access the Context API.
209+
For these non-UI classes, extend `UmbControllerBase` to gain the same context consumption capabilities as elements. This base class provides `getContext()` and `consumeContext()` methods. This allows any class with a controller host to access the Context API.
210210
211211
### Get as one-time reference
212212
This example creates an example service that can show a notification in the backoffice of Umbraco based on the given text.
@@ -268,16 +268,16 @@ export class ExampleService extends UmbControllerBase {
268268
```
269269
270270
## Manual context control
271-
In rare cases, you may need complete manual control over context consumption without extending `UmbControllerBase` or using element mixins. This is typically necessary when:
271+
In rare cases, you may need complete manual control over context consumption. This means not extending `UmbControllerBase` or using element mixins. This is typically necessary when:
272272
273273
- Integrating with third-party libraries or frameworks
274274
- Working with legacy code that can't be refactored
275275
- Building custom architectural patterns outside Umbraco's standard controller system
276276
277-
For these scenarios, use `UmbContextConsumer` directly. This low-level API gives you full control but requires manual lifecycle management (`hostConnected()`, `hostDisconnected()`, and `destroy()`).
277+
For these scenarios, use `UmbContextConsumer` directly. This low-level API gives you full control but requires manual lifecycle management. You must call `hostConnected()`, `hostDisconnected()`, and `destroy()`.
278278
279279
{% hint style="warning" %}
280-
**Use this approach only when necessary.** The methods shown in previous sections (`UmbLitElement`, `UmbElementMixin`, `UmbControllerBase`) handle lifecycle management automatically and suitable for most use cases.
280+
**Use this approach only when necessary.** The methods shown in previous sections handle lifecycle management automatically. These include `UmbLitElement`, `UmbElementMixin`, and `UmbControllerBase`. They are suitable for most use cases.
281281
{% endhint %}
282282
283283
### Get as one-time reference
@@ -322,7 +322,7 @@ export class NotificationService {
322322
```
323323
324324
### Get as a subscription
325-
In contrast to the one-time reference, a callback is provided. This makes it a subscription. You need to disconnect and destroy the context consumer yourself. This example creates a custom `DocumentService` that consumes the `Document Workspace Context`.
325+
In contrast to the one-time reference, a callback is provided. This makes it a subscription. You need to disconnect and destroy the context consumer yourself. This example creates a custom `DocumentService` that consumes the Document Workspace Context.
326326
327327
```javascript
328328
import { UmbContextConsumer } from '@umbraco-cms/backoffice/context-api';

0 commit comments

Comments
 (0)