fix(profiles): let the first profile not get shot out in broad daylight

This commit is contained in:
Samidy 2026-03-11 03:41:16 +03:00
parent 1412c7224f
commit 5794a12128

View file

@ -15,6 +15,7 @@ pb.autoCancellation(false);
const syncManager = { const syncManager = {
pb: pb, pb: pb,
_userRecordCache: null, _userRecordCache: null,
_getUserRecordPromise: null,
_isSyncing: false, _isSyncing: false,
async _getUserRecord(uid) { async _getUserRecord(uid) {
@ -24,12 +25,24 @@ const syncManager = {
return this._userRecordCache; return this._userRecordCache;
} }
if (this._getUserRecordPromise && this._getUserRecordPromise.uid === uid) {
return this._getUserRecordPromise.promise;
}
const promise = (async () => {
try { try {
const record = await this.pb.collection('DB_users').getFirstListItem(`firebase_id="${uid}"`, { f_id: uid }); const result = await this.pb.collection('DB_users').getList(1, 1, {
filter: `firebase_id="${uid}"`,
sort: '-username',
f_id: uid,
});
if (result.items.length > 0) {
const record = result.items[0];
this._userRecordCache = record; this._userRecordCache = record;
return record; return record;
} catch (error) { }
if (error.status === 404) {
try { try {
const newRecord = await this.pb.collection('DB_users').create( const newRecord = await this.pb.collection('DB_users').create(
{ {
@ -44,13 +57,27 @@ const syncManager = {
this._userRecordCache = newRecord; this._userRecordCache = newRecord;
return newRecord; return newRecord;
} catch (createError) { } catch (createError) {
const retryResult = await this.pb.collection('DB_users').getList(1, 1, {
filter: `firebase_id="${uid}"`,
f_id: uid,
});
if (retryResult.items.length > 0) {
this._userRecordCache = retryResult.items[0];
return this._userRecordCache;
}
console.error('[PocketBase] Failed to create user:', createError); console.error('[PocketBase] Failed to create user:', createError);
return null; return null;
} }
} } catch (error) {
console.error('[PocketBase] Failed to get user:', error); console.error('[PocketBase] Failed to get user:', error);
return null; return null;
} finally {
this._getUserRecordPromise = null;
} }
})();
this._getUserRecordPromise = { uid, promise };
return promise;
}, },
async getUserData() { async getUserData() {
@ -485,10 +512,8 @@ const syncManager = {
const updateData = { ...data }; const updateData = { ...data };
await this.pb.collection('DB_users').update(record.id, updateData, { f_id: user.$id }); const updated = await this.pb.collection('DB_users').update(record.id, updateData, { f_id: user.$id });
if (this._userRecordCache) { this._userRecordCache = updated;
this._userRecordCache = { ...this._userRecordCache, ...updateData };
}
}, },
async isUsernameTaken(username) { async isUsernameTaken(username) {