Skip to content

Commit f6c89e1

Browse files
committed
Version 3.0.2, code quality
1 parent ab3d202 commit f6c89e1

File tree

9 files changed

+848
-63
lines changed

9 files changed

+848
-63
lines changed

Distribution/app/index.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as messaging from "messaging";
44
var SETTINGS_TYPE = "cbor";
55
var SETTINGS_FILE = "settings.cbor";
66
var _settings;
7-
var _onsettingschange;
87
/**
98
* Initialize settings
109
* @param settings that will be managed by the module
@@ -13,32 +12,34 @@ var _onsettingschange;
1312
export function initialize(settings, callback) {
1413
// Save args
1514
_settings = settings;
16-
_onsettingschange = callback;
1715
// load settings from file adn update values
1816
loadSettings();
17+
addEventListeners(callback);
1918
// Send the full settings for first load from settings
20-
_onsettingschange(_settings);
19+
callback(_settings);
20+
}
21+
function addEventListeners(callback) {
22+
/**
23+
* Received message containing settings data
24+
*/
25+
messaging.peerSocket.addEventListener("message", function (evt) {
26+
// Get data
27+
var data = evt.data;
28+
// Test data type (it should be a setting)
29+
if (data.type === "setting") {
30+
// Update settings object
31+
_settings[data.key] = data.value;
32+
// Raise event with only the property changed
33+
var args = {};
34+
args[data.key] = data.value;
35+
callback(args);
36+
}
37+
});
38+
/**
39+
* Register for the unload event
40+
*/
41+
me.addEventListener("unload", saveSettings);
2142
}
22-
/**
23-
* Received message containing settings data
24-
*/
25-
messaging.peerSocket.addEventListener("message", function (evt) {
26-
// Get data
27-
var data = evt.data;
28-
// Test data type (it should be a setting)
29-
if (data.type === "setting") {
30-
// Update settings object
31-
_settings[data.key] = data.value;
32-
// Raise event with only the property changed
33-
var args = {};
34-
args[data.key] = data.value;
35-
_onsettingschange(args);
36-
}
37-
});
38-
/**
39-
* Register for the unload event
40-
*/
41-
me.addEventListener("unload", saveSettings);
4243
/**
4344
* update settings
4445
*/

Distribution/companion/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Initialize settings tools
33
* @param defaultSettings - Class with default settings
44
*/
5-
export declare function initialize(defaultSettings: any): void;
5+
export declare function initialize<T>(defaultSettings: T): void;
66
/**
77
* Set defaut setting
88
* @param key of setting to set
99
* @param value of setting to set
1010
*/
11-
export declare function setDefaultSetting(key: string, value: any): void;
11+
export declare function setDefaultSetting(key: string, value: unknown): void;

Distribution/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-fitbit-settings",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Module to simplify management of settings inside Fitbit OS applications",
55
"author": "Jérémy Jeanson (https://www.bugshunter.net)",
66
"license": "MIT",
@@ -21,10 +21,14 @@
2121
"devDependencies": {
2222
"@fitbit/sdk": "5.0.1",
2323
"fitbit-sdk-types": "6.0.0",
24-
"typescript": "4.0.3"
24+
"typescript": "4.0.3",
25+
"@typescript-eslint/eslint-plugin": "^4.3.0",
26+
"@typescript-eslint/parser": "^4.3.0",
27+
"eslint": "^7.10.0"
2528
},
2629
"scripts": {
2730
"build":"@powershell Copy-Item -Path ./package.json,../README.md -Destination ../distribution/ && tsc --build",
28-
"clean": "tsc --build --clean && @powershell Remove-Item ../distribution/*.*"
31+
"clean": "tsc --build --clean && @powershell Remove-Item ../distribution/*.*",
32+
"lint":"eslint . --ext .js,.jsx,.ts,.tsx"
2933
}
3034
}

Sources/.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# don't ever lint node_modules
2+
node_modules

Sources/.eslintrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": [
5+
"@typescript-eslint"
6+
],
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
11+
],
12+
"rules": {
13+
"vars-on-top": "error",
14+
"semi": "error"
15+
}
16+
}

Sources/app/index.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { MessageData } from "../common";
66
const SETTINGS_TYPE = "cbor";
77
const SETTINGS_FILE = "settings.cbor";
88

9-
let _settings: any;
10-
let _onsettingschange: (newSettings: any) => void;
9+
let _settings: Record<string, unknown>;
1110

1211
/**
1312
* Initialize settings
@@ -16,35 +15,37 @@ let _onsettingschange: (newSettings: any) => void;
1615
*/
1716
export function initialize<T>(settings: T, callback: (newSettings: T) => void): void {
1817
// Save args
19-
_settings = settings;
20-
_onsettingschange = callback;
18+
_settings = settings as Record<string, unknown>;
2119
// load settings from file adn update values
2220
loadSettings();
21+
addEventListeners(callback);
2322
// Send the full settings for first load from settings
24-
_onsettingschange(_settings);
23+
callback(_settings as T);
2524
}
2625

