@taicode/common-web 1.0.0 → 1.0.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/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "@taicode/common-web",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "author": "Alain",
5
5
  "license": "ISC",
6
6
  "description": "",
7
+ "scripts": {
8
+ "build": "tsc -p tsconfig.json",
9
+ "dev": "tsc -p tsconfig.json --watch"
10
+ },
7
11
  "peerDependencies": {
8
- "mobx": "^6.13.7",
9
- "react": "^18.2.0",
10
- "@types/react": "^19.1.8"
12
+ "mobx": ">=6",
13
+ "react": ">=18",
14
+ "@types/react": ">=18"
11
15
  },
12
16
  "exports": {
13
- "./utils/*": ["./source/utils/*"],
14
- "./hooks/*": ["./source/utils/*"]
17
+ "./utils/*": ["./output/utils/*"],
18
+ "./hooks/*": ["./output/hooks/*"]
15
19
  }
16
20
  }
@@ -0,0 +1,3 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ export declare class AsideCache<T> {
2
+ private accessor currentKey;
3
+ private accessor cache;
4
+ get value(): T | null;
5
+ handle<F extends ((...args: unknown[]) => Promise<T | null>)>(key: unknown, func: F): Promise<T | null>;
6
+ }
7
+ //# sourceMappingURL=aside.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,106 @@
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 };
@@ -0,0 +1,2 @@
1
+ export { AsideCache } from './aside';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA"}
@@ -0,0 +1 @@
1
+ export { AsideCache } from './aside';
@@ -1 +1 @@
1
- export { AsideCache } from './aside-cache'
1
+ export { AsideCache } from './aside'
package/tsconfig.json CHANGED
@@ -49,7 +49,7 @@
49
49
  // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
50
50
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
51
51
  // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
52
- // "outDir": "./", /* Specify an output folder for all emitted files. */
52
+ "outDir": "./output", /* Specify an output folder for all emitted files. */
53
53
  // "removeComments": true, /* Disable emitting comments. */
54
54
  // "noEmit": true, /* Disable emitting files from a compilation. */
55
55
  // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
@@ -99,5 +99,8 @@
99
99
  /* Completeness */
100
100
  // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
101
101
  "skipLibCheck": true /* Skip type checking all .d.ts files. */
102
- }
102
+ },
103
+ "include": [
104
+ "source"
105
+ ]
103
106
  }