Skip to content

Commit

Permalink
refactor(client): add AuthTransferHandler type
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 29, 2024
1 parent ed3afc0 commit 615421b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ type Client struct {
// Tracing.
tracer trace.Tracer

// onTransfer is called on authorization transfer.
onTransfer func(ctx context.Context, client *Client, tx func(ctx context.Context) error) error
// onTransfer is called in transfer.
onTransfer AuthTransferHandler
}

// NewClient creates new unstarted client.
Expand Down
10 changes: 3 additions & 7 deletions telegram/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ type Options struct {
// OpenTelemetry.
TracerProvider trace.TracerProvider

// OnTransfer is called on authorization transfer to acquire external lock.
//
// The function should call tx function to perform transfer, serializing it
// with external lock.
//
// The function must return error if tx function returned error.
OnTransfer func(ctx context.Context, c *Client, tx func(context.Context) error) error
// OnTransfer is called during authorization transfer.
// See [AuthTransferHandler] for details.
OnTransfer AuthTransferHandler
}

func (opt *Options) setDefaults() {
Expand Down
8 changes: 8 additions & 0 deletions telegram/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func (c *Client) exportAuth(ctx context.Context, dcID int) (*tg.AuthExportedAuth
return export, nil
}

// AuthTransferHandler is a function that is called during authorization transfer.
//
// The fn callback should be serialized by user id via external locking.
// You can call [Client.Self] to acquire current user id.
//
// The fn callback must return fn error if any.
type AuthTransferHandler func(ctx context.Context, client *Client, fn func(context.Context) error) error

func noopOnTransfer(ctx context.Context, _ *Client, fn func(context.Context) error) error {
return fn(ctx)
}
Expand Down

0 comments on commit 615421b

Please sign in to comment.