@view-models/core 1.0.0 → 1.2.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 CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A lightweight, framework-agnostic library for building reactive view models with TypeScript. Separate your business logic from your UI framework with a simple, testable pattern.
4
4
 
5
+ ![View models banner](./view-models.png)
6
+
5
7
  ## Why View Models?
6
8
 
7
9
  - **Framework Agnostic**: Write your logic once, use it with React, Preact, or any other framework
@@ -85,8 +87,8 @@ describe("CounterViewModel", () => {
85
87
 
86
88
  The view models are designed to work with framework-specific adapters. Upcoming adapters include:
87
89
 
88
- - **@view-models/react** - React hooks integration
89
- - **@view-models/preact** - Preact hooks integration
90
+ - [@view-models/react](https://github.com/sunesimonsen/view-models-react) - React hooks integration
91
+ - [@view-models/preact](https://github.com/sunesimonsen/view-models-preact) - Preact hooks integration
90
92
 
91
93
  These adapters will allow you to use the same view model with different frameworks:
92
94
 
@@ -200,9 +202,10 @@ class AppViewModel {
200
202
  }
201
203
  ```
202
204
 
203
- ### Handle Side Effects
205
+ ### Asynchronous actions
204
206
 
205
- Keep your update logic pure, but expose methods for side effects:
207
+ While the update logic is pure, you can use methods to execute asynchronous
208
+ actions:
206
209
 
207
210
  ```typescript
208
211
  class TodosViewModel extends ViewModel<TodosState> {
@@ -1,4 +1,3 @@
1
- type State = Readonly<object>;
2
1
  /**
3
2
  * Function that receives the current state and returns the new state.
4
3
  * The updater function should be pure and return a new state object.
@@ -7,7 +6,7 @@ type State = Readonly<object>;
7
6
  * @param currentState - The current state
8
7
  * @returns The new state
9
8
  */
10
- export type Updater<T extends State> = (currentState: T) => T;
9
+ export type Updater<T> = (currentState: T) => T;
11
10
  /**
12
11
  * Function that gets called when the state changes.
13
12
  *
@@ -21,7 +20,7 @@ export type ViewModelListener<T> = (state: T) => void;
21
20
  * A ViewModel manages state and notifies subscribers when the state changes.
22
21
  * Extend this class to create your own view models with custom business logic.
23
22
  *
24
- * @template T - The state type (must be a readonly object)
23
+ * @template T - The state type
25
24
  *
26
25
  * @example
27
26
  * ```typescript
@@ -44,7 +43,7 @@ export type ViewModelListener<T> = (state: T) => void;
44
43
  * counter.increment(); // Logs: Count: 1
45
44
  * ```
46
45
  */
47
- export declare abstract class ViewModel<T extends State> {
46
+ export declare abstract class ViewModel<T> {
48
47
  private _listeners;
49
48
  /**
50
49
  * Subscribe to state changes.
@@ -97,5 +96,4 @@ export declare abstract class ViewModel<T extends State> {
97
96
  */
98
97
  get state(): T;
99
98
  }
100
- export {};
101
99
  //# sourceMappingURL=ViewModel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ViewModel.d.ts","sourceRoot":"","sources":["../src/ViewModel.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;AAE9B;;;;;;;GAOG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,KAAK,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,8BAAsB,SAAS,CAAC,CAAC,SAAS,KAAK;IAC7C,OAAO,CAAC,UAAU,CAAwC;IAE1D;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAOrD,OAAO,CAAC,MAAM,CAAI;IAElB;;;;OAIG;gBACS,YAAY,EAAE,CAAC;IAI3B;;;;;;;;;;;;;;;;OAgBG;IACH,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAQpC;;;;OAIG;IACH,IAAI,KAAK,IAAI,CAAC,CAEb;CACF"}
1
+ {"version":3,"file":"ViewModel.d.ts","sourceRoot":"","sources":["../src/ViewModel.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,8BAAsB,SAAS,CAAC,CAAC;IAC/B,OAAO,CAAC,UAAU,CAAwC;IAE1D;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAOrD,OAAO,CAAC,MAAM,CAAI;IAElB;;;;OAIG;gBACS,YAAY,EAAE,CAAC;IAI3B;;;;;;;;;;;;;;;;OAgBG;IACH,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAQpC;;;;OAIG;IACH,IAAI,KAAK,IAAI,CAAC,CAEb;CACF"}
package/dist/ViewModel.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * A ViewModel manages state and notifies subscribers when the state changes.
5
5
  * Extend this class to create your own view models with custom business logic.
6
6
  *
7
- * @template T - The state type (must be a readonly object)
7
+ * @template T - The state type
8
8
  *
9
9
  * @example
10
10
  * ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@view-models/core",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "A lightweight, framework-agnostic library for building reactive view models with TypeScript",
5
5
  "keywords": [
6
6
  "view",
package/src/ViewModel.ts CHANGED
@@ -1,5 +1,3 @@
1
- type State = Readonly<object>;
2
-
3
1
  /**
4
2
  * Function that receives the current state and returns the new state.
5
3
  * The updater function should be pure and return a new state object.
@@ -8,7 +6,7 @@ type State = Readonly<object>;
8
6
  * @param currentState - The current state
9
7
  * @returns The new state
10
8
  */
11
- export type Updater<T extends State> = (currentState: T) => T;
9
+ export type Updater<T> = (currentState: T) => T;
12
10
 
13
11
  /**
14
12
  * Function that gets called when the state changes.
@@ -24,7 +22,7 @@ export type ViewModelListener<T> = (state: T) => void;
24
22
  * A ViewModel manages state and notifies subscribers when the state changes.
25
23
  * Extend this class to create your own view models with custom business logic.
26
24
  *
27
- * @template T - The state type (must be a readonly object)
25
+ * @template T - The state type
28
26
  *
29
27
  * @example
30
28
  * ```typescript
@@ -47,7 +45,7 @@ export type ViewModelListener<T> = (state: T) => void;
47
45
  * counter.increment(); // Logs: Count: 1
48
46
  * ```
49
47
  */
50
- export abstract class ViewModel<T extends State> {
48
+ export abstract class ViewModel<T> {
51
49
  private _listeners: Set<ViewModelListener<T>> = new Set();
52
50
 
53
51
  /**