1
- import { type Deferred , apiFor } from '@cardstack/runtime-common' ;
1
+ import {
2
+ type Deferred ,
3
+ type LooseSingleCardDocument ,
4
+ } from '@cardstack/runtime-common' ;
2
5
3
6
import {
4
7
type CardResource ,
5
8
getCard ,
6
9
} from '@cardstack/host/resources/card-resource' ;
7
10
8
11
import type { CardDef , Format } from 'https://cardstack.com/base/card-api' ;
9
- import type * as CardAPI from 'https://cardstack.com/base/card-api' ;
10
12
11
13
interface Args {
12
14
format : Format ;
13
15
owner : object ;
14
16
request ?: Deferred < CardDef | undefined > ;
15
17
stackIndex : number ;
16
- card ?: CardDef ;
18
+ newCard ?: { doc : LooseSingleCardDocument ; relativeTo : URL | undefined } ;
17
19
url ?: URL ;
18
20
cardResource ?: CardResource ;
19
21
isLinkedCard ?: boolean ; // TODO: consider renaming this so its clearer that we use this for being able to tell whether the card needs to be closed after saving
@@ -25,39 +27,32 @@ export class StackItem {
25
27
stackIndex : number ;
26
28
isLinkedCard ?: boolean ; // TODO: consider renaming this so its clearer that we use this for being able to tell whether the card needs to be closed after saving
27
29
private owner : object ;
28
- private newCard ?: CardDef ;
29
30
private cardResource ?: CardResource ;
30
- private newCardApiPromise : Promise < typeof CardAPI > | undefined ;
31
- private newCardApi : typeof CardAPI | undefined ;
32
31
33
32
constructor ( args : Args ) {
34
33
let {
35
34
format,
36
35
request,
37
36
stackIndex,
38
- card ,
37
+ newCard ,
39
38
url,
40
39
cardResource,
41
40
isLinkedCard,
42
41
owner,
43
42
} = args ;
44
- if ( ! card && ! cardResource && ! url ) {
43
+ if ( ! newCard && ! cardResource && ! url ) {
45
44
throw new Error (
46
- `Cannot create a StackItem without a 'card ' or a 'cardResource' or a 'url'` ,
45
+ `Cannot create a StackItem without a 'newCard ' or a 'cardResource' or a 'url'` ,
47
46
) ;
48
47
}
49
48
if ( cardResource ) {
50
49
this . cardResource = cardResource ;
51
- } else if ( card ?. id ) {
52
- // if the card is not actually new--load a resource instead
53
- this . cardResource = getCard ( owner , ( ) => card ! . id ) ;
54
50
} else if ( url ) {
55
51
this . cardResource = getCard ( owner , ( ) => url ! . href ) ;
56
- } else if ( card ) {
57
- this . newCard = card ;
58
- this . newCardApiPromise = apiFor ( this . card ) . then (
59
- ( api ) => ( this . newCardApi = api ) ,
60
- ) ;
52
+ } else if ( newCard ) {
53
+ this . cardResource = getCard ( owner , ( ) => newCard ! . doc , {
54
+ relativeTo : newCard . relativeTo ,
55
+ } ) ;
61
56
}
62
57
63
58
this . format = format ;
@@ -68,16 +63,11 @@ export class StackItem {
68
63
}
69
64
70
65
get url ( ) {
71
- return (
72
- ( this . cardResource ?. url ? new URL ( this . cardResource . url ) : undefined ) ??
73
- ( this . newCard ?. id ? new URL ( this . newCard . id ) : undefined )
74
- ) ;
66
+ return this . cardResource ?. url ? new URL ( this . cardResource . url ) : undefined ;
75
67
}
76
68
77
69
get card ( ) : CardDef {
78
- if ( this . newCard ) {
79
- return this . newCard ;
80
- } else if ( this . cardResource ) {
70
+ if ( this . cardResource ) {
81
71
if ( ! this . cardResource . card ) {
82
72
throw new Error ( `The CardResource for this StackItem has no card set` ) ;
83
73
}
@@ -88,9 +78,7 @@ export class StackItem {
88
78
}
89
79
90
80
get title ( ) {
91
- if ( this . newCard ) {
92
- return this . newCard . title ;
93
- } else if ( this . cardResource ?. card ) {
81
+ if ( this . cardResource ?. card ) {
94
82
return this . cardResource . card . title ;
95
83
}
96
84
return undefined ;
@@ -127,7 +115,7 @@ export class StackItem {
127
115
}
128
116
129
117
get api ( ) {
130
- let api = this . cardResource ?. api ?? this . newCardApi ;
118
+ let api = this . cardResource ?. api ;
131
119
if ( ! api ) {
132
120
throw new Error (
133
121
`API for stack item is not available yet--use this.ready() to wait for API availability` ,
@@ -137,22 +125,14 @@ export class StackItem {
137
125
}
138
126
139
127
async ready ( ) {
140
- await Promise . all ( [ this . cardResource ?. loaded , this . newCardApiPromise ] ) ;
128
+ await this . cardResource ?. loaded ;
141
129
}
142
130
143
131
clone ( args : Partial < Args > ) {
144
- let {
145
- card,
146
- format,
147
- request,
148
- isLinkedCard,
149
- owner,
150
- cardResource,
151
- stackIndex,
152
- } = this ;
132
+ let { format, request, isLinkedCard, owner, cardResource, stackIndex } =
133
+ this ;
153
134
return new StackItem ( {
154
135
cardResource,
155
- card,
156
136
format,
157
137
request,
158
138
isLinkedCard,
0 commit comments