forked from cardstack/boxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard-prerender.gts
168 lines (154 loc) · 4.95 KB
/
card-prerender.gts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type Owner from '@ember/owner';
import { getOwner, setOwner } from '@ember/owner';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { didCancel, enqueueTask } from 'ember-concurrency';
import { type IndexWriter } from '@cardstack/runtime-common';
import { readFileAsText as _readFileAsText } from '@cardstack/runtime-common/stream';
import {
getReader,
type IndexResults,
type Reader,
type RunnerOpts,
} from '@cardstack/runtime-common/worker';
import { CurrentRun } from '../lib/current-run';
import type LoaderService from '../services/loader-service';
import type LocalIndexer from '../services/local-indexer';
import type NetworkService from '../services/network';
import type RenderService from '../services/render-service';
// This component is used in a node/Fastboot context to perform
// server-side rendering for indexing as well as by the TestRealm
// to perform rendering for indexing in Ember test contexts.
export default class CardPrerender extends Component {
@service private declare loaderService: LoaderService;
@service private declare network: NetworkService;
@service private declare renderService: RenderService;
@service private declare fastboot: { isFastBoot: boolean };
@service private declare localIndexer: LocalIndexer;
constructor(owner: Owner, args: {}) {
super(owner, args);
if (this.fastboot.isFastBoot) {
try {
this.doRegistration.perform();
} catch (e: any) {
if (!didCancel(e)) {
throw e;
}
throw new Error(
`card-prerender component is missing or being destroyed before runner registration was completed`,
);
}
} else {
this.localIndexer.setup(
this.fromScratch.bind(this),
this.incremental.bind(this),
);
}
}
private async fromScratch(realmURL: URL): Promise<IndexResults> {
try {
let results = await this.doFromScratch.perform(realmURL);
return results;
} catch (e: any) {
if (!didCancel(e)) {
throw e;
}
}
throw new Error(
`card-prerender component is missing or being destroyed before from scratch index of realm ${realmURL} was completed`,
);
}
private async incremental(
url: URL,
realmURL: URL,
operation: 'delete' | 'update',
ignoreData: Record<string, string>,
): Promise<IndexResults> {
try {
let state = await this.doIncremental.perform(
url,
realmURL,
operation,
ignoreData,
);
return state;
} catch (e: any) {
if (!didCancel(e)) {
throw e;
}
}
throw new Error(
`card-prerender component is missing or being destroyed before incremental index of ${url} was completed`,
);
}
private doRegistration = enqueueTask(async () => {
let optsId = (globalThis as any).runnerOptsId;
if (optsId == null) {
throw new Error(`Runner Options Identifier was not set`);
}
let register = getRunnerOpts(optsId).registerRunner;
await register(this.fromScratch.bind(this), this.incremental.bind(this));
});
private doFromScratch = enqueueTask(async (realmURL: URL) => {
let { reader, indexWriter } = this.getRunnerParams(realmURL);
let currentRun = new CurrentRun({
realmURL,
reader,
indexWriter,
renderCard: this.renderService.renderCard.bind(this.renderService),
});
setOwner(currentRun, getOwner(this)!);
let current = await CurrentRun.fromScratch(currentRun);
this.renderService.indexRunDeferred?.fulfill();
return current;
});
private doIncremental = enqueueTask(
async (
url: URL,
realmURL: URL,
operation: 'delete' | 'update',
ignoreData: Record<string, string>,
) => {
let { reader, indexWriter } = this.getRunnerParams(realmURL);
let currentRun = new CurrentRun({
realmURL,
reader,
indexWriter,
ignoreData: { ...ignoreData },
renderCard: this.renderService.renderCard.bind(this.renderService),
});
setOwner(currentRun, getOwner(this)!);
let current = await CurrentRun.incremental(currentRun, {
url,
operation,
});
this.renderService.indexRunDeferred?.fulfill();
return current;
},
);
private getRunnerParams(realmURL: URL): {
reader: Reader;
indexWriter: IndexWriter;
} {
if (this.fastboot.isFastBoot) {
let optsId = (globalThis as any).runnerOptsId;
if (optsId == null) {
throw new Error(`Runner Options Identifier was not set`);
}
return {
reader: getRunnerOpts(optsId).reader,
indexWriter: getRunnerOpts(optsId).indexWriter,
};
} else {
return {
reader: getReader(this.network.authedFetch, realmURL),
indexWriter: this.localIndexer.indexWriter,
};
}
}
}
function getRunnerOpts(optsId: number): RunnerOpts {
return ((globalThis as any).getRunnerOpts as (optsId: number) => RunnerOpts)(
optsId,
);
}