@hairy/react-lib 1.24.0 → 1.26.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) {
@@ -123,28 +168,36 @@ function repack(c) {
123
168
  return c.component ? c : { component: c };
124
169
  }
125
170
 
126
- // src/components/utils/Trans.ts
127
- import HTML from "html-parse-stringify";
171
+ // src/components/utils/Trigger.ts
128
172
  import { createElement as createElement3 } from "react";
129
- import { useTranslation } from "react-i18next";
130
- function Trans({ i18nKey, ...additionalProps }) {
131
- const translation = useTranslation().t(i18nKey, additionalProps);
132
- return renderNodes(HTML.parse(translation), additionalProps);
133
- }
134
- function renderNodes(tokens, values) {
135
- let index = 0;
136
- return tokens.map((token) => {
137
- if (token.type === "text")
138
- return token.content;
139
- index++;
140
- const props = { ...token.attrs, key: index };
141
- return token.voidElement ? values[token.name] ? createElement3("span", { key: index }, values[token.name]) : createElement3(token.name, props) : createElement3(token.name, props, renderNodes(token.children, {}));
142
- });
173
+ import { useSnapshot } from "valtio";
174
+ import { proxyMap } from "valtio/utils";
175
+ var pendingTasks = proxyMap();
176
+ function createTracker(exposer) {
177
+ const Component = () => {
178
+ try {
179
+ exposer.deferred.resolve(exposer.fn(...exposer.args));
180
+ } catch (error) {
181
+ exposer.deferred.reject(error);
182
+ }
183
+ return null;
184
+ };
185
+ Component.key = exposer.id;
186
+ return Component;
187
+ }
188
+ function renderTracker(Tracker) {
189
+ return createElement3(Tracker, { key: Tracker.key });
143
190
  }
191
+ function Trigger() {
192
+ const values = [...useSnapshot(pendingTasks).values()];
193
+ return values.map(createTracker).map(renderTracker);
194
+ }
195
+ Trigger.id = 0;
196
+ Trigger.tasks = pendingTasks;
144
197
 
145
198
  // src/hooks/ref.ts
146
199
  import { useState } from "react";
147
- function ref(value) {
200
+ function ref2(value) {
148
201
  function define(value2) {
149
202
  if (typeof value2 === "function")
150
203
  return (prev) => define(value2(prev.value));
@@ -154,8 +207,8 @@ function ref(value) {
154
207
  enumerable: true
155
208
  });
156
209
  }
157
- const [ref2, set] = useState(define(value));
158
- return ref2;
210
+ const [ref3, set] = useState(define(value));
211
+ return ref3;
159
212
  }
160
213
 
161
214
  // src/hooks/useAsyncCallback.ts
@@ -312,16 +365,7 @@ import { watch } from "valtio/utils";
312
365
 
313
366
  // src/storage/defineStore.ts
314
367
  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
- }
368
+ import { proxy as proxy2, subscribe as subscribe2, useSnapshot as useSnapshot2 } from "valtio";
325
369
 
326
370
  // src/storage/persistant.ts
327
371
  import { proxy, subscribe } from "valtio";
@@ -374,10 +418,10 @@ function defineStore(store, options = {}) {
374
418
  Object.assign($state, patch);
375
419
  }
376
420
  function $signal(fn) {
377
- return createElement4(() => fn(useSnapshot($state)));
421
+ return createElement4(() => fn(useSnapshot2($state)));
378
422
  }
379
423
  $signal.status = function(fn) {
380
- return createElement4(() => fn(useSnapshot($status)));
424
+ return createElement4(() => fn(useSnapshot2($status)));
381
425
  };
382
426
  return {
383
427
  $subscribe,
@@ -390,7 +434,7 @@ function defineStore(store, options = {}) {
390
434
  ...$actions
391
435
  };
392
436
  }
393
- function track(action, status) {
437
+ function track2(action, status) {
394
438
  let loadings = 0;
395
439
  const tracking = () => loadings++ === 0 && (status.loading = true);
396
440
  const done = () => !--loadings && (status.loading = false);
@@ -417,7 +461,7 @@ function track(action, status) {
417
461
  function setupActions($state, actions, $actions, $status) {
418
462
  for (const key in actions) {
419
463
  $status[key] = { finished: false, loading: false, error: null };
420
- $actions[key] = track(actions[key].bind($state), $status[key]);
464
+ $actions[key] = track2(actions[key].bind($state), $status[key]);
421
465
  }
422
466
  }
423
467
  function setupGetters(state, $state, getters, $getters) {
@@ -479,15 +523,15 @@ function defineAsyncStore(fetch, options = {}) {
479
523
  }
480
524
 
481
525
  // src/storage/useStatus.tsx
482
- import { useSnapshot as useSnapshot2 } from "valtio";
526
+ import { useSnapshot as useSnapshot3 } from "valtio";
483
527
  function useStatus(store) {
484
- return useSnapshot2(store.$status);
528
+ return useSnapshot3(store.$status);
485
529
  }
486
530
 
487
531
  // src/storage/useStore.ts
488
- import { useSnapshot as useSnapshot3 } from "valtio";
532
+ import { useSnapshot as useSnapshot4 } from "valtio";
489
533
  function useStore(store) {
490
- return useSnapshot3(store.$state);
534
+ return useSnapshot4(store.$state);
491
535
  }
492
536
  export {
493
537
  Case,
@@ -497,13 +541,14 @@ export {
497
541
  Injector,
498
542
  Switch,
499
543
  Then,
500
- Trans,
544
+ Trigger,
501
545
  Unless,
502
546
  cls,
503
547
  defineAsyncStore,
504
548
  defineStore,
505
549
  proxyWithPersistant,
506
- ref,
550
+ ref2 as ref,
551
+ track,
507
552
  useAsyncCallback,
508
553
  useAsyncState,
509
554
  useDebounce,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.24.0",
4
+ "version": "1.26.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.24.0"
41
+ "@hairy/utils": "1.26.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",