跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

keepawake.go

50 行 · 1.2 KB历史下载
1// Package keepawake holds a host power request without keeping its display on.
2package keepawake
3
4import (
5 "context"
6 "time"
7)
8
9type State struct {
10 Active bool `json:"active"`
11 Error string `json:"error,omitempty"`
12}
13
14// Run owns the request until ctx ends, including time between phone connections.
15// A failed or unexpectedly exited helper is reported and retried. The caller
16// must wait for Run to return when shutting down its hosting service.
17func Run(ctx context.Context, report func(State)) {
18 run(ctx, report, hold)
19}
20
21func run(ctx context.Context, report func(State), acquire func(context.Context, func()) error) {
22 var previous State
23 emit := func(state State) {
24 if state != previous {
25 previous = state
26 report(state)
27 }
28 }
29 delay := time.Second
30 defer emit(State{})
31 for ctx.Err() == nil {
32 err := acquire(ctx, func() { emit(State{Active: true}); delay = time.Second })
33 if ctx.Err() != nil {
34 return
35 }
36 message := "主机防休眠请求已结束"
37 if err != nil {
38 message = err.Error()
39 }
40 emit(State{Error: message})
41 timer := time.NewTimer(delay)
42 select {
43 case <-ctx.Done():
44 timer.Stop()
45 return
46 case <-timer.C:
47 }
48 delay = min(30*time.Second, delay*2)
49 }
50}