diff --git a/packages/react-sdk/src/useDisplayName.ts b/packages/react-sdk/src/useDisplayName.ts index 4370cd45c..466bbd3c8 100644 --- a/packages/react-sdk/src/useDisplayName.ts +++ b/packages/react-sdk/src/useDisplayName.ts @@ -3,7 +3,7 @@ import { ObservableConfig, useObservable } from './useObservable' import { type ActionConfig, useAction } from './internals/useAction' export const useDisplayName = ( - member: Member, + member: Member | Myself, config?: ObservableConfig.FromObservable, ) => { const { data, ...rest } = useObservable(member?.observables.displayName, config) diff --git a/packages/react-sdk/src/useEnsAddress.ts b/packages/react-sdk/src/useEnsAddress.ts index b5f47fd78..efb9def3a 100644 --- a/packages/react-sdk/src/useEnsAddress.ts +++ b/packages/react-sdk/src/useEnsAddress.ts @@ -3,7 +3,7 @@ import { ObservableConfig, useObservable } from './useObservable' import { type ActionConfig, useAction } from './internals/useAction' export const useEnsAddress = ( - member: Member, + member: Member | Myself, config?: ObservableConfig.FromObservable, ) => { const { data, ...rest } = useObservable(member?.observables.ensAddress, config) diff --git a/packages/react-sdk/src/useNft.ts b/packages/react-sdk/src/useNft.ts index b5602d62e..a4e11d812 100644 --- a/packages/react-sdk/src/useNft.ts +++ b/packages/react-sdk/src/useNft.ts @@ -2,7 +2,10 @@ import type { Member, MemberNft, Myself } from '@river-build/sdk' import { ObservableConfig, useObservable } from './useObservable' import { type ActionConfig, useAction } from './internals/useAction' -export const useNft = (member: Member, config?: ObservableConfig.FromObservable) => { +export const useNft = ( + member: Member | Myself, + config?: ObservableConfig.FromObservable, +) => { const { data, ...rest } = useObservable(member?.observables.nft, config) return { ...data, diff --git a/packages/react-sdk/src/useUsername.ts b/packages/react-sdk/src/useUsername.ts index 3764d584e..a5d04e27d 100644 --- a/packages/react-sdk/src/useUsername.ts +++ b/packages/react-sdk/src/useUsername.ts @@ -3,7 +3,7 @@ import { ObservableConfig, useObservable } from './useObservable' import { type ActionConfig, useAction } from './internals/useAction' export const useUsername = ( - member: Member, + member: Member | Myself, config?: ObservableConfig.FromObservable, ) => { const { data, ...rest } = useObservable(member?.observables.username, config) diff --git a/packages/sdk/src/sync-agent/members/members.ts b/packages/sdk/src/sync-agent/members/members.ts index 71fec169d..e67bb230f 100644 --- a/packages/sdk/src/sync-agent/members/members.ts +++ b/packages/sdk/src/sync-agent/members/members.ts @@ -54,20 +54,18 @@ export class Members extends PersistedObservable { }) } - // Lazy loading the myself object, so we dont create unneeded e.g: if we're not in the stream yet - // but we create it if we want to access it get myself() { - if (this._myself) return this._myself - this._myself = new Myself(this.data.id, this.riverConnection, this.store) - this._myself.onStreamInitialized(this.data.id) - return this._myself + if (this._myself) { + return this._myself + } + const member = this.get(this.riverConnection.userId) + const my = new Myself(member, this.data.id, this.riverConnection) + this._myself = my + return my } get(userId: string) { check(isUserId(userId), 'invalid user id') - if (userId === this.riverConnection.userId) { - return this.myself - } // Its possible to get a member that its not in the userIds array, if the user left the stream for example // We can get a member that left, to get the last snapshot of the member if (!this.members[userId]) { @@ -98,16 +96,14 @@ export class Members extends PersistedObservable { (member) => member.userId, ) for (const userId of userIds) { - if (userId !== this.riverConnection.userId) { - if (!this.members[userId]) { - this.members[userId] = new Member( - userId, - streamId, - this.riverConnection, - this.store, - ) - this.members[userId].onStreamInitialized(streamId) - } + if (!this.members[userId]) { + this.members[userId] = new Member( + userId, + streamId, + this.riverConnection, + this.store, + ) + this.members[userId].onStreamInitialized(streamId) } } this.setData({ initialized: true, userIds }) @@ -130,52 +126,28 @@ export class Members extends PersistedObservable { private onMemberJoin = (streamId: string, userId: string): void => { if (streamId !== this.data.id) return this.setData({ userIds: [...this.data.userIds, userId] }) - if (userId === this.riverConnection.userId) { - if (!this._myself) { - this._myself = new Myself(streamId, this.riverConnection, this.store) - this._myself.onStreamInitialized(streamId) - } - } else { - if (!this.members[userId]) { - this.members[userId] = new Member( - userId, - streamId, - this.riverConnection, - this.store, - ) - this.members[userId].onStreamInitialized(streamId) - this.members[userId].observables.membership.onStreamMembershipUpdated( - streamId, - userId, - MembershipOp.SO_JOIN, - ) - } + if (!this.members[userId]) { + this.members[userId] = new Member(userId, streamId, this.riverConnection, this.store) + this.members[userId].onStreamInitialized(streamId) + this.members[userId].observables.membership.onStreamMembershipUpdated( + streamId, + userId, + MembershipOp.SO_JOIN, + ) } } private onMemberInvite = (streamId: string, userId: string): void => { if (streamId !== this.data.id) return this.setData({ userIds: [...this.data.userIds, userId] }) - if (userId === this.riverConnection.userId) { - if (!this._myself) { - this._myself = new Myself(streamId, this.riverConnection, this.store) - this._myself.onStreamInitialized(streamId) - } - } else { - if (!this.members[userId]) { - this.members[userId] = new Member( - userId, - streamId, - this.riverConnection, - this.store, - ) - this.members[userId].onStreamInitialized(streamId) - this.members[userId].observables.membership.onStreamMembershipUpdated( - streamId, - userId, - MembershipOp.SO_INVITE, - ) - } + if (!this.members[userId]) { + this.members[userId] = new Member(userId, streamId, this.riverConnection, this.store) + this.members[userId].onStreamInitialized(streamId) + this.members[userId].observables.membership.onStreamMembershipUpdated( + streamId, + userId, + MembershipOp.SO_INVITE, + ) } } diff --git a/packages/sdk/src/sync-agent/members/models/myself.ts b/packages/sdk/src/sync-agent/members/models/myself.ts index 79fb7455a..f8a8a94c1 100644 --- a/packages/sdk/src/sync-agent/members/models/myself.ts +++ b/packages/sdk/src/sync-agent/members/models/myself.ts @@ -1,17 +1,37 @@ import type { Address } from '@river-build/web3' -import type { Store } from '../../../store/store' import type { RiverConnection } from '../../river-connection/riverConnection' import { type NftModel } from './metadata/nft' import { addressFromUserId } from '../../../id' import { Member } from './member' -export class Myself extends Member { +export class Myself { + observables: Member['observables'] constructor( - private streamId: string, + public member: Member, + protected streamId: string, protected riverConnection: RiverConnection, - store: Store, ) { - super(riverConnection.userId, streamId, riverConnection, store) + this.observables = member.observables + } + + get username() { + return this.member.username + } + + get displayName() { + return this.member.displayName + } + + get ensAddress() { + return this.member.ensAddress + } + + get nft() { + return this.member.nft + } + + get membership() { + return this.member.membership } async setUsername(username: string) {