vibra 1.0.0 → 1.0.1
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 +31 -1
- package/dist/index.d.mts +44 -11
- package/dist/index.d.ts +44 -11
- package/dist/index.js +3 -5
- package/dist/index.mjs +3 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Vibra
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/vibra)
|
|
4
|
+
|
|
3
5
|
> A blazing fast, type-safe, and minimal state management library for TypeScript and JavaScript. Effortless reactivity, subscriptions, and memory safety in a tiny package.
|
|
4
6
|
|
|
5
7
|
## 🚀 Features
|
|
@@ -64,13 +66,41 @@ store.set(5);
|
|
|
64
66
|
// Both subscribers are notified
|
|
65
67
|
```
|
|
66
68
|
|
|
69
|
+
### Subscription Options
|
|
70
|
+
```typescript
|
|
71
|
+
const store = vibra(42);
|
|
72
|
+
|
|
73
|
+
// Default behavior: callback is not called on subscribe
|
|
74
|
+
store.subscribe(value => console.log('Changed:', value));
|
|
75
|
+
|
|
76
|
+
// With callOnSubscribe: callback is called immediately with current value
|
|
77
|
+
store.subscribe(
|
|
78
|
+
value => console.log('Current:', value),
|
|
79
|
+
{ callOnSubscribe: true }
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// Useful for initializing UI or state
|
|
83
|
+
const userStore = vibra({ name: 'Alice' });
|
|
84
|
+
userStore.subscribe(
|
|
85
|
+
user => updateUI(user),
|
|
86
|
+
{ callOnSubscribe: true } // Update UI immediately
|
|
87
|
+
);
|
|
88
|
+
```
|
|
89
|
+
|
|
67
90
|
## 🧩 API Reference
|
|
68
91
|
|
|
69
92
|
### `vibra<T>(initialValue: T)`
|
|
70
93
|
Returns a store object with:
|
|
71
94
|
- `get(): T` — Get the current value
|
|
72
95
|
- `set(value: T): void` — Set a new value (notifies subscribers if changed)
|
|
73
|
-
- `subscribe(callback: (value: T) => void): () => void` — Subscribe to changes
|
|
96
|
+
- `subscribe(callback: (value: T) => void, options?: SubscribeOptions): () => void` — Subscribe to changes. Returns an unsubscribe function.
|
|
97
|
+
|
|
98
|
+
#### SubscribeOptions
|
|
99
|
+
```typescript
|
|
100
|
+
interface SubscribeOptions {
|
|
101
|
+
callOnSubscribe?: boolean; // If true, callback is called immediately with current value
|
|
102
|
+
}
|
|
103
|
+
```
|
|
74
104
|
|
|
75
105
|
## 💡 Why Vibra?
|
|
76
106
|
- **Ultra-lightweight**: No bloat, just state
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for subscribing to a Vibra store
|
|
3
|
+
*/
|
|
4
|
+
interface SubscribeOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If true, the callback will be called immediately with the current value
|
|
7
|
+
* when subscribing. Defaults to false.
|
|
8
|
+
*/
|
|
9
|
+
callOnSubscribe?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Type for a store subscriber callback function
|
|
13
|
+
*/
|
|
14
|
+
type Subscriber<T> = (value: T) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Type for a store unsubscribe function
|
|
17
|
+
*/
|
|
18
|
+
type Unsubscribe = () => void;
|
|
19
|
+
/**
|
|
20
|
+
* Type for a store's public API
|
|
21
|
+
*/
|
|
22
|
+
interface Store<T> {
|
|
23
|
+
/**
|
|
24
|
+
* Get the current value of the store
|
|
25
|
+
*/
|
|
26
|
+
get(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Set a new value for the store
|
|
29
|
+
* @param value - The new value to set
|
|
30
|
+
*/
|
|
31
|
+
set(value: T): void;
|
|
32
|
+
/**
|
|
33
|
+
* Subscribe to changes in the store
|
|
34
|
+
* @param callback - Function to call when the value changes
|
|
35
|
+
* @param options - Optional subscription options
|
|
36
|
+
* @returns Function to unsubscribe from the store
|
|
37
|
+
*/
|
|
38
|
+
subscribe(callback: Subscriber<T>, options?: SubscribeOptions): Unsubscribe;
|
|
39
|
+
}
|
|
40
|
+
|
|
1
41
|
/**
|
|
2
42
|
* Vibra - A Simple Yet Powerful State Management Solution
|
|
3
43
|
*
|
|
@@ -15,7 +55,7 @@
|
|
|
15
55
|
*
|
|
16
56
|
* @template T - The type of the state value
|
|
17
57
|
* @param initialValue - The initial value for the state
|
|
18
|
-
* @returns
|
|
58
|
+
* @returns A store object implementing the Store interface
|
|
19
59
|
*
|
|
20
60
|
* @example
|
|
21
61
|
* ```typescript
|
|
@@ -36,14 +76,7 @@
|
|
|
36
76
|
* unsubscribe();
|
|
37
77
|
* ```
|
|
38
78
|
*/
|
|
39
|
-
declare function vibra<T>(initialValue: T):
|
|
40
|
-
|
|
41
|
-
set: (value: T) => void;
|
|
42
|
-
subscribe: (callback: (value: T) => void) => () => void;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
interface Main {
|
|
46
|
-
test: true;
|
|
47
|
-
}
|
|
79
|
+
declare function vibra<T>(initialValue: T): Store<T>;
|
|
80
|
+
type Vibra<T> = Store<T>;
|
|
48
81
|
|
|
49
|
-
export { type
|
|
82
|
+
export { type Store, type SubscribeOptions, type Subscriber, type Unsubscribe, type Vibra, vibra as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for subscribing to a Vibra store
|
|
3
|
+
*/
|
|
4
|
+
interface SubscribeOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If true, the callback will be called immediately with the current value
|
|
7
|
+
* when subscribing. Defaults to false.
|
|
8
|
+
*/
|
|
9
|
+
callOnSubscribe?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Type for a store subscriber callback function
|
|
13
|
+
*/
|
|
14
|
+
type Subscriber<T> = (value: T) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Type for a store unsubscribe function
|
|
17
|
+
*/
|
|
18
|
+
type Unsubscribe = () => void;
|
|
19
|
+
/**
|
|
20
|
+
* Type for a store's public API
|
|
21
|
+
*/
|
|
22
|
+
interface Store<T> {
|
|
23
|
+
/**
|
|
24
|
+
* Get the current value of the store
|
|
25
|
+
*/
|
|
26
|
+
get(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Set a new value for the store
|
|
29
|
+
* @param value - The new value to set
|
|
30
|
+
*/
|
|
31
|
+
set(value: T): void;
|
|
32
|
+
/**
|
|
33
|
+
* Subscribe to changes in the store
|
|
34
|
+
* @param callback - Function to call when the value changes
|
|
35
|
+
* @param options - Optional subscription options
|
|
36
|
+
* @returns Function to unsubscribe from the store
|
|
37
|
+
*/
|
|
38
|
+
subscribe(callback: Subscriber<T>, options?: SubscribeOptions): Unsubscribe;
|
|
39
|
+
}
|
|
40
|
+
|
|
1
41
|
/**
|
|
2
42
|
* Vibra - A Simple Yet Powerful State Management Solution
|
|
3
43
|
*
|
|
@@ -15,7 +55,7 @@
|
|
|
15
55
|
*
|
|
16
56
|
* @template T - The type of the state value
|
|
17
57
|
* @param initialValue - The initial value for the state
|
|
18
|
-
* @returns
|
|
58
|
+
* @returns A store object implementing the Store interface
|
|
19
59
|
*
|
|
20
60
|
* @example
|
|
21
61
|
* ```typescript
|
|
@@ -36,14 +76,7 @@
|
|
|
36
76
|
* unsubscribe();
|
|
37
77
|
* ```
|
|
38
78
|
*/
|
|
39
|
-
declare function vibra<T>(initialValue: T):
|
|
40
|
-
|
|
41
|
-
set: (value: T) => void;
|
|
42
|
-
subscribe: (callback: (value: T) => void) => () => void;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
interface Main {
|
|
46
|
-
test: true;
|
|
47
|
-
}
|
|
79
|
+
declare function vibra<T>(initialValue: T): Store<T>;
|
|
80
|
+
type Vibra<T> = Store<T>;
|
|
48
81
|
|
|
49
|
-
export { type
|
|
82
|
+
export { type Store, type SubscribeOptions, type Subscriber, type Unsubscribe, type Vibra, vibra as default };
|
package/dist/index.js
CHANGED
|
@@ -28,12 +28,10 @@ module.exports = __toCommonJS(index_exports);
|
|
|
28
28
|
function vibra(initialValue) {
|
|
29
29
|
let data = initialValue;
|
|
30
30
|
const subscribers = /* @__PURE__ */ new Set();
|
|
31
|
-
function subscribe(callback) {
|
|
31
|
+
function subscribe(callback, { callOnSubscribe = false } = {}) {
|
|
32
32
|
subscribers.add(callback);
|
|
33
|
-
callback(data);
|
|
34
|
-
return () =>
|
|
35
|
-
subscribers.delete(callback);
|
|
36
|
-
};
|
|
33
|
+
if (callOnSubscribe) callback(data);
|
|
34
|
+
return () => subscribers.delete(callback);
|
|
37
35
|
}
|
|
38
36
|
function set(value) {
|
|
39
37
|
if (data !== value) {
|
package/dist/index.mjs
CHANGED
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
function vibra(initialValue) {
|
|
3
3
|
let data = initialValue;
|
|
4
4
|
const subscribers = /* @__PURE__ */ new Set();
|
|
5
|
-
function subscribe(callback) {
|
|
5
|
+
function subscribe(callback, { callOnSubscribe = false } = {}) {
|
|
6
6
|
subscribers.add(callback);
|
|
7
|
-
callback(data);
|
|
8
|
-
return () =>
|
|
9
|
-
subscribers.delete(callback);
|
|
10
|
-
};
|
|
7
|
+
if (callOnSubscribe) callback(data);
|
|
8
|
+
return () => subscribers.delete(callback);
|
|
11
9
|
}
|
|
12
10
|
function set(value) {
|
|
13
11
|
if (data !== value) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vibra",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Vibra: A blazing fast, type-safe, and minimal state management library for TypeScript and JavaScript. Effortless reactivity, subscriptions, and memory safety in a tiny package.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|