@peerbit/react 0.0.33 → 0.0.34

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.
Files changed (71) hide show
  1. package/dist/src/index.d.ts +5 -0
  2. package/dist/src/index.d.ts.map +1 -0
  3. package/dist/src/index.js +5 -0
  4. package/dist/src/index.js.map +1 -0
  5. package/{lib/esm → dist/src}/lockstorage.d.ts +1 -0
  6. package/dist/src/lockstorage.d.ts.map +1 -0
  7. package/{lib/esm → dist/src}/lockstorage.js +2 -5
  8. package/dist/src/lockstorage.js.map +1 -0
  9. package/{lib/esm → dist/src}/useMount.d.ts +1 -0
  10. package/dist/src/useMount.d.ts.map +1 -0
  11. package/dist/src/useMount.js.map +1 -0
  12. package/{lib/esm → dist/src}/usePeer.d.ts +7 -3
  13. package/dist/src/usePeer.d.ts.map +1 -0
  14. package/{lib/esm → dist/src}/usePeer.js +25 -20
  15. package/dist/src/usePeer.js.map +1 -0
  16. package/{lib/esm → dist/src}/utils.d.ts +2 -4
  17. package/dist/src/utils.d.ts.map +1 -0
  18. package/{lib/esm → dist/src}/utils.js +17 -69
  19. package/dist/src/utils.js.map +1 -0
  20. package/package.json +59 -56
  21. package/src/index.ts +4 -14
  22. package/src/lockstorage.ts +224 -233
  23. package/src/useMount.ts +15 -0
  24. package/src/usePeer.tsx +406 -419
  25. package/src/utils.ts +99 -168
  26. package/README.md +0 -24
  27. package/lib/esm/__tests__/lockstorage.test.d.ts +0 -1
  28. package/lib/esm/__tests__/lockstorage.test.js +0 -237
  29. package/lib/esm/__tests__/lockstorage.test.js.map +0 -1
  30. package/lib/esm/__tests__/singletonLock.test.d.ts +0 -1
  31. package/lib/esm/__tests__/singletonLock.test.js +0 -71
  32. package/lib/esm/__tests__/singletonLock.test.js.map +0 -1
  33. package/lib/esm/__tests__/useQuery.dom.test.d.ts +0 -1
  34. package/lib/esm/__tests__/useQuery.dom.test.js +0 -433
  35. package/lib/esm/__tests__/useQuery.dom.test.js.map +0 -1
  36. package/lib/esm/__tests__/utils.test.d.ts +0 -1
  37. package/lib/esm/__tests__/utils.test.js +0 -66
  38. package/lib/esm/__tests__/utils.test.js.map +0 -1
  39. package/lib/esm/index.d.ts +0 -8
  40. package/lib/esm/index.js +0 -9
  41. package/lib/esm/index.js.map +0 -1
  42. package/lib/esm/lockstorage.js.map +0 -1
  43. package/lib/esm/useCount.d.ts +0 -11
  44. package/lib/esm/useCount.js +0 -43
  45. package/lib/esm/useCount.js.map +0 -1
  46. package/lib/esm/useLocal.d.ts +0 -20
  47. package/lib/esm/useLocal.js +0 -73
  48. package/lib/esm/useLocal.js.map +0 -1
  49. package/lib/esm/useMount.js.map +0 -1
  50. package/lib/esm/useOnline.d.ts +0 -11
  51. package/lib/esm/useOnline.js +0 -65
  52. package/lib/esm/useOnline.js.map +0 -1
  53. package/lib/esm/usePeer.js.map +0 -1
  54. package/lib/esm/useProgram.d.ts +0 -16
  55. package/lib/esm/useProgram.js +0 -114
  56. package/lib/esm/useProgram.js.map +0 -1
  57. package/lib/esm/useQuery.d.ts +0 -49
  58. package/lib/esm/useQuery.js +0 -418
  59. package/lib/esm/useQuery.js.map +0 -1
  60. package/lib/esm/utils.js.map +0 -1
  61. package/src/__tests__/lockstorage.test.ts +0 -285
  62. package/src/__tests__/singletonLock.test.ts +0 -85
  63. package/src/__tests__/useQuery.dom.test.ts +0 -518
  64. package/src/__tests__/utils.test.ts +0 -90
  65. package/src/useCount.tsx +0 -63
  66. package/src/useLocal.tsx +0 -125
  67. package/src/useMount.tsx +0 -15
  68. package/src/useOnline.tsx +0 -85
  69. package/src/useProgram.tsx +0 -148
  70. package/src/useQuery.tsx +0 -548
  71. /package/{lib/esm → dist/src}/useMount.js +0 -0
