UNPKG

7.73 kBTypeScriptView Raw
1import { WidgetTracker } from '@jupyterlab/apputils';
2import { IDataConnector, IRestorer } from '@jupyterlab/statedb';
3import { CommandRegistry } from '@lumino/commands';
4import { ReadonlyPartialJSONValue, Token } from '@lumino/coreutils';
5import { DockPanel, Widget } from '@lumino/widgets';
6import { ILabShell } from './shell';
7/**
8 * The layout restorer token.
9 */
10export declare const ILayoutRestorer: Token<ILayoutRestorer>;
11/**
12 * A static class that restores the widgets of the application when it reloads.
13 */
14export interface ILayoutRestorer extends IRestorer {
15 /**
16 * A promise resolved when the layout restorer is ready to receive signals.
17 */
18 restored: Promise<void>;
19 /**
20 * Add a widget to be tracked by the layout restorer.
21 */
22 add(widget: Widget, name: string): void;
23 /**
24 * Restore the widgets of a particular widget tracker.
25 *
26 * @param tracker - The widget tracker whose widgets will be restored.
27 *
28 * @param options - The restoration options.
29 */
30 restore<T extends Widget>(tracker: WidgetTracker<T>, options: IRestorer.IOptions<T>): Promise<any>;
31}
32/**
33 * The default implementation of a layout restorer.
34 *
35 * #### Notes
36 * The lifecycle for state restoration is subtle. The sequence of events is:
37 *
38 * 1. The layout restorer plugin is instantiated and makes a `fetch` call to
39 * the data connector that stores the layout restoration data. The `fetch`
40 * call returns a promise that resolves in step 6, below.
41 *
42 * 2. Other plugins that care about state restoration require the layout
43 * restorer as a dependency.
44 *
45 * 3. As each load-time plugin initializes (which happens before the front-end
46 * application has `started`), it instructs the layout restorer whether
47 * the restorer ought to `restore` its widgets by passing in its widget
48 * tracker.
49 * Alternatively, a plugin that does not require its own widget tracker
50 * (because perhaps it only creates a single widget, like a command palette),
51 * can simply `add` its widget along with a persistent unique name to the
52 * layout restorer so that its layout state can be restored when the lab
53 * application restores.
54 *
55 * 4. After all the load-time plugins have finished initializing, the front-end
56 * application `started` promise will resolve. This is the `first`
57 * promise that the layout restorer waits for. By this point, all of the
58 * plugins that care about restoration will have instructed the layout
59 * restorer to `restore` their widget trackers.
60 *
61 * 5. The layout restorer will then instruct each plugin's widget tracker
62 * to restore its state and reinstantiate whichever widgets it wants. The
63 * tracker returns a promise to the layout restorer that resolves when it
64 * has completed restoring the tracked widgets it cares about.
65 *
66 * 6. As each widget tracker finishes restoring the widget instances it cares
67 * about, it resolves the promise that was returned to the layout restorer
68 * (in step 5). After all of the promises that the restorer is awaiting have
69 * settled, the restorer then resolves the outstanding `fetch` promise
70 * (from step 1) and hands off a layout state object to the application
71 * shell's `restoreLayout` method for restoration.
72 *
73 * 7. Once the application shell has finished restoring the layout, the
74 * JupyterLab application's `restored` promise is resolved.
75 *
76 * Of particular note are steps 5 and 6: since data restoration of plugins
77 * is accomplished by executing commands, the command that is used to restore
78 * the data of each plugin must return a promise that only resolves when the
79 * widget has been created and added to the plugin's widget tracker.
80 */
81export declare class LayoutRestorer implements ILayoutRestorer {
82 /**
83 * Create a layout restorer.
84 */
85 constructor(options: LayoutRestorer.IOptions);
86 /**
87 * Whether full layout restoration is deferred and is currently incomplete.
88 *
89 * #### Notes
90 * This flag is useful for tracking when the application has started in
91 * 'single-document' mode and the main area has not yet been restored.
92 */
93 get isDeferred(): boolean;
94 /**
95 * A promise resolved when the layout restorer is ready to receive signals.
96 */
97 get restored(): Promise<void>;
98 /**
99 * Add a widget to be tracked by the layout restorer.
100 */
101 add(widget: Widget, name: string): void;
102 /**
103 * Fetch the layout state for the application.
104 *
105 * #### Notes
106 * Fetching the layout relies on all widget restoration to be complete, so
107 * calls to `fetch` are guaranteed to return after restoration is complete.
108 */
109 fetch(): Promise<ILabShell.ILayout>;
110 /**
111 * Restore the widgets of a particular widget tracker.
112 *
113 * @param tracker - The widget tracker whose widgets will be restored.
114 *
115 * @param options - The restoration options.
116 */
117 restore(tracker: WidgetTracker, options: IRestorer.IOptions<Widget>): Promise<any>;
118 /**
119 * Restore the application layout if its restoration has been deferred.
120 *
121 * @returns - the rehydrated main area.
122 */
123 restoreDeferred(): Promise<ILabShell.IMainArea | null>;
124 /**
125 * Save the layout state for the application.
126 */
127 save(layout: ILabShell.ILayout): Promise<void>;
128 /**
129 * Dehydrate a main area description into a serializable object.
130 */
131 private _dehydrateMainArea;
132 /**
133 * Rehydrate a serialized main area description object.
134 *
135 * #### Notes
136 * This function consumes data that can become corrupted, so it uses type
137 * coercion to guarantee the dehydrated object is safely processed.
138 */
139 private _rehydrateMainArea;
140 /**
141 * Dehydrate a down area description into a serializable object.
142 */
143 private _dehydrateDownArea;
144 /**
145 * Rehydrate a serialized side area description object.
146 *
147 * #### Notes
148 * This function consumes data that can become corrupted, so it uses type
149 * coercion to guarantee the dehydrated object is safely processed.
150 */
151 private _rehydrateDownArea;
152 /**
153 * Dehydrate a side area description into a serializable object.
154 */
155 private _dehydrateSideArea;
156 /**
157 * Rehydrate a serialized side area description object.
158 *
159 * #### Notes
160 * This function consumes data that can become corrupted, so it uses type
161 * coercion to guarantee the dehydrated object is safely processed.
162 */
163 private _rehydrateSideArea;
164 /**
165 * Handle a widget disposal.
166 */
167 private _onWidgetDisposed;
168 private _connector;
169 private _deferred;
170 private _deferredMainArea?;
171 private _first;
172 private _firstDone;
173 private _promisesDone;
174 private _promises;
175 private _restored;
176 private _registry;
177 private _trackers;
178 private _widgets;
179 private _mode;
180}
181/**
182 * A namespace for `LayoutRestorer` statics.
183 */
184export declare namespace LayoutRestorer {
185 /**
186 * The configuration options for layout restorer instantiation.
187 */
188 interface IOptions {
189 /**
190 * The data connector used for layout saving and fetching.
191 */
192 connector: IDataConnector<ReadonlyPartialJSONValue>;
193 /**
194 * The initial promise that has to be resolved before restoration.
195 *
196 * #### Notes
197 * This promise should equal the JupyterLab application `started` notifier.
198 */
199 first: Promise<any>;
200 /**
201 * The application command registry.
202 */
203 registry: CommandRegistry;
204 /**
205 * The DockPanel mode.
206 */
207 mode?: DockPanel.Mode;
208 }
209}