File size: 2,224 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
package instance

import (
	"github.com/pinchtab/pinchtab/internal/allocation"
	"github.com/pinchtab/pinchtab/internal/bridge"
)

// Manager is the InstanceManager facade.
// It composes Repository, Locator, and Allocator —
// delegating all calls, owning zero business logic.
type Manager struct {
	Repo      *Repository
	Locator   *Locator
	Allocator *Allocator
}

// NewManager creates a fully wired Manager.
func NewManager(launcher InstanceLauncher, fetcher TabFetcher, policy allocation.Policy) *Manager {
	if policy == nil {
		policy = &allocation.FCFS{}
	}

	repo := NewRepository(launcher)
	locator := NewLocator(repo, fetcher)
	allocator := NewAllocator(repo, policy)

	return &Manager{
		Repo:      repo,
		Locator:   locator,
		Allocator: allocator,
	}
}

// --- Lifecycle (delegates to Repository) ---

// Launch starts a new instance.
func (m *Manager) Launch(name, port string, headless bool) (*bridge.Instance, error) {
	return m.Repo.Launch(name, port, headless)
}

// Stop terminates an instance and invalidates its tab cache.
func (m *Manager) Stop(id string) error {
	m.Locator.InvalidateInstance(id)
	return m.Repo.Stop(id)
}

// List returns all tracked instances.
func (m *Manager) List() []bridge.Instance {
	return m.Repo.List()
}

// Get returns an instance by ID.
func (m *Manager) Get(id string) (*bridge.Instance, bool) {
	return m.Repo.Get(id)
}

// Running returns only running instances.
func (m *Manager) Running() []bridge.Instance {
	return m.Repo.Running()
}

// --- Discovery (delegates to Locator) ---

// FindInstanceByTabID returns the instance that owns a tab.
func (m *Manager) FindInstanceByTabID(tabID string) (*bridge.Instance, error) {
	return m.Locator.FindInstanceByTabID(tabID)
}

// RegisterTab adds a tab→instance mapping to the cache.
func (m *Manager) RegisterTab(tabID, instanceID string) {
	m.Locator.Register(tabID, instanceID)
}

// InvalidateTab removes a tab from the cache.
func (m *Manager) InvalidateTab(tabID string) {
	m.Locator.Invalidate(tabID)
}

// --- Allocation (delegates to Allocator) ---

// Allocate selects a running instance using the configured policy.
func (m *Manager) Allocate() (*bridge.Instance, error) {
	return m.Allocator.Allocate()
}