-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarker.go
More file actions
59 lines (49 loc) · 1.45 KB
/
marker.go
File metadata and controls
59 lines (49 loc) · 1.45 KB
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
package xterm
// Ported from xterm.js src/common/buffer/Marker.ts.
import "sync/atomic"
// markerNextID is the global auto-incrementing marker ID counter.
var markerNextID int64
// Marker represents a line position in the buffer that is tracked across
// scrollback trimming, insertions, and deletions.
type Marker struct {
id int64
Line int
IsDisposed bool
disposables []Disposable
onDisposeEmitter EventEmitter[struct{}]
}
// NewMarker creates a marker at the given line.
func NewMarker(line int) *Marker {
return &Marker{
id: atomic.AddInt64(&markerNextID, 1),
Line: line,
}
}
// ID returns the unique marker identifier.
func (m *Marker) ID() int64 {
return m.id
}
// OnDispose registers a listener called when the marker is disposed.
func (m *Marker) OnDispose(listener func(struct{})) Disposable {
return m.onDisposeEmitter.Event(listener)
}
// Register adds a disposable to be cleaned up when the marker is disposed.
// Returns the disposable for chaining.
func (m *Marker) Register(d Disposable) Disposable {
m.disposables = append(m.disposables, d)
return d
}
// Dispose disposes the marker, firing OnDispose and cleaning up all registered disposables.
func (m *Marker) Dispose() {
if m.IsDisposed {
return
}
m.IsDisposed = true
m.Line = -1
// Fire before disposing registered disposables so listeners can react.
m.onDisposeEmitter.Fire(struct{}{})
for _, d := range m.disposables {
d.Dispose()
}
m.disposables = nil
}