Files
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

120 lines
4.0 KiB
Go

package code
import (
"bytes"
"errors"
)
// These payload limits are independent of the text editor's history limit.
// History accounting deliberately counts shared snapshots more than once; it
// does not include allocator rounding, text history or other process memory.
const (
maxVolatileBytes = 4 << 20
maxVolatileHistoryBytes = 64 << 20
)
type volatileState struct {
current []byte
budget int64
}
// AttachVolatile enables one initially empty, in-memory state alongside this
// text journal. It is not serialized by Bytes or MarkSaved. The document owner
// must guard its save, reload, close and recovery routes while it is dirty.
// This is owner-thread state, not a synchronization mechanism or a second log.
func (d *Doc) AttachVolatile() error {
if d == nil || d.volatile != nil {
return errors.New("volatile document state is already attached or unavailable")
}
d.volatile = &volatileState{budget: maxVolatileHistoryBytes}
return nil
}
// Volatile returns a private copy. A nil value with attached=true is the clean
// initial state; nil and empty payloads are treated as equivalent.
func (d *Doc) Volatile() (value []byte, attached bool) {
if d == nil || d.volatile == nil {
return nil, false
}
return bytes.Clone(d.volatile.current), true
}
// VolatileDirty remains true until the in-memory state returns to its empty
// initial value. MarkSaved and SetSaved acknowledge text only.
func (d *Doc) VolatileDirty() bool {
return d != nil && d.volatile != nil && len(d.volatile.current) != 0
}
// HasVolatileHistory also reports structured states reachable through Undo or
// Redo when the current value is empty. A text-only recovery cannot preserve
// those states. This is owner-thread inspection, not validation or a save guard.
func (d *Doc) HasVolatileHistory() bool {
if d == nil || d.volatile == nil {
return false
}
if d.VolatileDirty() {
return true
}
for _, groups := range [][]group{d.undo, d.redo} {
for _, g := range groups {
if g.volatile {
return true
}
}
}
return false
}
// ReplaceVolatile creates one undo step in the text document's existing log.
// expectedRev guards both text and volatile changes since the caller captured
// its snapshot. It does not check external files or grant permission to save.
// Oversized input or retained history is rejected without changing either log.
func (d *Doc) ReplaceVolatile(expectedRev uint64, value []byte) (bool, error) {
if d == nil || d.volatile == nil {
return false, errors.New("volatile document state is not attached")
}
if d.rev != expectedRev {
return false, errors.New("document changed before volatile edit")
}
if len(value) > maxVolatileBytes {
return false, errors.New("volatile document state exceeds its size budget")
}
if bytes.Equal(d.volatile.current, value) {
return false, nil
}
if d.rev == ^uint64(0) {
return false, errors.New("document revision exhausted")
}
// Redo entries are about to be released. Counting each retained reference,
// rather than deduplicating backing arrays, gives a conservative upper bound.
used := int64(len(d.volatile.current))
for _, g := range d.undo {
if g.volatile {
used += int64(len(g.volatileBefore)) + int64(len(g.volatileAfter))
if used > d.volatile.budget {
return false, errors.New("volatile document history exceeds its memory budget")
}
}
}
needed := int64(len(d.volatile.current)) + 2*int64(len(value))
if used > d.volatile.budget-needed {
return false, errors.New("volatile document history exceeds its memory budget")
}
next := bytes.Clone(value)
d.clearRedo()
d.seal()
d.undo = append(d.undo, group{kind: KindOther, before: d.Sel, after: d.Sel,
sealed: true, volatile: true, volatileBefore: d.volatile.current, volatileAfter: next})
d.trimUndo()
d.volatile.current = next
d.advanceVolatile()
return true, nil
}
func (d *Doc) advanceVolatile() {
d.rev++
// The text has not moved, but MapSpan's bounded revision journal must still
// contain one entry for each revision so its coverage check stays sound.
d.journal(0, 0, 0)
}