Documents
Legacy address for preview.2 documentation. Use the versioned page. The API is the same.
Optional app library · SDK 0.1.0-preview.2.
Track named text buffers, edits and pending loads/saves. This is a document model, not a file API: your app performs the actual authorized read or write.
From the extracted SDK folder, create a sibling app with ./lui.cmd new ../MyWorkbench --template workbench. Keep its Vendor files, lui.json, module lock and SDK pin together. See library setup.
Import require("@LuiWorkbench/Documents") after the project declares that library. An import copied into a basic starter is not enough.
Example
Section titled “Example”Replace Src/Main.luau in that prepared project with this complete file.
local Window = Instance.new("Window", {Title = "Library example", Size = UDim2.fromOffset(640, 300)})local Status = Instance.new("TextLabel", {Parent = Window, Text = "Ready", Position = UDim2.fromOffset(16, 190), Size = UDim2.fromOffset(600, 72)})local Documents = require("@LuiWorkbench/Documents")local Store = Documents.New()Store:Open("Notes", "Notes.txt", "Original")Store:SetText("Notes", "Edited")local Closed, Reason = Store:Close("Notes")Status.Text = if Closed then "Closed" else "Close refused: " .. (Reason or "Unknown")Window.Destroying:Connect(function() Store:Dispose() end)Window.Visible = trueExpected result: The status says Close refused: Dirty. Documents tracks editing state; this example does not claim to save a file.
Functions
Section titled “Functions”Documents.New(): StoreBehavior and limits
Section titled “Behavior and limits”Open returns an immutable view; an existing ID returns its current buffer. Get/List return immutable snapshots, never the mutable store. SetText increments the revision only when bytes change. Dirty compares current bytes to the last successfully saved bytes, so undoing to the saved content becomes clean. Maximum 32 documents, 65,534 UTF-8 bytes per buffer, no embedded NUL, ID 128 and label 256 bytes. Text ownership is bounded; no filesystem paths are resolved.
BeginLoad(Id) captures a clean document’s identity and revision before an
application-owned asynchronous read. CompleteLoad(Receipt, Text) installs the
text as a clean baseline while preserving its label and tab order. Pass nil for
a failed read. Empty text is a successful empty document. Completion consumes
the receipt and returns (Applied, Reason?), with Stale, Retired, Failed,
InvalidText or RevisionLimit on refusal.
Only one load may be pending per document. Loading while dirty or saving is
refused. Editing, including editing then undoing, makes a pending result stale.
Beginning a save, closing/reopening the ID, canceling the load or disposing the
store retires its receipt. CancelLoad(Receipt) reports whether it retired an
active load. The application owns and cancels its actual service request.
One BeginSave receipt per document may be pending. It captures the exact revision and immutable submitted bytes. CompleteSave accepts only the original receipt object once. Success updates the saved baseline, preserving any later edits; failure leaves it unchanged. CancelSave retires the receipt and returns whether it was pending. Cancel the associated application IO separately. Close refuses SavePending even with Discard, then refuses Dirty unless Discard is explicitly true. Close/reopen cannot reuse an earlier receipt. Move uses a final 1-based position in the global tab order. Dispose retires receipts and releases buffers; the application owns any pending service request and must cancel it separately.
All calls belong on the runtime scheduler thread. Pure model semantics can run headlessly on Windows/Linux; UI and platform qualification are separate. No native ABI, Instance reflection or global service changes are needed: exported types in each module are the declarations consumed by project analysis.
Exported type declarations — advanced reference
These types describe returned models/controllers and their options. They are not additional classes to create.
Document
Section titled “Document”export type Document = {read Id: string, read Label: string, read Text: string, read Revision: number, read Dirty: boolean}Receipt
Section titled “Receipt”export type Receipt = {read Id: string, read Revision: number, read Text: string}LoadReceipt
Section titled “LoadReceipt”export type LoadReceipt = {read Id: string, read Revision: number}export type Store = { Open: (Self: Store, Id: string, Label: string, Text: string) -> Document, Get: (Self: Store, Id: string) -> Document?, List: (Self: Store) -> {Document}, SetText: (Self: Store, Id: string, Text: string) -> Document, BeginLoad: (Self: Store, Id: string) -> LoadReceipt, CompleteLoad: (Self: Store, Token: LoadReceipt, Text: string?) -> (boolean, string?), CancelLoad: (Self: Store, Token: LoadReceipt) -> boolean, BeginSave: (Self: Store, Id: string) -> Receipt, CompleteSave: (Self: Store, Token: Receipt, Succeeded: boolean) -> (boolean, string?), CancelSave: (Self: Store, Token: Receipt) -> boolean, Close: (Self: Store, Id: string, Discard: boolean?) -> (boolean, string?), Move: (Self: Store, Id: string, Index: number) -> (), Dispose: (Self: Store) -> (),}Deeper implementation reading
Section titled “Deeper implementation reading”The library source is useful when extending the framework or understanding cleanup. Private support modules such as Scope/State are implementation details, not additional supported app APIs.