( conn: DbConnection, identity: Identity, token: string )
| 30 | } |
| 31 | |
| 32 | function onConnect( |
| 33 | conn: DbConnection, |
| 34 | identity: Identity, |
| 35 | token: string |
| 36 | ): void { |
| 37 | console.log('\nConnected to SpacetimeDB!'); |
| 38 | console.log(`Identity: ${identity.toHexString().slice(0, 16)}...`); |
| 39 | |
| 40 | // Save token for future connections |
| 41 | saveToken(token); |
| 42 | |
| 43 | // Subscribe to all tables |
| 44 | conn |
| 45 | .subscriptionBuilder() |
| 46 | .onApplied(ctx => { |
| 47 | // Show current people |
| 48 | const people = [...ctx.db.person.iter()]; |
| 49 | console.log(`\nCurrent people (${people.length}):`); |
| 50 | if (people.length === 0) { |
| 51 | console.log(' (none yet)'); |
| 52 | } else { |
| 53 | for (const person of people) { |
| 54 | console.log(` - ${person.name}`); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | console.log('\nPress Ctrl+C to exit'); |
| 59 | }) |
| 60 | .onError((_ctx, err) => { |
| 61 | console.error('Subscription error:', err); |
| 62 | }) |
| 63 | .subscribeToAllTables(); |
| 64 | |
| 65 | // Register callbacks for table changes |
| 66 | conn.db.person.onInsert((_ctx: EventContext, person) => { |
| 67 | console.log(`[Added] ${person.name}`); |
| 68 | }); |
| 69 | |
| 70 | conn.db.person.onDelete((_ctx: EventContext, person) => { |
| 71 | console.log(`[Removed] ${person.name}`); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | function onDisconnect(_ctx: ErrorContext, error?: Error): void { |
| 76 | if (error) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…