Skip to content

Commit

Permalink
Update dependencies (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 12, 2022
1 parent 8ea5766 commit 3408ae3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/esm-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- run: npm install
- run: npm run build --if-present
- run: npm pack --dry-run
- run: npm pack --silent 2>/dev/null | xargs -n1 tar -xzf
- run: npm pack | tail -1 | xargs -n1 tar -xzf
- uses: actions/upload-artifact@v2
with:
path: package
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/download-artifact@v2
- run: npm install ./artifact
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js
- run: npx parcel@2.0.0-beta.2 build index.js
- run: npx parcel build index.js
- run: cat dist/index.js
Rollup:
runs-on: ubuntu-latest
Expand Down
48 changes: 24 additions & 24 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {debounce} from 'throttle-debounce';
import serialize from 'dom-form-serializer/lib/serialize';
import deserialize from 'dom-form-serializer/lib/deserialize';
import {isBackgroundPage} from 'webext-detect-page';
import {isBackground} from 'webext-detect-page';
import {compressToEncodedURIComponent, decompressFromEncodedURIComponent} from 'lz-string';

async function shouldRunMigrations(): Promise<boolean> {
Expand Down Expand Up @@ -49,14 +49,14 @@ async function shouldRunMigrations(): Promise<boolean> {
],
}
*/
export interface Setup<TOptions extends Options> {
export interface Setup<UserOptions extends Options> {
storageName?: string;
logging?: boolean;
defaults?: TOptions;
defaults?: UserOptions;
/**
* A list of functions to call when the extension is updated.
*/
migrations?: Array<Migration<TOptions>>;
migrations?: Array<Migration<UserOptions>>;
}

/**
Expand All @@ -70,9 +70,9 @@ export interface Options {
/*
Handler signature for when an extension updates.
*/
export type Migration<TOptions extends Options> = (savedOptions: TOptions, defaults: TOptions) => void;
export type Migration<UserOptions extends Options> = (savedOptions: UserOptions, defaults: UserOptions) => void;

class OptionsSync<TOptions extends Options> {
class OptionsSync<UserOptions extends Options> {
public static migrations = {
/**
Helper method that removes any option that isn't defined in the defaults. It's useful to avoid leaving old options taking up space.
Expand All @@ -88,7 +88,7 @@ class OptionsSync<TOptions extends Options> {

storageName: string;

defaults: TOptions;
defaults: UserOptions;

_form: HTMLFormElement | undefined;

Expand All @@ -100,11 +100,11 @@ class OptionsSync<TOptions extends Options> {
*/
constructor({
// `as` reason: https://github.com/fregante/webext-options-sync/pull/21#issuecomment-500314074
defaults = {} as TOptions,
defaults = {} as UserOptions,
storageName = 'options',
migrations = [],
logging = true,
}: Setup<TOptions> = {}) {
}: Setup<UserOptions> = {}) {
this.storageName = storageName;
this.defaults = defaults;
this._handleFormInput = debounce(300, this._handleFormInput.bind(this));
Expand All @@ -130,7 +130,7 @@ class OptionsSync<TOptions extends Options> {
document.body.style.color = color;
}
*/
async getAll(): Promise<TOptions> {
async getAll(): Promise<UserOptions> {
await this._migrations;
return this._getAll();
}
Expand All @@ -140,7 +140,7 @@ class OptionsSync<TOptions extends Options> {
@param newOptions - A map of default options as strings or booleans. The keys will have to match the form fields' `name` attributes.
*/
async setAll(newOptions: TOptions): Promise<void> {
async setAll(newOptions: UserOptions): Promise<void> {
await this._migrations;
return this._setAll(newOptions);
}
Expand All @@ -150,7 +150,7 @@ class OptionsSync<TOptions extends Options> {
@param newOptions - A map of default options as strings or booleans. The keys will have to match the form fields' `name` attributes.
*/
async set(newOptions: Partial<TOptions>): Promise<void> {
async set(newOptions: Partial<UserOptions>): Promise<void> {
return this.setAll({...await this.getAll(), ...newOptions});
}

Expand Down Expand Up @@ -187,8 +187,8 @@ class OptionsSync<TOptions extends Options> {
console[method](...args);
}

private async _getAll(): Promise<TOptions> {
return new Promise<TOptions>((resolve, reject) => {
private async _getAll(): Promise<UserOptions> {
return new Promise<UserOptions>((resolve, reject) => {
chrome.storage.sync.get(this.storageName, result => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
Expand All @@ -199,7 +199,7 @@ class OptionsSync<TOptions extends Options> {
});
}

private async _setAll(newOptions: TOptions): Promise<void> {
private async _setAll(newOptions: UserOptions): Promise<void> {
this._log('log', 'Saving options', newOptions);
return new Promise((resolve, reject) => {
chrome.storage.sync.set({
Expand All @@ -214,8 +214,8 @@ class OptionsSync<TOptions extends Options> {
});
}

private _encode(options: TOptions): string {
const thinnedOptions: Partial<TOptions> = {...options};
private _encode(options: UserOptions): string {
const thinnedOptions: Partial<UserOptions> = {...options};
for (const [key, value] of Object.entries(thinnedOptions)) {
if (this.defaults[key] === value) {
delete thinnedOptions[key];
Expand All @@ -227,17 +227,17 @@ class OptionsSync<TOptions extends Options> {
return compressToEncodedURIComponent(JSON.stringify(thinnedOptions));
}

private _decode(options: string | TOptions): TOptions {
private _decode(options: string | UserOptions): UserOptions {
let decompressed = options;
if (typeof options === 'string') {
decompressed = JSON.parse(decompressFromEncodedURIComponent(options)!) as TOptions;
decompressed = JSON.parse(decompressFromEncodedURIComponent(options)!) as UserOptions;
}

return {...this.defaults, ...decompressed as TOptions};
return {...this.defaults, ...decompressed as UserOptions};
}

private async _runMigrations(migrations: Array<Migration<TOptions>>): Promise<void> {
if (migrations.length === 0 || !isBackgroundPage() || !await shouldRunMigrations()) {
private async _runMigrations(migrations: Array<Migration<UserOptions>>): Promise<void> {
if (migrations.length === 0 || !isBackground() || !await shouldRunMigrations()) {
return;
}

Expand Down Expand Up @@ -272,7 +272,7 @@ class OptionsSync<TOptions extends Options> {
event.preventDefault();
}

private _updateForm(form: HTMLFormElement, options: TOptions): void {
private _updateForm(form: HTMLFormElement, options: UserOptions): void {
// Reduce changes to only values that have changed
const currentFormState = this._parseForm(form);
for (const [key, value] of Object.entries(options)) {
Expand All @@ -289,7 +289,7 @@ class OptionsSync<TOptions extends Options> {
}

// Parse form into object, except invalid fields
private _parseForm(form: HTMLFormElement): Partial<TOptions> {
private _parseForm(form: HTMLFormElement): Partial<UserOptions> {
const include: string[] = [];

// Don't serialize disabled and invalid fields
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,28 @@
}
},
"dependencies": {
"webext-detect-page": "^3.0.2"
"webext-detect-page": "^4.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-node-resolve": "^13.0.5",
"@rollup/plugin-typescript": "^8.2.5",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-typescript": "^8.3.0",
"@sindresorhus/tsconfig": "^2.0.0",
"@types/chrome": "0.0.158",
"@types/chrome": "0.0.176",
"@types/estree": "^0.0.50",
"@types/lz-string": "^1.3.34",
"@types/throttle-debounce": "^2.1.0",
"ava": "^3.15.0",
"ava": "^4.0.1",
"dom-form-serializer": "^2.0.0",
"lz-string": "^1.4.4",
"npm-run-all": "^4.1.5",
"rollup": "^2.57.0",
"rollup": "^2.63.0",
"rollup-plugin-terser": "^7.0.2",
"sinon": "^11.1.2",
"sinon": "^12.0.1",
"sinon-chrome": "^3.0.1",
"throttle-debounce": "^3.0.1",
"type-fest": "^2.3.4",
"typescript": "^4.4.3",
"xo": "^0.44.0"
"type-fest": "^2.9.0",
"typescript": "^4.5.4",
"xo": "^0.47.0"
}
}
5 changes: 4 additions & 1 deletion test/_fixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import chrome from 'sinon-chrome';

global.location = new URL('chrome://1234/_generated_background_page.html');
global.location = {
origin: 'chrome://abc',
pathname: '/_generated_background_page.html',
};
global.chrome = chrome;
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const simpleSetup = {

test.beforeEach(() => {
chrome.flush();
chrome.runtime.getManifest.returns({
version: 2,
background: {scripts: 'background.js'},
});
chrome.storage.sync.set.yields(undefined);
chrome.management.getSelf.yields({
installType: 'development',
Expand Down

0 comments on commit 3408ae3

Please sign in to comment.