frontend: housekeeping and clean up Store code
This commit is contained in:
parent
e3b4e28428
commit
d1654546fa
7 changed files with 23 additions and 25 deletions
|
@ -1,5 +1,4 @@
|
|||
<script>
|
||||
import { fly } from "svelte/transition";
|
||||
import { quintInOut } from "svelte/easing";
|
||||
import { overlayStore } from "../../stores";
|
||||
import request from "../../request";
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<script>
|
||||
import { fade, fly } from "svelte/transition";
|
||||
import { quintInOut } from "svelte/easing";
|
||||
import { overlayStore } from "../../stores";
|
||||
import request from "../../request";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<script>
|
||||
import { fade, fly } from "svelte/transition";
|
||||
import { quintInOut } from "svelte/easing";
|
||||
import { overlayStore } from "../../stores";
|
||||
import request from "../../request";
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<script>
|
||||
import { fly } from "svelte/transition";
|
||||
import { quintInOut } from "svelte/easing";
|
||||
import { overlayStore } from "../../stores";
|
||||
import request from "../../request";
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<script>
|
||||
import { XIcon } from "svelte-feather-icons";
|
||||
import { quintInOut } from "svelte/easing";
|
||||
import { fly } from "svelte/transition";
|
||||
import { maybeFly } from "../../animations";
|
||||
import { overlayStore } from "../../stores";
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue