Skip to content

Commit

Permalink
refactor: simplify internal state by depending on derived connection
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-weglarz committed Aug 2, 2024
1 parent 14c85cb commit baaff33
Showing 1 changed file with 6 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ export class NetworkStatus {
);
#online: boolean = $state(false);
#updatedAt: Date = $state(new Date());
#downlink?: NetworkInformation["downlink"] = $state();
#downlinkMax?: NetworkInformation["downlinkMax"] = $state();
#effectiveType?: NetworkInformation["effectiveType"] = $state();
#rtt?: NetworkInformation["rtt"] = $state();
#saveData?: NetworkInformation["saveData"] = $state();
#type?: NetworkInformation["type"] = $state();

constructor() {
onMount(() => {
Expand All @@ -64,14 +58,6 @@ export class NetworkStatus {
if (!this.#navigator) return;
this.#online = this.#navigator.onLine;
this.#updatedAt = new Date();
if (!this.#connection) return;

this.#downlink = this.#connection.downlink;
this.#downlinkMax = this.#connection.downlinkMax;
this.#effectiveType = this.#connection.effectiveType;
this.#rtt = this.#connection.rtt;
this.#saveData = this.#connection.saveData;
this.#type = this.#connection.type;
};

/**
Expand Down Expand Up @@ -102,7 +88,7 @@ export class NetworkStatus {
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlink
*/
get downlink() {
return this.#downlink;
return this.#connection?.downlink;
}

/**
Expand All @@ -111,7 +97,7 @@ export class NetworkStatus {
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlinkMax
*/
get downlinkMax() {
return this.#downlinkMax;
return this.#connection?.downlinkMax;
}

/**
Expand All @@ -121,7 +107,7 @@ export class NetworkStatus {
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType
*/
get effectiveType() {
return this.#effectiveType;
return this.#connection?.effectiveType;
}

/**
Expand All @@ -130,22 +116,22 @@ export class NetworkStatus {
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/rtt
*/
get rtt() {
return this.#rtt;
return this.#connection?.rtt;
}

/**
* @desc {true} if the user has set a reduced data usage option on the user agent.
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData
*/
get saveData() {
return this.#saveData;
return this.#connection?.saveData;
}

/**
* @desc The type of connection a device is using to communicate with the network.
* @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/type
*/
get type() {
return this.#type;
return this.#connection?.type;
}
}

0 comments on commit baaff33

Please sign in to comment.