Skip to content

Commit

Permalink
fix: fix server launch in different scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk authored and BroKun committed Aug 15, 2024
1 parent b1ede64 commit a93f7c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .changeset/grumpy-goats-exist.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
'@difizen/libro-widget': patch
---

fix(kernel): fix server ready defer when use launch repeatly
fix: fix server launch in different scenarios
39 changes: 24 additions & 15 deletions packages/libro-kernel/src/server/server-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ServerManager {
@prop()
kernelspec?: ISpecModels;

protected defer: Deferred<ISpecModels>;
protected defer: Deferred<ISpecModels> = new Deferred<ISpecModels>();

get ready() {
return this.defer.promise;
Expand All @@ -34,34 +34,43 @@ export class ServerManager {
this.kernelSpecRestAPI = kernelSpecRestAPI;
}

loading = false;

async launch() {
const oldDefer = this.defer;
this.defer = new Deferred<ISpecModels>();
let current = this.defer;

// 启动中
if (this.loading) {
return current.promise;
}

// 启动过
if (this.loaded) {
this.loaded = false;
current = new Deferred<ISpecModels>();
this.defer = current;
}

// 首次启动
this.loading = true;
this.doLaunch()
.then((r) => {
if (oldDefer) {
oldDefer.resolve(r);
}
this.defer.resolve(r);
return;
this.loading = false;
this.loaded = true;
return current.resolve(r);
})
.catch(() => {
//
});

return this.defer.promise;
.catch(console.error);
return current.promise;
}

protected async doLaunch(): Promise<ISpecModels> {
const kernelspec = await this.doGetServerStatus();
this.launching = true;
this.loaded = false;
if (!kernelspec) {
await timeout(1000);
return await this.doLaunch();
}
this.launching = false;
this.loaded = true;
this.kernelspec = kernelspec;
return kernelspec;
}
Expand Down

0 comments on commit a93f7c9

Please sign in to comment.