-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SyncAgent, stub out spaces and channels (#296)
- Loading branch information
Showing
11 changed files
with
226 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Client } from '../../../client' | ||
import { PersistedObservable, persistedObservable } from '../../../observable/persistedObservable' | ||
import { Identifiable, Store } from '../../../store/store' | ||
import { RiverConnection } from '../../river-connection/riverConnection' | ||
|
||
export interface ChannelMetadata { | ||
name: string | ||
} | ||
|
||
export interface ChannelModel extends Identifiable { | ||
id: string | ||
spaceId: string | ||
isJoined: boolean | ||
metadata?: ChannelMetadata | ||
} | ||
|
||
@persistedObservable({ tableName: 'channel' }) | ||
export class Channel extends PersistedObservable<ChannelModel> { | ||
constructor( | ||
id: string, | ||
spaceId: string, | ||
private riverConnection: RiverConnection, | ||
store: Store, | ||
) { | ||
super({ id, spaceId, isJoined: false }, store) | ||
} | ||
|
||
protected override async onLoaded() { | ||
this.riverConnection.registerView(this.onClientStarted) | ||
} | ||
|
||
private onClientStarted = (_client: Client) => { | ||
return () => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Client } from '../../../client' | ||
import { PersistedObservable, persistedObservable } from '../../../observable/persistedObservable' | ||
import { Identifiable, Store } from '../../../store/store' | ||
import { RiverConnection } from '../../river-connection/riverConnection' | ||
|
||
export interface SpaceMetadata { | ||
name: string | ||
} | ||
|
||
export interface SpaceModel extends Identifiable { | ||
id: string | ||
channelIds: string[] | ||
metadata?: SpaceMetadata | ||
} | ||
|
||
@persistedObservable({ tableName: 'space' }) | ||
export class Space extends PersistedObservable<SpaceModel> { | ||
constructor(id: string, private riverConnection: RiverConnection, store: Store) { | ||
super({ id, channelIds: [] }, store) | ||
} | ||
|
||
protected override async onLoaded() { | ||
this.riverConnection.registerView(this.onClientStarted) | ||
} | ||
|
||
private onClientStarted = (_client: Client) => { | ||
return () => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @group with-entitilements | ||
*/ | ||
import { dlogger } from '@river-build/dlog' | ||
import { TestUser } from '../utils/testUser.test' | ||
|
||
const logger = dlogger('csb:test:spaces') | ||
|
||
describe('spaces.test.ts', () => { | ||
logger.log('start') | ||
const testUser = new TestUser() | ||
|
||
test('create/leave/join space', async () => { | ||
const syncAgent = await testUser.makeSyncAgent() | ||
await syncAgent.start() | ||
expect(syncAgent.spaces.value.status).not.toBe('loading') | ||
const { spaceId } = await syncAgent.user.createSpace( | ||
{ spaceName: 'BlastOff' }, | ||
testUser.signer, | ||
) | ||
expect(syncAgent.spaces.data.spaceIds.length).toBe(1) | ||
expect(syncAgent.spaces.data.spaceIds[0]).toBe(spaceId) | ||
// expect(bob.spaces.getSpaces().length).toBe(1) | ||
// expect(bob.spaces.getSpaces()[0].id).toBe(spaceId) | ||
// expect(bob.spaces.getSpace(spaceId)).toBeDefined() | ||
// const space = bob.spaces.getSpace(spaceId)! | ||
// expect(space.data.spaceIds.length).toBe(1) | ||
// await waitFor(() => expect(space.data.spaceIds.length).toBe(1) | ||
await syncAgent.stop() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Identifiable, Store } from '../../store/store' | ||
import { | ||
PersistedModel, | ||
PersistedObservable, | ||
persistedObservable, | ||
} from '../../observable/persistedObservable' | ||
import { Space } from './models/space' | ||
import { User } from '../user/user' | ||
import { UserMembershipsModel } from '../user/models/userMemberships' | ||
import { MembershipOp } from '@river-build/proto' | ||
import { isSpaceStreamId } from '../../id' | ||
import { RiverConnection } from '../river-connection/riverConnection' | ||
|
||
export interface SpacesModel extends Identifiable { | ||
id: '0' // single data blobs need a fixed key | ||
spaceIds: string[] // joined spaces | ||
} | ||
|
||
@persistedObservable({ tableName: 'spaces' }) | ||
export class Spaces extends PersistedObservable<SpacesModel> { | ||
private spaces: Record<string, Space> = {} | ||
private user: User | ||
private riverConnection: RiverConnection | ||
|
||
constructor(riverConnection: RiverConnection, user: User, store: Store) { | ||
super({ id: '0', spaceIds: [] }, store) | ||
this.riverConnection = riverConnection | ||
this.user = user | ||
} | ||
|
||
protected override async onLoaded() { | ||
this.user.streams.memberships.subscribe( | ||
(userMemberships) => { | ||
this.onUserDataChanged(userMemberships) | ||
}, | ||
{ fireImediately: true }, | ||
) | ||
} | ||
|
||
getSpace(spaceId: string): Space | undefined { | ||
return this.spaces[spaceId] | ||
} | ||
|
||
private onUserDataChanged(userData: PersistedModel<UserMembershipsModel>) { | ||
if (userData.status === 'loading') { | ||
return | ||
} | ||
const spaceIds = Object.values(userData.data.memberships) | ||
.filter((m) => isSpaceStreamId(m.streamId) && m.op === MembershipOp.SO_JOIN) | ||
.map((m) => m.streamId) | ||
|
||
this.setData({ spaceIds }) | ||
|
||
for (const spaceId of spaceIds) { | ||
if (!this.spaces[spaceId]) { | ||
this.spaces[spaceId] = new Space(spaceId, this.riverConnection, this.store) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @group with-entitilements | ||
*/ | ||
import { dlogger } from '@river-build/dlog' | ||
import { SyncAgent } from './syncAgent' | ||
import { TestUser } from './utils/testUser.test' | ||
|
||
const logger = dlogger('csb:test:syncAgents') | ||
|
||
describe('syncAgents.test.ts', () => { | ||
let bobUser: TestUser | ||
let aliceUser: TestUser | ||
let bob: SyncAgent | ||
let alice: SyncAgent | ||
|
||
beforeEach(async () => { | ||
bobUser = new TestUser() | ||
aliceUser = new TestUser() | ||
bob = await bobUser.makeSyncAgent() | ||
alice = await aliceUser.makeSyncAgent() | ||
}) | ||
|
||
afterEach(async () => { | ||
await bob.stop() | ||
await alice.stop() | ||
}) | ||
|
||
test('syncAgents', async () => { | ||
logger.log('syncAgents') | ||
await Promise.all([bob.start(), alice.start()]) | ||
|
||
const { spaceId } = await bob.user.createSpace({ spaceName: 'BlastOff' }, bobUser.signer) | ||
expect(spaceId).toBeDefined() | ||
}) | ||
}) |