mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
test&doc: add example and update doc for function package
This commit is contained in:
@@ -83,7 +83,7 @@ func Delay(delay time.Duration, fn any, args ...any) {
|
||||
unsafeInvokeFunc(fn, args...)
|
||||
}
|
||||
|
||||
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked..
|
||||
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
|
||||
// Play: https://go.dev/play/p/absuEGB_GN7
|
||||
func Debounced(fn func(), duration time.Duration) func() {
|
||||
// Catch programming error while constructing the closure
|
||||
|
||||
@@ -3,12 +3,18 @@ package function
|
||||
import "time"
|
||||
|
||||
// Watcher is used for record code excution time
|
||||
// Play: Todo
|
||||
type Watcher struct {
|
||||
startTime int64
|
||||
stopTime int64
|
||||
excuting bool
|
||||
}
|
||||
|
||||
// Start the watch timer.
|
||||
func NewWatcher() *Watcher {
|
||||
return &Watcher{}
|
||||
}
|
||||
|
||||
// Start the watch timer.
|
||||
func (w *Watcher) Start() {
|
||||
w.startTime = time.Now().UnixNano()
|
||||
|
||||
22
function/watcher_example_test.go
Normal file
22
function/watcher_example_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package function
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleWatcher() {
|
||||
w := NewWatcher()
|
||||
|
||||
w.Start()
|
||||
|
||||
longRunningTask()
|
||||
|
||||
w.Stop()
|
||||
|
||||
// eapsedTime := w.GetElapsedTime().Milliseconds()
|
||||
|
||||
fmt.Println("foo")
|
||||
|
||||
w.Reset()
|
||||
|
||||
// Output:
|
||||
// foo
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func TestWatcher(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestWatcher")
|
||||
|
||||
w := &Watcher{}
|
||||
w := NewWatcher()
|
||||
w.Start()
|
||||
|
||||
longRunningTask()
|
||||
|
||||
Reference in New Issue
Block a user