react-usectx 1.2.0 → 1.2.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
@@ -11,46 +11,46 @@ npm i react-usectx --save
11
11
  ## Add it to your project
12
12
 
13
13
  ```js
14
- import useCtx from "react-usectx";
14
+ import {useGlobalState} from "react-usectx";
15
15
  ```
16
16
 
17
17
  ## Usage
18
18
 
19
19
  ```js
20
20
  // get both current state and set function
21
- const [myContext, setMyContext] = useCtx("stateName");
21
+ const [myState, setMyState] = useGlobalState("stateName");
22
22
  ```
23
23
 
24
24
  ## Other methods:
25
25
 
26
- ### getCtx
26
+ ### getGlobalState
27
27
 
28
28
  ```js
29
29
  // get current state only
30
30
 
31
- import { getCtx } from "react-usectx";
31
+ import { getGlobalState } from "react-usectx";
32
32
 
33
- const myContext = getCtx("stateName");
33
+ const myState = getGlobalState("stateName");
34
34
  ```
35
35
 
36
- ### updateCtx
36
+ ### updateGlobalState
37
37
 
38
38
  ```js
39
39
  // set function only
40
40
 
41
- import { updateCtx } from "react-usectx";
41
+ import { updateGlobalState } from "react-usectx";
42
42
 
43
- const setMyContext = updateCtx("stateName");
43
+ const setMyState = updateGlobalState("stateName");
44
44
 
45
- setMyContext({ name: "Rick Ross" });
45
+ setMyState({ name: "Rick Ross" });
46
46
  ```
47
47
 
48
- ### useReducer
48
+ ### useGlobalReducer
49
49
 
50
50
  ```js
51
- import { useReducer } from "react-usectx";
51
+ import { useGlobalReducer } from "react-usectx";
52
52
 
53
- const fullName = useReducer("stateName", (data) => {
53
+ const fullName = useGlobalReducer("stateName", (data) => {
54
54
  return `${data.firstName} ${data.lastName}`;
55
55
  });
56
56
  ```
