@hairy/react-lib 1.23.0 → 1.25.0

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/index.js CHANGED
@@ -31,6 +31,51 @@ cls.append = function(value, newClass) {
31
31
  return value ? `${value} ${newClass}` : newClass;
32
32
  };
33
33
 
34
+ // ../util-core/src/util/noop.ts
35
+ var noop = () => {
36
+ };
37
+
38
+ // ../util-core/src/util/deferred.ts
39
+ var Deferred = class extends Promise {
40
+ resolve;
41
+ reject;
42
+ constructor(executor = noop) {
43
+ let _resolve, _reject;
44
+ super((resolve_, reject_) => {
45
+ _resolve = resolve_;
46
+ _reject = reject_;
47
+ return executor(resolve_, reject_);
48
+ });
49
+ this.resolve = (value) => {
50
+ _resolve(value);
51
+ return this;
52
+ };
53
+ this.reject = (reason) => {
54
+ _reject(reason);
55
+ return this;
56
+ };
57
+ }
58
+ };
59
+
60
+ // ../util-core/src/util/json.ts
61
+ function jsonTryParse(text) {
62
+ try {
63
+ return JSON.parse(text || "");
64
+ } catch {
65
+ return void 0;
66
+ }
67
+ }
68
+
69
+ // src/utils/track.ts
70
+ import { ref } from "valtio";
71
+ function track(fn, ...args) {
72
+ const deferred = ref(new Deferred());
73
+ const exposer = { fn, args, deferred, id: ++Trigger.id };
74
+ Trigger.tasks.set(exposer.id, exposer);
75
+ deferred.then(() => Trigger.tasks.delete(exposer.id));
76
+ return deferred;
77
+ }
78
+
34
79
  // src/utils/wrapper.ts
35
80
  import { createElement } from "react";
36
81
  function wrapper(tag, props, children) {
@@ -142,9 +187,36 @@ function renderNodes(tokens, values) {
142
187
  });
143
188
  }
144
189
 
190
+ // src/components/utils/Trigger.ts
191
+ import { createElement as createElement4 } from "react";
192
+ import { useSnapshot } from "valtio";
193
+ import { proxyMap } from "valtio/utils";
194
+ var pendingTasks = proxyMap();
195
+ function createTracker(exposer) {
196
+ const Component = () => {
197
+ try {
198
+ exposer.deferred.resolve(exposer.fn(...exposer.args));
199
+ } catch (error) {
200
+ exposer.deferred.reject(error);
201
+ }
202
+ return null;
203
+ };
204
+ Component.key = exposer.id;
205
+ return Component;
206
+ }
207
+ function renderTracker(Tracker) {
208
+ return createElement4(Tracker, { key: Tracker.key });
209
+ }
210
+ function Trigger() {
211
+ const values = [...useSnapshot(pendingTasks).values()];
212
+ return values.map(createTracker).map(renderTracker);
213
+ }
214
+ Trigger.id = 0;
215
+ Trigger.tasks = pendingTasks;
216
+
145
217
  // src/hooks/ref.ts
146
218
  import { useState } from "react";
147
- function ref(value) {
219
+ function ref2(value) {
148
220
  function define(value2) {
149
221
  if (typeof value2 === "function")
150
222
  return (prev) => define(value2(prev.value));
@@ -154,8 +226,8 @@ function ref(value) {
154
226
  enumerable: true
155
227
  });
156
228
  }
157
- const [ref2, set] = useState(define(value));
158
- return ref2;
229
+ const [ref3, set] = useState(define(value));
230
+ return ref3;
159
231
  }
160
232
 
161
233
  // src/hooks/useAsyncCallback.ts
@@ -311,17 +383,8 @@ function useWhenever(source, cb, options) {
311
383
  import { watch } from "valtio/utils";
312
384
 
313
385
  // src/storage/defineStore.ts
314
- import { createElement as createElement4 } from "react";
315
- import { proxy as proxy2, subscribe as subscribe2, useSnapshot } from "valtio";
316
-
317
- // ../util-core/src/util/json.ts
318
- function jsonTryParse(text) {
319
- try {
320
- return JSON.parse(text || "");
321
- } catch {
322
- return void 0;
323
- }
324
- }
386
+ import { createElement as createElement5 } from "react";
387
+ import { proxy as proxy2, subscribe as subscribe2, useSnapshot as useSnapshot2 } from "valtio";
325
388
 
326
389
  // src/storage/persistant.ts
327
390
  import { proxy, subscribe } from "valtio";
@@ -374,10 +437,10 @@ function defineStore(store, options = {}) {
374
437
  Object.assign($state, patch);
375
438
  }
376
439
  function $signal(fn) {
377
- return createElement4(() => fn(useSnapshot($state)));
440
+ return createElement5(() => fn(useSnapshot2($state)));
378
441
  }
379
442
  $signal.status = function(fn) {
380
- return createElement4(() => fn(useSnapshot($status)));
443
+ return createElement5(() => fn(useSnapshot2($status)));
381
444
  };
382
445
  return {
383
446
  $subscribe,
@@ -390,7 +453,7 @@ function defineStore(store, options = {}) {
390
453
  ...$actions
391
454
  };
392
455
  }
393
- function track(action, status) {
456
+ function track2(action, status) {
394
457
  let loadings = 0;
395
458
  const tracking = () => loadings++ === 0 && (status.loading = true);
396
459
  const done = () => !--loadings && (status.loading = false);
@@ -417,7 +480,7 @@ function track(action, status) {
417
480
  function setupActions($state, actions, $actions, $status) {
418
481
  for (const key in actions) {
419
482
  $status[key] = { finished: false, loading: false, error: null };
420
- $actions[key] = track(actions[key].bind($state), $status[key]);
483
+ $actions[key] = track2(actions[key].bind($state), $status[key]);
421
484
  }
422
485
  }
423
486
  function setupGetters(state, $state, getters, $getters) {
@@ -479,15 +542,15 @@ function defineAsyncStore(fetch, options = {}) {
479
542
  }
480
543
 
481
544
  // src/storage/useStatus.tsx
482
- import { useSnapshot as useSnapshot2 } from "valtio";
545
+ import { useSnapshot as useSnapshot3 } from "valtio";
483
546
  function useStatus(store) {
484
- return useSnapshot2(store.$status);
547
+ return useSnapshot3(store.$status);
485
548
  }
486
549
 
487
550
  // src/storage/useStore.ts
488
- import { useSnapshot as useSnapshot3 } from "valtio";
551
+ import { useSnapshot as useSnapshot4 } from "valtio";
489
552
  function useStore(store) {
490
- return useSnapshot3(store.$state);
553
+ return useSnapshot4(store.$state);
491
554
  }
492
555
  export {
493
556
  Case,
@@ -498,12 +561,14 @@ export {
498
561
  Switch,
499
562
  Then,
500
563
  Trans,
564
+ Trigger,
501
565
  Unless,
502
566
  cls,
503
567
  defineAsyncStore,
504
568
  defineStore,
505
569
  proxyWithPersistant,
506
- ref,
570
+ ref2 as ref,
571
+ track,
507
572
  useAsyncCallback,
508
573
  useAsyncState,
509
574
  useDebounce,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.23.0",
4
+ "version": "1.25.0",
5
5
  "description": "Library for react",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "react-dom": "^18.2.0",
39
39
  "react-i18next": "^14.1.2",
40
40
  "react-use": "^17.6.0",
41
- "@hairy/utils": "1.23.0"
41
+ "@hairy/utils": "1.25.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",