Skip to content

Commit 68068b0

Browse files
committed
Parallel all the requests
1 parent 0ed2fde commit 68068b0

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

docs-app/app/routes/application.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ export default class ApplicationRoute extends Route {
2929
});
3030

3131
await this.docs.setup({
32-
apiDocs: await import('kolay/api-docs:virtual'),
33-
manifest: await import('kolay/manifest:virtual'),
32+
apiDocs: import('kolay/api-docs:virtual'),
33+
manifest: import('kolay/manifest:virtual'),
3434
resolve: {
35-
'ember-primitives': await import('ember-primitives'),
36-
kolay: await import('kolay'),
35+
'ember-primitives': import('ember-primitives'),
36+
kolay: import('kolay'),
3737
},
3838
rehypePlugins: [
3939
[

docs-app/app/templates/nav.gts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Component from '@glimmer/component';
22
import { service } from '@ember/service';
33

4-
import { pascalCase } from "change-case";
4+
import { pascalCase } from 'change-case';
55

66
import type { TOC } from '@ember/component/template-only';
77
import type RouterService from '@ember/routing/router-service';

ui/src/services/kolay/docs.ts

+56-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export default class DocsService extends Service {
3131
* The module of the manifest virtual module.
3232
* This should be set to `await import('kolay/manifest:virtual')
3333
*/
34-
manifest?: any;
34+
manifest?: Promise<any>;
3535

3636
/**
3737
* The module of the api docs virtual module.
3838
* This should be set to `await import('kolay/api-docs:virtual')
3939
*/
40-
apiDocs?: any;
40+
apiDocs?: Promise<any>;
4141

4242
/**
4343
* Additional invokables that you'd like to have access to
@@ -58,7 +58,7 @@ export default class DocsService extends Service {
5858
* and allows you to have access to private libraries without
5959
* needing to publish those libraries to NPM.
6060
*/
61-
resolve?: Record<string, Record<string, unknown>>;
61+
resolve?: Record<string, Promise<Record<string, unknown>>>;
6262

6363
/**
6464
* Provide additional remark plugins to the default markdown compiler.
@@ -73,17 +73,23 @@ export default class DocsService extends Service {
7373
*/
7474
rehypePlugins?: unknown[];
7575
}) => {
76+
let [manifest, apiDocs, resolve] = await Promise.all([
77+
options.manifest,
78+
options.apiDocs,
79+
promiseHash(options.resolve),
80+
]);
81+
7682
if (options.manifest) {
77-
this.loadManifest = options.manifest.load;
83+
this.loadManifest = manifest.load;
7884
}
7985

8086
if (options.apiDocs) {
81-
this.apiDocs._packages = options.apiDocs.packages;
82-
this.apiDocs.loadApiDocs = options.apiDocs.loadApiDocs;
87+
this.apiDocs._packages = apiDocs.packages;
88+
this.apiDocs.loadApiDocs = apiDocs.loadApiDocs;
8389
}
8490

8591
if (options.resolve) {
86-
this.additionalResolves = options.resolve;
92+
this.additionalResolves = resolve;
8793
}
8894

8995
if (options.topLevelScope) {
@@ -191,6 +197,49 @@ export default class DocsService extends Service {
191197
};
192198
}
193199

200+
/**
201+
* RSVP.hash, but native
202+
*/
203+
async function promiseHash<T>(
204+
obj?: Record<string, Promise<T>>,
205+
): Promise<Record<string, T>> {
206+
let result: Record<string, T> = {};
207+
208+
if (!obj) {
209+
return result;
210+
}
211+
212+
let keys: string[] = [];
213+
let promises = [];
214+
215+
for (let [key, promise] of Object.entries(obj)) {
216+
keys.push(key);
217+
promises.push(promise);
218+
}
219+
220+
assert(
221+
`Something went wrong when resolving a promise Hash`,
222+
keys.length === promises.length,
223+
);
224+
225+
let resolved = await Promise.all(promises);
226+
227+
for (let i = 0; i < resolved.length; i++) {
228+
let key = keys[i];
229+
let resolvedValue = resolved[i];
230+
231+
assert(`Missing key for index ${i}`, key);
232+
assert(
233+
`Resolved value for key ${key} is not an object`,
234+
typeof resolvedValue === 'object',
235+
);
236+
237+
result[key] = resolvedValue;
238+
}
239+
240+
return result;
241+
}
242+
194243
// DO NOT DELETE: this is how TypeScript knows how to look up your services.
195244
declare module '@ember/service' {
196245
interface Registry {

0 commit comments

Comments
 (0)