mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-12-03 19:07:09 +00:00
Allow to pin nodes to top of the list (#48)
* Refactored persistent state a bit * Allow nodes to be pinned to top
This commit is contained in:
1 parent
44d91a54d5
commit
1559b82eb0
24 files changed
+194
-86
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { Maybe, Stringified, stringify, parse } from '@dotstats/common';
|
||||
|
||||
export class Persistent<Data> {
|
||||
private readonly onChange: (value: Data) => void;
|
||||
private readonly key: string;
|
||||
private value: Data;
|
||||
|
||||
constructor(key: string, initial: Data, onChange: (value: Readonly<Data>) => void) {
|
||||
this.key = key;
|
||||
this.onChange = onChange;
|
||||
|
||||
const stored = window.localStorage.getItem(key) as Maybe<Stringified<Data>>;
|
||||
|
||||
if (stored) {
|
||||
this.value = parse(stored);
|
||||
} else {
|
||||
this.value = initial;
|
||||
}
|
||||
|
||||
window.addEventListener('storage', (event) => {
|
||||
if (event.key === this.key) {
|
||||
this.value = parse(event.newValue as any as Stringified<Data>);
|
||||
|
||||
this.onChange(this.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public get(): Readonly<Data> {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public set(value: Data) {
|
||||
this.value = value;
|
||||
window.localStorage.setItem(this.key, stringify(this.value) as any as string);
|
||||
this.onChange(this.value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Persistent } from './';
|
||||
|
||||
export class PersistentObject<Data extends object> {
|
||||
private readonly inner: Persistent<Data>;
|
||||
|
||||
constructor(key: string, initial: Data, onChange: (value: Data) => void) {
|
||||
this.inner = new Persistent(key, initial, onChange);
|
||||
}
|
||||
|
||||
public raw(): Readonly<Data> {
|
||||
return this.inner.get();
|
||||
}
|
||||
|
||||
public get<K extends keyof Data>(key: K): Data[K] {
|
||||
return this.inner.get()[key];
|
||||
}
|
||||
|
||||
public set<K extends keyof Data>(key: K, value: Data[K]) {
|
||||
const data: Data = Object.assign({}, this.raw());
|
||||
data[key] = value;
|
||||
this.inner.set(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Persistent } from './';
|
||||
|
||||
export class PersistentSet<Item> {
|
||||
private readonly inner: Persistent<Item[]>;
|
||||
private value: Set<Item>;
|
||||
|
||||
constructor(key: string, onChange: (value: Set<Item>) => void) {
|
||||
this.inner = new Persistent(key, [], (raw: Readonly<Item[]>) => onChange(this.value = new Set(raw as Item[])));
|
||||
this.value = new Set(this.inner.get() as Item[]);
|
||||
}
|
||||
|
||||
public get(): Set<Item> {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public add(item: Item) {
|
||||
this.value.add(item);
|
||||
this.inner.set(Array.from(this.value));
|
||||
}
|
||||
|
||||
public delete(item: Item) {
|
||||
this.value.delete(item);
|
||||
this.inner.set(Array.from(this.value));
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this.inner.set([]);
|
||||
}
|
||||
|
||||
public has(item: Item): boolean {
|
||||
return this.value.has(item);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './Persistent';
|
||||
export * from './PersistentObject';
|
||||
export * from './PersistentSet';
|
||||
Reference in new issue
Block a user