Skip to content

Cannot unsubscribe queries #154

Open
Open
@puttehi

Description

@puttehi

Hey, I have been learning and playing around with TSDB and found either a big oversight in the SDK, or a big knowledge gap in my head.

SubscriptionHandleImpl.unsubscribe() calls a non-existent function on <handle>.sub that throws and crashes the program: https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/subscription_builder_impl.ts#L199C5-L199C43

Example code:

const subscribeTo = (queries) => {
        const previousHandle = this._handle
        this._handle = SpacetimeDB.subscribeTo(queries)

        if (!previousHandle?.isActive()) {
            return
        }

        console.debug(`Cancelling previous subscription (active: ${previousHandle.isActive()})`)

        previousHandle.unsubscribe() // Crash
}

// EDIT: `SpacetimeDB.subscribeTo(queries)` is a custom implementation pasted below:
public static subscribeTo(queries: string[], onAppliedCallback?: OnApplied | OnApplied[]) {
    // Handle undefined case
    onAppliedCallback = onAppliedCallback ?? []
    // Force to array
    const cbs = Array.isArray(onAppliedCallback) ? onAppliedCallback : [onAppliedCallback]

    console.log(`Subscribing to a group of ${queries.length} queries...`)
    const builder = SpacetimeDB.conn
        .subscriptionBuilder()
        .onApplied((_ctx) => {
            console.log(`Subscribed to a group of ${queries.length} queries!`)
        })

    for (const cb of cbs) {
        builder.onApplied(cb)
    }

    return builder.subscribe(queries);
};

Which throws:

bundle.min.js:90399 Uncaught TypeError: this.db.unsubscribe is not a function
    at SubscriptionHandleImpl.unsubscribe (bundle.min.js:90399:29)
    at subscribeTo ...
    ...

Debug view of SubscriptionHandleImpl.db:

Image

The alternative callback version SubscriptionHandleImpl.unsubscribeThen() does not seem to unsubscribe (..?), or it is somehow hidden in the event system ("end"?) and not clear from the source: https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/subscription_builder_impl.ts#L212

How to unsubscribe? Should I perhaps not worry about subscription lifetimes? Where is my misunderstanding of usage if this is intended behavior?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions