fix waiting to load files

shared-server
bat 3 years ago
parent a8245328ed
commit 4110d021fd

@ -15,7 +15,6 @@ export class Frontend {
this.apiBaseUrl = apiBaseUrl this.apiBaseUrl = apiBaseUrl
this.appBaseUrl = appBaseUrl this.appBaseUrl = appBaseUrl
this.files = {} this.files = {}
this.tasks = []
} }
repoUrl(user, repo) { repoUrl(user, repo) {
@ -92,28 +91,21 @@ export class Frontend {
const resp = await fetch(url) const resp = await fetch(url)
this.checkOk(resp) this.checkOk(resp)
const contents = await resp.json() const contents = await resp.json()
for (const item of contents) { await Promise.all(contents.map(item => (
if (item.type === 'dir') { item.type === 'dir' ?
this.tasks.push(this.loadRepo(user, repo, { this.loadRepo(user, repo, {
...opts, ...opts,
srcPath: [...srcPath, item.name], srcPath: [...srcPath, item.name],
destPath: [...destPath, item.name], destPath: [...destPath, item.name],
})) }) :
} else { this.loadFile(
this.tasks.push(this.loadFile(
user, user,
repo, repo,
item.last_commit_sha, item.last_commit_sha,
[...srcPath, item.name], [...srcPath, item.name],
[...destPath, item.name], [...destPath, item.name],
)) )
} )))
}
let prevCount = 0
while (this.tasks.count > prevCount) {
prevCount = this.tasks.length
await Promise.all(this.tasks)
}
} }
async serve(event) { async serve(event) {

@ -1,5 +1,5 @@
import { Auth } from "./auth" //import { Auth } from "./auth"
import { Frontend } from "./frontend" //import { Frontend } from "./frontend"
export class Server { export class Server {
async getEnv(variables) { async getEnv(variables) {
@ -37,13 +37,16 @@ export class Server {
} }
async init() { async init() {
if (this.port === undefined) {
await this.configure()
}
this.auth = new Auth() this.auth = new Auth()
this.frontend = new Frontend({ this.frontend = new Frontend({
appBaseUrl: this.giteaAppBaseUrl, appBaseUrl: this.giteaAppBaseUrl,
apiBaseUrl: this.giteaApiBaseUrl, apiBaseUrl: this.giteaApiBaseUrl,
}) })
const promises = [] await Promise.all([
const repos = [ ...([
'loader', 'loader',
'editor', 'editor',
'forms', 'forms',
@ -52,20 +55,19 @@ export class Server {
'dialog', 'dialog',
'storage', 'storage',
'editor-lib-codemirror', 'editor-lib-codemirror',
] ].map(repo => (
for (const repo of repos) { server.frontend.loadRepo(
promises.push(server.frontend.loadRepo(
'macchiato', 'macchiato',
repo, repo,
{srcPath: [], destPath: [repo]} {srcPath: [], destPath: [repo]}
)) )
} ))),
promises.push(server.frontend.loadRepo( server.frontend.loadRepo(
'macchiato', 'macchiato',
'pages', 'pages',
{srcPath: [], destPath: []} {srcPath: [], destPath: []}
)) ),
promises.push(server.frontend.loadRepo( server.frontend.loadRepo(
'macchiato', 'macchiato',
'server', 'server',
{ {
@ -73,8 +75,8 @@ export class Server {
destPath: ['server'], destPath: ['server'],
ref: 'shared-server' ref: 'shared-server'
} }
)) ),
await Promise.all(promises) ])
this.frontend.files['/app.js'] = ( this.frontend.files['/app.js'] = (
this.frontend.files['/server/app.js'] this.frontend.files['/server/app.js']
) )
@ -110,8 +112,9 @@ export class Server {
} }
async serve() { async serve() {
await this.configure() if (!this.server) {
await this.init() await this.init()
}
this.server = Deno.listen({ this.server = Deno.listen({
port: this.port, port: this.port,
}) })
@ -119,10 +122,4 @@ export class Server {
this.serveConn(conn) this.serveConn(conn)
} }
} }
async run() {
await this.configure()
await this.init()
await this.serve()
}
} }
Loading…
Cancel
Save