@@ -62,28 +62,101 @@ const fullName = useReducer("stateName", (data) => {
62
62
  ```js
63
63
  const myContextID = "MyUniqueIdentifier";
64
64
 
65
- useCtx(myContextID);
65
+ useGlobalState(myContextID);
66
66
  ```
67
67
 
68
68
  The stateName serves as a unique identifier for a shared state object. Internally, the state manager maintains a list of state entries, each associated with a distinct stateName.
69
69
 
70
- When any of the provided functions—useCtx, updateCtx, or getCtx—are invoked, the state manager checks whether a state object with the specified stateName already exists. If it does not, a new state entry is automatically created and initialized.
70
+ When any of the provided functions—useGlobalState, updateGlobalState, or getGlobalState—are invoked, the state manager checks whether a state object with the specified stateName already exists. If it does not, a new state entry is automatically created and initialized.
71
71
 
72
- This mechanism enables global state sharing across components. For example, calling getCtx("example-1") in one component will return the same state reference when called with "example-1" in any other component within the same document. This allows for seamless state synchronization across different parts of your application without the need for explicit context providers.
72
+ This mechanism enables global state sharing across components. For example, calling getGlobalState("example-1") in one component will return the same state reference when called with "example-1" in any other component within the same document. This allows for seamless state synchronization across different parts of your application without the need for explicit context providers.
73
73
 
74
74
  ### initialState
75
75
 
76
76
  ```js
77
- useCtx(myContextID, { name: "John" });
77
+ useGlobalState(myContextID, { name: "John" });
78
78
  ```
79
79
 
80
80
  The value you want the state to be initially. It can be a value of any type.
81
81
 
82
+
83
+ ### API Methods
84
+
85
+ ```js
86
+ const api = stateApi(myContextID);
87
+ ```
88
+
89
+ Manualy control the flow of the data.
90
+
91
+
92
+ ### subscribe
93
+
94
+ ```js
95
+ api.subscribe(data => {
96
+ console.log(data)
97
+ });
98
+ ```
99
+
100
+ Subscribe for changes
101
+
102
+
103
+ ### unsubscribe
104
+
105
+ ```js
106
+ const subscription = data => {
107
+ console.log(data)
108
+ }
109
+ api.unsubscribe(subscription);
110
+ ```
111
+
112
+ unsubscribe from specific subscription
113
+
114
+
115
+ ### commit
116
+
117
+ ```js
118
+ api.commit({
119
+ my: "data"
120
+ });
121
+ ```
122
+
123
+ Sets data, and triggers all subscriptions and react hooks.
124
+
125
+
126
+ ### setState
127
+
128
+ ```js
129
+ api.setState({
130
+ my: "data"
131
+ });
132
+ ```
133
+
134
+ Sets data 'silently', without triggering anything.
135
+
136
+
137
+ ### undo
138
+
139
+ ```js
140
+ api.undo();
141
+ ```
142
+
143
+ Sets state to previous (if any), and triggers all subscriptions and react hooks.
144
+
145
+ ### redo
146
+
147
+ ```js
148
+ api.redo();
149
+ ```
150
+
151
+ Sets data to next state if undo was called previously, and triggers all subscriptions and react hooks.
152
+
153
+
154
+
82
155
  ## Example
83
156
 
84
157
  ```js
85
158
  function Page() {
86
- const [title, setTitle] = useCtx("title");
159
+ const [title, setTitle] = useGlobalState("title");
87
160
 
88
161
  return (
89
162
  <div className="wrapper">
@@ -102,7 +175,7 @@ function Page() {
102
175
  }
103
176
 
104
177
  function Title() {
105
- const title: string = getContext("title");
178
+ const title: string = getGlobalState("title");
106
179
  return (
107
180
  <h1>
108
181
  Component: <br />
@@ -116,7 +189,7 @@ function Title() {
116
189
 
117
190
  ```js
118
191
  function Page() {
119
- const [title, setTitle] = useCtx("title");
192
+ const [title, setTitle] = useGlobalState("title");
120
193
 
121
194
  return (
122
195
  <div className="wrapper">
@@ -135,7 +208,7 @@ function Page() {
135
208
  }
136
209
 
137
210
  function Title() {
138
- const reducedTitle: string = useReducer(
211
+ const reducedTitle: string = useGlobalReducer(
139
212
  "title",
140
213
  (title) => `Title is: ${title.toUpperCase()}`
141
214
  );
package/context.js CHANGED
@@ -51,3 +51,6 @@ export function useReducer(name, modiFier, useCache = false) {
51
51
  };
52
52
  return useSyncExternalStore(subscription, getState, getState);
53
53
  }
54
+ export function stateApi(name) {
55
+ return getInstance(name);
56
+ }
package/create-context.js CHANGED
@@ -1,11 +1,18 @@
1
1
  export class CreateContext {
2
- constructor(initialState) {
2
+ constructor(initialState, options = {}) {
3
+ const defaults = {
4
+ historySize: 99
5
+ };
6
+ this.settings = Object.assign({}, defaults, options);
3
7
  if (initialState !== undefined) {
4
8
  this.setState(initialState);
5
9
  }
6
10
  }
11
+ settings;
7
12
  #eventHandlers = [];
8
13
  #state = null;
14
+ #history = [];
15
+ #activeVersion = -1;
9
16
  subscribe(handler) {
10
17
  this.#eventHandlers.push(handler);
11
18
  }
@@ -19,7 +26,32 @@ export class CreateContext {
19
26
  this.setState(state);
20
27
  this.#eventHandlers.forEach((callback) => callback.call(undefined, state));
21
28
  }
22
- setState(state) {
29
+ undo() {
30
+ const prev = this.#history[this.#activeVersion - 1];
31
+ if (prev) {
32
+ this.setState(prev, this.#history.indexOf(prev));
33
+ }
34
+ }
35
+ redo() {
36
+ const next = this.#history[this.#activeVersion + 1];
37
+ if (next) {
38
+ this.setState(next, this.#history.indexOf(next));
39
+ }
40
+ }
41
+ setState(state, _av) {
42
+ if (typeof _av === 'undefined') {
43
+ if ((this.#history.length - 1) > this.#activeVersion) {
44
+ this.#history.splice(this.#activeVersion + 1, this.#history.length);
45
+ }
46
+ this.#activeVersion = this.#history.length - 1;
47
+ }
48
+ this.#history.push(state);
49
+ if (this.#history.length > this.settings.historySize) {
50
+ this.#history.shift();
51
+ }
52
+ if (typeof _av === 'number') {
53
+ this.#activeVersion = _av;
54
+ }
23
55
  this.#state = state;
24
56
  }
25
57
  getState() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-usectx",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "repository": {
package/src/context.ts CHANGED
@@ -75,3 +75,7 @@ export function useReducer<Snapshot>(
75
75
  };
76
76
  return useSyncExternalStore(subscription, getState, getState);
77
77
  }
78
+
79
+ export function stateApi(name: string): InstanceType<typeof CreateContext> {
80
+ return getInstance(name)
81
+ }
@@ -1,16 +1,30 @@
1
1
  import { eventHandlers, state } from "./types";
2
2
 
3
+
4
+ export interface CreateContextOptions {
5
+ historySize?: number
6
+ }
7
+
3
8
  export class CreateContext {
4
- constructor(initialState?: state) {
9
+ constructor(initialState?: state, options: CreateContextOptions = {}) {
10
+ const defaults: CreateContextOptions = {
11
+ historySize: 999
12
+ }
13
+ this.settings = Object.assign({}, defaults, options)
5
14
  if (initialState !== undefined) {
6
15
  this.setState(initialState);
7
16
  }
8
17
  }
9
18
 
19
+ settings: CreateContextOptions;
20
+
10
21
  #eventHandlers: eventHandlers = [];
11
22
 
12
23
  #state: state = null;
13
24
 
25
+ #history: any[] = [];
26
+ #activeVersion:number = -1;
27
+
14
28
  subscribe(handler: Function): void {
15
29
  this.#eventHandlers.push(handler);
16
30
  }
@@ -27,7 +41,35 @@ export class CreateContext {
27
41
  this.#eventHandlers.forEach((callback) => callback.call(undefined, state));
28
42
  }
29
43
 
30
- setState(state: state): void {
44
+ undo(){
45
+ const prev = this.#history[this.#activeVersion - 1];
46
+ if(prev) {
47
+ this.setState(prev, this.#history.indexOf(prev))
48
+ }
49
+ }
50
+
51
+ redo() {
52
+ const next = this.#history[this.#activeVersion + 1];
53
+ if(next) {
54
+ this.setState(next, this.#history.indexOf(next))
55
+ }
56
+ }
57
+
58
+ setState(state: state, _av?: number): void {
59
+ if (typeof _av === 'undefined') {
60
+ if((this.#history.length - 1) > this.#activeVersion) {
61
+ this.#history.splice(this.#activeVersion + 1, this.#history.length)
62
+ }
63
+ this.#activeVersion = this.#history.length - 1;
64
+ }
65
+ this.#history.push(state);
66
+ if (this.#history.length > this.settings.historySize!) {
67
+ this.#history.shift();
68
+ }
69
+ if (typeof _av === 'number') {
70
+ this.#activeVersion = _av;
71
+ }
72
+
31
73
  this.#state = state;
32
74
  }
33
75
 
File without changes