@@ -14,260 +14,251 @@ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
14
  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
15
  THIS SOFTWARE.
16
16
  */
17
-
18
17
  import debugFn from "debug";
19
18
  import { v4 as uuid } from "uuid";
19
+
20
20
  const debug = debugFn("FastMutex");
21
21
 
22
22
  export class FastMutex {
23
- clientId: string;
24
- xPrefix: string;
25
- yPrefix: string;
26
- timeout: number;
27
- localStorage: any;
28
- intervals: Map<string, any>;
23
+ clientId: string;
24
+ xPrefix: string;
25
+ yPrefix: string;
26
+ timeout: number;
27
+ localStorage: any;
28
+ intervals: Map<string, any>;
29
29
 
30
- constructor({
31
- clientId = uuid(),
32
- xPrefix = "_MUTEX_LOCK_X_",
33
- yPrefix = "_MUTEX_LOCK_Y_",
34
- timeout = 5000,
35
- localStorage = undefined as any,
36
- } = {}) {
37
- this.clientId = clientId;
38
- this.xPrefix = xPrefix;
39
- this.yPrefix = yPrefix;
40
- this.timeout = timeout;
41
- this.intervals = new Map();
30
+ constructor({
31
+ clientId = uuid(),
32
+ xPrefix = "_MUTEX_LOCK_X_",
33
+ yPrefix = "_MUTEX_LOCK_Y_",
34
+ timeout = 5000,
35
+ localStorage = undefined as any,
36
+ } = {}) {
37
+ this.clientId = clientId;
38
+ this.xPrefix = xPrefix;
39
+ this.yPrefix = yPrefix;
40
+ this.timeout = timeout;
41
+ this.intervals = new Map();
42
42
 
43
- this.localStorage = localStorage || window.localStorage;
44
- }
43
+ this.localStorage = localStorage || window.localStorage;
44
+ }
45
45
 
46
- lock(
47
- key: string,
48
- keepLocked?: () => boolean,
49
- options?: { replaceIfSameClient?: boolean }
50
- ): Promise<{
51
- restartCount: number;
52
- contentionCount: number;
53
- locksLost: number;
54
- }> {
55
- debug(
56
- 'Attempting to acquire Lock on "%s" using FastMutex instance "%s"',
57
- key,
58
- this.clientId
59
- );
60
- const x = this.xPrefix + key;
61
- const y = this.yPrefix + key;
62
- let acquireStart = Date.now();
63
- return new Promise((resolve, reject) => {
64
- let restartCount = 0;
65
- let contentionCount = 0;
66
- let locksLost = 0;
46
+ lock(
47
+ key: string,
48
+ keepLocked?: () => boolean,
49
+ options?: { replaceIfSameClient?: boolean },
50
+ ): Promise<{
51
+ restartCount: number;
52
+ contentionCount: number;
53
+ locksLost: number;
54
+ }> {
55
+ debug(
56
+ 'Attempting to acquire Lock on "%s" using FastMutex instance "%s"',
57
+ key,
58
+ this.clientId,
59
+ );
60
+ const x = this.xPrefix + key;
61
+ const y = this.yPrefix + key;
62
+ let acquireStart = Date.now();
63
+ return new Promise((resolve, reject) => {
64
+ let restartCount = 0;
65
+ let contentionCount = 0;
66
+ let locksLost = 0;
67
67
 
68
- const acquireLock = (key: string) => {
69
- // If the option is set and the same client already holds both keys,
70
- // update the expiry and resolve immediately.
71
- if (options?.replaceIfSameClient) {
72
- const currentX = this.getItem(x);
73
- const currentY = this.getItem(y);
74
- if (
75
- currentX === this.clientId &&
76
- currentY === this.clientId
77
- ) {
78
- // Update expiry so that the lock is effectively "replaced"
79
- this.setItem(x, this.clientId, keepLocked);
80
- this.setItem(y, this.clientId, keepLocked);
81
- debug(
82
- 'FastMutex client "%s" replaced its own lock on "%s".',
83
- this.clientId,
84
- key
85
- );
86
- return resolve({
87
- restartCount,
88
- contentionCount,
89
- locksLost,
90
- });
91
- }
92
- }
68
+ const acquireLock = (key: string) => {
69
+ // If the option is set and the same client already holds both keys,
70
+ // update the expiry and resolve immediately.
71
+ if (options?.replaceIfSameClient) {
72
+ const currentX = this.getItem(x);
73
+ const currentY = this.getItem(y);
74
+ if (currentX === this.clientId && currentY === this.clientId) {
75
+ // Update expiry so that the lock is effectively "replaced"
76
+ this.setItem(x, this.clientId, keepLocked);
77
+ this.setItem(y, this.clientId, keepLocked);
78
+ debug(
79
+ 'FastMutex client "%s" replaced its own lock on "%s".',
80
+ this.clientId,
81
+ key,
82
+ );
83
+ return resolve({
84
+ restartCount,
85
+ contentionCount,
86
+ locksLost,
87
+ });
88
+ }
89
+ }
93
90
 
94
- // Check for overall retries/timeouts.
95
- if (
96
- restartCount > 1000 ||
97
- contentionCount > 1000 ||
98
- locksLost > 1000
99
- ) {
100
- return reject("Failed to resolve lock");
101
- }
102
- const elapsedTime = Date.now() - acquireStart;
103
- if (elapsedTime >= this.timeout) {
104
- debug(
105
- 'Lock on "%s" could not be acquired within %sms by FastMutex client "%s"',
106
- key,
107
- this.timeout,
108
- this.clientId
109
- );
110
- return reject(
111
- new Error(
112
- `Lock could not be acquired within ${this.timeout}ms`
113
- )
114
- );
115
- }
91
+ // Check for overall retries/timeouts.
92
+ if (restartCount > 1000 || contentionCount > 1000 || locksLost > 1000) {
93
+ return reject("Failed to resolve lock");
94
+ }
95
+ const elapsedTime = Date.now() - acquireStart;
96
+ if (elapsedTime >= this.timeout) {
97
+ debug(
98
+ 'Lock on "%s" could not be acquired within %sms by FastMutex client "%s"',
99
+ key,
100
+ this.timeout,
101
+ this.clientId,
102
+ );
103
+ return reject(
104
+ new Error(`Lock could not be acquired within ${this.timeout}ms`),
105
+ );
106
+ }
116
107
 
117
- // First, set key X.
118
- this.setItem(x, this.clientId, keepLocked);
108
+ // First, set key X.
109
+ this.setItem(x, this.clientId, keepLocked);
119
110
 
120
- // Check if key Y exists (another client may be acquiring the lock)
121
- let lsY = this.getItem(y);
122
- if (lsY) {
123
- debug("Lock exists on Y (%s), restarting...", lsY);
124
- restartCount++;
125
- setTimeout(() => acquireLock(key), 10);
126
- return;
127
- }
111
+ // Check if key Y exists (another client may be acquiring the lock)
112
+ let lsY = this.getItem(y);
113
+ if (lsY) {
114
+ debug("Lock exists on Y (%s), restarting...", lsY);
115
+ restartCount++;
116
+ setTimeout(() => acquireLock(key), 10);
117
+ return;
118
+ }
128
119
 
129
- // Request the inner lock by setting Y.
130
- this.setItem(y, this.clientId, keepLocked);
120
+ // Request the inner lock by setting Y.
121
+ this.setItem(y, this.clientId, keepLocked);
131
122
 
132
- // Re-check X; if it was changed, we have contention.
133
- let lsX = this.getItem(x);
134
- if (lsX !== this.clientId) {
135
- contentionCount++;
136
- debug('Lock contention detected. X="%s"', lsX);
137
- setTimeout(() => {
138
- lsY = this.getItem(y);
139
- if (lsY === this.clientId) {
140
- debug(
141
- 'FastMutex client "%s" won the lock contention on "%s"',
142
- this.clientId,
143
- key
144
- );
145
- resolve({
146
- restartCount,
147
- contentionCount,
148
- locksLost,
149
- });
150
- } else {
151
- restartCount++;
152
- locksLost++;
153
- debug(
154
- 'FastMutex client "%s" lost the lock contention on "%s" to another process (%s). Restarting...',
155
- this.clientId,
156
- key,
157
- lsY
158
- );
159
- setTimeout(() => acquireLock(key), 10);
160
- }
161
- }, 50);
162
- return;
163
- }
123
+ // Re-check X; if it was changed, we have contention.
124
+ let lsX = this.getItem(x);
125
+ if (lsX !== this.clientId) {
126
+ contentionCount++;
127
+ debug('Lock contention detected. X="%s"', lsX);
128
+ setTimeout(() => {
129
+ lsY = this.getItem(y);
130
+ if (lsY === this.clientId) {
131
+ debug(
132
+ 'FastMutex client "%s" won the lock contention on "%s"',
133
+ this.clientId,
134
+ key,
135
+ );
136
+ resolve({
137
+ restartCount,
138
+ contentionCount,
139
+ locksLost,
140
+ });
141
+ } else {
142
+ restartCount++;
143
+ locksLost++;
144
+ debug(
145
+ 'FastMutex client "%s" lost the lock contention on "%s" to another process (%s). Restarting...',
146
+ this.clientId,
147
+ key,
148
+ lsY,
149
+ );
150
+ setTimeout(() => acquireLock(key), 10);
151
+ }
152
+ }, 50);
153
+ return;
154
+ }
164
155
 
165
- // No contention: lock is acquired.
166
- debug(
167
- 'FastMutex client "%s" acquired a lock on "%s" with no contention',
168
- this.clientId,
169
- key
170
- );
171
- resolve({ restartCount, contentionCount, locksLost });
172
- };
156
+ // No contention: lock is acquired.
157
+ debug(
158
+ 'FastMutex client "%s" acquired a lock on "%s" with no contention',
159
+ this.clientId,
160
+ key,
161
+ );
162
+ resolve({ restartCount, contentionCount, locksLost });
163
+ };
173
164
 
174
- acquireLock(key);
175
- });
176
- }
165
+ acquireLock(key);
166
+ });
167
+ }
177
168
 
178
- isLocked(key: string) {
179
- const x = this.xPrefix + key;
180
- const y = this.yPrefix + key;
181
- return !!this.getItem(x) || !!this.getItem(y);
182
- }
169
+ isLocked(key: string) {
170
+ const x = this.xPrefix + key;
171
+ const y = this.yPrefix + key;
172
+ return !!this.getItem(x) || !!this.getItem(y);
173
+ }
183
174
 
184
- getLockedInfo(key: string): string | undefined {
185
- const x = this.xPrefix + key;
186
- const y = this.yPrefix + key;
187
- return this.getItem(x) || this.getItem(y);
188
- }
175
+ getLockedInfo(key: string): string | undefined {
176
+ const x = this.xPrefix + key;
177
+ const y = this.yPrefix + key;
178
+ return this.getItem(x) || this.getItem(y);
179
+ }
189
180
 
190
- release(key: string) {
191
- debug(
192
- 'FastMutex client "%s" is releasing lock on "%s"',
193
- this.clientId,
194
- key
195
- );
196
- let ps = [this.yPrefix + key, this.xPrefix + key];
197
- for (const p of ps) {
198
- clearInterval(this.intervals.get(p));
199
- this.intervals.delete(p);
200
- this.localStorage.removeItem(p);
201
- }
202
- }
181
+ release(key: string) {
182
+ debug(
183
+ 'FastMutex client "%s" is releasing lock on "%s"',
184
+ this.clientId,
185
+ key,
186
+ );
187
+ let ps = [this.yPrefix + key, this.xPrefix + key];
188
+ for (const p of ps) {
189
+ clearInterval(this.intervals.get(p));
190
+ this.intervals.delete(p);
191
+ this.localStorage.removeItem(p);
192
+ }
193
+ }
203
194
 
204
- /**
205
- * Helper function to wrap all values in an object that includes the time (so
206
- * that we can expire it in the future) and json.stringify's it
207
- */
208
- setItem(key: string, value: any, keepLocked?: () => boolean) {
209
- if (!keepLocked) {
210
- return this.localStorage.setItem(
211
- key,
212
- JSON.stringify({
213
- expiresAt: new Date().getTime() + this.timeout,
214
- value,
215
- })
216
- );
217
- } else {
218
- let getExpiry = () => +new Date() + this.timeout;
219
- const ret = this.localStorage.setItem(
220
- key,
221
- JSON.stringify({
222
- expiresAt: getExpiry(),
223
- value,
224
- })
225
- );
226
- const interval = setInterval(() => {
227
- if (!keepLocked()) {
228
- this.localStorage.setItem(
229
- // TODO, release directly?
230
- key,
231
- JSON.stringify({
232
- expiresAt: 0,
233
- value,
234
- })
235
- );
236
- } else {
237
- this.localStorage.setItem(
238
- key,
239
- JSON.stringify({
240
- expiresAt: getExpiry(), // bump expiry
241
- value,
242
- })
243
- );
244
- }
245
- }, this.timeout);
246
- this.intervals.set(key, interval);
247
- return ret;
248
- }
249
- }
195
+ /**
196
+ * Helper function to wrap all values in an object that includes the time (so
197
+ * that we can expire it in the future) and json.stringify's it
198
+ */
199
+ setItem(key: string, value: any, keepLocked?: () => boolean) {
200
+ if (!keepLocked) {
201
+ return this.localStorage.setItem(
202
+ key,
203
+ JSON.stringify({
204
+ expiresAt: new Date().getTime() + this.timeout,
205
+ value,
206
+ }),
207
+ );
208
+ } else {
209
+ let getExpiry = () => +new Date() + this.timeout;
210
+ const ret = this.localStorage.setItem(
211
+ key,
212
+ JSON.stringify({
213
+ expiresAt: getExpiry(),
214
+ value,
215
+ }),
216
+ );
217
+ const interval = setInterval(() => {
218
+ if (!keepLocked()) {
219
+ this.localStorage.setItem(
220
+ // TODO, release directly?
221
+ key,
222
+ JSON.stringify({
223
+ expiresAt: 0,
224
+ value,
225
+ }),
226
+ );
227
+ } else {
228
+ this.localStorage.setItem(
229
+ key,
230
+ JSON.stringify({
231
+ expiresAt: getExpiry(), // bump expiry
232
+ value,
233
+ }),
234
+ );
235
+ }
236
+ }, this.timeout);
237
+ this.intervals.set(key, interval);
238
+ return ret;
239
+ }
240
+ }
250
241
 
251
- /**
252
- * Helper function to parse JSON encoded values set in localStorage
253
- */
254
- getItem(key: string): string | undefined {
255
- const item = this.localStorage.getItem(key);
256
- if (!item) return;
242
+ /**
243
+ * Helper function to parse JSON encoded values set in localStorage
244
+ */
245
+ getItem(key: string): string | undefined {
246
+ const item = this.localStorage.getItem(key);
247
+ if (!item) return;
257
248
 
258
- const parsed = JSON.parse(item);
259
- if (new Date().getTime() - parsed.expiresAt >= this.timeout) {
260
- debug(
261
- 'FastMutex client "%s" removed an expired record on "%s"',
262
- this.clientId,
263
- key
264
- );
265
- this.localStorage.removeItem(key);
266
- clearInterval(this.intervals.get(key));
267
- this.intervals.delete(key);
268
- return;
269
- }
249
+ const parsed = JSON.parse(item);
250
+ if (new Date().getTime() - parsed.expiresAt >= this.timeout) {
251
+ debug(
252
+ 'FastMutex client "%s" removed an expired record on "%s"',
253
+ this.clientId,
254
+ key,
255
+ );
256
+ this.localStorage.removeItem(key);
257
+ clearInterval(this.intervals.get(key));
258
+ this.intervals.delete(key);
259
+ return;
260
+ }
270
261
 
271
- return JSON.parse(item).value;
272
- }
262
+ return JSON.parse(item).value;
263
+ }
273
264
  }
@@ -0,0 +1,15 @@
1
+ import { type EffectCallback, useEffect, useRef } from "react";
2
+
3
+ export const useMount = (effect: EffectCallback) => {
4
+ const mounted = useRef(false);
5
+
6
+ useEffect(() => {
7
+ if (!mounted.current) {
8
+ effect();
9
+ }
10
+
11
+ mounted.current = true;
12
+
13
+ return () => {};
14
+ }, [mounted.current]);
15
+ };