-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
98 lines (91 loc) · 2.15 KB
/
Copy pathstorage.js
File metadata and controls
98 lines (91 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright (c) 2026 HOOX · AXIS · hoox-sh
* SPDX-License-Identifier: AGPL-3.0-only
*/
// localStorage persistence for AXIS
// Keys: script, symbol, interval, run mode, API key (local-only; not synced)
const STORAGE_KEY = 'pynescript.axis.v1';
/**
* @typedef {{
* script?: string,
* symbol?: string,
* interval?: string,
* mode?: 'local' | 'cloud',
* apiKey?: string,
* savedAt?: number,
* }} AxisState
*/
/**
* @returns {AxisState | null}
*/
export function loadState() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return null;
const data = JSON.parse(raw);
if (!data || typeof data !== 'object') return null;
return data;
} catch (e) {
console.warn('loadState failed', e);
return null;
}
}
/**
* @param {AxisState} partial
* @returns {AxisState}
*/
export function saveState(partial = {}) {
const prev = loadState() || {};
const next = {
...prev,
...partial,
savedAt: Date.now(),
};
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
} catch (e) {
console.warn('saveState failed (quota?)', e);
}
return next;
}
export function clearState() {
try {
localStorage.removeItem(STORAGE_KEY);
} catch (e) {
/* ignore */
}
}
/**
* Debounced save helper.
* @param {() => AxisState} getPartial
* @param {number} [ms]
*/
export function createAutoSaver(getPartial, ms = 800) {
let timer = null;
return {
schedule() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
try {
saveState(getPartial());
} catch (e) {
console.warn('auto-save failed', e);
}
}, ms);
},
flush() {
if (timer) {
clearTimeout(timer);
timer = null;
}
return saveState(getPartial());
},
cancel() {
if (timer) {
clearTimeout(timer);
timer = null;
}
},
};
}