@medyll/idae-stator 0.1.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/README.md +0 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/stator/Stator.d.ts +32 -0
- package/dist/stator/Stator.js +99 -0
- package/package.json +57 -0
package/README.md
ADDED
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Represents primitive types */
|
|
2
|
+
type Primitive = string | number | boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Represents the state type
|
|
5
|
+
* For primitives, it wraps the value in an object
|
|
6
|
+
* For non-primitives, it keeps the original type
|
|
7
|
+
*/
|
|
8
|
+
type State<T> = {
|
|
9
|
+
stator: T;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Callback function type for state changes
|
|
13
|
+
* @param oldValue The previous value of the state
|
|
14
|
+
* @param newValue The new value of the state
|
|
15
|
+
*/
|
|
16
|
+
type StateChangeHandler<T> = (oldValue: T, newValue: T) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Augmented state type that includes the onchange handler
|
|
19
|
+
*/
|
|
20
|
+
type AugmentedState<T> = State<T> & {
|
|
21
|
+
onchange?: StateChangeHandler<T>;
|
|
22
|
+
toString: () => string;
|
|
23
|
+
valueOf: () => T;
|
|
24
|
+
[Symbol.toPrimitive]: (hint: string) => Primitive;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Creates a stateful object with change tracking
|
|
28
|
+
* @param initialState The initial state value
|
|
29
|
+
* @returns A proxy object that wraps the state and provides change tracking
|
|
30
|
+
*/
|
|
31
|
+
export declare function stator<T>(initialState: T): AugmentedState<T>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a stateful object with change tracking
|
|
3
|
+
* @param initialState The initial state value
|
|
4
|
+
* @returns A proxy object that wraps the state and provides change tracking
|
|
5
|
+
*/
|
|
6
|
+
export function stator(initialState) {
|
|
7
|
+
/**
|
|
8
|
+
* Checks if a value is of primitive type
|
|
9
|
+
* @param val The value to check
|
|
10
|
+
* @returns True if the value is a primitive, false otherwise
|
|
11
|
+
*/
|
|
12
|
+
function isPrimitive(val) {
|
|
13
|
+
return typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Logs errors with a custom prefix
|
|
17
|
+
* @param message The error message
|
|
18
|
+
* @param error The error object
|
|
19
|
+
*/
|
|
20
|
+
function logError(message, error) {
|
|
21
|
+
console.error(`[Stator Error] ${message}`, error);
|
|
22
|
+
}
|
|
23
|
+
// Create the state object
|
|
24
|
+
const state = { stator: initialState };
|
|
25
|
+
// Default onchange handler
|
|
26
|
+
let onchange;
|
|
27
|
+
// Proxy handler to intercept property access and modifications
|
|
28
|
+
const handler = {
|
|
29
|
+
get(target, property, receiver) {
|
|
30
|
+
try {
|
|
31
|
+
if (property === 'onchange') {
|
|
32
|
+
return onchange;
|
|
33
|
+
}
|
|
34
|
+
if (property === 'stator') {
|
|
35
|
+
return target.stator;
|
|
36
|
+
}
|
|
37
|
+
if (property === 'toString') {
|
|
38
|
+
return function () {
|
|
39
|
+
return String(target.stator);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (property === 'valueOf') {
|
|
43
|
+
return function () {
|
|
44
|
+
return target.stator;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (property === Symbol.toPrimitive) {
|
|
48
|
+
return function (hint) {
|
|
49
|
+
if (hint === 'number')
|
|
50
|
+
return Number(target.stator);
|
|
51
|
+
if (hint === 'string')
|
|
52
|
+
return String(target.stator);
|
|
53
|
+
return isPrimitive(target.stator) ? target.stator : String(target.stator);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return Reflect.get(target.stator, property, receiver);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
logError(`Error getting property ${String(property)}:`, error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
set(target, property, value, receiver) {
|
|
64
|
+
if (property === 'onchange') {
|
|
65
|
+
if (typeof value !== 'function') {
|
|
66
|
+
throw new TypeError('onchange must be a function');
|
|
67
|
+
}
|
|
68
|
+
onchange = value;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const oldValue = target.stator;
|
|
73
|
+
let newValue;
|
|
74
|
+
if (property === 'stator') {
|
|
75
|
+
newValue = value;
|
|
76
|
+
target.stator = value;
|
|
77
|
+
}
|
|
78
|
+
else if (typeof target.stator === 'object' && target.stator !== null) {
|
|
79
|
+
Reflect.set(target.stator, property, value);
|
|
80
|
+
newValue = { ...target.stator };
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
throw new Error('Cannot set property on primitive value');
|
|
84
|
+
}
|
|
85
|
+
if (onchange && !Object.is(oldValue, newValue)) {
|
|
86
|
+
onchange(oldValue, newValue);
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
logError(`Error setting property ${String(property)}:`, error);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// ... (autres méthodes inchangées)
|
|
96
|
+
};
|
|
97
|
+
// Create and return the proxied state object
|
|
98
|
+
return new Proxy(state, handler);
|
|
99
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@medyll/idae-stator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"dev": "vite dev",
|
|
6
|
+
"build": "vite build && npm run package",
|
|
7
|
+
"preview": "vite preview",
|
|
8
|
+
"package": "svelte-kit sync && svelte-package && publint",
|
|
9
|
+
"package:watch": "svelte-kit sync && svelte-package --watch",
|
|
10
|
+
"prepublishOnly": "npm run package",
|
|
11
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
12
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
13
|
+
"test": "vitest",
|
|
14
|
+
"lint": "prettier --check . && eslint .",
|
|
15
|
+
"format": "prettier --write ."
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"svelte": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"!dist/**/*.test.*",
|
|
26
|
+
"!dist/**/*.spec.*"
|
|
27
|
+
],
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"svelte": "^5.0.0-next.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@medyll/idae-prettier-config": "^1.0.0",
|
|
33
|
+
"@sveltejs/adapter-auto": "^3.0.0",
|
|
34
|
+
"@sveltejs/kit": "^2.0.0",
|
|
35
|
+
"@sveltejs/package": "^2.0.0",
|
|
36
|
+
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
|
37
|
+
"@types/eslint": "^8.56.7",
|
|
38
|
+
"eslint": "^9.0.0",
|
|
39
|
+
"eslint-config-prettier": "^9.1.0",
|
|
40
|
+
"eslint-plugin-svelte": "^2.36.0",
|
|
41
|
+
"globals": "^15.0.0",
|
|
42
|
+
"prettier": "^3.1.1",
|
|
43
|
+
"prettier-plugin-svelte": "^3.1.2",
|
|
44
|
+
"publint": "^0.1.9",
|
|
45
|
+
"svelte": "^5.0.0-next.183",
|
|
46
|
+
"svelte-check": "^3.6.0",
|
|
47
|
+
"tslib": "^2.4.1",
|
|
48
|
+
"typescript": "^5.0.0",
|
|
49
|
+
"typescript-eslint": "^8.0.0-alpha.20",
|
|
50
|
+
"vite": "^5.0.11",
|
|
51
|
+
"vitest": "^1.2.0"
|
|
52
|
+
},
|
|
53
|
+
"svelte": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"type": "module",
|
|
56
|
+
"scope": "medyll"
|
|
57
|
+
}
|