Skip to content

Commit

Permalink
feat(html): extend resolve helper support to all content values
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed May 6, 2021
1 parent 564d875 commit 0ede2e8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions docs/misc/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ html.resolve(promise: Promise, placeholder: Function, delay = 200): Function
```

* **arguments**:
* `promise` - promise, which should resolve/reject update function
* `promise` - promise, which should resolve to content expression value
* `placeholder` - update function for render content until promise is resolved or rejected
* `delay` - delay in milliseconds, after which placeholder is rendered
* `delay` - delay in milliseconds, after which placeholder is rendered
* **returns**:
* update function compatible with content expression
* update function compatible with content expression

## svg

Expand Down
6 changes: 3 additions & 3 deletions docs/template-engine/promises.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ html.resolve(promise: Promise, placeholder: Function, delay = 200): Function
```

* **arguments**:
* `promise` - promise, which should resolve/reject update function
* `promise` - promise, which should resolve to content expression value
* `placeholder` - update function for render content until promise is resolved or rejected
* `delay` - delay in milliseconds, after which placeholder is rendered
* `delay` - delay in milliseconds, after which placeholder is rendered
* **returns**:
* update function compatible with content expression
* update function compatible with content expression

```javascript
const promise = asyncApi().then(...);
Expand Down
5 changes: 3 additions & 2 deletions src/template/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { storePointer } from "../utils.js";
import resolveTemplateValue from "./resolvers/value.js";

function resolveValue({ target }, setter) {
let value;
Expand Down Expand Up @@ -118,11 +119,11 @@ export function resolve(promise, placeholder, delay = 200) {

promiseMap.set(target, promise);

promise.then(template => {
promise.then(value => {
if (timeout) clearTimeout(timeout);

if (promiseMap.get(target) === promise) {
template(host, target);
resolveTemplateValue(host, target, value);
promiseMap.set(target, null);
}
});
Expand Down
13 changes: 13 additions & 0 deletions test/spec/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,19 @@ describe("html:", () => {
done();
});
});

it("supports passing values", done => {
const promise = Promise.resolve(["a", "b"]);
// prettier-ignore
const flush = html`${html.resolve(promise)}`;

flush(fragment);

Promise.resolve().then(() => {
expect(fragment.innerHTML).toBe("ab");
done();
});
});
});

describe("style method", () => {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ declare namespace hybrids {
): EventHandler<E>;

function resolve<E>(
promise: Promise<UpdateFunction<E>>,
promise: Promise<any>,
placeholder?: UpdateFunction<E>,
delay?: number,
): UpdateFunction<E>;
Expand Down

0 comments on commit 0ede2e8

Please sign in to comment.