Previewing Changes#
This guide shows how to see what an edit would do before you commit it. It covers previewing a drag or a resize, keeping previews in sync with the project's baseline, checking the resource load a change would cause, and trying multi-step edits or a leveling run on a copy of the project.
How previews work#
A solved Project holds one accepted baseline: the dates from its last solve or level. Every preview starts from that baseline.
Project#preview(changes, options) copies the baseline, applies a SchedulingChanges value to the copy and schedules it. When the call returns, the copy is gone. The project stays as it was, so you can call preview as often as the UI needs, for example on every mouse move during a drag.
You get back a regular ScheduleResult:
updatesare relative to the baseline. They list the tasks the change would move and the tasks the change itself touched, the same way thesolve(null, โฆ)after anapplyreports them. An empty change set gives emptyupdates.conflicts,cyclesandnotesdescribe the previewed state. A preview can show the cycle a new link would create, and the project never holds that cycle.baselineIdnames the baseline the preview read.
A preview differs from apply in two ways. It isn't partial: if any operation in the change set is invalid, the whole call throws InputException for the first rejected operation, with its path and message. It also needs a clean, solved baseline. Before the first solve, and after any apply until the next solve, it throws ProjectException with code NO_BASELINE.
For a change that takes more than one step, or for a level run, use Project#copy() instead. A copy is O(1): it shares state with the original, and the first edit on either side gives that side its own data. Edit, solve or level the copy, and the original stays as it was.
| You want to see | Use |
|---|---|
| The effect of one change set (a drag, a resize, a new link) | preview |
| The load a change would put on resources | preview with PreviewOptions.profile |
| Several edits in a row, each solved | copy(), then apply + solve(null, โฆ) on the copy |
| What leveling would do | copy(), then level on the copy |
Previewing an edit#
Build the edit as a SchedulingChanges, the same value you'd pass to apply, and pass it to preview.
;
;
The solve puts b right after a, on January 3. The preview prints task b: 2026-01-05 00:00 .. 2026-01-06 00:00. The project doesn't change: the next solve(null, โฆ) returns the cached result of the first solve, the same object, so the last line prints unchanged: true.
When the user drops the task, send the same SchedulingChanges to apply and then call solve(null, โฆ). See Data Updates.
PreviewOptions takes the same diagnostic switches as SolveOptions:
withSubtree(new Subtree(id))previews only one branch.withReasons(true)adds aReasonto each update, which tells you why a task would move.withAnalytics(...)adds the critical path and slack of the previewed state.
Previews stay sparse: there's no includeUnchanged option. The full list is on the PreviewOptions reference.
Guarding with baseline tokens#
A UI that previews in the background can hit a race. Another request applies an edit or reloads the data, and the preview now runs against a baseline the user never saw. Baseline tokens catch this.
Every solve and level result carries baselineId, an opaque token for the accepted baseline. Pass it back with PreviewOptions.withBaselineId(...) (or in SchedulingChanges.baselineId). If the token no longer names the current baseline, preview throws ProjectException with code STALE_BASELINE instead of returning dates for the wrong state.
;
;
A few rules for tokens:
- Take the token from the latest
solveorlevelresult. A preview result carries the same token it read, so a chain of previews during one drag can reuse it. - The token changes whenever the data changes: new data, an
apply, a solve that moves dates, leveling edits. - Tokens are opaque strings. Don't parse them, store them across runs or compare them between processes. The same data can get a new token on a repeat run.
previewchecksPreviewOptions.baselineIdandSchedulingChanges.baselineIdseparately. Leave bothnullto skip the check.
For how to handle STALE_BASELINE and the other project errors, see Error handling.
Preview with load#
Set PreviewOptions.profile to a ProfileQuery to see the resource load a change would cause. The result then has profile, the same per-bucket load and capacity that loadProfile returns, but computed on the previewed dates. Use it to show a resource histogram while the user drags a task.
;
;
Both tasks need dev on January 2, so that bucket's load goes over its capacity of 1. With ProfileTasks.OVER, each overloaded bucket also lists the tasks that share it.
The query covers buckets that start in [from, to). By default it reports every resource, and the bucket size (basis) comes from config.durationUnit. Narrow it with withResources(...), or set withBasis(...). ProfileQuery has no baselineId of its own: the preview checks PreviewOptions.baselineId. Every preview also reports overallocations when the project has assignments, so you can skip the profile if you only need the overloaded windows. See Resource load and the ProfileQuery reference.
Copying a project#
preview takes one change set. To see the result of several edits in a row, each one solved before the next, copy the project and work on the copy.
;
;
Edits on one copy never affect the other. The copy is a full Project with its own baseline, cache and baselineId, so you can preview it, level it, evaluate against it or keep editing it. To accept the draft, replace the original with it, or replay the same changes on the original with apply. To discard it, drop the reference.
Copy before level too. level accepts the leveled dates as the project's new baseline, so running it on a copy is how you show the user a leveling proposal before committing it:
static void
The step-by-step version is in Preview leveling. To compare several alternative change sets with scores, use evaluate instead. It works on disposable copies internally. See Evaluating alternatives.
Copies also help with threads. A Project isn't synchronized, and even copy() updates sharing flags on the original. To use one project from several threads, guard it with a lock, or make one copy per thread on a single thread and hand each thread its own. See Share a project between threads.