@janus-scheduler/counter-core 0.1.0 → 0.1.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/dist/CounterCore.d.ts +65 -0
- package/dist/CounterCore.js +61 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PUBLIC state shape exposed to consumers
|
|
3
|
+
* Represents a snapshot of the counter state
|
|
4
|
+
*/
|
|
5
|
+
export type CounterState = {
|
|
6
|
+
count: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Listener function type
|
|
10
|
+
* - Receives updated state snapshots
|
|
11
|
+
* - Does not return anything
|
|
12
|
+
*/
|
|
13
|
+
type Listener = (state: CounterState) => void;
|
|
14
|
+
/**
|
|
15
|
+
* CounterCore
|
|
16
|
+
* -----------
|
|
17
|
+
* A framework-agnostic, headless state engine.
|
|
18
|
+
* Owns counter state and notifies subscribers on changes.
|
|
19
|
+
*/
|
|
20
|
+
export declare class CounterCore {
|
|
21
|
+
/**
|
|
22
|
+
* Internal state owned by the core
|
|
23
|
+
* This should never be mutated directly from outside
|
|
24
|
+
*/
|
|
25
|
+
private state;
|
|
26
|
+
/**
|
|
27
|
+
* Set of subscribers (framework wrappers, consumers)
|
|
28
|
+
* Each listener is notified when state changes
|
|
29
|
+
*/
|
|
30
|
+
private listeners;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new CounterCore instance
|
|
33
|
+
* @param initialValue optional starting value (default = 0)
|
|
34
|
+
*/
|
|
35
|
+
constructor(initialValue?: number);
|
|
36
|
+
/**
|
|
37
|
+
* PUBLIC API
|
|
38
|
+
* Returns a safe snapshot of the current state
|
|
39
|
+
* (spread used to avoid exposing internal reference)
|
|
40
|
+
*/
|
|
41
|
+
getState(): CounterState;
|
|
42
|
+
/**
|
|
43
|
+
* PUBLIC API
|
|
44
|
+
* Increment counter value
|
|
45
|
+
*/
|
|
46
|
+
increment(): void;
|
|
47
|
+
/**
|
|
48
|
+
* PUBLIC API
|
|
49
|
+
* Decrement counter value
|
|
50
|
+
*/
|
|
51
|
+
decrement(): void;
|
|
52
|
+
/**
|
|
53
|
+
* PUBLIC API
|
|
54
|
+
* Subscribe to state changes
|
|
55
|
+
* Used by framework wrappers
|
|
56
|
+
* @returns unsubscribe function
|
|
57
|
+
*/
|
|
58
|
+
subscribe(listener: Listener): () => void;
|
|
59
|
+
/**
|
|
60
|
+
* INTERNAL helper
|
|
61
|
+
* Emits a snapshot of state to all subscribers
|
|
62
|
+
*/
|
|
63
|
+
private emit;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CounterCore
|
|
3
|
+
* -----------
|
|
4
|
+
* A framework-agnostic, headless state engine.
|
|
5
|
+
* Owns counter state and notifies subscribers on changes.
|
|
6
|
+
*/
|
|
7
|
+
export class CounterCore {
|
|
8
|
+
/**
|
|
9
|
+
* Create a new CounterCore instance
|
|
10
|
+
* @param initialValue optional starting value (default = 0)
|
|
11
|
+
*/
|
|
12
|
+
constructor(initialValue = 0) {
|
|
13
|
+
//initialize state and listeners set
|
|
14
|
+
this.state = { count: initialValue };
|
|
15
|
+
this.listeners = new Set();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* PUBLIC API
|
|
19
|
+
* Returns a safe snapshot of the current state
|
|
20
|
+
* (spread used to avoid exposing internal reference)
|
|
21
|
+
*/
|
|
22
|
+
getState() {
|
|
23
|
+
return { ...this.state };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* PUBLIC API
|
|
27
|
+
* Increment counter value
|
|
28
|
+
*/
|
|
29
|
+
increment() {
|
|
30
|
+
this.state.count += 2;
|
|
31
|
+
this.emit();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* PUBLIC API
|
|
35
|
+
* Decrement counter value
|
|
36
|
+
*/
|
|
37
|
+
decrement() {
|
|
38
|
+
this.state.count -= 1;
|
|
39
|
+
this.emit();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* PUBLIC API
|
|
43
|
+
* Subscribe to state changes
|
|
44
|
+
* Used by framework wrappers
|
|
45
|
+
* @returns unsubscribe function
|
|
46
|
+
*/
|
|
47
|
+
subscribe(listener) {
|
|
48
|
+
this.listeners.add(listener);
|
|
49
|
+
return () => {
|
|
50
|
+
this.listeners.delete(listener);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* INTERNAL helper
|
|
55
|
+
* Emits a snapshot of state to all subscribers
|
|
56
|
+
*/
|
|
57
|
+
emit() {
|
|
58
|
+
const snapshot = { ...this.state };
|
|
59
|
+
this.listeners.forEach((listener) => listener(snapshot));
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./CounterCore";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./CounterCore";
|