generated from matrix-org/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
32 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package rust | ||
|
||
// This is a recurring pattern in the ffi bindings, where a param is an interface | ||
// which has a single OnUpdate(T) method. Rather than having many specific impls, | ||
// just have a generic one which can be listened to via a channel. Using a channel | ||
// means we can time out more easily using select {}. | ||
type genericStateListener[T any] struct { | ||
ch chan T | ||
isClosed bool | ||
} | ||
|
||
func newGenericStateListener[T any]() *genericStateListener[T] { | ||
return &genericStateListener[T]{ | ||
ch: make(chan T), | ||
} | ||
} | ||
|
||
func (l *genericStateListener[T]) Close() { | ||
l.isClosed = true | ||
close(l.ch) | ||
} | ||
|
||
func (l *genericStateListener[T]) OnUpdate(state T) { | ||
if l.isClosed { | ||
return | ||
} | ||
l.ch <- state | ||
} |
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