Skip to content

Commit

Permalink
fix: @warp-drive-ember, dont leak empty slot
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Apr 19, 2024
1 parent 458f110 commit f5ab2e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
21 changes: 11 additions & 10 deletions packages/ember/src/-private/await.gts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { assert } from '@ember/debug';
import Component from '@glimmer/component';
import { getPromiseState } from './promise-state.ts';
import { Awaitable } from '@ember-data/request';

export function notNull<T>(x: null): never;
export function notNull<T>(x: T): Exclude<T, null>;
export function notNull<T>(x: T | null) {
assert('Expected a non-null value, but got null', x !== null);
return x;
}
export const and = (x: unknown, y: unknown) => Boolean(x && y);
interface ThrowSignature<E = Error | string | object> {
Args: {
Expand Down Expand Up @@ -40,15 +33,23 @@ export class Await<T, E> extends Component<AwaitSignature<T, E>> {
return getPromiseState<T, E>(this.args.promise);
}

get error() {
return this.state.error as E;
}

get result() {
return this.state.result as T;
}

<template>
{{#if this.state.isPending}}
{{yield to="pending"}}
{{else if (and this.state.isError (has-block "error"))}}
{{yield (notNull this.state.error) to="error"}}
{{yield this.error to="error"}}
{{else if this.state.isSuccess}}
{{yield (notNull this.state.result) to="success"}}
{{yield this.result to="success"}}
{{else}}
<Throw @error={{(notNull this.state.error)}} />
<Throw @error={{this.error}} />
{{/if}}
</template>
}
15 changes: 13 additions & 2 deletions packages/ember/src/-private/request.gts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ import type Store from '@ember-data/store';

import { getRequestState } from './request-state.ts';
import type { RequestLoadingState } from './request-state.ts';
import { and, notNull, Throw } from './await.gts';
import { and, Throw } from './await.gts';
import { tracked } from '@glimmer/tracking';

function notNull<T>(x: null): never;
function notNull<T>(x: T): Exclude<T, null>;
function notNull<T>(x: T | null) {
assert('Expected a non-null value, but got null', x !== null);
return x;
}

const not = (x: unknown) => !x;
// default to 30 seconds unavailable before we refresh
const DEFAULT_DEADLINE = 30_000;
Expand Down Expand Up @@ -244,6 +251,10 @@ export class Request<T> extends Component<RequestSignature<T>> {
return getRequestState<T>(this.request);
}

get result() {
return this.reqState.result as T;
}

<template>
{{#if this.reqState.isLoading}}
{{yield this.reqState.loadingState to="loading"}}
Expand All @@ -252,7 +263,7 @@ export class Request<T> extends Component<RequestSignature<T>> {
{{else if (and this.reqState.isError (has-block "error"))}}
{{yield (notNull this.reqState.error) this.errorFeatures to="error"}}
{{else if this.reqState.isSuccess}}
{{yield (notNull this.reqState.result) this.contentFeatures to="content"}}
{{yield this.result this.contentFeatures to="content"}}
{{else if (not this.reqState.isCancelled)}}
<Throw @error={{(notNull this.reqState.error)}} />
{{/if}}
Expand Down

0 comments on commit f5ab2e3

Please sign in to comment.