@view-models/core 1.1.0 → 2.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/README.md +2 -2
- package/dist/ViewModel.d.ts +8 -12
- package/dist/ViewModel.d.ts.map +1 -1
- package/dist/ViewModel.js +2 -2
- package/package.json +1 -1
- package/src/ViewModel.ts +11 -16
package/README.md
CHANGED
|
@@ -87,8 +87,8 @@ describe("CounterViewModel", () => {
|
|
|
87
87
|
|
|
88
88
|
The view models are designed to work with framework-specific adapters. Upcoming adapters include:
|
|
89
89
|
|
|
90
|
-
-
|
|
91
|
-
-
|
|
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
|
|
92
92
|
|
|
93
93
|
These adapters will allow you to use the same view model with different frameworks:
|
|
94
94
|
|
package/dist/ViewModel.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The base type of view model state.
|
|
3
|
-
*/
|
|
4
|
-
export type State = Readonly<object>;
|
|
5
1
|
/**
|
|
6
2
|
* Function that receives the current state and returns the new state.
|
|
7
3
|
* The updater function should be pure and return a new state object.
|
|
@@ -10,21 +6,21 @@ export type State = Readonly<object>;
|
|
|
10
6
|
* @param currentState - The current state
|
|
11
7
|
* @returns The new state
|
|
12
8
|
*/
|
|
13
|
-
export type Updater<T
|
|
9
|
+
export type Updater<T> = (currentState: T) => T;
|
|
14
10
|
/**
|
|
15
11
|
* Function that gets called when the state changes.
|
|
16
12
|
*
|
|
17
13
|
* @template T - The state type
|
|
18
14
|
* @param state - The new state
|
|
19
15
|
*/
|
|
20
|
-
export type ViewModelListener
|
|
16
|
+
export type ViewModelListener = () => void;
|
|
21
17
|
/**
|
|
22
18
|
* Abstract base class for creating reactive view models.
|
|
23
19
|
*
|
|
24
20
|
* A ViewModel manages state and notifies subscribers when the state changes.
|
|
25
21
|
* Extend this class to create your own view models with custom business logic.
|
|
26
22
|
*
|
|
27
|
-
* @template
|
|
23
|
+
* @template S - The state type
|
|
28
24
|
*
|
|
29
25
|
* @example
|
|
30
26
|
* ```typescript
|
|
@@ -47,7 +43,7 @@ export type ViewModelListener<T> = (state: T) => void;
|
|
|
47
43
|
* counter.increment(); // Logs: Count: 1
|
|
48
44
|
* ```
|
|
49
45
|
*/
|
|
50
|
-
export declare abstract class ViewModel<
|
|
46
|
+
export declare abstract class ViewModel<S> {
|
|
51
47
|
private _listeners;
|
|
52
48
|
/**
|
|
53
49
|
* Subscribe to state changes.
|
|
@@ -67,14 +63,14 @@ export declare abstract class ViewModel<T extends State> {
|
|
|
67
63
|
* unsubscribe();
|
|
68
64
|
* ```
|
|
69
65
|
*/
|
|
70
|
-
subscribe(listener: ViewModelListener
|
|
66
|
+
subscribe(listener: ViewModelListener): () => void;
|
|
71
67
|
private _state;
|
|
72
68
|
/**
|
|
73
69
|
* Create a new ViewModel with the given initial state.
|
|
74
70
|
*
|
|
75
71
|
* @param initialState - The initial state of the view model
|
|
76
72
|
*/
|
|
77
|
-
constructor(initialState:
|
|
73
|
+
constructor(initialState: S);
|
|
78
74
|
/**
|
|
79
75
|
* Update the state and notify all subscribers.
|
|
80
76
|
*
|
|
@@ -92,12 +88,12 @@ export declare abstract class ViewModel<T extends State> {
|
|
|
92
88
|
* }));
|
|
93
89
|
* ```
|
|
94
90
|
*/
|
|
95
|
-
protected update(updater: Updater<
|
|
91
|
+
protected update(updater: Updater<S>): void;
|
|
96
92
|
/**
|
|
97
93
|
* Get the current state.
|
|
98
94
|
*
|
|
99
95
|
* @returns The current state
|
|
100
96
|
*/
|
|
101
|
-
get state():
|
|
97
|
+
get state(): S;
|
|
102
98
|
}
|
|
103
99
|
//# sourceMappingURL=ViewModel.d.ts.map
|
package/dist/ViewModel.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ViewModel.d.ts","sourceRoot":"","sources":["../src/ViewModel.ts"],"names":[],"mappings":"AAAA
|
|
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,GAAG,MAAM,IAAI,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,8BAAsB,SAAS,CAAC,CAAC;IAC/B,OAAO,CAAC,UAAU,CAAqC;IAEvD;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,IAAI;IAOlD,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
|
|
7
|
+
* @template S - The state type
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```typescript
|
|
@@ -81,7 +81,7 @@ export class ViewModel {
|
|
|
81
81
|
update(updater) {
|
|
82
82
|
this._state = updater(this._state);
|
|
83
83
|
for (const listener of this._listeners) {
|
|
84
|
-
listener(
|
|
84
|
+
listener();
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**
|
package/package.json
CHANGED
package/src/ViewModel.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The base type of view model state.
|
|
3
|
-
*/
|
|
4
|
-
export type State = Readonly<object>;
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* Function that receives the current state and returns the new state.
|
|
8
3
|
* The updater function should be pure and return a new state object.
|
|
@@ -11,7 +6,7 @@ export type State = Readonly<object>;
|
|
|
11
6
|
* @param currentState - The current state
|
|
12
7
|
* @returns The new state
|
|
13
8
|
*/
|
|
14
|
-
export type Updater<T
|
|
9
|
+
export type Updater<T> = (currentState: T) => T;
|
|
15
10
|
|
|
16
11
|
/**
|
|
17
12
|
* Function that gets called when the state changes.
|
|
@@ -19,7 +14,7 @@ export type Updater<T extends State> = (currentState: T) => T;
|
|
|
19
14
|
* @template T - The state type
|
|
20
15
|
* @param state - The new state
|
|
21
16
|
*/
|
|
22
|
-
export type ViewModelListener
|
|
17
|
+
export type ViewModelListener = () => void;
|
|
23
18
|
|
|
24
19
|
/**
|
|
25
20
|
* Abstract base class for creating reactive view models.
|
|
@@ -27,7 +22,7 @@ export type ViewModelListener<T> = (state: T) => void;
|
|
|
27
22
|
* A ViewModel manages state and notifies subscribers when the state changes.
|
|
28
23
|
* Extend this class to create your own view models with custom business logic.
|
|
29
24
|
*
|
|
30
|
-
* @template
|
|
25
|
+
* @template S - The state type
|
|
31
26
|
*
|
|
32
27
|
* @example
|
|
33
28
|
* ```typescript
|
|
@@ -50,8 +45,8 @@ export type ViewModelListener<T> = (state: T) => void;
|
|
|
50
45
|
* counter.increment(); // Logs: Count: 1
|
|
51
46
|
* ```
|
|
52
47
|
*/
|
|
53
|
-
export abstract class ViewModel<
|
|
54
|
-
private _listeners: Set<ViewModelListener
|
|
48
|
+
export abstract class ViewModel<S> {
|
|
49
|
+
private _listeners: Set<ViewModelListener> = new Set();
|
|
55
50
|
|
|
56
51
|
/**
|
|
57
52
|
* Subscribe to state changes.
|
|
@@ -71,21 +66,21 @@ export abstract class ViewModel<T extends State> {
|
|
|
71
66
|
* unsubscribe();
|
|
72
67
|
* ```
|
|
73
68
|
*/
|
|
74
|
-
subscribe(listener: ViewModelListener
|
|
69
|
+
subscribe(listener: ViewModelListener): () => void {
|
|
75
70
|
this._listeners.add(listener);
|
|
76
71
|
return () => {
|
|
77
72
|
this._listeners.delete(listener);
|
|
78
73
|
};
|
|
79
74
|
}
|
|
80
75
|
|
|
81
|
-
private _state:
|
|
76
|
+
private _state: S;
|
|
82
77
|
|
|
83
78
|
/**
|
|
84
79
|
* Create a new ViewModel with the given initial state.
|
|
85
80
|
*
|
|
86
81
|
* @param initialState - The initial state of the view model
|
|
87
82
|
*/
|
|
88
|
-
constructor(initialState:
|
|
83
|
+
constructor(initialState: S) {
|
|
89
84
|
this._state = initialState;
|
|
90
85
|
}
|
|
91
86
|
|
|
@@ -106,11 +101,11 @@ export abstract class ViewModel<T extends State> {
|
|
|
106
101
|
* }));
|
|
107
102
|
* ```
|
|
108
103
|
*/
|
|
109
|
-
protected update(updater: Updater<
|
|
104
|
+
protected update(updater: Updater<S>) {
|
|
110
105
|
this._state = updater(this._state);
|
|
111
106
|
|
|
112
107
|
for (const listener of this._listeners) {
|
|
113
|
-
listener(
|
|
108
|
+
listener();
|
|
114
109
|
}
|
|
115
110
|
}
|
|
116
111
|
|
|
@@ -119,7 +114,7 @@ export abstract class ViewModel<T extends State> {
|
|
|
119
114
|
*
|
|
120
115
|
* @returns The current state
|
|
121
116
|
*/
|
|
122
|
-
get state():
|
|
117
|
+
get state(): S {
|
|
123
118
|
return this._state;
|
|
124
119
|
}
|
|
125
120
|
}
|