@cripty2001/utils 0.0.175 → 0.0.177
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Appstorage.d.ts +37 -0
- package/dist/Appstorage.js +126 -0
- package/dist/Searcher/index.js +2 -3
- package/package.json +5 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { JSONEncodable } from '@cripty2001/utils';
|
|
2
|
+
import { Whispr } from '@cripty2001/whispr';
|
|
3
|
+
export type AppstorageData = Record<string, JSONEncodable>;
|
|
4
|
+
export type AppstorageItemData<T extends AppstorageData> = {
|
|
5
|
+
key: string;
|
|
6
|
+
data: T;
|
|
7
|
+
rev: number;
|
|
8
|
+
deleted: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare class Appstorage {
|
|
11
|
+
private static readonly instances;
|
|
12
|
+
static getInstance(key: string): Appstorage;
|
|
13
|
+
readonly PREFIX: string;
|
|
14
|
+
index: Whispr<Record<string, AppstorageItem<any>>>;
|
|
15
|
+
private _setIndex;
|
|
16
|
+
private readonly refreshInterval;
|
|
17
|
+
private constructor();
|
|
18
|
+
add<T extends AppstorageData>(key: string, data: T): AppstorageItem<T>;
|
|
19
|
+
get<T extends AppstorageData>(key: string): AppstorageItem<T>;
|
|
20
|
+
private listData;
|
|
21
|
+
private refresh;
|
|
22
|
+
}
|
|
23
|
+
declare class AppstorageItem<T extends AppstorageData> {
|
|
24
|
+
readonly PREFIX: string;
|
|
25
|
+
private static readonly instances;
|
|
26
|
+
private static readonly refreshInterval;
|
|
27
|
+
readonly key: string;
|
|
28
|
+
readonly data: Whispr<AppstorageItemData<T>>;
|
|
29
|
+
readonly update: (data: T) => void;
|
|
30
|
+
readonly remove: () => void;
|
|
31
|
+
private _setData;
|
|
32
|
+
static get(PREFIX: string, key: string): AppstorageItem<any>;
|
|
33
|
+
private constructor();
|
|
34
|
+
private refresh;
|
|
35
|
+
private loadData;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Appstorage = void 0;
|
|
4
|
+
const dispatcher_1 = require("@cripty2001/utils/dispatcher");
|
|
5
|
+
const whispr_1 = require("@cripty2001/whispr");
|
|
6
|
+
class Appstorage {
|
|
7
|
+
static instances = new Map();
|
|
8
|
+
static getInstance(key) {
|
|
9
|
+
if (!this.instances.has(key))
|
|
10
|
+
this.instances.set(key, new Appstorage(key));
|
|
11
|
+
return this.instances.get(key);
|
|
12
|
+
}
|
|
13
|
+
PREFIX;
|
|
14
|
+
index;
|
|
15
|
+
_setIndex;
|
|
16
|
+
refreshInterval = setInterval(() => {
|
|
17
|
+
this.refresh();
|
|
18
|
+
}, 200);
|
|
19
|
+
constructor(PREFIX) {
|
|
20
|
+
this.PREFIX = PREFIX;
|
|
21
|
+
[this.index, this._setIndex] = whispr_1.Whispr.create({});
|
|
22
|
+
}
|
|
23
|
+
add(key, data) {
|
|
24
|
+
const k = `${this.PREFIX}${key}`;
|
|
25
|
+
if (localStorage.getItem(k) !== null)
|
|
26
|
+
throw new Error(`${key} already exists in storage`);
|
|
27
|
+
localStorage.setItem(k, JSON.stringify({
|
|
28
|
+
key: k,
|
|
29
|
+
data: data,
|
|
30
|
+
rev: 0,
|
|
31
|
+
deleted: false
|
|
32
|
+
}));
|
|
33
|
+
return AppstorageItem.get(this.PREFIX, key);
|
|
34
|
+
}
|
|
35
|
+
get(key) {
|
|
36
|
+
return AppstorageItem.get(this.PREFIX, key);
|
|
37
|
+
}
|
|
38
|
+
listData() {
|
|
39
|
+
const toReturn = [];
|
|
40
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
41
|
+
const key = localStorage.key(i);
|
|
42
|
+
if (key === null || !key.startsWith(this.PREFIX))
|
|
43
|
+
continue;
|
|
44
|
+
toReturn.push(key.replace(this.PREFIX, ""));
|
|
45
|
+
}
|
|
46
|
+
return toReturn;
|
|
47
|
+
}
|
|
48
|
+
refresh() {
|
|
49
|
+
const newitems = this.listData()
|
|
50
|
+
.filter(key => this.index.value[key] === undefined)
|
|
51
|
+
.map(key => ({
|
|
52
|
+
key: key,
|
|
53
|
+
ref: this.get(key)
|
|
54
|
+
}))
|
|
55
|
+
.filter(item => !item.ref.data.value.deleted)
|
|
56
|
+
.reduce((acc, item) => {
|
|
57
|
+
acc[item.key] = item.ref;
|
|
58
|
+
return acc;
|
|
59
|
+
}, {});
|
|
60
|
+
if (Object.keys(newitems).length > 0) {
|
|
61
|
+
this._setIndex({
|
|
62
|
+
...this.index.value,
|
|
63
|
+
...newitems
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.Appstorage = Appstorage;
|
|
69
|
+
class AppstorageItem {
|
|
70
|
+
PREFIX;
|
|
71
|
+
static instances = new Map();
|
|
72
|
+
static refreshInterval = setInterval(() => {
|
|
73
|
+
this.instances.forEach(item => item.refresh());
|
|
74
|
+
}, 200);
|
|
75
|
+
key;
|
|
76
|
+
data;
|
|
77
|
+
update;
|
|
78
|
+
remove;
|
|
79
|
+
_setData;
|
|
80
|
+
static get(PREFIX, key) {
|
|
81
|
+
const k = `${PREFIX}${key}`;
|
|
82
|
+
if (!this.instances.has(k))
|
|
83
|
+
this.instances.set(k, new AppstorageItem(PREFIX, key));
|
|
84
|
+
return this.instances.get(k);
|
|
85
|
+
}
|
|
86
|
+
constructor(PREFIX, key) {
|
|
87
|
+
this.PREFIX = PREFIX;
|
|
88
|
+
this.key = key;
|
|
89
|
+
[this.data, this._setData] = whispr_1.Whispr.create(this.loadData());
|
|
90
|
+
this.update = (data) => {
|
|
91
|
+
this._setData({
|
|
92
|
+
key: `${this.PREFIX}${this.key}`,
|
|
93
|
+
rev: this.data.value.rev + 1,
|
|
94
|
+
deleted: false,
|
|
95
|
+
data: data
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
this.remove = () => {
|
|
99
|
+
this._setData({
|
|
100
|
+
key: `${this.PREFIX}${this.key}`,
|
|
101
|
+
rev: this.data.value.rev + 1,
|
|
102
|
+
deleted: true,
|
|
103
|
+
data: {}
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
new dispatcher_1.Dispatcher(this.data, async () => {
|
|
107
|
+
const curr = this.loadData();
|
|
108
|
+
if (this.data.value.rev > curr.rev) {
|
|
109
|
+
localStorage.setItem(`${this.PREFIX}${this.key}`, JSON.stringify(this.data.value));
|
|
110
|
+
}
|
|
111
|
+
}, 500);
|
|
112
|
+
}
|
|
113
|
+
refresh() {
|
|
114
|
+
const data = this.loadData();
|
|
115
|
+
if (data.rev > this.data.value.rev) {
|
|
116
|
+
console.log("Updating", this.data.value.rev, "->", data.rev);
|
|
117
|
+
this._setData(data);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
loadData() {
|
|
121
|
+
const raw_data = localStorage.getItem(`${this.PREFIX}${this.key}`);
|
|
122
|
+
if (raw_data === null)
|
|
123
|
+
throw new Error(`${this.key} does not exist in storage`);
|
|
124
|
+
return JSON.parse(raw_data);
|
|
125
|
+
}
|
|
126
|
+
}
|
package/dist/Searcher/index.js
CHANGED
|
@@ -7,10 +7,9 @@ class Searcher {
|
|
|
7
7
|
this.data = data;
|
|
8
8
|
}
|
|
9
9
|
search(query, limit) {
|
|
10
|
-
if (query === "")
|
|
11
|
-
return this.data;
|
|
12
10
|
return this.data
|
|
13
|
-
.filter(item =>
|
|
11
|
+
.filter(item => query === '' ||
|
|
12
|
+
item.queries.some(q => q.includes(query.toLowerCase())))
|
|
14
13
|
.sort((a, b) => a.order - b.order)
|
|
15
14
|
.slice(0, limit);
|
|
16
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.177",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -58,6 +58,10 @@
|
|
|
58
58
|
"import": "./dist/Dispatcher.js",
|
|
59
59
|
"types": "./dist/Dispatcher.d.ts"
|
|
60
60
|
},
|
|
61
|
+
"./appstorage": {
|
|
62
|
+
"import": "./dist/AppStorage/index.js",
|
|
63
|
+
"types": "./dist/AppStorage/index.d.ts"
|
|
64
|
+
},
|
|
61
65
|
"./searcher": {
|
|
62
66
|
"import": "./dist/Searcher/index.js",
|
|
63
67
|
"types": "./dist/Searcher/index.d.ts"
|