frontend: housekeeping and clean up Store code

This commit is contained in:
hippoz 2022-05-07 04:04:54 +03:00
parent e3b4e28428
commit d1654546fa
Signed by: hippoz
GPG key ID: 7C52899193467641
7 changed files with 23 additions and 25 deletions

View file

@ -1,10 +1,9 @@
<script>
import { fly } from "svelte/transition";
import { quintInOut } from "svelte/easing";
import { overlayStore } from "../../stores";
import request from "../../request";
import { apiRoute } from "../../storage";
import { maybeFly } from "../../animations";
import { maybeFly } from "../../animations";
let username = "";
let password = "";

View file

@ -1,10 +1,9 @@
<script>
import { fade, fly } from "svelte/transition";
import { quintInOut } from "svelte/easing";
import { overlayStore } from "../../stores";
import request from "../../request";
import { apiRoute } from "../../storage";
import { maybeFade, maybeFly } from "../../animations";
import { maybeFade, maybeFly } from "../../animations";
let channelName = "";
let createButtonEnabled = true;

View file

@ -1,5 +1,4 @@
<script>
import { fade, fly } from "svelte/transition";
import { maybeFade, maybeFly } from "../../animations";
import { quintInOut } from "svelte/easing";
import { overlayStore } from "../../stores";

View file

@ -1,10 +1,9 @@
<script>
import { fade, fly } from "svelte/transition";
import { quintInOut } from "svelte/easing";
import { overlayStore } from "../../stores";
import request from "../../request";
import { apiRoute } from "../../storage";
import { maybeFade, maybeFly } from "../../animations";
import { maybeFade, maybeFly } from "../../animations";
export let message;

View file

@ -1,11 +1,10 @@
<script>
import { fly } from "svelte/transition";
import { quintInOut } from "svelte/easing";
import { overlayStore } from "../../stores";
import request from "../../request";
import { apiRoute } from "../../storage";
import { authWithToken } from "../../auth";
import { maybeFly } from "../../animations";
import { maybeFly } from "../../animations";
let username = "";
let password = "";

View file

@ -1,8 +1,7 @@
<script>
import { XIcon } from "svelte-feather-icons";
import { quintInOut } from "svelte/easing";
import { fly } from "svelte/transition";
import { maybeFly } from "../../animations";
import { maybeFly } from "../../animations";
import { overlayStore } from "../../stores";
export let message;

View file

@ -6,26 +6,24 @@ import { apiRoute, getItem, setItem } from "./storage";
const storeLog = logger("Store");
class Store {
constructor(value=null, name="[unnamed]") {
constructor(value=null, name="[no name]") {
this._handlers = [];
this.value = value;
this.name = name;
}
// like subscribe, but without initially calling it
// like subscribe, but without initially calling the handler
watch(handler) {
const newLength = this._handlers.push(handler);
const handlerIndex = newLength - 1;
storeLog(`(${this.name}) Calling handler (watch/initial)`, this.value);
const handlerIndex = this._handlers.push(handler) - 1;
storeLog(`(${this.name}) (watch/handler) New handler`, this.value);
return () => {
this._handlers.splice(handlerIndex, 1);
};
}
subscribe(handler) {
const newLength = this._handlers.push(handler);
const handlerIndex = newLength - 1;
storeLog(`(${this.name}) Calling handler (subscribe/initial)`, this.value);
const handlerIndex = this._handlers.push(handler) - 1;
storeLog(`(${this.name}) (subscribe/initial) Calling handler`, this.value);
handler(this.value);
return () => {
this._handlers.splice(handlerIndex, 1);
@ -33,15 +31,18 @@ class Store {
}
set(value) {
if (value === this.value)
return;
this.value = value;
this.updated();
}
updated() {
storeLog(`(${this.name}) Calling all (${this._handlers.length}) handlers (updated)`, this.value);
this._handlers.forEach(e => {
e(this.value);
});
storeLog(`(${this.name}) (updated) Calling all (${this._handlers.length}) handlers`, this.value);
for (let i = this._handlers.length - 1; i >= 0; i--) {
this._handlers[i](this.value);
}
}
}
@ -307,15 +308,18 @@ class OverlayStore extends Store {
}
close(name) {
if (!this.value[name])
return;
this.value[name] = null;
this.updated();
}
}
export const selectedChannel = new Store({ id: getItem("app:cache:openChannelId"), name: "none", creator_id: -1 }, "selectedChannel");
export const showSidebar = new Store(false, "showSidebar");
export const showChannelView = new Store(true, "showChannelView");
export const showSidebar = new Store(true, "showSidebar");
export const smallViewport = new Store(false, "smallViewport");
export const showChannelView = new Store(true, "showChannelView");
export const theme = new StorageItemStore("app:visual:theme");
export const doAnimations = new StorageItemStore("app:behavior:doAnimations");
export const channels = new ChannelsStore();