Skip to content

Commit fcdb370

Browse files
authored
Add a promise delegate while saving, and save on input event (#6)
1 parent ce9efd3 commit fcdb370

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/manager.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PromiseDelegate } from '@lumino/coreutils';
2+
13
import {
24
ISecret,
35
ISecretsConnector,
@@ -26,6 +28,12 @@ export class SecretsManager implements ISecretsManager {
2628
*/
2729
constructor(options: SecretsManager.IOptions) {
2830
this._connector = options.connector;
31+
this._ready = new PromiseDelegate<void>();
32+
this._ready.resolve();
33+
}
34+
35+
get ready(): Promise<void> {
36+
return this._ready.promise;
2937
}
3038

3139
/**
@@ -49,6 +57,7 @@ export class SecretsManager implements ISecretsManager {
4957
if (!this._connector.list) {
5058
return;
5159
}
60+
await this._ready.promise;
5261
return await this._connector.list(namespace);
5362
}
5463

@@ -85,15 +94,15 @@ export class SecretsManager implements ISecretsManager {
8594
// Fill the password if the input is empty and a value is fetched by the data
8695
// connector.
8796
input.value = secret.value;
88-
input.dispatchEvent(new Event('change'));
97+
input.dispatchEvent(new Event('input'));
8998
if (callback) {
9099
callback(secret.value);
91100
}
92101
} else if (input.value && input.value !== secret?.value) {
93102
// Otherwise save the current input value using the data connector.
94103
this._set(attachedId, { namespace, id, value: input.value });
95104
}
96-
input.addEventListener('change', this._onchange);
105+
input.addEventListener('input', this._onInput);
97106
}
98107

99108
/**
@@ -121,6 +130,7 @@ export class SecretsManager implements ISecretsManager {
121130
if (!this._connector.fetch) {
122131
return;
123132
}
133+
await this._ready.promise;
124134
return this._connector.fetch(id);
125135
}
126136

@@ -144,20 +154,23 @@ export class SecretsManager implements ISecretsManager {
144154
this._connector.remove(id);
145155
}
146156

147-
/**
148-
* Function triggered when the input value changed.
149-
*/
150-
private _onchange = (e: Event): void => {
157+
private _onInput = async (e: Event): Promise<void> => {
158+
// Wait for an hypothetic current password saving.
159+
await this._ready.promise;
160+
// Reset the ready status.
161+
this._ready = new PromiseDelegate<void>();
151162
const target = e.target as HTMLInputElement;
152163
const attachedId = target.dataset.secretsId;
153164
if (attachedId) {
154165
const splitId = attachedId.split(':');
155166
const namespace = splitId.shift();
156167
const id = splitId.join(':');
157168
if (namespace && id) {
158-
this._set(attachedId, { namespace, id, value: target.value });
169+
await this._set(attachedId, { namespace, id, value: target.value });
159170
}
160171
}
172+
// resolve the ready status.
173+
this._ready.resolve();
161174
};
162175

163176
/**
@@ -166,13 +179,14 @@ export class SecretsManager implements ISecretsManager {
166179
private _detach(attachedId: string): void {
167180
const input = this._attachedInputs.get(attachedId);
168181
if (input) {
169-
input.removeEventListener('change', this._onchange);
182+
input.removeEventListener('input', this._onInput);
170183
}
171184
this._attachedInputs.delete(attachedId);
172185
}
173186

174187
private _connector: ISecretsConnector;
175188
private _attachedInputs = new Map<string, HTMLInputElement>();
189+
private _ready: PromiseDelegate<void>;
176190
}
177191

178192
namespace Private {

0 commit comments

Comments
 (0)