@taicode/common-web 1.0.5 → 1.1.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.
Files changed (71) hide show
  1. package/output/helpers/service/index.d.ts +1 -0
  2. package/output/helpers/service/index.d.ts.map +1 -0
  3. package/output/helpers/service/index.js +1 -0
  4. package/output/helpers/service/service.d.ts +5 -0
  5. package/output/helpers/service/service.d.ts.map +1 -0
  6. package/output/helpers/service/service.js +2 -0
  7. package/output/helpers/side-cache/index.d.ts +2 -0
  8. package/output/helpers/side-cache/index.d.ts.map +1 -0
  9. package/output/helpers/side-cache/index.js +1 -0
  10. package/{source/utils/cache/aside.d.ts → output/helpers/side-cache/side-cache.d.ts} +2 -2
  11. package/output/helpers/side-cache/side-cache.d.ts.map +1 -0
  12. package/output/{utils/cache/aside.js → helpers/side-cache/side-cache.js} +12 -12
  13. package/output/helpers/side-cache/side-cache.test.d.ts +2 -0
  14. package/output/helpers/side-cache/side-cache.test.d.ts.map +1 -0
  15. package/output/helpers/side-cache/side-cache.test.js +89 -0
  16. package/output/helpers/use-observer/index.d.ts +2 -0
  17. package/output/helpers/use-observer/index.d.ts.map +1 -0
  18. package/output/helpers/use-observer/index.js +1 -0
  19. package/output/{hooks/mobx.d.ts → helpers/use-observer/use-observer.d.ts} +1 -1
  20. package/output/helpers/use-observer/use-observer.d.ts.map +1 -0
  21. package/output/helpers/use-observer/use-observer.test.d.ts +2 -0
  22. package/output/helpers/use-observer/use-observer.test.d.ts.map +1 -0
  23. package/output/helpers/use-observer/use-observer.test.jsx +134 -0
  24. package/package.json +23 -16
  25. package/output/hooks/mobx.d.ts.map +0 -1
  26. package/output/utils/cache/aside.d.ts +0 -7
  27. package/output/utils/cache/aside.d.ts.map +0 -1
  28. package/output/utils/cache/index.d.ts +0 -2
  29. package/output/utils/cache/index.d.ts.map +0 -1
  30. package/output/utils/cache/index.js +0 -1
  31. package/source/catalyst/CHANGELOG.md +0 -136
  32. package/source/catalyst/README.md +0 -65
  33. package/source/catalyst/alert.tsx +0 -95
  34. package/source/catalyst/auth-layout.tsx +0 -11
  35. package/source/catalyst/avatar.tsx +0 -84
  36. package/source/catalyst/badge.tsx +0 -82
  37. package/source/catalyst/button.tsx +0 -204
  38. package/source/catalyst/checkbox.tsx +0 -157
  39. package/source/catalyst/combobox.tsx +0 -188
  40. package/source/catalyst/description-list.tsx +0 -37
  41. package/source/catalyst/dialog.tsx +0 -86
  42. package/source/catalyst/divider.tsx +0 -20
  43. package/source/catalyst/dropdown.tsx +0 -183
  44. package/source/catalyst/fieldset.tsx +0 -91
  45. package/source/catalyst/heading.tsx +0 -27
  46. package/source/catalyst/input.tsx +0 -94
  47. package/source/catalyst/link.tsx +0 -21
  48. package/source/catalyst/listbox.tsx +0 -177
  49. package/source/catalyst/navbar.tsx +0 -96
  50. package/source/catalyst/pagination.tsx +0 -98
  51. package/source/catalyst/radio.tsx +0 -142
  52. package/source/catalyst/select.tsx +0 -68
  53. package/source/catalyst/sidebar-layout.tsx +0 -82
  54. package/source/catalyst/sidebar.tsx +0 -142
  55. package/source/catalyst/stacked-layout.tsx +0 -79
  56. package/source/catalyst/switch.tsx +0 -195
  57. package/source/catalyst/table.tsx +0 -124
  58. package/source/catalyst/text.tsx +0 -40
  59. package/source/catalyst/textarea.tsx +0 -54
  60. package/source/hooks/mobx.d.ts +0 -3
  61. package/source/hooks/mobx.d.ts.map +0 -1
  62. package/source/hooks/mobx.js +0 -16
  63. package/source/hooks/mobx.ts +0 -24
  64. package/source/utils/cache/aside.d.ts.map +0 -1
  65. package/source/utils/cache/aside.js +0 -106
  66. package/source/utils/cache/aside.ts +0 -39
  67. package/source/utils/cache/index.d.ts +0 -2
  68. package/source/utils/cache/index.d.ts.map +0 -1
  69. package/source/utils/cache/index.js +0 -1
  70. package/source/utils/cache/index.ts +0 -1
  71. /package/output/{hooks/mobx.js → helpers/use-observer/use-observer.js} +0 -0
