Commit da956e5a by Sixong.Zhu

im

parent 7ede10fd
Showing with 20 additions and 13 deletions
...@@ -14,12 +14,13 @@ class ChatCacheDatabaseController { ...@@ -14,12 +14,13 @@ class ChatCacheDatabaseController {
private readonly chatMessageKey = "chat-message"; private readonly chatMessageKey = "chat-message";
private setuping = false; private setuping = false;
private setupError = false;
private waitSetupCompleted() { private waitSetupCompleted() {
return new Promise<void>((resolve) => { return new Promise<void>((resolve, reject) => {
const checker = () => { const checker = () => {
if (!this.setuping) { if (!this.setuping) {
resolve(); this.setupError ? reject(new Error(`IM index database setup failed`)) : resolve();
} else { } else {
setTimeout(() => checker(), 200); setTimeout(() => checker(), 200);
} }
...@@ -32,43 +33,45 @@ class ChatCacheDatabaseController { ...@@ -32,43 +33,45 @@ class ChatCacheDatabaseController {
if (this.setuping) { if (this.setuping) {
return this.waitSetupCompleted(); return this.waitSetupCompleted();
} }
return new Promise<void>((resolve) => { return new Promise<void>((resolve, reject) => {
if (uid && indexedDB) { if (uid && indexedDB) {
this.setuping = true; this.setuping = true;
const r = indexedDB.open( const r = indexedDB.open(
"u-" + (this.uid = uid), "u-" + (this.uid = uid),
this.listVersion this.listVersion
); );
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const setupDb = () => { const setupDb = () => {
if (this.setuping) { if (this.setuping) {
if (that.db) { if (this.db) {
try { try {
that.buildTables(that.db, that.chatListKey); this.buildTables(this.db, this.chatListKey);
console.log( console.log(
`build index database for chat completed, 100%` `build index database for chat completed, 100%`
); );
} catch (e) { } catch (e) {
this.setupError = true;
console.error(e); console.error(e);
reject();
} }
} }
this.setuping = false; this.setuping = false;
} }
resolve(); resolve();
}; };
r.onsuccess = function(e) { r.onsuccess = (e) => {
that.db = (e.target as any).result; this.db = (e.target as any).result;
console.log(`index database init comepleted`); console.log(`index database init comepleted`);
setupDb(); setupDb();
}; };
r.onupgradeneeded = function(e) { r.onupgradeneeded = (e) => {
that.db = (e.target as any).result; this.db = (e.target as any).result;
console.log(`upgrade database comepleted`); console.log(`upgrade database comepleted`);
setupDb(); setupDb();
}; };
r.onerror = function(e) { r.onerror = (e) => {
console.log(`index database init failed, ${e}`); console.log(`index database init failed, ${e}`);
this.setupError = true;
reject();
}; };
} else { } else {
resolve(); resolve();
...@@ -128,7 +131,11 @@ class ChatCacheDatabaseController { ...@@ -128,7 +131,11 @@ class ChatCacheDatabaseController {
} }
private buildTransaction(key: string) { 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) { 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