27-
/**
28-
* Received message containing settings data
29-
*/
30-
messaging.peerSocket.addEventListener("message", (evt) => {
31-
// Get data
32-
const data = evt.data as MessageData;
33-
// Test data type (it should be a setting)
34-
if (data.type === "setting") {
35-
// Update settings object
36-
_settings[data.key] = data.value;
37-
// Raise event with only the property changed
38-
const args = {};
39-
args[data.key] = data.value;
40-
_onsettingschange(args);
41-
}
42-
});
26+
function addEventListeners<T>(callback: (newSettings: T) => void): void {
27+
/**
28+
* Received message containing settings data
29+
*/
30+
messaging.peerSocket.addEventListener("message", (evt) => {
31+
// Get data
32+
const data = evt.data as MessageData;
33+
// Test data type (it should be a setting)
34+
if (data.type === "setting") {
35+
// Update settings object
36+
_settings[data.key] = data.value;
37+
// Raise event with only the property changed
38+
const args = {};
39+
args[data.key] = data.value;
40+
callback(args as T);
41+
}
42+
});
4343

44-
/**
45-
* Register for the unload event
46-
*/
47-
me.addEventListener("unload", saveSettings);
44+
/**
45+
* Register for the unload event
46+
*/
47+
me.addEventListener("unload", saveSettings);
48+
}
4849

4950
/**
5051
* update settings
@@ -53,7 +54,7 @@ function loadSettings() {
5354
const settings = getSettings();
5455
if (settings === undefined) return;
5556
// Update each property defined in file
56-
for (let key in settings) {
57+
for (const key in settings) {
5758
// Get value
5859
const value = settings[key];
5960
// update settigns value if deined
@@ -66,7 +67,7 @@ function loadSettings() {
6667
/**
6768
* Load settings from filesystem
6869
*/
69-
function getSettings(): any {
70+
function getSettings(): Record<string, unknown> | undefined {
7071
try {
7172
return fs.readFileSync(SETTINGS_FILE, SETTINGS_TYPE);
7273
} catch (ex) {

Sources/companion/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MessageData } from "../common";
66
* Initialize settings tools
77
* @param defaultSettings - Class with default settings
88
*/
9-
export function initialize(defaultSettings: any): void {
9+
export function initialize<T>(defaultSettings: T): void {
1010
// Whensettings changed -> send the new value
1111
settingsStorage.onchange = (e) => {
1212
if (e.key !== null && e.oldValue !== e.newValue && e.newValue !== undefined) {
@@ -40,9 +40,9 @@ function sendValue(key: string, value: string | null): void {
4040
* Init default settings
4141
* @param defaultSettings
4242
*/
43-
function setDefaultSettings(defaultSettings: any): void {
43+
function setDefaultSettings<T>(defaultSettings: T): void {
4444
// For each properties of the settings class
45-
for (let key in defaultSettings) {
45+
for (const key in defaultSettings) {
4646
// Get the value
4747
const value = defaultSettings[key];
4848
// Test if value is defined
@@ -57,7 +57,7 @@ function setDefaultSettings(defaultSettings: any): void {
5757
* @param key of setting to set
5858
* @param value of setting to set
5959
*/
60-
export function setDefaultSetting(key: string, value: any): void {
60+
export function setDefaultSetting(key: string, value: unknown): void {
6161
if (getSetting(key) === null) {
6262
setSetting(key, value);
6363
}
@@ -80,7 +80,7 @@ function getSetting(key: string): string | null {
8080
* @param key
8181
* @param value
8282
*/
83-
function setSetting(key: string, value: any): void {
83+
function setSetting(key: string, value: unknown): void {
8484
try {
8585
settingsStorage.setItem(key, JSON.stringify(value));
8686
}

Sources/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-fitbit-settings",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Module to simplify management of settings inside Fitbit OS applications",
55
"author": "Jérémy Jeanson (https://www.bugshunter.net)",
66
"license": "MIT",
@@ -21,10 +21,14 @@
2121
"devDependencies": {
2222
"@fitbit/sdk": "5.0.1",
2323
"fitbit-sdk-types": "6.0.0",
24-
"typescript": "4.0.3"
24+
"typescript": "4.0.3",
25+
"@typescript-eslint/eslint-plugin": "^4.3.0",
26+
"@typescript-eslint/parser": "^4.3.0",
27+
"eslint": "^7.10.0"
2528
},
2629
"scripts": {
2730
"build":"@powershell Copy-Item -Path ./package.json,../README.md -Destination ../distribution/ && tsc --build",
28-
"clean": "tsc --build --clean && @powershell Remove-Item ../distribution/*.*"
31+
"clean": "tsc --build --clean && @powershell Remove-Item ../distribution/*.*",
32+
"lint":"eslint . --ext .js,.jsx,.ts,.tsx"
2933
}
3034
}

0 commit comments

Comments
 (0)