File size: 2,839 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { ServicesAccessor } from '#/_base/di/instantiation';
import type { IDisposable } from '#/_base/di/lifecycle';
import type { ISessionScopeHandle } from '#/_base/di/scope';
import { ISessionIndex } from '#/app/sessionIndex/sessionIndex';
import { ISessionManager, type ISessionManager as SessionManager } from '#/app/sessionManager/sessionManager';
import { ITelemetryService } from '#/app/telemetry/telemetry';
import { isError2 } from '#/errors';
import type { Program } from '#/program/program';
import { ISessionContext } from '#/session/sessionContext/sessionContext';
import type { ResumeSessionOptions } from '#/workspace/sessionLifecycle/sessionLifecycle';
import { IWorkspaceInstanceManager } from '#/workspace/workspaceInstance/workspaceInstanceManager';

export async function programForSession(
  accessor: ServicesAccessor,
  sessionId: string,
): Promise<Program | undefined> {
  const manager = accessor.get(ISessionManager);
  const live = manager.get(sessionId);
  if (live !== undefined) {
    const workspaceId = live.accessor.get(ISessionContext).workspaceId;
    return accessor.get(IWorkspaceInstanceManager).get(workspaceId)?.program;
  }
  const summary = await accessor.get(ISessionIndex).get(sessionId);
  if (summary === undefined) return undefined;
  const workspace = await accessor.get(IWorkspaceInstanceManager).getOrCreate({
    workspaceId: summary.workspaceId,
    root: summary.cwd,
  });
  return workspace.program;
}

export async function resumeSessionById(
  accessor: ServicesAccessor,
  sessionId: string,
  opts?: ResumeSessionOptions,
): Promise<ISessionScopeHandle | undefined> {
  try {
    return await accessor.get(ISessionManager).resume(sessionId, opts);
  } catch (error) {
    accessor
      .get(ITelemetryService)
      .withContext({ session_id: sessionId })
      .track2('session_load_failed', {
        reason: isError2(error) ? error.code : error instanceof Error ? error.name : 'unknown',
      });
    throw error;
  }
}

export function getLiveSessionById(
  accessor: ServicesAccessor,
  sessionId: string,
): ISessionScopeHandle | undefined {
  return accessor.get(ISessionManager).get(sessionId);
}

export async function closeSessionById(
  accessor: ServicesAccessor,
  sessionId: string,
): Promise<void> {
  await accessor.get(ISessionManager).close(sessionId);
}

type SessionLifecycleEvents = Required<
  Pick<SessionManager, 'onDidCloseSession' | 'onDidArchiveSession'>
>;

export function followSessionLifecycles(
  accessor: ServicesAccessor,
  follow: (service: SessionLifecycleEvents) => IDisposable,
): IDisposable {
  const manager = accessor.get(ISessionManager);
  if (manager.onDidCloseSession === undefined || manager.onDidArchiveSession === undefined) {
    return { dispose: () => {} };
  }
  return follow(manager as SessionLifecycleEvents);
}