memorio 0.2.19 → 1.0.0
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/.editorconfig +31 -0
- package/.gitattributes +1 -0
- package/.private/npm +1 -0
- package/.vscode/extension.json +15 -0
- package/.vscode/settings.json +90 -0
- package/config/dispatch.ts +49 -0
- package/config/global.ts +36 -0
- package/coverage/clover.xml +260 -0
- package/coverage/coverage-final.json +10 -0
- package/coverage/lcov-report/a51.memorio/config/dispatch.ts.html +232 -0
- package/coverage/lcov-report/a51.memorio/config/global.ts.html +193 -0
- package/coverage/lcov-report/a51.memorio/config/index.html +131 -0
- package/coverage/lcov-report/a51.memorio/dist/config/dispatch.js.html +157 -0
- package/coverage/lcov-report/a51.memorio/dist/config/global.js.html +142 -0
- package/coverage/lcov-report/a51.memorio/dist/config/index.html +131 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/cache/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/cache/index.js.html +103 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/observer/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/observer/index.js.html +280 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/state/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/state/index.js.html +460 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/store/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/dist/functions/store/index.js.html +367 -0
- package/coverage/lcov-report/a51.memorio/dist/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/dist/init.js.html +106 -0
- package/coverage/lcov-report/a51.memorio/functions/cache/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/functions/cache/index.ts.html +142 -0
- package/coverage/lcov-report/a51.memorio/functions/observer/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/functions/observer/index.ts.html +448 -0
- package/coverage/lcov-report/a51.memorio/functions/session/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/functions/session/index.ts.html +409 -0
- package/coverage/lcov-report/a51.memorio/functions/state/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/functions/state/index.ts.html +682 -0
- package/coverage/lcov-report/a51.memorio/functions/store/index.html +116 -0
- package/coverage/lcov-report/a51.memorio/functions/store/index.ts.html +409 -0
- package/coverage/lcov-report/a51.memorio/index.html +131 -0
- package/coverage/lcov-report/a51.memorio/index.ts.html +88 -0
- package/coverage/lcov-report/a51.memorio/init.ts.html +115 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +206 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/session/index.html +116 -0
- package/coverage/lcov-report/session/index.ts.html +571 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/state/index.html +116 -0
- package/coverage/lcov-report/state/index.ts.html +1090 -0
- package/coverage/lcov-report/store/index.html +116 -0
- package/coverage/lcov-report/store/index.ts.html +556 -0
- package/coverage/lcov.info +518 -0
- package/dist/README.md +319 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1 -0
- package/dist/package.json +98 -0
- package/dist/types/cache.d.ts +27 -0
- package/dist/types/memorio.d.ts +18 -0
- package/dist/types/observer.d.ts +47 -0
- package/dist/types/session.d.ts +91 -0
- package/dist/types/state.d.ts +71 -0
- package/dist/types/store.d.ts +91 -0
- package/esbuild.config.mjs +54 -0
- package/eslint.config.mjs +174 -0
- package/examples/bg.jpg +0 -0
- package/examples/bootstrap.less +92 -0
- package/examples/favicon.ico +0 -0
- package/examples/index.htm +65 -0
- package/examples/javascript.js +2 -0
- package/examples/responsive.less +47 -0
- package/examples/styles.less +169 -0
- package/functions/cache/index.ts +19 -0
- package/functions/observer/index.ts +121 -0
- package/functions/session/index.ts +108 -0
- package/functions/state/index.ts +199 -0
- package/functions/store/index.ts +108 -0
- package/index.ts +1 -0
- package/init.ts +12 -0
- package/jest.config.mjs +33 -0
- package/jest.setup.js +2 -0
- package/package.json +4 -10
- package/tests/basic.test.ts +50 -0
- package/tests/cache.test.ts +52 -0
- package/tests/observer.test.ts +62 -0
- package/tests/setup.ts +48 -0
- package/tests/state.test.ts +45 -0
- package/tests/store.test.ts +62 -0
- package/tsconfig.json +67 -0
- package/index.js +0 -1
- /package/{CHANGELOG.md → .github/CHANGELOG.md} +0 -0
- /package/{CITATION.cff → .github/CITATION.cff} +0 -0
- /package/{CODEOWNERS → .github/CODEOWNERS} +0 -0
- /package/{CODE_OF_CONDUCT.md → .github/CODE_OF_CONDUCT.md} +0 -0
- /package/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +0 -0
- /package/{COPYRIGHT.md → .github/COPYRIGHT.md} +0 -0
- /package/{FUNDING.yml → .github/FUNDING.yml} +0 -0
- /package/{HISTORY.md → .github/HISTORY.md} +0 -0
- /package/{LICENSE.md → .github/LICENSE.md} +0 -0
- /package/{README.md → .github/README.md} +0 -0
- /package/{SECURITY.md → .github/SECURITY.md} +0 -0
- /package/{SUMMARY.md → .github/SUMMARY.md} +0 -0
- /package/{dependabot.yml → .github/dependabot.yml} +0 -0
- /package/{documents → .github/documents}/OBSERVER.md +0 -0
- /package/{documents → .github/documents}/STATE.md +0 -0
- /package/{documents → .github/documents}/STORE.md +0 -0
- /package/{pull_request_template.md → .github/pull_request_template.md} +0 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Object.defineProperty(
|
|
2
|
+
window,
|
|
3
|
+
"cache",
|
|
4
|
+
{
|
|
5
|
+
value: new Proxy({}, {}),
|
|
6
|
+
enumerable: false,
|
|
7
|
+
configurable: false
|
|
8
|
+
}
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// Object.defineProperties(
|
|
12
|
+
// cache,
|
|
13
|
+
// {
|
|
14
|
+
// get: {},
|
|
15
|
+
// set: {}
|
|
16
|
+
// }
|
|
17
|
+
// )
|
|
18
|
+
|
|
19
|
+
export { }
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
if (!globalThis.observer) globalThis.observer = null
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(
|
|
4
|
+
globalThis,
|
|
5
|
+
'observer',
|
|
6
|
+
{
|
|
7
|
+
enumerable: false
|
|
8
|
+
}
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
12
|
+
// START OBSERVER FUNCTION
|
|
13
|
+
|
|
14
|
+
observer = (s, cb = null, option = true) => {
|
|
15
|
+
|
|
16
|
+
// CHECK IF 'STATE.' OR 'STORE.' EXIST
|
|
17
|
+
const checkStateOrStore = (s: string) => {
|
|
18
|
+
const t = s.split('.')
|
|
19
|
+
if (t[0] !== 'state') {
|
|
20
|
+
console.error(`Observer Error: You need to declare 'state.' or 'store.'. The '${s}' string is incorrect!`)
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!checkStateOrStore(s)) return
|
|
27
|
+
|
|
28
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
29
|
+
|
|
30
|
+
// LIST ALL ARRAY FROM STATE
|
|
31
|
+
if (!s && !cb) {
|
|
32
|
+
console.error('Observer Error: You need to setup observer correctly, Some parameters are missed!')
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
////////////////////////////
|
|
37
|
+
|
|
38
|
+
// CHECK
|
|
39
|
+
if (!s && cb) {
|
|
40
|
+
console.error(`Observer Error: You need to declare what state need to be monitored as string like 'state.test'.`)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
////////////////////////////
|
|
45
|
+
|
|
46
|
+
// RECALL
|
|
47
|
+
if (s && !cb) {
|
|
48
|
+
globalThis.memorio.dispatch.listen(
|
|
49
|
+
String(s),
|
|
50
|
+
{
|
|
51
|
+
detail: {
|
|
52
|
+
name: String(s)
|
|
53
|
+
}
|
|
54
|
+
} as any
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
console.debug('called: ', s)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
////////////////////////////
|
|
62
|
+
|
|
63
|
+
// SETUP
|
|
64
|
+
if (s && cb) {
|
|
65
|
+
if (typeof s !== 'string' || typeof cb !== 'function') {
|
|
66
|
+
console.error(`Observer Error: name of state need to be a 'string' like 'state.test' and the callback need to be a 'function'`)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
globalThis.memorio.dispatch.listen(s, cb, option)
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
76
|
+
|
|
77
|
+
Object.defineProperties(
|
|
78
|
+
observer,
|
|
79
|
+
{
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* See the list of all your observer (eventListener) actually installed.
|
|
83
|
+
* @returns The list of all installed observers.
|
|
84
|
+
*/
|
|
85
|
+
list: {
|
|
86
|
+
get: () => globalThis.events
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
/////////////////////////////////////////////////////
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Remove an observer.
|
|
93
|
+
* @param name The name of the observer to remove.
|
|
94
|
+
*/
|
|
95
|
+
remove: {
|
|
96
|
+
value: (name: any) => {
|
|
97
|
+
if (!name) return
|
|
98
|
+
globalThis.events[name] = ''
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
/////////////////////////////////////////////////////
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Remove all observers.
|
|
107
|
+
* @returns void
|
|
108
|
+
*/
|
|
109
|
+
removeAll: {
|
|
110
|
+
get: () => {
|
|
111
|
+
Object.entries(observer.list).forEach((el: any) => {
|
|
112
|
+
globalThis.events[el[0]]
|
|
113
|
+
})
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
Object.freeze(observer)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
|
|
2
|
+
Object.defineProperty(
|
|
3
|
+
globalThis,
|
|
4
|
+
'session',
|
|
5
|
+
{
|
|
6
|
+
value: new Proxy({}, {}),
|
|
7
|
+
enumerable: false,
|
|
8
|
+
configurable: false
|
|
9
|
+
}
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
Object.defineProperties(
|
|
13
|
+
session,
|
|
14
|
+
{
|
|
15
|
+
get: {
|
|
16
|
+
value(name: string) {
|
|
17
|
+
if (!name) return
|
|
18
|
+
try {
|
|
19
|
+
const item = sessionStorage.getItem(name)
|
|
20
|
+
if (item) return JSON.parse(item)
|
|
21
|
+
return item
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.error(`Error parsing session item '${name}':`, err)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
set: {
|
|
29
|
+
value(name: string, value: any) {
|
|
30
|
+
if (!name) return
|
|
31
|
+
try {
|
|
32
|
+
|
|
33
|
+
if (value === null || value === undefined) sessionStorage.setItem(name, JSON.stringify(null))
|
|
34
|
+
else if (typeof value === 'object' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string') sessionStorage.setItem(name, JSON.stringify(value))
|
|
35
|
+
else if (typeof value === 'function') console.error('It\'s not secure to session functions.')
|
|
36
|
+
|
|
37
|
+
} catch (err) {
|
|
38
|
+
|
|
39
|
+
console.error(`Error setting session item '${name}':`, err)
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
remove: {
|
|
46
|
+
value(name: string) {
|
|
47
|
+
if (!name) return
|
|
48
|
+
if (sessionStorage.getItem(name)) {
|
|
49
|
+
sessionStorage.removeItem(name)
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
delete: {
|
|
56
|
+
value(name: string) {
|
|
57
|
+
session.remove(name)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
removeAll: {
|
|
63
|
+
value() {
|
|
64
|
+
sessionStorage.clear()
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
clearAll: {
|
|
70
|
+
value() {
|
|
71
|
+
session.removeAll()
|
|
72
|
+
return true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
quota: {
|
|
77
|
+
value() {
|
|
78
|
+
if ('storage' in navigator && 'estimate' in navigator.storage) {
|
|
79
|
+
navigator.storage.estimate()
|
|
80
|
+
.then(
|
|
81
|
+
({ usage, quota }) => {
|
|
82
|
+
if (usage && quota) console.debug(`Using ${usage / 1024} out of ${quota / 1024} Mb.`)
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
.catch(err => { console.error('Error estimating quota:', err) })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
size: {
|
|
91
|
+
value() {
|
|
92
|
+
let totalSize = 0
|
|
93
|
+
for (const key in sessionStorage) {
|
|
94
|
+
if (sessionStorage.hasOwnProperty(key)) {
|
|
95
|
+
const item = sessionStorage.getItem(key)
|
|
96
|
+
if (item) {
|
|
97
|
+
totalSize += item.length
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return totalSize
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
Object.freeze(session)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Object.defineProperty(
|
|
2
|
+
memorio,
|
|
3
|
+
'objPath',
|
|
4
|
+
{
|
|
5
|
+
writable: false,
|
|
6
|
+
configurable: false,
|
|
7
|
+
enumerable: false,
|
|
8
|
+
value: (prop: string, object: string[], separator: string = '.'): string => {
|
|
9
|
+
return object.concat(prop).join(separator)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
/////////////////////////////////////////////////////
|
|
15
|
+
|
|
16
|
+
export const buildProxy = (obj: Record<string, any>, callback: (props: any) => void, tree: string[] = []): any => {
|
|
17
|
+
|
|
18
|
+
// EVENT FUNCTION FOR OBSERVER
|
|
19
|
+
const event = (name: string) => {
|
|
20
|
+
const array = name.split('.')
|
|
21
|
+
array.forEach(
|
|
22
|
+
(x, i) => {
|
|
23
|
+
const command = array.slice(0, i + 1).join('.')
|
|
24
|
+
globalThis.memorio.dispatch.set(command, { detail: { name: command } })
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
///////////////////////////////////////////////
|
|
30
|
+
|
|
31
|
+
// CREATE THE PROXY
|
|
32
|
+
return new Proxy(
|
|
33
|
+
obj,
|
|
34
|
+
{
|
|
35
|
+
get(target: any, prop: any) {
|
|
36
|
+
// Handle special methods first
|
|
37
|
+
if (prop === 'list') {
|
|
38
|
+
const result = {}
|
|
39
|
+
for (const key in target) {
|
|
40
|
+
if (typeof target[key] !== 'function' && !['list', 'remove', 'removeAll'].includes(key)) {
|
|
41
|
+
result[key] = target[key]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (prop === 'remove') {
|
|
48
|
+
return function (key: string) {
|
|
49
|
+
if (key in target && !['list', 'remove', 'removeAll'].includes(key)) {
|
|
50
|
+
delete target[key]
|
|
51
|
+
return true
|
|
52
|
+
}
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (prop === 'removeAll') {
|
|
58
|
+
return function () {
|
|
59
|
+
for (const key in target) {
|
|
60
|
+
if (typeof target[key] !== 'function' && !['list', 'remove', 'removeAll'].includes(key)) {
|
|
61
|
+
delete target[key]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (Object.isFrozen(target[prop])) return target[prop]
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const value = Reflect.get(target, prop)
|
|
72
|
+
if (value && typeof value === 'object' && ['Array', 'Object'].includes(value.constructor.name)) {
|
|
73
|
+
return buildProxy(value, callback, tree.concat(prop as string))
|
|
74
|
+
}
|
|
75
|
+
return value
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('Error: ', error)
|
|
78
|
+
return undefined
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
set(target: any, prop: string, value: any): boolean {
|
|
83
|
+
|
|
84
|
+
if (target[prop] && typeof target[prop] === 'object' && Object.isFrozen(target[prop])) {
|
|
85
|
+
console.error(`Error: state '${prop}' is locked`)
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
|
|
91
|
+
const path = globalThis.memorio.objPath(prop as string, tree)
|
|
92
|
+
|
|
93
|
+
callback(
|
|
94
|
+
{
|
|
95
|
+
action: 'set',
|
|
96
|
+
path,
|
|
97
|
+
target,
|
|
98
|
+
newValue: value,
|
|
99
|
+
previousValue: Reflect.get(target, prop)
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
event('state.' + path)
|
|
104
|
+
|
|
105
|
+
Reflect.set(target, prop, value)
|
|
106
|
+
|
|
107
|
+
// DEFINE LOCK PROPERTY FUNCTION
|
|
108
|
+
if (target[prop] && typeof target[prop] === 'object') {
|
|
109
|
+
|
|
110
|
+
Reflect.defineProperty(
|
|
111
|
+
target[prop],
|
|
112
|
+
'lock',
|
|
113
|
+
{
|
|
114
|
+
value() {
|
|
115
|
+
Object.defineProperty(
|
|
116
|
+
target,
|
|
117
|
+
prop,
|
|
118
|
+
{
|
|
119
|
+
writable: false,
|
|
120
|
+
enumerable: false
|
|
121
|
+
// configurable: false
|
|
122
|
+
}
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
Object.freeze(target[prop])
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return true
|
|
133
|
+
|
|
134
|
+
} catch (error) {
|
|
135
|
+
|
|
136
|
+
console.error('Error in set trap:', error)
|
|
137
|
+
return false
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
deleteProperty(target: any, prop: string | symbol): boolean {
|
|
144
|
+
try {
|
|
145
|
+
const path = globalThis.memorio.objPath(prop as string, tree)
|
|
146
|
+
callback({ action: 'delete', path, target })
|
|
147
|
+
return Reflect.deleteProperty(target, prop)
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error('Error in deleteProperty trap:', error)
|
|
150
|
+
return false
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Initialize base state object
|
|
161
|
+
const baseState = {}
|
|
162
|
+
|
|
163
|
+
// SET STATE AS PROXY
|
|
164
|
+
!globalThis?.state
|
|
165
|
+
? globalThis.state = buildProxy(baseState, () => { })
|
|
166
|
+
: globalThis.state = state
|
|
167
|
+
|
|
168
|
+
///////////////////////////////////////////////
|
|
169
|
+
|
|
170
|
+
// TEST IF STATE IS A PROXY
|
|
171
|
+
const testProxy = new WeakSet()
|
|
172
|
+
|
|
173
|
+
testProxy.add(state)
|
|
174
|
+
|
|
175
|
+
setInterval(
|
|
176
|
+
() => {
|
|
177
|
+
if (!testProxy.has(state)) {
|
|
178
|
+
alert('memorio State is compromised, check if you override it and please reload the page')
|
|
179
|
+
for (let i = 1; i < 99999; i++) clearInterval(i)
|
|
180
|
+
stop()
|
|
181
|
+
}
|
|
182
|
+
}, 1000
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
///////////////////////////////////////////////
|
|
186
|
+
|
|
187
|
+
// DEFINE THE STATE IN GLOBAL
|
|
188
|
+
Object.defineProperty(
|
|
189
|
+
globalThis,
|
|
190
|
+
'state',
|
|
191
|
+
{
|
|
192
|
+
enumerable: false,
|
|
193
|
+
configurable: false
|
|
194
|
+
}
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
///////////////////////////////////////////////
|
|
198
|
+
|
|
199
|
+
// END
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
|
|
2
|
+
Object.defineProperty(
|
|
3
|
+
globalThis,
|
|
4
|
+
'store',
|
|
5
|
+
{
|
|
6
|
+
value: new Proxy({}, {}),
|
|
7
|
+
enumerable: false,
|
|
8
|
+
configurable: false
|
|
9
|
+
}
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
Object.defineProperties(
|
|
13
|
+
store,
|
|
14
|
+
{
|
|
15
|
+
get: {
|
|
16
|
+
value(name: string) {
|
|
17
|
+
if (!name) return
|
|
18
|
+
try {
|
|
19
|
+
const item = localStorage.getItem(name)
|
|
20
|
+
if (item) return JSON.parse(item)
|
|
21
|
+
return item
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.error(`Error parsing store item '${name}':`, err)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
set: {
|
|
29
|
+
value(name: string, value: any) {
|
|
30
|
+
if (!name) return
|
|
31
|
+
try {
|
|
32
|
+
|
|
33
|
+
if (value === null || value === undefined) localStorage.setItem(name, JSON.stringify(null))
|
|
34
|
+
else if (typeof value === 'object' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string') localStorage.setItem(name, JSON.stringify(value))
|
|
35
|
+
else if (typeof value === 'function') console.error('It\'s not secure to store functions.')
|
|
36
|
+
|
|
37
|
+
} catch (err) {
|
|
38
|
+
|
|
39
|
+
console.error(`Error setting store item '${name}':`, err)
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
remove: {
|
|
46
|
+
value(name: string) {
|
|
47
|
+
if (!name) return
|
|
48
|
+
if (localStorage.getItem(name)) {
|
|
49
|
+
localStorage.removeItem(name)
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
delete: {
|
|
56
|
+
value(name: string) {
|
|
57
|
+
store.remove(name)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
removeAll: {
|
|
63
|
+
value() {
|
|
64
|
+
localStorage.clear()
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
clearAll: {
|
|
70
|
+
value() {
|
|
71
|
+
store.removeAll()
|
|
72
|
+
return true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
quota: {
|
|
77
|
+
value() {
|
|
78
|
+
if ('storage' in navigator && 'estimate' in navigator.storage) {
|
|
79
|
+
navigator.storage.estimate()
|
|
80
|
+
.then(
|
|
81
|
+
({ usage, quota }) => {
|
|
82
|
+
if (usage && quota) console.debug(`Using ${usage / 1024} out of ${quota / 1024} Mb.`)
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
.catch(err => { console.error('Error estimating quota:', err) })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
size: {
|
|
91
|
+
value() {
|
|
92
|
+
let totalSize = 0
|
|
93
|
+
for (const key in localStorage) {
|
|
94
|
+
if (localStorage.hasOwnProperty(key)) {
|
|
95
|
+
const item = localStorage.getItem(key)
|
|
96
|
+
if (item) {
|
|
97
|
+
totalSize += item.length
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return totalSize
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
Object.freeze(store)
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./init"
|
package/init.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// LOAD SCRIPTS
|
|
3
|
+
|
|
4
|
+
import "dphelper.types"
|
|
5
|
+
|
|
6
|
+
import './config/global'
|
|
7
|
+
import './config/dispatch'
|
|
8
|
+
import './functions/state'
|
|
9
|
+
import './functions/observer'
|
|
10
|
+
import './functions/store'
|
|
11
|
+
import './functions/session'
|
|
12
|
+
import './functions/cache'
|
package/jest.config.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** @type {import('jest').Config} */
|
|
2
|
+
export default {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'jsdom',
|
|
5
|
+
setupFilesAfterEnv: ['./tests/setup.ts'],
|
|
6
|
+
transform: {
|
|
7
|
+
'^.+\\.tsx?$': ['ts-jest', {
|
|
8
|
+
useESM: true
|
|
9
|
+
}]
|
|
10
|
+
},
|
|
11
|
+
extensionsToTreatAsEsm: ['.ts'],
|
|
12
|
+
moduleNameMapper: {
|
|
13
|
+
'^(\\.{1,2}/.*)\\.js$': '$1'
|
|
14
|
+
},
|
|
15
|
+
collectCoverageFrom: [
|
|
16
|
+
'functions/**/*.ts',
|
|
17
|
+
'config/**/*.ts',
|
|
18
|
+
'*.ts',
|
|
19
|
+
'!**/*.d.ts',
|
|
20
|
+
'!**/node_modules/**',
|
|
21
|
+
'!**/dist/**'
|
|
22
|
+
],
|
|
23
|
+
coverageDirectory: 'coverage',
|
|
24
|
+
coverageReporters: ['json', 'lcov', 'text', 'clover'],
|
|
25
|
+
coverageThreshold: {
|
|
26
|
+
global: {
|
|
27
|
+
branches: 80,
|
|
28
|
+
functions: 80,
|
|
29
|
+
lines: 80,
|
|
30
|
+
statements: 80
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/jest.setup.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "memorio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,9 +21,6 @@
|
|
|
21
21
|
"typings": "./types/*",
|
|
22
22
|
"description": "Memorio, State + Observer and Store for a easy life ",
|
|
23
23
|
"license": "MIT",
|
|
24
|
-
"deprecated": false,
|
|
25
|
-
"preferGlobal": true,
|
|
26
|
-
"target": "node",
|
|
27
24
|
"copyright": "Dario Passariello, BigLogic ca - a51.dev is a BigLogic project",
|
|
28
25
|
"homepage": "https://a51.gitbook.io/memorio",
|
|
29
26
|
"author": {
|
|
@@ -46,9 +43,6 @@
|
|
|
46
43
|
"email": "valeriacalascaglitta@gmail.com"
|
|
47
44
|
}
|
|
48
45
|
],
|
|
49
|
-
"globals": {
|
|
50
|
-
"memorio": {}
|
|
51
|
-
},
|
|
52
46
|
"keywords": [
|
|
53
47
|
"biglogic",
|
|
54
48
|
"a51",
|
|
@@ -83,8 +77,8 @@
|
|
|
83
77
|
"@eslint/js": "9.34.0",
|
|
84
78
|
"@types/jest": "30.0.0",
|
|
85
79
|
"@types/node": "^24.3.0",
|
|
86
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
87
|
-
"@typescript-eslint/parser": "8.
|
|
80
|
+
"@typescript-eslint/eslint-plugin": "8.41.0",
|
|
81
|
+
"@typescript-eslint/parser": "8.41.0",
|
|
88
82
|
"dphelper.types": "0.0.19",
|
|
89
83
|
"esbuild": "^0.25.9",
|
|
90
84
|
"esbuild-plugin-clean": "^1.0.1",
|
|
@@ -93,7 +87,7 @@
|
|
|
93
87
|
"jest": "30.0.5",
|
|
94
88
|
"jest-environment-jsdom": "30.0.5",
|
|
95
89
|
"ts-jest": "29.4.1",
|
|
96
|
-
"ts-loader": "^9.5.
|
|
90
|
+
"ts-loader": "^9.5.4",
|
|
97
91
|
"ts-node": "10.9.2",
|
|
98
92
|
"tslib": "^2.8.1",
|
|
99
93
|
"typescript": "5.9.2"
|