-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab9a948
commit 355b0cd
Showing
12 changed files
with
229 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export class localStorageDbItem { | ||
constructor(localStorageParent, key) { | ||
this.parent = localStorageParent; | ||
this.key = key; | ||
} | ||
get data() { | ||
return localStorage.getItem(this.key) | ||
} | ||
set data(data) { | ||
if (typeof data != "string") throw "require type string for save" | ||
localStorage.setItem(this.key, data); | ||
this.parent.emit("changeItem", this); | ||
} | ||
defaultData(defaultData) { | ||
if (defaultData && this.data == null) this.data = defaultData; | ||
this.parent.emit("initItem", this); | ||
return this; | ||
} | ||
Destroy() { | ||
this.parent.keys = this.parent.keys.filter(e => e.key != this.key).map(e => e.getBase()) | ||
} | ||
getBase() { | ||
return { key: this.key } | ||
} | ||
toJSON(data) { | ||
return JSON.parse(data ? data : this.data); | ||
} | ||
toString(data) { | ||
return data ? JSON.stringify(data) : this.data; | ||
} | ||
onChange(fns) { | ||
return this.parent.on("ChangeItem", ({ detail: { key, newValue, oldValue } }) => { | ||
if (key == this.key) fns({ newValue, oldValue }); | ||
}) | ||
} | ||
} | ||
|
||
export class localStorageDb extends EventTarget { | ||
constructor() { | ||
super(); | ||
this._keys = []; | ||
window.addEventListener("storage", ({ key, newValue, oldValue }) => { | ||
if (key !== null) this.emit("ChangeItem", { key, newValue, oldValue }); | ||
else this.emit("ClearStorage"); | ||
}) | ||
} | ||
get keys() { | ||
return this._keys.map(({ key }) => new localStorageDbItem(this, key)) | ||
} | ||
set keys(data) { | ||
this._keys = data; | ||
} | ||
get(key) { | ||
const Item = this.keys.find((e) => e.key == key); | ||
if (!Item) throw "not exist element" | ||
return Item | ||
} | ||
add(key) { | ||
if (typeof key !== "string") throw "require key type string"; | ||
const Item = new localStorageDbItem(this, key); | ||
this._keys = [...this._keys, Item.getBase()]; | ||
this.emit("addItem", Item) | ||
return Item; | ||
} | ||
emit(name, data) { | ||
if (data) return this.dispatchEvent(new CustomEvent(name, { detail: data, cancelable: true })) | ||
else return this.dispatchEvent(new Event(name, { cancelable: true })) | ||
} | ||
on(name, callback) { | ||
this.addEventListener(name, callback); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { writable, get as getStoreData } from "svelte/store"; | ||
import { localStorageDb } from "./localStorage"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
class StoreItem { | ||
constructor(Store, data) { | ||
this.parent = Store; | ||
this._data = data; | ||
} | ||
get id() { | ||
return this._data.id; | ||
} | ||
get data() { | ||
return this._data; | ||
} | ||
set data(data) { | ||
this._data = data; | ||
} | ||
edit(data) { | ||
Object.keys(data).forEach((k) => (this.data[k] != data[k]) ? this.data[k] = data[k] : ""); | ||
this.parent.update(e => e.map(e => e.id == this.id ? this.data : e)) | ||
this.parent.emit("edit", this); | ||
} | ||
Destroy() { | ||
this.parent.update(e => e.filter(e => e.id != this.id)) | ||
this.parent.emit("del", this); | ||
} | ||
} | ||
|
||
export class Store extends EventTarget { | ||
Destroy() { | ||
throw new Error("Method not implemented."); | ||
} | ||
constructor(fnsDefaultStore) { | ||
super(); | ||
this.store = writable([], fnsDefaultStore); | ||
} | ||
get subscribe() { | ||
return this.store.subscribe; | ||
} | ||
get update() { | ||
return this.store.update; | ||
} | ||
get data() { | ||
return getStoreData(this.store).map((data) => new StoreItem(this, data)); | ||
} | ||
set data(data) { | ||
this.store.set(data); | ||
} | ||
add(data) { | ||
if (!data.id) data.id = uuidv4(); | ||
this.update((e) => [...e, data]) | ||
this.emit("add"); | ||
return this; | ||
} | ||
get(id) { | ||
const Item = this.data.find((e) => e.id == id); | ||
if (!Item) throw "not exist element" | ||
return Item; | ||
} | ||
clear() { | ||
this.data = []; | ||
this.emit("clear"); | ||
} | ||
import(data) { | ||
this.data = data; | ||
this.emit("import"); | ||
} | ||
emit(name, data) { | ||
if (data) return this.dispatchEvent(new CustomEvent(name, { detail: data, cancelable: true })) | ||
else return this.dispatchEvent(new Event(name, { cancelable: true })) | ||
} | ||
on(name, callback) { | ||
this.addEventListener(name, callback); | ||
return this; | ||
} | ||
} | ||
|
||
export class StoreUseLocalStorage extends Store { | ||
constructor(fnsUnsuscribe) { | ||
super((setInternalStore) => { | ||
console.log("start") | ||
this.Keys = new localStorageDb(); | ||
const store = this.Keys.add("store").defaultData("[]") | ||
setInternalStore(store.toJSON()); | ||
store.onChange(({ newValue: data }) => { | ||
if (!document.hasFocus()) setInternalStore(store.toJSON(data)) | ||
}) | ||
this.Keys.on("ClearStorage", (_) => setInternalStore([])) | ||
return fnsUnsuscribe; | ||
}); | ||
this.Destroy = this.subscribe((data) => { | ||
const store = this.Keys.get("store"); | ||
store.data = store.toString(data); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.