@@ -1,124 +0,0 @@
1
- 'use client'
2
-
3
- import clsx from 'clsx'
4
- import type React from 'react'
5
- import { createContext, useContext, useState } from 'react'
6
- import { Link } from './link'
7
-
8
- const TableContext = createContext<{ bleed: boolean; dense: boolean; grid: boolean; striped: boolean }>({
9
- bleed: false,
10
- dense: false,
11
- grid: false,
12
- striped: false,
13
- })
14
-
15
- export function Table({
16
- bleed = false,
17
- dense = false,
18
- grid = false,
19
- striped = false,
20
- className,
21
- children,
22
- ...props
23
- }: { bleed?: boolean; dense?: boolean; grid?: boolean; striped?: boolean } & React.ComponentPropsWithoutRef<'div'>) {
24
- return (
25
- <TableContext.Provider value={{ bleed, dense, grid, striped } as React.ContextType<typeof TableContext>}>
26
- <div className="flow-root">
27
- <div {...props} className={clsx(className, '-mx-(--gutter) overflow-x-auto whitespace-nowrap')}>
28
- <div className={clsx('inline-block min-w-full align-middle', !bleed && 'sm:px-(--gutter)')}>
29
- <table className="min-w-full text-left text-sm/6 text-zinc-950 dark:text-white">{children}</table>
30
- </div>
31
- </div>
32
- </div>
33
- </TableContext.Provider>
34
- )
35
- }
36
-
37
- export function TableHead({ className, ...props }: React.ComponentPropsWithoutRef<'thead'>) {
38
- return <thead {...props} className={clsx(className, 'text-zinc-500 dark:text-zinc-400')} />
39
- }
40
-
41
- export function TableBody(props: React.ComponentPropsWithoutRef<'tbody'>) {
42
- return <tbody {...props} />
43
- }
44
-
45
- const TableRowContext = createContext<{ href?: string; target?: string; title?: string }>({
46
- href: undefined,
47
- target: undefined,
48
- title: undefined,
49
- })
50
-
51
- export function TableRow({
52
- href,
53
- target,
54
- title,
55
- className,
56
- ...props
57
- }: { href?: string; target?: string; title?: string } & React.ComponentPropsWithoutRef<'tr'>) {
58
- let { striped } = useContext(TableContext)
59
-
60
- return (
61
- <TableRowContext.Provider value={{ href, target, title } as React.ContextType<typeof TableRowContext>}>
62
- <tr
63
- {...props}
64
- className={clsx(
65
- className,
66
- href &&
67
- 'has-[[data-row-link][data-focus]]:outline-2 has-[[data-row-link][data-focus]]:-outline-offset-2 has-[[data-row-link][data-focus]]:outline-blue-500 dark:focus-within:bg-white/2.5',
68
- striped && 'even:bg-zinc-950/2.5 dark:even:bg-white/2.5',
69
- href && striped && 'hover:bg-zinc-950/5 dark:hover:bg-white/5',
70
- href && !striped && 'hover:bg-zinc-950/2.5 dark:hover:bg-white/2.5'
71
- )}
72
- />
73
- </TableRowContext.Provider>
74
- )
75
- }
76
-
77
- export function TableHeader({ className, ...props }: React.ComponentPropsWithoutRef<'th'>) {
78
- let { bleed, grid } = useContext(TableContext)
79
-
80
- return (
81
- <th
82
- {...props}
83
- className={clsx(
84
- className,
85
- 'border-b border-b-zinc-950/10 px-4 py-2 font-medium first:pl-(--gutter,--spacing(2)) last:pr-(--gutter,--spacing(2)) dark:border-b-white/10',
86
- grid && 'border-l border-l-zinc-950/5 first:border-l-0 dark:border-l-white/5',
87
- !bleed && 'sm:first:pl-1 sm:last:pr-1'
88
- )}
89
- />
90
- )
91
- }
92
-
93
- export function TableCell({ className, children, ...props }: React.ComponentPropsWithoutRef<'td'>) {
94
- let { bleed, dense, grid, striped } = useContext(TableContext)
95
- let { href, target, title } = useContext(TableRowContext)
96
- let [cellRef, setCellRef] = useState<HTMLElement | null>(null)
97
-
98
- return (
99
- <td
100
- ref={href ? setCellRef : undefined}
101
- {...props}
102
- className={clsx(
103
- className,
104
- 'relative px-4 first:pl-(--gutter,--spacing(2)) last:pr-(--gutter,--spacing(2))',
105
- !striped && 'border-b border-zinc-950/5 dark:border-white/5',
106
- grid && 'border-l border-l-zinc-950/5 first:border-l-0 dark:border-l-white/5',
107
- dense ? 'py-2.5' : 'py-4',
108
- !bleed && 'sm:first:pl-1 sm:last:pr-1'
109
- )}
110
- >
111
- {href && (
112
- <Link
113
- data-row-link
114
- href={href}
115
- target={target}
116
- aria-label={title}
117
- tabIndex={cellRef?.previousElementSibling === null ? 0 : -1}
118
- className="absolute inset-0 focus:outline-hidden"
119
- />
120
- )}
121
- {children}
122
- </td>
123
- )
124
- }
@@ -1,40 +0,0 @@
1
- import clsx from 'clsx'
2
- import { Link } from './link'
3
-
4
- export function Text({ className, ...props }: React.ComponentPropsWithoutRef<'p'>) {
5
- return (
6
- <p
7
- data-slot="text"
8
- {...props}
9
- className={clsx(className, 'text-base/6 text-zinc-500 sm:text-sm/6 dark:text-zinc-400')}
10
- />
11
- )
12
- }
13
-
14
- export function TextLink({ className, ...props }: React.ComponentPropsWithoutRef<typeof Link>) {
15
- return (
16
- <Link
17
- {...props}
18
- className={clsx(
19
- className,
20
- 'text-zinc-950 underline decoration-zinc-950/50 data-hover:decoration-zinc-950 dark:text-white dark:decoration-white/50 dark:data-hover:decoration-white'
21
- )}
22
- />
23
- )
24
- }
25
-
26
- export function Strong({ className, ...props }: React.ComponentPropsWithoutRef<'strong'>) {
27
- return <strong {...props} className={clsx(className, 'font-medium text-zinc-950 dark:text-white')} />
28
- }
29
-
30
- export function Code({ className, ...props }: React.ComponentPropsWithoutRef<'code'>) {
31
- return (
32
- <code
33
- {...props}
34
- className={clsx(
35
- className,
36
- 'rounded-sm border border-zinc-950/10 bg-zinc-950/2.5 px-0.5 text-sm font-medium text-zinc-950 sm:text-[0.8125rem] dark:border-white/20 dark:bg-white/5 dark:text-white'
37
- )}
38
- />
39
- )
40
- }
@@ -1,54 +0,0 @@
1
- import * as Headless from '@headlessui/react'
2
- import clsx from 'clsx'
3
- import React, { forwardRef } from 'react'
4
-
5
- export const Textarea = forwardRef(function Textarea(
6
- {
7
- className,
8
- resizable = true,
9
- ...props
10
- }: { className?: string; resizable?: boolean } & Omit<Headless.TextareaProps, 'as' | 'className'>,
11
- ref: React.ForwardedRef<HTMLTextAreaElement>
12
- ) {
13
- return (
14
- <span
15
- data-slot="control"
16
- className={clsx([
17
- className,
18
- // Basic layout
19
- 'relative block w-full',
20
- // Background color + shadow applied to inset pseudo element, so shadow blends with border in light mode
21
- 'before:absolute before:inset-px before:rounded-[calc(var(--radius-lg)-1px)] before:bg-white before:shadow-sm',
22
- // Background color is moved to control and shadow is removed in dark mode so hide `before` pseudo
23
- 'dark:before:hidden',
24
- // Focus ring
25
- 'after:pointer-events-none after:absolute after:inset-0 after:rounded-lg after:ring-transparent after:ring-inset sm:focus-within:after:ring-2 sm:focus-within:after:ring-blue-500',
26
- // Disabled state
27
- 'has-data-disabled:opacity-50 has-data-disabled:before:bg-zinc-950/5 has-data-disabled:before:shadow-none',
28
- ])}
29
- >
30
- <Headless.Textarea
31
- ref={ref}
32
- {...props}
33
- className={clsx([
34
- // Basic layout
35
- 'relative block h-full w-full appearance-none rounded-lg px-[calc(--spacing(3.5)-1px)] py-[calc(--spacing(2.5)-1px)] sm:px-[calc(--spacing(3)-1px)] sm:py-[calc(--spacing(1.5)-1px)]',
36
- // Typography
37
- 'text-base/6 text-zinc-950 placeholder:text-zinc-500 sm:text-sm/6 dark:text-white',
38
- // Border
39
- 'border border-zinc-950/10 data-hover:border-zinc-950/20 dark:border-white/10 dark:data-hover:border-white/20',
40
- // Background color
41
- 'bg-transparent dark:bg-white/5',
42
- // Hide default focus styles
43
- 'focus:outline-hidden',
44
- // Invalid state
45
- 'data-invalid:border-red-500 data-invalid:data-hover:border-red-500 dark:data-invalid:border-red-600 dark:data-invalid:data-hover:border-red-600',
46
- // Disabled state
47
- 'disabled:border-zinc-950/20 dark:disabled:border-white/15 dark:disabled:bg-white/2.5 dark:data-hover:disabled:border-white/15',
48
- // Resizable
49
- resizable ? 'resize-y' : 'resize-none',
50
- ])}
51
- />
52
- </span>
53
- )
54
- })
@@ -1,3 +0,0 @@
1
- export declare function useObserver<T>(initialValue: T): T;
2
- export declare function useObserver<T, U>(initialValue: T, selector: (v: T) => U): U;
3
- //# sourceMappingURL=mobx.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mobx.d.ts","sourceRoot":"","sources":["mobx.ts"],"names":[],"mappings":"AAGA,wBAAgB,WAAW,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,CAAA;AAClD,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA"}
@@ -1,16 +0,0 @@
1
- import { reaction } from 'mobx';
2
- import React, { useState } from 'react';
3
- export function useObserver(initialValue, selector) {
4
- const [, refresh] = useState({});
5
- React.useEffect(() => {
6
- if (initialValue == null)
7
- return;
8
- if (selector == null)
9
- return;
10
- return reaction(() => selector(initialValue), () => refresh({}));
11
- }, [selector, initialValue]);
12
- if (selector == null) {
13
- return initialValue;
14
- }
15
- return selector(initialValue);
16
- }
@@ -1,24 +0,0 @@
1
- import { reaction } from 'mobx'
2
- import React, { useState } from 'react'
3
-
4
- export function useObserver<T>(initialValue: T): T
5
- export function useObserver<T, U>(initialValue: T, selector: (v: T) => U): U
6
- export function useObserver<T, U>(initialValue: T, selector?: (v: T) => U): U | T {
7
- const [, refresh] = useState({})
8
-
9
- React.useEffect(() => {
10
- if (initialValue == null) return
11
- if (selector == null) return
12
- return reaction(
13
- () => selector(initialValue),
14
- () => refresh({})
15
- )
16
- }, [selector, initialValue])
17
-
18
-
19
- if (selector == null) {
20
- return initialValue as T
21
- }
22
-
23
- return selector(initialValue)
24
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"aside.d.ts","sourceRoot":"","sources":["aside.ts"],"names":[],"mappings":"AAOA,qBAAa,UAAU,CAAC,CAAC;IAEvB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAsB;IAGjD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoC;IAE1D,IACW,KAAK,IAAI,CAAC,GAAG,IAAI,CAG3B;IAGY,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;CAiBrH"}
@@ -1,106 +0,0 @@
1
- var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
2
- var useValue = arguments.length > 2;
3
- for (var i = 0; i < initializers.length; i++) {
4
- value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
5
- }
6
- return useValue ? value : void 0;
7
- };
8
- var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
9
- function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
10
- var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
11
- var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
12
- var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
13
- var _, done = false;
14
- for (var i = decorators.length - 1; i >= 0; i--) {
15
- var context = {};
16
- for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
17
- for (var p in contextIn.access) context.access[p] = contextIn.access[p];
18
- context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
19
- var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
20
- if (kind === "accessor") {
21
- if (result === void 0) continue;
22
- if (result === null || typeof result !== "object") throw new TypeError("Object expected");
23
- if (_ = accept(result.get)) descriptor.get = _;
24
- if (_ = accept(result.set)) descriptor.set = _;
25
- if (_ = accept(result.init)) initializers.unshift(_);
26
- }
27
- else if (_ = accept(result)) {
28
- if (kind === "field") initializers.unshift(_);
29
- else descriptor[key] = _;
30
- }
31
- }
32
- if (target) Object.defineProperty(target, contextIn.name, descriptor);
33
- done = true;
34
- };
35
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
36
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
37
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
38
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
39
- };
40
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
41
- if (kind === "m") throw new TypeError("Private method is not writable");
42
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
43
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
44
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
45
- };
46
- import { action, computed, observable, runInAction } from 'mobx';
47
- let AsideCache = (() => {
48
- var _a, _AsideCache_currentKey_accessor_storage, _AsideCache_cache_accessor_storage;
49
- var _b, _c;
50
- let _instanceExtraInitializers = [];
51
- let _currentKey_decorators;
52
- let _currentKey_initializers = [];
53
- let _currentKey_extraInitializers = [];
54
- let _cache_decorators;
55
- let _cache_initializers = [];
56
- let _cache_extraInitializers = [];
57
- let _get_value_decorators;
58
- let _handle_decorators;
59
- return _a = class AsideCache {
60
- get currentKey() { return __classPrivateFieldGet(this, _AsideCache_currentKey_accessor_storage, "f"); }
61
- set currentKey(value) { __classPrivateFieldSet(this, _AsideCache_currentKey_accessor_storage, value, "f"); }
62
- get cache() { return __classPrivateFieldGet(this, _AsideCache_cache_accessor_storage, "f"); }
63
- set cache(value) { __classPrivateFieldSet(this, _AsideCache_cache_accessor_storage, value, "f"); }
64
- get value() {
65
- var _b;
66
- if (this.currentKey == null)
67
- return null;
68
- return ((_b = this.cache[this.currentKey]) === null || _b === void 0 ? void 0 : _b.data) || null;
69
- }
70
- async handle(key, func) {
71
- const keyStringify = JSON.stringify(key); // 不需要跨设备、软件一致,所以直接用 JSON.stringify
72
- runInAction(() => this.currentKey = keyStringify);
73
- const funcReturn = await func();
74
- if (funcReturn == null)
75
- return funcReturn;
76
- runInAction(() => {
77
- this.cache = Object.assign(Object.assign({}, this.cache), { [keyStringify]: {
78
- data: funcReturn,
79
- createTime: new Date().toISOString()
80
- } });
81
- });
82
- return funcReturn;
83
- }
84
- constructor() {
85
- _AsideCache_currentKey_accessor_storage.set(this, (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _currentKey_initializers, null)));
86
- _AsideCache_cache_accessor_storage.set(this, (__runInitializers(this, _currentKey_extraInitializers), __runInitializers(this, _cache_initializers, {})));
87
- __runInitializers(this, _cache_extraInitializers);
88
- }
89
- },
90
- _AsideCache_currentKey_accessor_storage = new WeakMap(),
91
- _AsideCache_cache_accessor_storage = new WeakMap(),
92
- (() => {
93
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
94
- _currentKey_decorators = [observable];
95
- _cache_decorators = [(_b = observable).ref.bind(_b)];
96
- _get_value_decorators = [computed];
97
- _handle_decorators = [(_c = action).bound.bind(_c)];
98
- __esDecorate(_a, null, _currentKey_decorators, { kind: "accessor", name: "currentKey", static: false, private: false, access: { has: obj => "currentKey" in obj, get: obj => obj.currentKey, set: (obj, value) => { obj.currentKey = value; } }, metadata: _metadata }, _currentKey_initializers, _currentKey_extraInitializers);
99
- __esDecorate(_a, null, _cache_decorators, { kind: "accessor", name: "cache", static: false, private: false, access: { has: obj => "cache" in obj, get: obj => obj.cache, set: (obj, value) => { obj.cache = value; } }, metadata: _metadata }, _cache_initializers, _cache_extraInitializers);
100
- __esDecorate(_a, null, _get_value_decorators, { kind: "getter", name: "value", static: false, private: false, access: { has: obj => "value" in obj, get: obj => obj.value }, metadata: _metadata }, null, _instanceExtraInitializers);
101
- __esDecorate(_a, null, _handle_decorators, { kind: "method", name: "handle", static: false, private: false, access: { has: obj => "handle" in obj, get: obj => obj.handle }, metadata: _metadata }, null, _instanceExtraInitializers);
102
- if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
103
- })(),
104
- _a;
105
- })();
106
- export { AsideCache };
@@ -1,39 +0,0 @@
1
- import { action, computed, observable, runInAction } from 'mobx'
2
-
3
- interface CacheValue<T> {
4
- data: T
5
- createTime: string
6
- }
7
-
8
- export class AsideCache<T> {
9
- @observable
10
- private accessor currentKey: string | null = null
11
-
12
- @observable.ref
13
- private accessor cache: Record<string, CacheValue<T>> = {}
14
-
15
- @computed
16
- public get value(): T | null {
17
- if (this.currentKey == null) return null
18
- return this.cache[this.currentKey]?.data || null
19
- }
20
-
21
- @action.bound
22
- public async handle<F extends ((...args: unknown[]) => Promise<T | null>)>(key: unknown, func: F): Promise<T | null> {
23
- const keyStringify = JSON.stringify(key) // 不需要跨设备、软件一致,所以直接用 JSON.stringify
24
- runInAction(() => this.currentKey = keyStringify)
25
- const funcReturn = await func()
26
- if (funcReturn == null) return funcReturn
27
- runInAction(() => {
28
- this.cache = {
29
- ...this.cache,
30
- [keyStringify]: {
31
- data: funcReturn,
32
- createTime: new Date().toISOString()
33
- }
34
- }
35
- })
36
-
37
- return funcReturn
38
- }
39
- }
@@ -1,2 +0,0 @@
1
- export { AsideCache } from './aside';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA"}
@@ -1 +0,0 @@
1
- export { AsideCache } from './aside';
@@ -1 +0,0 @@
1
- export { AsideCache } from './aside'