react-usectx 1.2.1 → 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
@@ -79,6 +79,79 @@ useGlobalState(myContextID, { name: "John" });
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
@@ -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 />
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.1",
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