Skip to content

Commit 9568946

Browse files
authored
Dispatch event on save error (#81)
1 parent d86e6b5 commit 9568946

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,21 @@ class OptionsSync<UserOptions extends Options> {
313313
return;
314314
}
315315

316-
await this.set(this._parseForm(field.form!));
316+
try {
317+
await this.set(this._parseForm(field.form!));
318+
} catch (error) {
319+
field.dispatchEvent(new CustomEvent('options-sync:save-error', {
320+
bubbles: true,
321+
detail: error,
322+
}));
323+
throw error;
324+
}
325+
326+
field.dispatchEvent(new CustomEvent('options-sync:save-success', {
327+
bubbles: true,
328+
}));
329+
330+
// TODO: Deprecated; drop in next major
317331
field.form!.dispatchEvent(new CustomEvent('options-sync:form-synced', {
318332
bubbles: true,
319333
}));

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Main features:
77
- Define your default options
88
- Add autoload and autosave to your options `<form>`
99
- Run migrations on update
10+
- Import/export helpers
1011

1112
This also lets you very easily have [separate options for each domain](https://github.com/fregante/webext-options-sync-per-domain) with the help of `webext-options-sync-per-domain`.
1213

@@ -283,6 +284,11 @@ This returns a Promise that will resolve with all the options.
283284

284285
Any defaults or saved options will be loaded into the `<form>` and any change will automatically be saved via `chrome.storage.sync`. It also looks for any buttons with `js-import` or `js-export` classes that when clicked will allow the user to export and import the options to a JSON file.
285286

287+
- `options-sync:save-success`: Fired on the edited field when the form is saved.
288+
- `options-sync:save-error`: Fired on the edited field when the form is not saved due to an error. The error is passed as the `detail` property.
289+
290+
Saving can fail when the storage quota is exceeded for example. You should handle this case and display a message to the user.
291+
286292
##### form
287293

288294
Type: `HTMLFormElement`, `string`
@@ -308,7 +314,3 @@ Opens the browser’s file picker to import options from a previously-saved JSON
308314
- [webext-dynamic-content-scripts](https://github.com/fregante/webext-dynamic-content-scripts) - Automatically registers your content_scripts on domains added via permission.request.
309315
- [Awesome-WebExtensions](https://github.com/fregante/Awesome-WebExtensions) - A curated list of awesome resources for WebExtensions development.
310316
- [More…](https://github.com/fregante/webext-fun)
311-
312-
## License
313-
314-
MIT © [Federico Brigante](https://fregante.com)

0 commit comments

Comments
 (0)