-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
85 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default class SettingsModel | ||
{ | ||
// Add new settings here. | ||
|
||
public constructor(); | ||
public constructor(storageData: Record<string, any>); | ||
public constructor(storageData?: Record<string, any>) | ||
{ | ||
if (!storageData) | ||
return; | ||
// TODO: Convert storageData to SettingsModel. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import CollectionModel from "./CollectionModel"; | ||
import GroupModel from "./GroupModel"; | ||
import TabModel from "./TabModel"; | ||
import SettingsModel from "./SettingsModel"; | ||
|
||
export { CollectionModel, GroupModel, TabModel }; | ||
export { SettingsModel, CollectionModel, GroupModel, TabModel }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,45 @@ | ||
export {} | ||
import { Browser, Storage } from "webextension-polyfill"; | ||
import { SettingsModel } from "../../Models/Data"; | ||
import { Event, ext } from "../../Utils"; | ||
|
||
export default class SettingsService | ||
{ | ||
private constructor() {} | ||
|
||
public static readonly Changed = new Event<Browser, Partial<SettingsModel>>(); | ||
|
||
public static async GetSettings(): Promise<SettingsModel> | ||
{ | ||
let fallbackOptions = new SettingsModel(); | ||
|
||
if (!ext) | ||
return fallbackOptions; | ||
|
||
let options: Record<string, any> = await ext.storage.sync.get(fallbackOptions); | ||
|
||
if (!ext.storage.sync.onChanged.hasListener(SettingsService.OnStorageChanged)) | ||
ext.storage.sync.onChanged.addListener(SettingsService.OnStorageChanged); | ||
|
||
return new SettingsModel(options); | ||
} | ||
|
||
public static async SetSettings(changes: Partial<SettingsModel>): Promise<void> | ||
{ | ||
if (ext) | ||
await ext.storage.sync.set(changes); | ||
else | ||
SettingsService.Changed.Invoke(null, changes); | ||
} | ||
|
||
private static OnStorageChanged(changes: { [key: string]: Storage.StorageChange }): void | ||
{ | ||
let propsList: string[] = Object.keys(new SettingsService()); | ||
let settings: { [key: string]: any; } = {}; | ||
|
||
Object.entries(changes) | ||
.filter(i => propsList.includes(i[0])) | ||
.map(i => settings[i[0]] = i[1].newValue); | ||
|
||
SettingsService.Changed.Invoke(ext, settings as Partial<SettingsModel>); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export type EventHandler<S, T> = (sender: S, args: T) => void; | ||
|
||
export default class Event<S = any, T = any> | ||
{ | ||
private _handlers: EventHandler<S, T>[] = []; | ||
|
||
public AddListener(handler: EventHandler<S, T>): void | ||
{ | ||
this._handlers.push(handler); | ||
} | ||
|
||
public RemoveListener(handler: EventHandler<S, T>): void | ||
{ | ||
this._handlers = this._handlers.filter(i => i !== handler); | ||
} | ||
|
||
public Invoke(sender: S, args: T): void | ||
{ | ||
this._handlers.forEach(i => i(sender, args)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Browser from "webextension-polyfill"; | ||
import { Browser } from "webextension-polyfill"; | ||
|
||
const ext: typeof Browser | null = (process.env.NODE_ENV !== "development") ? require("webextension-polyfill") : null; | ||
const ext: Browser | null = (process.env.NODE_ENV !== "development") ? require("webextension-polyfill") : null; | ||
export default ext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import ext from "./ext"; | ||
import Event, { EventHandler } from "./Event"; | ||
|
||
export { ext }; | ||
export { ext, Event, EventHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters