@yiin/reactive-proxy-state 1.0.5 → 1.0.7

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.
Files changed (2) hide show
  1. package/README.md +60 -17
  2. package/package.json +9 -4
package/README.md CHANGED
@@ -6,6 +6,10 @@ A simple, standalone reactivity library inspired by Vue 3's reactivity system, d
6
6
 
7
7
  **Note:** This library currently only supports synchronous effect execution.
8
8
 
9
+ ## Documentation
10
+
11
+ For comprehensive documentation, visit our [documentation site](https://Yiin.github.io/reactive-proxy-state/).
12
+
9
13
  ## Installation
10
14
 
11
15
  ```bash
@@ -91,60 +95,99 @@ console.log(unref(123)); // 123 (returns non-refs as is)
91
95
  ```
92
96
 
93
97
  ### `computed<T>(getter: () => T): ComputedRef<T>`
98
+ ### `computed<T>(options: { get: () => T, set: (value: T) => void }): WritableComputedRef<T>`
99
+
100
+ Creates a computed property based on a getter function or a getter/setter pair.
94
101
 
95
- Creates a computed property based on a getter function. The getter tracks reactive dependencies (`ref`s or reactive object properties) and its result is cached. The computed value only recalculates when a dependency changes.
96
- Computed refs are read-only.
102
+ - **Getter-only:** The getter tracks reactive dependencies (`ref`s or reactive object properties) and its result is cached. The computed value only recalculates when a dependency changes. Computed refs created this way are **read-only**.
103
+ - **Getter/Setter:** Provides both a getter for deriving the value and a setter for mutating underlying reactive state when the computed ref's `.value` is assigned.
97
104
 
98
105
  ```typescript
99
106
  import { ref, computed, watchEffect, isComputed } from '@yiin/reactive-proxy-state';
100
107
 
108
+ // Read-only computed
101
109
  const firstName = ref('John');
102
110
  const lastName = ref('Doe');
103
111
 
104
- const fullName = computed(() => {
105
- console.log('Computing fullName...');
112
+ const readOnlyFullName = computed(() => {
113
+ console.log('Computing readOnlyFullName...');
106
114
  return `${firstName.value} ${lastName.value}`;
107
115
  });
108
116
 
109
117
  // Accessing .value triggers computation
110
- console.log(fullName.value);
111
- // Output: Computing fullName...
118
+ console.log(readOnlyFullName.value);
119
+ // Output: Computing readOnlyFullName...
112
120
  // Output: John Doe
113
121
 
114
122
  // Accessing again uses the cache
115
- console.log(fullName.value);
123
+ console.log(readOnlyFullName.value);
116
124
  // Output: John Doe
117
125
 
118
126
  watchEffect(() => {
119
- console.log('Full name changed:', fullName.value);
127
+ console.log('Read-only full name changed:', readOnlyFullName.value);
120
128
  });
121
- // Output: Full name changed: John Doe
129
+ // Output: Read-only full name changed: John Doe
122
130
 
123
131
  // Changing a dependency marks computed as dirty
124
132
  firstName.value = 'Jane';
125
133
 
126
134
  // Accessing .value again triggers re-computation and the effect
127
- console.log(fullName.value);
128
- // Output: Computing fullName...
129
- // Output: Full name changed: Jane Doe
135
+ console.log(readOnlyFullName.value);
136
+ // Output: Computing readOnlyFullName...
137
+ // Output: Read-only full name changed: Jane Doe
130
138
  // Output: Jane Doe
131
139
 
132
140
  // Chained computed
133
- const message = computed(() => `User: ${fullName.value}`);
141
+ const message = computed(() => `User: ${readOnlyFullName.value}`);
134
142
  console.log(message.value); // User: Jane Doe
135
143
 
136
144
  lastName.value = 'Smith';
137
- // Output: Computing fullName...
138
- // Output: Full name changed: Jane Smith
145
+ // Output: Computing readOnlyFullName...
146
+ // Output: Read-only full name changed: Jane Smith
139
147
  console.log(message.value); // User: Jane Smith (message recomputed automatically)
140
148
 
141
149
  // Read-only check
150
+ console.warn = () => console.log('Warning triggered!'); // Mock console.warn
142
151
  try {
143
- (fullName as any).value = 'Test'; // Throws warning
152
+ (readOnlyFullName as any).value = 'Test'; // Triggers warning
144
153
  } catch (e) { /* ... */ }
154
+ // Output: Warning triggered!
155
+ console.log(readOnlyFullName.value); // Jane Smith (value unchanged)
156
+
157
+ // Writable computed
158
+ const source = ref(1);
159
+ const plusOne = computed({
160
+ get: () => source.value + 1,
161
+ set: (newValue) => {
162
+ console.log(`Setting source based on new value: ${newValue}`);
163
+ source.value = newValue - 1;
164
+ }
165
+ });
166
+
167
+ console.log(plusOne.value); // 2 (Initial get)
168
+ console.log(source.value); // 1
169
+
170
+ watchEffect(() => {
171
+ console.log('Writable computed changed:', plusOne.value);
172
+ });
173
+ // Output: Writable computed changed: 2
174
+
175
+ // Set the writable computed value
176
+ plusOne.value = 10;
177
+ // Output: Setting source based on new value: 10
178
+ // Output: Writable computed changed: 10
179
+
180
+ console.log(plusOne.value); // 10
181
+ console.log(source.value); // 9 (Source was updated by the setter)
182
+
183
+ // Changing the source ref also updates the computed
184
+ source.value = 20;
185
+ // Output: Writable computed changed: 21
186
+ console.log(plusOne.value); // 21
145
187
 
146
188
  // Helper
147
- console.log(isComputed(fullName)); // true
189
+ console.log(isComputed(readOnlyFullName)); // true
190
+ console.log(isComputed(plusOne)); // true
148
191
  console.log(isComputed(firstName)); // false
149
192
  ```
150
193
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yiin/reactive-proxy-state",
3
3
  "private": false,
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "description": "A simple, standalone reactivity library using Proxies",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -20,7 +20,10 @@
20
20
  "build": "tsc",
21
21
  "test": "bun test",
22
22
  "test:watch": "bun test --watch",
23
- "test:bench": "bun --bun ./benchmarks/state-sync.bench.ts"
23
+ "test:bench": "bun --bun ./benchmarks/state-sync.bench.ts",
24
+ "docs:dev": "vitepress dev docs",
25
+ "docs:build": "vitepress build docs",
26
+ "docs:preview": "vitepress preview docs"
24
27
  },
25
28
  "keywords": [
26
29
  "state",
@@ -41,12 +44,14 @@
41
44
  "bugs": {
42
45
  "url": "https://github.com/Yiin/reactive-proxy-state/issues"
43
46
  },
44
- "homepage": "https://github.com/Yiin/reactive-proxy-state#readme",
47
+ "homepage": "https://Yiin.github.io/reactive-proxy-state/",
45
48
  "publishConfig": {
46
49
  "access": "public"
47
50
  },
48
51
  "devDependencies": {
49
52
  "bun-types": "^1.2.8",
50
- "typescript": "^5.0.4"
53
+ "typescript": "^5.0.4",
54
+ "vitepress": "^1.6.3",
55
+ "vue": "^3.5.13"
51
56
  }
52
57
  }