vibra 1.0.0 → 1.0.2

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
@@ -1,5 +1,7 @@
1
1
  # Vibra
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/vibra.svg)](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 (immediately calls with current value). Returns an unsubscribe function.
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 An object containing methods to interact with the state
58
+ * @returns A store object implementing the Store interface
19
59
  *
20
60
  * @example
21
61
  * ```typescript
@@ -36,14 +76,11 @@
36
76
  * unsubscribe();
37
77
  * ```
38
78
  */
39
- declare function vibra<T>(initialValue: T): {
40
- get: () => T;
41
- set: (value: T) => void;
42
- subscribe: (callback: (value: T) => void) => () => void;
43
- };
79
+ declare function vibra<T>(initialValue: T): Store<T>;
80
+ type Vibra<T> = Store<T>;
44
81
 
45
- interface Main {
46
- test: true;
82
+ declare class VibraError extends Error {
83
+ constructor(message: string);
47
84
  }
48
85
 
49
- export { type Main, vibra as default };
86
+ export { type Store, type SubscribeOptions, type Subscriber, type Unsubscribe, type Vibra, VibraError, 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 An object containing methods to interact with the state
58
+ * @returns A store object implementing the Store interface
19
59
  *
20
60
  * @example
21
61
  * ```typescript
@@ -36,14 +76,11 @@
36
76
  * unsubscribe();
37
77
  * ```
38
78
  */
39
- declare function vibra<T>(initialValue: T): {
40
- get: () => T;
41
- set: (value: T) => void;
42
- subscribe: (callback: (value: T) => void) => () => void;
43
- };
79
+ declare function vibra<T>(initialValue: T): Store<T>;
80
+ type Vibra<T> = Store<T>;
44
81
 
45
- interface Main {
46
- test: true;
82
+ declare class VibraError extends Error {
83
+ constructor(message: string);
47
84
  }
48
85
 
49
- export { type Main, vibra as default };
86
+ export { type Store, type SubscribeOptions, type Subscriber, type Unsubscribe, type Vibra, VibraError, vibra as default };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ VibraError: () => vibra_error_default,
23
24
  default: () => vibra_default
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
@@ -28,12 +29,10 @@ module.exports = __toCommonJS(index_exports);
28
29
  function vibra(initialValue) {
29
30
  let data = initialValue;
30
31
  const subscribers = /* @__PURE__ */ new Set();
31
- function subscribe(callback) {
32
+ function subscribe(callback, { callOnSubscribe = false } = {}) {
32
33
  subscribers.add(callback);
33
- callback(data);
34
- return () => {
35
- subscribers.delete(callback);
36
- };
34
+ if (callOnSubscribe) callback(data);
35
+ return () => subscribers.delete(callback);
37
36
  }
38
37
  function set(value) {
39
38
  if (data !== value) {
@@ -51,3 +50,16 @@ function vibra(initialValue) {
51
50
  };
52
51
  }
53
52
  var vibra_default = vibra;
53
+
54
+ // src/vibra-error.ts
55
+ var VibraError = class extends Error {
56
+ constructor(message) {
57
+ super(message);
58
+ this.name = "VibraError";
59
+ }
60
+ };
61
+ var vibra_error_default = VibraError;
62
+ // Annotate the CommonJS export names for ESM import in node:
63
+ 0 && (module.exports = {
64
+ VibraError
65
+ });
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) {
@@ -25,6 +23,16 @@ function vibra(initialValue) {
25
23
  };
26
24
  }
27
25
  var vibra_default = vibra;
26
+
27
+ // src/vibra-error.ts
28
+ var VibraError = class extends Error {
29
+ constructor(message) {
30
+ super(message);
31
+ this.name = "VibraError";
32
+ }
33
+ };
34
+ var vibra_error_default = VibraError;
28
35
  export {
36
+ vibra_error_default as VibraError,
29
37
  vibra_default as default
30
38
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vibra",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
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",