UNPKG

7.05 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 { 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 * A promise resolved when the layout restorer is ready to receive signals.
88 */
89 get restored(): Promise<void>;
90 /**
91 * Add a widget to be tracked by the layout restorer.
92 */
93 add(widget: Widget, name: string): void;
94 /**
95 * Fetch the layout state for the application.
96 *
97 * #### Notes
98 * Fetching the layout relies on all widget restoration to be complete, so
99 * calls to `fetch` are guaranteed to return after restoration is complete.
100 */
101 fetch(): Promise<ILabShell.ILayout>;
102 /**
103 * Restore the widgets of a particular widget tracker.
104 *
105 * @param tracker - The widget tracker whose widgets will be restored.
106 *
107 * @param options - The restoration options.
108 */
109 restore(tracker: WidgetTracker, options: IRestorer.IOptions<Widget>): Promise<any>;
110 /**
111 * Save the layout state for the application.
112 */
113 save(data: ILabShell.ILayout): Promise<void>;
114 /**
115 * Dehydrate a main area description into a serializable object.
116 */
117 private _dehydrateMainArea;
118 /**
119 * Reydrate a serialized main area description object.
120 *
121 * #### Notes
122 * This function consumes data that can become corrupted, so it uses type
123 * coercion to guarantee the dehydrated object is safely processed.
124 */
125 private _rehydrateMainArea;
126 /**
127 * Dehydrate a down area description into a serializable object.
128 */
129 private _dehydrateDownArea;
130 /**
131 * Reydrate a serialized side area description object.
132 *
133 * #### Notes
134 * This function consumes data that can become corrupted, so it uses type
135 * coercion to guarantee the dehydrated object is safely processed.
136 */
137 private _rehydrateDownArea;
138 /**
139 * Dehydrate a side area description into a serializable object.
140 */
141 private _dehydrateSideArea;
142 /**
143 * Reydrate a serialized side area description object.
144 *
145 * #### Notes
146 * This function consumes data that can become corrupted, so it uses type
147 * coercion to guarantee the dehydrated object is safely processed.
148 */
149 private _rehydrateSideArea;
150 /**
151 * Handle a widget disposal.
152 */
153 private _onWidgetDisposed;
154 private _connector;
155 private _first;
156 private _firstDone;
157 private _promisesDone;
158 private _promises;
159 private _restored;
160 private _registry;
161 private _trackers;
162 private _widgets;
163}
164/**
165 * A namespace for `LayoutRestorer` statics.
166 */
167export declare namespace LayoutRestorer {
168 /**
169 * The configuration options for layout restorer instantiation.
170 */
171 interface IOptions {
172 /**
173 * The data connector used for layout saving and fetching.
174 */
175 connector: IDataConnector<ReadonlyPartialJSONValue>;
176 /**
177 * The initial promise that has to be resolved before restoration.
178 *
179 * #### Notes
180 * This promise should equal the JupyterLab application `started` notifier.
181 */
182 first: Promise<any>;
183 /**
184 * The application command registry.
185 */
186 registry: CommandRegistry;
187 }
188}