Commit c7763c7d by Sixong.Zhu
parents 13f3b8a0 da956e5a
Showing with 20 additions and 13 deletions
......@@ -14,12 +14,13 @@ class ChatCacheDatabaseController {
private readonly chatMessageKey = "chat-message";
private setuping = false;
private setupError = false;
private waitSetupCompleted() {
return new Promise<void>((resolve) => {
return new Promise<void>((resolve, reject) => {
const checker = () => {
if (!this.setuping) {
resolve();
this.setupError ? reject(new Error(`IM index database setup failed`)) : resolve();
} else {
setTimeout(() => checker(), 200);
}
......@@ -32,43 +33,45 @@ class ChatCacheDatabaseController {
if (this.setuping) {
return this.waitSetupCompleted();
}
return new Promise<void>((resolve) => {
return new Promise<void>((resolve, reject) => {
if (uid && indexedDB) {
this.setuping = true;
const r = indexedDB.open(
"u-" + (this.uid = uid),
this.listVersion
);
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const setupDb = () => {
if (this.setuping) {
if (that.db) {
if (this.db) {
try {
that.buildTables(that.db, that.chatListKey);
this.buildTables(this.db, this.chatListKey);
console.log(
`build index database for chat completed, 100%`
);
} catch (e) {
this.setupError = true;
console.error(e);
reject();
}
}
this.setuping = false;
}
resolve();
};
r.onsuccess = function(e) {
that.db = (e.target as any).result;
r.onsuccess = (e) => {
this.db = (e.target as any).result;
console.log(`index database init comepleted`);
setupDb();
};
r.onupgradeneeded = function(e) {
that.db = (e.target as any).result;
r.onupgradeneeded = (e) => {
this.db = (e.target as any).result;
console.log(`upgrade database comepleted`);
setupDb();
};
r.onerror = function(e) {
r.onerror = (e) => {
console.log(`index database init failed, ${e}`);
this.setupError = true;
reject();
};
} else {
resolve();
......@@ -128,7 +131,11 @@ class ChatCacheDatabaseController {
}
private buildTransaction(key: string) {
return this.db.transaction(key, "readwrite");
try { return this.db.transaction(key, "readwrite"); }
catch{
window.location.reload();
throw new Error(`transition failed`);
}
}
private buildStore(key: string) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment