Skip to content

Commit

Permalink
fix weird async bug for setActiveUser
Browse files Browse the repository at this point in the history
  • Loading branch information
jackson-dean committed Jun 24, 2023
1 parent 0219eec commit 03e3ddd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/identity/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,12 +976,12 @@ describe('identity', () => {
it('sets the active user', async () => {
const pubKey1 = 'fake-pub-key-1';
const pubKey2 = 'fake-pub-key-2';
const storageProvider = new AsyncStorageFake();
await storageProvider.setItem(
const asyncStorageProvider = new AsyncStorageFake();
await asyncStorageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
pubKey1
);
await storageProvider.setItem(
await asyncStorageProvider.setItem(
LOCAL_STORAGE_KEYS.identityUsers,
JSON.stringify({
[pubKey1]: {
Expand All @@ -1000,9 +1000,8 @@ describe('identity', () => {
);

const asyncIdentity = new Identity<AsyncStorage>(windowFake, apiFake);
asyncIdentity.configure({ storageProvider });

asyncIdentity.setActiveUser(pubKey2);
asyncIdentity.configure({ storageProvider: asyncStorageProvider });
await asyncIdentity.setActiveUser(pubKey2);
const snapshot = await asyncIdentity.snapshot();
expect(snapshot.currentUser?.publicKey).toEqual(pubKey2);
});
Expand Down
18 changes: 11 additions & 7 deletions src/identity/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,11 @@ export class Identity<T extends StorageProvider> {
*/
setActiveUser(publicKey: string): T extends Storage ? void : Promise<void> {
const maybePromise = this.#setActiveUser(publicKey);

if (typeof maybePromise?.then === 'function') {
// we're in async storage mode
return maybePromise.then(() => {
return (this.#getState() as Promise<IdentityState>).then((state) => {
const snapshotPromise = this.snapshot() as Promise<IdentityState>;
return snapshotPromise.then((state) => {
this.refreshDerivedKeyPermissions();
this.#subscribers.forEach((s) =>
s({
Expand Down Expand Up @@ -1489,7 +1489,7 @@ export class Identity<T extends StorageProvider> {

return this.#storageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
publicKey
newActivePublicKey
) as any;
};

Expand Down Expand Up @@ -1647,6 +1647,10 @@ export class Identity<T extends StorageProvider> {
derivedKeyRegistered: false,
},
});
await this.#storageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
currentUser.publicKey
);
} else if (
this.#pendingWindowRequest?.event ===
NOTIFICATION_EVENTS.LOGIN_START &&
Expand Down Expand Up @@ -1862,6 +1866,10 @@ export class Identity<T extends StorageProvider> {
derivedSeedHex: seedHex,
},
});
await this.#storageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
payload.publicKeyBase58Check
);

return await this.#authorizePrimaryDerivedKey(
payload.publicKeyBase58Check
Expand Down Expand Up @@ -1922,10 +1930,6 @@ export class Identity<T extends StorageProvider> {
})
);
}
await this.#storageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
masterPublicKey
);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class LocalStorageFake implements Storage {
}
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export class AsyncStorageFake implements AsyncStorage {
db: Record<string, string> = {};

Expand All @@ -37,7 +36,6 @@ export class AsyncStorageFake implements AsyncStorage {
}

async clear() {
await sleep(1);
this.db = {};
}

Expand All @@ -46,17 +44,14 @@ export class AsyncStorageFake implements AsyncStorage {
}

async getItem(key: string) {
await sleep(1);
return this.db[key] ?? null;
}

async setItem(key: string, value: string) {
await sleep(1);
this.db[key] = value;
}

async removeItem(key: string) {
await sleep(1);
delete this.db[key];
}
}
Expand Down

0 comments on commit 03e3ddd

Please sign in to comment.