@view-models/preact 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/LICENSE +21 -0
- package/README.md +132 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/useModelState.d.ts +32 -0
- package/dist/useModelState.d.ts.map +1 -0
- package/dist/useModelState.js +37 -0
- package/dist/useSyncExternalStore.d.ts +12 -0
- package/dist/useSyncExternalStore.d.ts.map +1 -0
- package/dist/useSyncExternalStore.js +48 -0
- package/package.json +64 -0
- package/src/index.ts +1 -0
- package/src/useModelState.ts +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sune Simonsen <sune@we-knowhow.dk>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @view-models/preact
|
|
2
|
+
|
|
3
|
+
Preact integration for [@view-models/core](https://github.com/sunesimonsen/view-models-core).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @view-models/preact @view-models/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The `useModelState` hook allows you to subscribe to state updates from a `ViewModel` instance.
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { ViewModel } from "@view-models/core";
|
|
17
|
+
import { useModelState } from "@view-models/preact";
|
|
18
|
+
|
|
19
|
+
type CounterState = Readonly<{ count: number }>;
|
|
20
|
+
|
|
21
|
+
class CounterViewModel extends ViewModel<CounterState> {
|
|
22
|
+
increment = () => {
|
|
23
|
+
this.update(({ count }) => ({ count: count + 1 }));
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
decrement = () => {
|
|
27
|
+
this.update(({ count }) => ({ count: count - 1 }));
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const counterModel = new CounterViewModel({ count: 0 });
|
|
32
|
+
|
|
33
|
+
function Counter() {
|
|
34
|
+
const { count } = useModelState(counterModel);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<p>Count: {count}</p>
|
|
39
|
+
<button onClick={counterModel.increment}>+</button>
|
|
40
|
+
<button onClick={counterModel.decrement}>-</button>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Creating view models inside components
|
|
47
|
+
|
|
48
|
+
When you need to create a view model from within a Preact component, use `useMemo` to ensure the model is only created once.
|
|
49
|
+
|
|
50
|
+
It's good practice to provide both a component that creates its own model and a customizable version that accepts a model as a prop:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useMemo } from "preact/hooks";
|
|
54
|
+
import { ViewModel } from "@view-models/core";
|
|
55
|
+
import { useModelState } from "@view-models/preact";
|
|
56
|
+
|
|
57
|
+
type CounterState = Readonly<{ count: number }>;
|
|
58
|
+
|
|
59
|
+
class CounterViewModel extends ViewModel<CounterState> {
|
|
60
|
+
increment = () => {
|
|
61
|
+
this.update(({ count }) => ({ count: count + 1 }));
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
decrement = () => {
|
|
65
|
+
this.update(({ count }) => ({ count: count - 1 }));
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Customizable version that accepts a model
|
|
70
|
+
function CustomCounter({ model }: { model: CounterViewModel }) {
|
|
71
|
+
const { count } = useModelState(model);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<div>
|
|
75
|
+
<p>Count: {count}</p>
|
|
76
|
+
<button onClick={model.increment}>+</button>
|
|
77
|
+
<button onClick={model.decrement}>-</button>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Simple version that creates its own model
|
|
83
|
+
function Counter() {
|
|
84
|
+
const model = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
85
|
+
return <CustomCounter model={model} />;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API
|
|
90
|
+
|
|
91
|
+
### `useModelState<T>(model: ViewModel<T>): T`
|
|
92
|
+
|
|
93
|
+
A Preact hook that subscribes to state updates from a ViewModel instance.
|
|
94
|
+
|
|
95
|
+
**Parameters:**
|
|
96
|
+
|
|
97
|
+
- `model`: A ViewModel instance to subscribe to
|
|
98
|
+
|
|
99
|
+
**Returns:**
|
|
100
|
+
|
|
101
|
+
- The current state of the ViewModel
|
|
102
|
+
|
|
103
|
+
**Features:**
|
|
104
|
+
|
|
105
|
+
- Automatically subscribes to state updates when the component mounts
|
|
106
|
+
- Automatically unsubscribes when the component unmounts
|
|
107
|
+
- Re-renders the component when the ViewModel state changes
|
|
108
|
+
- Multiple components can subscribe to the same ViewModel
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT License
|
|
113
|
+
|
|
114
|
+
Copyright (c) 2026 Sune Simonsen <sune@we-knowhow.dk>
|
|
115
|
+
|
|
116
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
117
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
118
|
+
in the Software without restriction, including without limitation the rights
|
|
119
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
120
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
121
|
+
furnished to do so, subject to the following conditions:
|
|
122
|
+
|
|
123
|
+
The above copyright notice and this permission notice shall be included in all
|
|
124
|
+
copies or substantial portions of the Software.
|
|
125
|
+
|
|
126
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
127
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
128
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
129
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
130
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
131
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
132
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useModelState.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ViewModel, State } from "@view-models/core";
|
|
2
|
+
/**
|
|
3
|
+
* A Preact hook that subscribes a component to a ViewModel's state updates.
|
|
4
|
+
*
|
|
5
|
+
* This hook connects a Preact component to a ViewModel instance, automatically
|
|
6
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The state type, which must extend the State interface
|
|
9
|
+
* @param model - The ViewModel instance to subscribe to
|
|
10
|
+
* @returns The current state from the ViewModel
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
15
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function Counter() {
|
|
19
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
20
|
+
* const { count } = useModelState(counterModel);
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <div>
|
|
24
|
+
* <p>Count: {count}</p>
|
|
25
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function useModelState<T extends State>(model: ViewModel<T>): T;
|
|
32
|
+
//# sourceMappingURL=useModelState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useModelState.d.ts","sourceRoot":"","sources":["../src/useModelState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAYrE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useEffect, useState } from "preact/hooks";
|
|
2
|
+
/**
|
|
3
|
+
* A Preact hook that subscribes a component to a ViewModel's state updates.
|
|
4
|
+
*
|
|
5
|
+
* This hook connects a Preact component to a ViewModel instance, automatically
|
|
6
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The state type, which must extend the State interface
|
|
9
|
+
* @param model - The ViewModel instance to subscribe to
|
|
10
|
+
* @returns The current state from the ViewModel
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
15
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function Counter() {
|
|
19
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
20
|
+
* const { count } = useModelState(counterModel);
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <div>
|
|
24
|
+
* <p>Count: {count}</p>
|
|
25
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useModelState(model) {
|
|
32
|
+
const [state, setState] = useState(model.state);
|
|
33
|
+
useEffect(() => model.subscribe((newState) => {
|
|
34
|
+
setState(newState);
|
|
35
|
+
}), [model]);
|
|
36
|
+
return state;
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom implementation of useSyncExternalStore for Preact.
|
|
3
|
+
*
|
|
4
|
+
* This hook subscribes to an external store and returns its current state.
|
|
5
|
+
* It ensures that the component re-renders when the store's state changes.
|
|
6
|
+
*
|
|
7
|
+
* @param subscribe - Function to subscribe to the store. Should return an unsubscribe function.
|
|
8
|
+
* @param getSnapshot - Function that returns the current state of the store.
|
|
9
|
+
* @returns The current state from the store.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useSyncExternalStore<T>(subscribe: (callback: () => void) => () => void, getSnapshot: () => T): T;
|
|
12
|
+
//# sourceMappingURL=useSyncExternalStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSyncExternalStore.d.ts","sourceRoot":"","sources":["../src/useSyncExternalStore.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,EAC/C,WAAW,EAAE,MAAM,CAAC,GACnB,CAAC,CA2CH"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useState, useEffect } from "preact/hooks";
|
|
2
|
+
/**
|
|
3
|
+
* Custom implementation of useSyncExternalStore for Preact.
|
|
4
|
+
*
|
|
5
|
+
* This hook subscribes to an external store and returns its current state.
|
|
6
|
+
* It ensures that the component re-renders when the store's state changes.
|
|
7
|
+
*
|
|
8
|
+
* @param subscribe - Function to subscribe to the store. Should return an unsubscribe function.
|
|
9
|
+
* @param getSnapshot - Function that returns the current state of the store.
|
|
10
|
+
* @returns The current state from the store.
|
|
11
|
+
*/
|
|
12
|
+
export function useSyncExternalStore(subscribe, getSnapshot) {
|
|
13
|
+
// Store the snapshot in an object to enable proper equality checks
|
|
14
|
+
// This allows us to use Object.is in the setState updater
|
|
15
|
+
const [state, setState] = useState(() => ({
|
|
16
|
+
snapshot: getSnapshot(),
|
|
17
|
+
}));
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
// Track if the component is still mounted to prevent setState on unmounted component
|
|
20
|
+
let isMounted = true;
|
|
21
|
+
// Callback invoked when the external store updates
|
|
22
|
+
const handleUpdate = () => {
|
|
23
|
+
if (!isMounted)
|
|
24
|
+
return;
|
|
25
|
+
const newSnapshot = getSnapshot();
|
|
26
|
+
// Only update state if the snapshot has changed
|
|
27
|
+
// This prevents unnecessary re-renders
|
|
28
|
+
setState((prev) => {
|
|
29
|
+
if (Object.is(prev.snapshot, newSnapshot)) {
|
|
30
|
+
return prev;
|
|
31
|
+
}
|
|
32
|
+
return { snapshot: newSnapshot };
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
// Subscribe to the external store
|
|
36
|
+
const unsubscribe = subscribe(handleUpdate);
|
|
37
|
+
// Check for updates that may have occurred during subscription
|
|
38
|
+
// This handles the race condition where the store updates between
|
|
39
|
+
// the initial render and the effect running
|
|
40
|
+
handleUpdate();
|
|
41
|
+
// Cleanup: mark as unmounted and unsubscribe
|
|
42
|
+
return () => {
|
|
43
|
+
isMounted = false;
|
|
44
|
+
unsubscribe();
|
|
45
|
+
};
|
|
46
|
+
}, [subscribe, getSnapshot]);
|
|
47
|
+
return state.snapshot;
|
|
48
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@view-models/preact",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Preact integration for @view-models/core",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:watch": "vitest",
|
|
23
|
+
"test:coverage": "vitest run --coverage",
|
|
24
|
+
"format": "prettier --write .",
|
|
25
|
+
"format:check": "prettier --check .",
|
|
26
|
+
"prepublishOnly": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"preact",
|
|
30
|
+
"hooks",
|
|
31
|
+
"view-models",
|
|
32
|
+
"state-management"
|
|
33
|
+
],
|
|
34
|
+
"author": "Sune Simonsen",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/sunesimonsen/view-models-preact.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/sunesimonsen/view-models-preact/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/sunesimonsen/view-models-preact#readme",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@view-models/core": "^1.1.0",
|
|
49
|
+
"preact": ">=10.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@testing-library/preact": "^3.2.4",
|
|
53
|
+
"@view-models/core": "^1.1.0",
|
|
54
|
+
"@vitest/coverage-v8": "^2.1.8",
|
|
55
|
+
"jsdom": "^26.0.0",
|
|
56
|
+
"preact": "^10.26.1",
|
|
57
|
+
"prettier": "^3.4.2",
|
|
58
|
+
"typescript": "^5.7.3",
|
|
59
|
+
"vitest": "^2.1.8"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useModelState.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ViewModel, State } from "@view-models/core";
|
|
2
|
+
import { useEffect, useState } from "preact/hooks";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A Preact hook that subscribes a component to a ViewModel's state updates.
|
|
6
|
+
*
|
|
7
|
+
* This hook connects a Preact component to a ViewModel instance, automatically
|
|
8
|
+
* subscribing to state changes and triggering re-renders when the state updates.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The state type, which must extend the State interface
|
|
11
|
+
* @param model - The ViewModel instance to subscribe to
|
|
12
|
+
* @returns The current state from the ViewModel
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* class CounterViewModel extends ViewModel<{ count: number }> {
|
|
17
|
+
* increment = () => this.update(({ count }) => ({ count: count + 1 }));
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* function Counter() {
|
|
21
|
+
* const counterModel = useMemo(() => new CounterViewModel({ count: 0 }), []);
|
|
22
|
+
* const { count } = useModelState(counterModel);
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <div>
|
|
26
|
+
* <p>Count: {count}</p>
|
|
27
|
+
* <button onClick={counterModel.increment}>+</button>
|
|
28
|
+
* </div>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function useModelState<T extends State>(model: ViewModel<T>): T {
|
|
34
|
+
const [state, setState] = useState<T>(model.state);
|
|
35
|
+
|
|
36
|
+
useEffect(
|
|
37
|
+
() =>
|
|
38
|
+
model.subscribe((newState) => {
|
|
39
|
+
setState(newState);
|
|
40
|
+
}),
|
|
41
|
+
[model],
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return state;
|
|
45
|
+
}
|