1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
18 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
19 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
20 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
21 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
22 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.WidgetManager = exports.WidgetFactory = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const widgets_1 = require("@phosphor/widgets");
|
30 | const common_1 = require("../common");
|
31 | const stableJsonStringify = require("fast-json-stable-stringify");
|
32 |
|
33 | exports.WidgetFactory = Symbol('WidgetFactory');
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | let WidgetManager = class WidgetManager {
|
40 | constructor() {
|
41 | this.widgets = new Map();
|
42 | this.pendingWidgetPromises = new Map();
|
43 | this.onWillCreateWidgetEmitter = new common_1.Emitter();
|
44 | |
45 |
|
46 |
|
47 |
|
48 | this.onWillCreateWidget = this.onWillCreateWidgetEmitter.event;
|
49 | this.onDidCreateWidgetEmitter = new common_1.Emitter();
|
50 | this.onDidCreateWidget = this.onDidCreateWidgetEmitter.event;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | getWidgets(factoryId) {
|
59 | const result = [];
|
60 | for (const [key, widget] of this.widgets.entries()) {
|
61 | if (this.fromKey(key).factoryId === factoryId) {
|
62 | result.push(widget);
|
63 | }
|
64 | }
|
65 | return result;
|
66 | }
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | tryGetWidget(factoryId, options) {
|
78 | const key = this.toKey({ factoryId, options });
|
79 | const existing = this.widgets.get(key);
|
80 | if (existing instanceof widgets_1.Widget) {
|
81 | return existing;
|
82 | }
|
83 | return undefined;
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | tryGetPendingWidget(factoryId, options) {
|
93 | const key = this.toKey({ factoryId, options });
|
94 | return this.doGetWidget(key);
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | async getWidget(factoryId, options) {
|
104 | const key = this.toKey({ factoryId, options });
|
105 | const pendingWidget = this.doGetWidget(key);
|
106 | const widget = pendingWidget && await pendingWidget;
|
107 | return widget;
|
108 | }
|
109 | doGetWidget(key) {
|
110 | var _a;
|
111 | const pendingWidget = (_a = this.widgets.get(key)) !== null && _a !== void 0 ? _a : this.pendingWidgetPromises.get(key);
|
112 | if (pendingWidget) {
|
113 | return pendingWidget;
|
114 | }
|
115 | return undefined;
|
116 | }
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | async getOrCreateWidget(factoryId, options) {
|
125 | const key = this.toKey({ factoryId, options });
|
126 | const existingWidget = this.doGetWidget(key);
|
127 | if (existingWidget) {
|
128 | return existingWidget;
|
129 | }
|
130 | const factory = this.factories.get(factoryId);
|
131 | if (!factory) {
|
132 | throw Error("No widget factory '" + factoryId + "' has been registered.");
|
133 | }
|
134 | try {
|
135 | const widgetPromise = factory.createWidget(options);
|
136 | this.pendingWidgetPromises.set(key, widgetPromise);
|
137 | const widget = await widgetPromise;
|
138 | await common_1.WaitUntilEvent.fire(this.onWillCreateWidgetEmitter, { factoryId, widget });
|
139 | this.widgets.set(key, widget);
|
140 | widget.disposed.connect(() => this.widgets.delete(key));
|
141 | this.onDidCreateWidgetEmitter.fire({ factoryId, widget });
|
142 | return widget;
|
143 | }
|
144 | finally {
|
145 | this.pendingWidgetPromises.delete(key);
|
146 | }
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | getDescription(widget) {
|
155 | for (const [key, aWidget] of this.widgets.entries()) {
|
156 | if (aWidget === widget) {
|
157 | return this.fromKey(key);
|
158 | }
|
159 | }
|
160 | return undefined;
|
161 | }
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | toKey(options) {
|
169 | return stableJsonStringify(options);
|
170 | }
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | fromKey(key) {
|
178 | return JSON.parse(key);
|
179 | }
|
180 | get factories() {
|
181 | if (!this._cachedFactories) {
|
182 | this._cachedFactories = new Map();
|
183 | for (const factory of this.factoryProvider.getContributions()) {
|
184 | if (factory.id) {
|
185 | this._cachedFactories.set(factory.id, factory);
|
186 | }
|
187 | else {
|
188 | this.logger.error('Invalid ID for factory: ' + factory + ". ID was: '" + factory.id + "'.");
|
189 | }
|
190 | }
|
191 | }
|
192 | return this._cachedFactories;
|
193 | }
|
194 | };
|
195 | __decorate([
|
196 | (0, inversify_1.inject)(common_1.ContributionProvider),
|
197 | (0, inversify_1.named)(exports.WidgetFactory),
|
198 | __metadata("design:type", Object)
|
199 | ], WidgetManager.prototype, "factoryProvider", void 0);
|
200 | __decorate([
|
201 | (0, inversify_1.inject)(common_1.ILogger),
|
202 | __metadata("design:type", Object)
|
203 | ], WidgetManager.prototype, "logger", void 0);
|
204 | WidgetManager = __decorate([
|
205 | (0, inversify_1.injectable)()
|
206 | ], WidgetManager);
|
207 | exports.WidgetManager = WidgetManager;
|
208 |
|
\ | No newline at end of file |