Skip to content

Commit 98e53ae

Browse files
Luuk PetersLuuk Peters
authored andcommitted
Refactor context handling in NotificationService and DocumentService; add lifecycle methods for host connection management
1 parent 31f5d23 commit 98e53ae

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

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

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,29 @@ export class NotificationService {
301301
// No callback = one-time use
302302
);
303303

304-
// Trigger connection and get result as Promise
305-
consumer.hostConnected();
306-
const notificationContext = await consumer.asPromise();
307-
308-
if (!notificationContext) {
309-
throw new Error('Notification context not found!');
310-
}
311-
312-
notificationContext.peek("positive", {
313-
data: {
314-
headline: "Success",
315-
message: message
304+
try {
305+
// Trigger connection and get result as Promise
306+
consumer.hostConnected();
307+
308+
// The promise will reject if the context is not found within one animation frame
309+
// You can prevent this with: consumer.asPromise({ preventTimeout: true })
310+
const notificationContext = await consumer.asPromise();
311+
312+
if (!notificationContext) {
313+
throw new Error('Notification context not found!');
316314
}
317-
});
318-
319-
// Consumer auto-destroys when no callback provided
315+
316+
notificationContext.peek("positive", {
317+
data: {
318+
headline: "Success",
319+
message: message
320+
}
321+
});
322+
} finally {
323+
// Manually clean up to ensure proper disconnection
324+
consumer.hostDisconnected();
325+
consumer.destroy();
326+
}
320327
}
321328
}
322329
```
@@ -339,24 +346,60 @@ export class DocumentService {
339346
UMB_DOCUMENT_WORKSPACE_CONTEXT,
340347
(context) => {
341348
if (context) {
349+
// Context is available
342350
console.log("I've got the document workspace context: ", context);
343351
this.#workspaceContext = context;
344352
} else {
353+
// Context not yet available OR has been removed
354+
// This can happen on initial creation or when unprovided
345355
console.log("The document workspace context is gone, I will make sure my code disassembles properly.");
346356
this.#workspaceContext = null;
347357
}
348358
}
349359
);
350-
351-
// Manually trigger the connection
360+
}
361+
362+
// Public method that should be called by the host element's connectedCallback
363+
hostConnected() {
352364
this.#workspaceContextConsumer.hostConnected();
353365
}
354366

367+
// Public method that should be called by the host element's disconnectedCallback
368+
hostDisconnected() {
369+
this.#workspaceContextConsumer.hostDisconnected();
370+
}
371+
355372
destroy() {
356373
// Manually clean up
357-
this.#workspaceContextConsumer.hostDisconnected();
358374
this.#workspaceContextConsumer.destroy();
359375
}
360376
}
377+
```
378+
379+
To use this service, the host element must call the lifecycle methods:
380+
381+
```javascript
382+
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
383+
import { DocumentService } from './document-service.js';
384+
385+
export default class MyElement extends UmbLitElement {
386+
#documentService;
361387

388+
constructor() {
389+
super();
390+
this.#documentService = new DocumentService(this);
391+
}
392+
393+
connectedCallback() {
394+
super.connectedCallback();
395+
// Notify the service that the host is connected
396+
this.#documentService.hostConnected();
397+
}
398+
399+
disconnectedCallback() {
400+
super.disconnectedCallback();
401+
// Notify the service that the host is disconnected
402+
this.#documentService.hostDisconnected();
403+
}
404+
}
362405
```

16/umbraco-cms/customizing/foundation/context-api/context-api-fundamentals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The Context API exists to solve common problems in complex user interfaces:
2828
To understand the Context API, it's important to understand the terminology that is used in the rest of the documentation.
2929

3030
### Context
31-
An object that encapsulates both data and methods to interact with that data. This object can be provided to descending DOM elements. A context represents a specific capability or state that multiple elements might need to access. Examples include workspace context, content data, user permissions, or specialized services. Contexts encapsulate both data and methods, making them more than just data containers. Unlike repositories, a context is always only available within the scope of a certain element and its descendants.
31+
An object that encapsulates both data and methods to interact with that data. This object can be provided to descending DOM elements. A context represents a specific capability or state that multiple elements might need to access. Examples include workspace context, content data, user permissions, or specialized services. Contexts encapsulate both data and methods, making them more than data containers. Unlike repositories, a context is always only available within the scope of a certain element and its descendants.
3232

3333
### Context provider
3434
An element that creates and makes a context available to its descending elements. The provider is responsible for the context's lifecycle. One element can provide multiple different contexts if needed.

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@ description: >-
44
incorporate central logic.
55
---
66

7-
-----
8-
New structure:
9-
10-
Page 3: Creating Contexts
11-
• How to create contexts
12-
• How to define your own custom tokens (building on the token knowledge from core concepts)
13-
-----
14-
157
# Provide a Context
168

179
## Provide a Context API
18-
1910
The recommended approach is to base your Context API on the `UmbContextBase` class, which provides automatic context registration. The following example shows how it's used:
2011

2112
```typescript

0 commit comments

Comments
 (0)