@brightspace-ui/core 2.180.9 → 2.181.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ # LoadingCompleteMixin
2
+
3
+ The `LoadingCompleteMixin` contains boilerplate code for [`getLoadingComplete`](https://github.com/BrightspaceUI/testing#waiting-for-asynchronous-components). It simplifies fixture setup for custom components in automated tests, and provides a `loadingComplete` promise that can be used internally.
4
+
5
+ ## Usage
6
+ Apply the mixin and call `resolveLoadingComplete()` once your component has finished loading
7
+
8
+ ```js
9
+ import { LoadingCompleteMixin } from '@brightspace-ui/core/mixins/loading-complete/loading-complete-mixin.js';
10
+
11
+ class MyComponent extends LoadingCompleteMixin(LitElement) {
12
+
13
+ connectedCallback() {
14
+ this._fetchMyData().then(this.resolveLoadingComplete);
15
+ }
16
+
17
+ }
18
+ ```
19
+
20
+ To make use of the `loadingComplete` promise, simply `await` it where needed:
21
+
22
+ ```js
23
+ async removeSkeleton() {
24
+ await this.loadingComplete;
25
+ this.skeleton = false;
26
+ }
27
+ ```
28
+
29
+ If for any reason `resolveLoadingComplete` is never called, `loadingComplete` won't resolve and any consumers will hang.
30
+
31
+ ### `getLoadingComplete`
32
+
33
+ In some cases, instead of finding one spot to call `resolveLoadingComplete`, you may find it easier to `await` a set of promises in a custom `getLoadingComplete` method.
34
+
35
+ You'll also need to use this method if you're working in a general-use mixin rather than directly in a component.
36
+
37
+ ```js
38
+ class MyComponent extends LoadingCompleteMixin(LitElement) {
39
+
40
+ async getLoadingComplete() {
41
+ await super.getLoadingComplete();
42
+ await this._myCustomIconImport;
43
+ await Promise.all(this._allMyDataPromises);
44
+ }
45
+ }
46
+ ```
47
+ Note that the work to generate these promises should have already started, before `firstUpdated`, and we simply `await` them here.
@@ -0,0 +1,30 @@
1
+ import { dedupeMixin } from '@open-wc/dedupe-mixin';
2
+
3
+ export const LoadingCompleteMixin = dedupeMixin((superclass) => class extends superclass {
4
+
5
+ #loadingCompleteResolve;
6
+
7
+ // eslint-disable-next-line sort-class-members/sort-class-members
8
+ #loadingCompletePromise = !Object.prototype.hasOwnProperty.call(this.constructor.prototype, 'getLoadingComplete')
9
+ ? new Promise(resolve => this.#loadingCompleteResolve = resolve)
10
+ : Promise.resolve();
11
+
12
+ get loadingComplete() {
13
+ return this.getLoadingComplete();
14
+ }
15
+
16
+ get resolveLoadingComplete() {
17
+ return () => {
18
+ if (this.#loadingCompleteResolve) {
19
+ this.#loadingCompleteResolve();
20
+ this.#loadingCompleteResolve = null;
21
+ }
22
+ };
23
+ }
24
+
25
+ async getLoadingComplete() {
26
+ await super.getLoadingComplete?.();
27
+ return this.#loadingCompletePromise;
28
+ }
29
+
30
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brightspace-ui/core",
3
- "version": "2.180.9",
3
+ "version": "2.181.0",
4
4
  "description": "A collection of accessible, free, open-source web components for building Brightspace applications",
5
5
  "type": "module",
6
6
  "repository": "https://github.com/BrightspaceUI/core.git",