Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new using syntax so we don't have to use a callback for withSession #2144

Open
2 tasks
sairanjit opened this issue Jan 7, 2025 · 0 comments
Open
2 tasks

Comments

@sairanjit
Copy link
Contributor

This change simplifies session management by automatically handling cleanup with Symbol.asyncDispose https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management

Before

public async withSession<Return>(callback: (session: Session) => Return): Promise<Awaited<Return>> {
    let session: Session | undefined = undefined
    try {
      session = await this.store.session(this.profile).open()

      const result = await callback(session)

      return result
    } finally {
      if (session?.handle) {
        await session.close()
      }
    }
  }

After

public async getSession() {
    const session = await this.store.session(this.profile).open()
    return {
      session,
      [Symbol.asyncDispose]: async () => {
        if (session?.handle) {
          await session.close()
        }
      },
    }
  }

And usage of the session will also be changed from
Before

await agentContext.wallet.withSession((session) =>
  session.insert({ category: record.type, name: record.id, value, tags })
)

After

await using session = await agentContext.wallet.getSession()

await session.session.insert({ category: record.type, name: record.id, value, tags })

Some initial changes that are required to start using the using keyword are :

  • Update prettier to v3 and also dependent packages
  • Update tsconfig with "lib": ["ESNext.Disposable"]

@TimoGlastra Do you think any more changes are required or this would be a good start ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant