@planet-matrix/mobius-model 0.5.0 → 0.9.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/CHANGELOG.md +61 -0
- package/README.md +123 -36
- package/dist/index.js +715 -4
- package/dist/index.js.map +981 -13
- package/oxlint.config.ts +6 -0
- package/package.json +36 -18
- package/src/abort/README.md +92 -0
- package/src/abort/abort-manager.ts +278 -0
- package/src/abort/abort-signal-listener-manager.ts +81 -0
- package/src/abort/index.ts +2 -0
- package/src/ai/README.md +1 -0
- package/src/ai/ai.ts +107 -0
- package/src/ai/chat-completion-ai/aihubmix-chat-completion.ts +78 -0
- package/src/ai/chat-completion-ai/chat-completion-ai.ts +270 -0
- package/src/ai/chat-completion-ai/chat-completion.ts +189 -0
- package/src/ai/chat-completion-ai/index.ts +7 -0
- package/src/ai/chat-completion-ai/lingyiwanwu-chat-completion.ts +78 -0
- package/src/ai/chat-completion-ai/ohmygpt-chat-completion.ts +78 -0
- package/src/ai/chat-completion-ai/openai-next-chat-completion.ts +78 -0
- package/src/ai/embedding-ai/embedding-ai.ts +63 -0
- package/src/ai/embedding-ai/embedding.ts +50 -0
- package/src/ai/embedding-ai/index.ts +4 -0
- package/src/ai/embedding-ai/openai-next-embedding.ts +23 -0
- package/src/ai/index.ts +4 -0
- package/src/aio/README.md +100 -0
- package/src/aio/content.ts +141 -0
- package/src/aio/index.ts +3 -0
- package/src/aio/json.ts +127 -0
- package/src/aio/prompt.ts +246 -0
- package/src/basic/README.md +72 -116
- package/src/basic/error.ts +19 -5
- package/src/basic/function.ts +83 -64
- package/src/basic/index.ts +1 -0
- package/src/basic/is.ts +152 -71
- package/src/basic/promise.ts +29 -8
- package/src/basic/schedule.ts +111 -0
- package/src/basic/stream.ts +135 -25
- package/src/basic/string.ts +2 -33
- package/src/color/README.md +105 -0
- package/src/color/index.ts +3 -0
- package/src/color/internal.ts +42 -0
- package/src/color/rgb/analyze.ts +236 -0
- package/src/color/rgb/construct.ts +130 -0
- package/src/color/rgb/convert.ts +227 -0
- package/src/color/rgb/derive.ts +303 -0
- package/src/color/rgb/index.ts +6 -0
- package/src/color/rgb/internal.ts +208 -0
- package/src/color/rgb/parse.ts +302 -0
- package/src/color/rgb/serialize.ts +144 -0
- package/src/color/types.ts +57 -0
- package/src/color/xyz/analyze.ts +80 -0
- package/src/color/xyz/construct.ts +19 -0
- package/src/color/xyz/convert.ts +71 -0
- package/src/color/xyz/index.ts +3 -0
- package/src/color/xyz/internal.ts +23 -0
- package/src/credential/README.md +107 -0
- package/src/credential/api-key.ts +158 -0
- package/src/credential/bearer.ts +73 -0
- package/src/credential/index.ts +4 -0
- package/src/credential/json-web-token.ts +96 -0
- package/src/credential/password.ts +170 -0
- package/src/cron/README.md +86 -0
- package/src/cron/cron.ts +87 -0
- package/src/cron/index.ts +1 -0
- package/src/css/README.md +93 -0
- package/src/css/class.ts +559 -0
- package/src/css/index.ts +1 -0
- package/src/drizzle/README.md +1 -0
- package/src/drizzle/drizzle.ts +1 -0
- package/src/drizzle/helper.ts +47 -0
- package/src/drizzle/index.ts +5 -0
- package/src/drizzle/infer.ts +52 -0
- package/src/drizzle/kysely.ts +8 -0
- package/src/drizzle/pagination.ts +200 -0
- package/src/email/README.md +1 -0
- package/src/email/index.ts +1 -0
- package/src/email/resend.ts +25 -0
- package/src/encoding/README.md +66 -79
- package/src/encoding/base64.ts +13 -4
- package/src/environment/README.md +97 -0
- package/src/environment/basic.ts +26 -0
- package/src/environment/device.ts +311 -0
- package/src/environment/feature.ts +285 -0
- package/src/environment/geo.ts +337 -0
- package/src/environment/index.ts +7 -0
- package/src/environment/runtime.ts +400 -0
- package/src/environment/snapshot.ts +60 -0
- package/src/environment/variable.ts +239 -0
- package/src/event/README.md +90 -0
- package/src/event/class-event-proxy.ts +229 -0
- package/src/event/common.ts +29 -0
- package/src/event/event-manager.ts +203 -0
- package/src/event/index.ts +4 -0
- package/src/event/instance-event-proxy.ts +187 -0
- package/src/event/internal.ts +24 -0
- package/src/exception/README.md +96 -0
- package/src/exception/browser.ts +219 -0
- package/src/exception/index.ts +4 -0
- package/src/exception/nodejs.ts +169 -0
- package/src/exception/normalize.ts +106 -0
- package/src/exception/types.ts +99 -0
- package/src/form/README.md +25 -0
- package/src/form/index.ts +1 -0
- package/src/form/inputor-controller/base.ts +874 -0
- package/src/form/inputor-controller/boolean.ts +39 -0
- package/src/form/inputor-controller/file.ts +39 -0
- package/src/form/inputor-controller/form.ts +181 -0
- package/src/form/inputor-controller/helper.ts +117 -0
- package/src/form/inputor-controller/index.ts +17 -0
- package/src/form/inputor-controller/multi-select.ts +99 -0
- package/src/form/inputor-controller/number.ts +116 -0
- package/src/form/inputor-controller/select.ts +109 -0
- package/src/form/inputor-controller/text.ts +82 -0
- package/src/http/READMD.md +1 -0
- package/src/http/api/api-core.ts +84 -0
- package/src/http/api/api-handler.ts +79 -0
- package/src/http/api/api-host.ts +47 -0
- package/src/http/api/api-result.ts +56 -0
- package/src/http/api/api-schema.ts +154 -0
- package/src/http/api/api-server.ts +130 -0
- package/src/http/api/api-test.ts +142 -0
- package/src/http/api/api-type.ts +37 -0
- package/src/http/api/api.ts +81 -0
- package/src/http/api/index.ts +11 -0
- package/src/http/api-adapter/api-core-node-http.ts +260 -0
- package/src/http/api-adapter/api-host-node-http.ts +156 -0
- package/src/http/api-adapter/api-result-arktype.ts +297 -0
- package/src/http/api-adapter/api-result-zod.ts +286 -0
- package/src/http/api-adapter/index.ts +5 -0
- package/src/http/bin/gen-api-list/gen-api-list.ts +126 -0
- package/src/http/bin/gen-api-list/index.ts +1 -0
- package/src/http/bin/gen-api-test/gen-api-test.ts +136 -0
- package/src/http/bin/gen-api-test/index.ts +1 -0
- package/src/http/bin/gen-api-type/calc-code.ts +25 -0
- package/src/http/bin/gen-api-type/gen-api-type.ts +127 -0
- package/src/http/bin/gen-api-type/index.ts +2 -0
- package/src/http/bin/index.ts +2 -0
- package/src/http/index.ts +3 -0
- package/src/huawei/README.md +1 -0
- package/src/huawei/index.ts +2 -0
- package/src/huawei/moderation/index.ts +1 -0
- package/src/huawei/moderation/moderation.ts +355 -0
- package/src/huawei/obs/esdk-obs-nodejs.d.ts +87 -0
- package/src/huawei/obs/index.ts +1 -0
- package/src/huawei/obs/obs.ts +42 -0
- package/src/identifier/README.md +92 -0
- package/src/identifier/id.ts +119 -0
- package/src/identifier/index.ts +2 -0
- package/src/identifier/uuid.ts +187 -0
- package/src/index.ts +33 -1
- package/src/json/README.md +92 -0
- package/src/json/index.ts +1 -0
- package/src/json/repair.ts +18 -0
- package/src/log/README.md +79 -0
- package/src/log/index.ts +5 -0
- package/src/log/log-emitter.ts +72 -0
- package/src/log/log-record.ts +10 -0
- package/src/log/log-scheduler.ts +74 -0
- package/src/log/log-type.ts +8 -0
- package/src/log/logger.ts +554 -0
- package/src/openai/README.md +1 -0
- package/src/openai/index.ts +1 -0
- package/src/openai/openai.ts +510 -0
- package/src/orchestration/README.md +91 -0
- package/src/orchestration/coordination/barrier.ts +214 -0
- package/src/orchestration/coordination/count-down-latch.ts +215 -0
- package/src/orchestration/coordination/errors.ts +98 -0
- package/src/orchestration/coordination/index.ts +16 -0
- package/src/orchestration/coordination/internal/wait-constraints.ts +95 -0
- package/src/orchestration/coordination/internal/wait-queue.ts +109 -0
- package/src/orchestration/coordination/keyed-lock.ts +168 -0
- package/src/orchestration/coordination/mutex.ts +257 -0
- package/src/orchestration/coordination/permit.ts +127 -0
- package/src/orchestration/coordination/read-write-lock.ts +444 -0
- package/src/orchestration/coordination/semaphore.ts +280 -0
- package/src/orchestration/dispatching/dispatcher.ts +83 -0
- package/src/orchestration/dispatching/index.ts +2 -0
- package/src/orchestration/dispatching/selector/base-selector.ts +39 -0
- package/src/orchestration/dispatching/selector/down-count-selector.ts +119 -0
- package/src/orchestration/dispatching/selector/index.ts +2 -0
- package/src/orchestration/index.ts +3 -0
- package/src/orchestration/scheduling/index.ts +2 -0
- package/src/orchestration/scheduling/scheduler.ts +103 -0
- package/src/orchestration/scheduling/task.ts +32 -0
- package/src/random/README.md +56 -86
- package/src/random/base.ts +66 -0
- package/src/random/index.ts +5 -1
- package/src/random/random-boolean.ts +40 -0
- package/src/random/random-integer.ts +60 -0
- package/src/random/random-number.ts +72 -0
- package/src/random/random-string.ts +66 -0
- package/src/reactor/README.md +4 -0
- package/src/reactor/reactor-core/primitive.ts +9 -9
- package/src/reactor/reactor-core/reactive-system.ts +5 -5
- package/src/request/README.md +108 -0
- package/src/request/fetch/base.ts +108 -0
- package/src/request/fetch/browser.ts +285 -0
- package/src/request/fetch/general.ts +20 -0
- package/src/request/fetch/index.ts +4 -0
- package/src/request/fetch/nodejs.ts +285 -0
- package/src/request/index.ts +2 -0
- package/src/request/request/base.ts +250 -0
- package/src/request/request/general.ts +64 -0
- package/src/request/request/index.ts +3 -0
- package/src/request/request/resource.ts +68 -0
- package/src/result/README.md +4 -0
- package/src/result/controller.ts +54 -0
- package/src/result/either.ts +193 -0
- package/src/result/index.ts +2 -0
- package/src/route/README.md +105 -0
- package/src/route/adapter/browser.ts +122 -0
- package/src/route/adapter/driver.ts +56 -0
- package/src/route/adapter/index.ts +2 -0
- package/src/route/index.ts +3 -0
- package/src/route/router/index.ts +2 -0
- package/src/route/router/route.ts +630 -0
- package/src/route/router/router.ts +1642 -0
- package/src/route/uri/hash.ts +308 -0
- package/src/route/uri/index.ts +7 -0
- package/src/route/uri/pathname.ts +376 -0
- package/src/route/uri/search.ts +413 -0
- package/src/singleton/README.md +79 -0
- package/src/singleton/factory.ts +55 -0
- package/src/singleton/index.ts +2 -0
- package/src/singleton/manager.ts +204 -0
- package/src/socket/README.md +105 -0
- package/src/socket/client/index.ts +2 -0
- package/src/socket/client/socket-unit.ts +660 -0
- package/src/socket/client/socket.ts +203 -0
- package/src/socket/common/index.ts +2 -0
- package/src/socket/common/socket-unit-common.ts +23 -0
- package/src/socket/common/socket-unit-heartbeat.ts +427 -0
- package/src/socket/index.ts +3 -0
- package/src/socket/server/index.ts +3 -0
- package/src/socket/server/server.ts +183 -0
- package/src/socket/server/socket-unit.ts +449 -0
- package/src/socket/server/socket.ts +264 -0
- package/src/storage/README.md +107 -0
- package/src/storage/index.ts +1 -0
- package/src/storage/table.ts +449 -0
- package/src/timer/README.md +86 -0
- package/src/timer/expiration/expiration-manager.ts +594 -0
- package/src/timer/expiration/index.ts +3 -0
- package/src/timer/expiration/min-heap.ts +208 -0
- package/src/timer/expiration/remaining-manager.ts +241 -0
- package/src/timer/index.ts +1 -0
- package/src/tube/README.md +99 -0
- package/src/tube/helper.ts +138 -0
- package/src/tube/index.ts +2 -0
- package/src/tube/tube.ts +880 -0
- package/src/type/README.md +54 -307
- package/src/type/class.ts +2 -2
- package/src/type/index.ts +14 -14
- package/src/type/is.ts +265 -2
- package/src/type/object.ts +37 -0
- package/src/type/string.ts +7 -2
- package/src/type/tuple.ts +6 -6
- package/src/type/union.ts +16 -0
- package/src/web/README.md +77 -0
- package/src/web/capture.ts +35 -0
- package/src/web/clipboard.ts +97 -0
- package/src/web/dom.ts +117 -0
- package/src/web/download.ts +16 -0
- package/src/web/event.ts +46 -0
- package/src/web/index.ts +10 -0
- package/src/web/local-storage.ts +113 -0
- package/src/web/location.ts +28 -0
- package/src/web/permission.ts +172 -0
- package/src/web/script-loader.ts +432 -0
- package/src/weixin/README.md +1 -0
- package/src/weixin/index.ts +2 -0
- package/src/weixin/official-account/authorization.ts +159 -0
- package/src/weixin/official-account/index.ts +2 -0
- package/src/weixin/official-account/js-api.ts +134 -0
- package/src/weixin/open/index.ts +1 -0
- package/src/weixin/open/oauth2.ts +133 -0
- package/tests/unit/abort/abort-manager.spec.ts +225 -0
- package/tests/unit/abort/abort-signal-listener-manager.spec.ts +62 -0
- package/tests/unit/ai/ai.spec.ts +85 -0
- package/tests/unit/aio/content.spec.ts +105 -0
- package/tests/unit/aio/json.spec.ts +147 -0
- package/tests/unit/aio/prompt.spec.ts +111 -0
- package/tests/unit/basic/array.spec.ts +1 -1
- package/tests/unit/basic/error.spec.ts +16 -4
- package/tests/unit/basic/schedule.spec.ts +74 -0
- package/tests/unit/basic/stream.spec.ts +91 -38
- package/tests/unit/basic/string.spec.ts +0 -9
- package/tests/unit/color/rgb/analyze.spec.ts +110 -0
- package/tests/unit/color/rgb/construct.spec.ts +56 -0
- package/tests/unit/color/rgb/convert.spec.ts +60 -0
- package/tests/unit/color/rgb/derive.spec.ts +103 -0
- package/tests/unit/color/rgb/parse.spec.ts +66 -0
- package/tests/unit/color/rgb/serialize.spec.ts +46 -0
- package/tests/unit/color/xyz/analyze.spec.ts +33 -0
- package/tests/unit/color/xyz/construct.spec.ts +10 -0
- package/tests/unit/color/xyz/convert.spec.ts +18 -0
- package/tests/unit/credential/api-key.spec.ts +37 -0
- package/tests/unit/credential/bearer.spec.ts +23 -0
- package/tests/unit/credential/json-web-token.spec.ts +23 -0
- package/tests/unit/credential/password.spec.ts +41 -0
- package/tests/unit/cron/cron.spec.ts +84 -0
- package/tests/unit/css/class.spec.ts +157 -0
- package/tests/unit/environment/basic.spec.ts +20 -0
- package/tests/unit/environment/device.spec.ts +146 -0
- package/tests/unit/environment/feature.spec.ts +388 -0
- package/tests/unit/environment/geo.spec.ts +111 -0
- package/tests/unit/environment/runtime.spec.ts +364 -0
- package/tests/unit/environment/snapshot.spec.ts +4 -0
- package/tests/unit/environment/variable.spec.ts +190 -0
- package/tests/unit/event/class-event-proxy.spec.ts +225 -0
- package/tests/unit/event/event-manager.spec.ts +246 -0
- package/tests/unit/event/instance-event-proxy.spec.ts +187 -0
- package/tests/unit/exception/browser.spec.ts +213 -0
- package/tests/unit/exception/nodejs.spec.ts +144 -0
- package/tests/unit/exception/normalize.spec.ts +57 -0
- package/tests/unit/form/inputor-controller/base.spec.ts +458 -0
- package/tests/unit/form/inputor-controller/boolean.spec.ts +30 -0
- package/tests/unit/form/inputor-controller/file.spec.ts +27 -0
- package/tests/unit/form/inputor-controller/form.spec.ts +120 -0
- package/tests/unit/form/inputor-controller/helper.spec.ts +67 -0
- package/tests/unit/form/inputor-controller/multi-select.spec.ts +34 -0
- package/tests/unit/form/inputor-controller/number.spec.ts +36 -0
- package/tests/unit/form/inputor-controller/select.spec.ts +49 -0
- package/tests/unit/form/inputor-controller/text.spec.ts +34 -0
- package/tests/unit/http/api/api-core-host.spec.ts +207 -0
- package/tests/unit/http/api/api-schema.spec.ts +120 -0
- package/tests/unit/http/api/api-server.spec.ts +363 -0
- package/tests/unit/http/api/api-test.spec.ts +117 -0
- package/tests/unit/http/api/api.spec.ts +121 -0
- package/tests/unit/http/api-adapter/node-http.spec.ts +191 -0
- package/tests/unit/identifier/id.spec.ts +71 -0
- package/tests/unit/identifier/uuid.spec.ts +85 -0
- package/tests/unit/json/repair.spec.ts +11 -0
- package/tests/unit/log/log-emitter.spec.ts +33 -0
- package/tests/unit/log/log-scheduler.spec.ts +40 -0
- package/tests/unit/log/log-type.spec.ts +7 -0
- package/tests/unit/log/logger.spec.ts +237 -0
- package/tests/unit/openai/openai.spec.ts +64 -0
- package/tests/unit/orchestration/coordination/barrier.spec.ts +96 -0
- package/tests/unit/orchestration/coordination/count-down-latch.spec.ts +63 -0
- package/tests/unit/orchestration/coordination/errors.spec.ts +29 -0
- package/tests/unit/orchestration/coordination/keyed-lock.spec.ts +109 -0
- package/tests/unit/orchestration/coordination/mutex.spec.ts +132 -0
- package/tests/unit/orchestration/coordination/permit.spec.ts +43 -0
- package/tests/unit/orchestration/coordination/read-write-lock.spec.ts +154 -0
- package/tests/unit/orchestration/coordination/semaphore.spec.ts +135 -0
- package/tests/unit/orchestration/dispatching/dispatcher.spec.ts +41 -0
- package/tests/unit/orchestration/dispatching/selector/down-count-selector.spec.ts +81 -0
- package/tests/unit/orchestration/scheduling/scheduler.spec.ts +103 -0
- package/tests/unit/random/base.spec.ts +58 -0
- package/tests/unit/random/random-boolean.spec.ts +25 -0
- package/tests/unit/random/random-integer.spec.ts +32 -0
- package/tests/unit/random/random-number.spec.ts +33 -0
- package/tests/unit/random/random-string.spec.ts +22 -0
- package/tests/unit/reactor/alien-signals-effect.spec.ts +11 -10
- package/tests/unit/reactor/preact-signal.spec.ts +1 -2
- package/tests/unit/request/fetch/browser.spec.ts +222 -0
- package/tests/unit/request/fetch/general.spec.ts +43 -0
- package/tests/unit/request/fetch/nodejs.spec.ts +225 -0
- package/tests/unit/request/request/base.spec.ts +385 -0
- package/tests/unit/request/request/general.spec.ts +161 -0
- package/tests/unit/route/router/route.spec.ts +431 -0
- package/tests/unit/route/router/router.spec.ts +407 -0
- package/tests/unit/route/uri/hash.spec.ts +72 -0
- package/tests/unit/route/uri/pathname.spec.ts +147 -0
- package/tests/unit/route/uri/search.spec.ts +107 -0
- package/tests/unit/singleton/singleton.spec.ts +49 -0
- package/tests/unit/socket/client.spec.ts +208 -0
- package/tests/unit/socket/server.spec.ts +135 -0
- package/tests/unit/socket/socket-unit-heartbeat.spec.ts +214 -0
- package/tests/unit/storage/table.spec.ts +620 -0
- package/tests/unit/timer/expiration/expiration-manager.spec.ts +464 -0
- package/tests/unit/timer/expiration/min-heap.spec.ts +71 -0
- package/tests/unit/timer/expiration/remaining-manager.spec.ts +234 -0
- package/tests/unit/tube/helper.spec.ts +139 -0
- package/tests/unit/tube/tube.spec.ts +501 -0
- package/.oxlintrc.json +0 -5
- package/src/random/uuid.ts +0 -103
- package/tests/unit/random/uuid.spec.ts +0 -37
package/src/basic/function.ts
CHANGED
|
@@ -26,23 +26,28 @@ export const functionIife = <T extends AnyFunction>(
|
|
|
26
26
|
return fn(...args)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* 表示只会保留首次执行结果的一次性函数。
|
|
31
|
+
*/
|
|
29
32
|
export type FunctionOnce<T extends AnyFunction> =
|
|
30
33
|
(...args: Parameters<T>) => ReturnType<T>
|
|
31
34
|
/**
|
|
32
|
-
*
|
|
35
|
+
* 生成一个只在首次调用时真正执行的函数。
|
|
33
36
|
*
|
|
34
37
|
* @example
|
|
35
38
|
* ```
|
|
36
|
-
* const
|
|
37
|
-
* //
|
|
38
|
-
* const
|
|
39
|
-
* //
|
|
40
|
-
* const
|
|
41
|
-
*
|
|
39
|
+
* const exampleFn1 = functionOnce((x: number) => x * 2)
|
|
40
|
+
* // Expect: 10
|
|
41
|
+
* const example1 = exampleFn1(5)
|
|
42
|
+
* // Expect: 10
|
|
43
|
+
* const example2 = exampleFn1(10)
|
|
44
|
+
*
|
|
42
45
|
* let callCount = 0
|
|
43
|
-
* const
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
+
* const exampleFn2 = functionOnce((x: number) => x * 2, (times) => { callCount = times })
|
|
47
|
+
* exampleFn2(5)
|
|
48
|
+
* exampleFn2(10)
|
|
49
|
+
* // Expect: 2
|
|
50
|
+
* const example3 = callCount
|
|
46
51
|
* ```
|
|
47
52
|
*/
|
|
48
53
|
export const functionOnce = <T extends AnyFunction>(
|
|
@@ -67,27 +72,30 @@ export const functionOnce = <T extends AnyFunction>(
|
|
|
67
72
|
}
|
|
68
73
|
}
|
|
69
74
|
|
|
75
|
+
/**
|
|
76
|
+
* 表示简单防抖后的函数类型。
|
|
77
|
+
*/
|
|
70
78
|
export type FunctionDebouncedSimple<T extends AnyFunction> =
|
|
71
79
|
(...args: Parameters<T>) => void
|
|
80
|
+
|
|
72
81
|
/**
|
|
73
|
-
*
|
|
74
|
-
* the original function until after a specified wait time
|
|
75
|
-
* has elapsed since the last time the debounced function was called.
|
|
82
|
+
* 生成一个简单防抖函数,在最后一次调用后的指定时间再触发原函数。
|
|
76
83
|
*
|
|
77
84
|
* @example
|
|
78
85
|
* ```
|
|
79
|
-
* const
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
86
|
+
* const records: number[] = []
|
|
87
|
+
* const exampleFn1 = functionDebounceSimple((x: number) => { records.push(x) }, 10)
|
|
88
|
+
* exampleFn1(5)
|
|
89
|
+
* exampleFn1(10)
|
|
90
|
+
* // Expect: records.length === 0
|
|
91
|
+
* const example1 = records.length === 0
|
|
84
92
|
* ```
|
|
85
93
|
*/
|
|
86
94
|
export const functionDebounceSimple = <T extends AnyFunction>(
|
|
87
95
|
fn: T,
|
|
88
96
|
ms: number
|
|
89
97
|
): FunctionDebouncedSimple<T> => {
|
|
90
|
-
let timer:
|
|
98
|
+
let timer: ReturnType<typeof setTimeout>
|
|
91
99
|
return (...args) => {
|
|
92
100
|
clearTimeout(timer)
|
|
93
101
|
timer = setTimeout(() => {
|
|
@@ -96,29 +104,30 @@ export const functionDebounceSimple = <T extends AnyFunction>(
|
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
106
|
|
|
107
|
+
/**
|
|
108
|
+
* 表示返回 `Promise` 的防抖函数类型。
|
|
109
|
+
*/
|
|
99
110
|
export type FunctionDebounced<T extends AnyFunction>
|
|
100
111
|
= (...args: Parameters<T>) => Promise<ReturnType<T>>
|
|
101
112
|
/**
|
|
102
|
-
*
|
|
103
|
-
* to the result of the original function. If the debounced function is called
|
|
104
|
-
* multiple times within the debounce period, it will only execute the original
|
|
105
|
-
* function once, and all calls will receive the same result.
|
|
113
|
+
* 生成一个返回 `Promise` 的防抖函数,并在防抖窗口内合并并发调用。
|
|
106
114
|
*
|
|
107
115
|
* @example
|
|
108
116
|
* ```
|
|
109
|
-
* const
|
|
110
|
-
*
|
|
111
|
-
* const
|
|
112
|
-
* //
|
|
113
|
-
* const
|
|
114
|
-
* //
|
|
117
|
+
* const exampleFn1 = debounce(async (x: number) => x * 2, 10)
|
|
118
|
+
* const example1 = exampleFn1(5)
|
|
119
|
+
* const example2 = exampleFn1(10)
|
|
120
|
+
* // Expect: example1 instanceof Promise
|
|
121
|
+
* const example3 = example1 instanceof Promise
|
|
122
|
+
* // Expect: example2 instanceof Promise
|
|
123
|
+
* const example4 = example2 instanceof Promise
|
|
115
124
|
* ```
|
|
116
125
|
*/
|
|
117
126
|
export const debounce = <T extends AnyFunction>(
|
|
118
127
|
fn: T,
|
|
119
128
|
ms: number
|
|
120
129
|
): FunctionDebounced<T> => {
|
|
121
|
-
let timer:
|
|
130
|
+
let timer: ReturnType<typeof setTimeout>
|
|
122
131
|
let waiting: AnyFunction[] = []
|
|
123
132
|
return async (...args) => {
|
|
124
133
|
clearTimeout(timer)
|
|
@@ -139,25 +148,23 @@ export const debounce = <T extends AnyFunction>(
|
|
|
139
148
|
}
|
|
140
149
|
|
|
141
150
|
const THROTTLE_TYPE_ERROR = new TypeError("Throttle: fn must be a SyncFunction or AsyncFunction")
|
|
151
|
+
/**
|
|
152
|
+
* 表示基于时间窗口的简单节流函数类型。
|
|
153
|
+
*/
|
|
142
154
|
export type FunctionThrottleTimeSimple<T extends AnyFunction> =
|
|
143
155
|
(...args: Parameters<T>) => void
|
|
144
156
|
/**
|
|
145
|
-
*
|
|
146
|
-
* once per every specified milliseconds. The throttled function will execute the original
|
|
147
|
-
* function immediately on the first call, and then ignore subsequent calls until the
|
|
148
|
-
* specified time has elapsed.
|
|
157
|
+
* 生成一个简单时间节流函数,在指定时间窗口内最多执行一次原函数。
|
|
149
158
|
*
|
|
150
|
-
*
|
|
151
|
-
* during which the original function can be invoked is greater than the sum of the
|
|
152
|
-
* specified milliseconds and the execution time of the original function.
|
|
159
|
+
* 当 `strict` 为 `true` 时,节流窗口会同时考虑等待时长与目标函数执行时长。
|
|
153
160
|
*
|
|
154
161
|
* @example
|
|
155
162
|
* ```
|
|
156
163
|
* let called = 0
|
|
157
|
-
* const
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* // Expect:
|
|
164
|
+
* const exampleFn1 = functionThrottleTimeSimple(() => { called += 1 }, 100)
|
|
165
|
+
* exampleFn1()
|
|
166
|
+
* exampleFn1()
|
|
167
|
+
* // Expect: 1
|
|
161
168
|
* const example1 = called
|
|
162
169
|
* ```
|
|
163
170
|
*/
|
|
@@ -212,19 +219,21 @@ export const functionThrottleTimeSimple = <T extends AnyFunction>(
|
|
|
212
219
|
}
|
|
213
220
|
}
|
|
214
221
|
|
|
222
|
+
/**
|
|
223
|
+
* 表示执行期间丢弃重复调用的简单节流函数类型。
|
|
224
|
+
*/
|
|
215
225
|
export type FunctionThrottleSimple<T extends AnyFunction> =
|
|
216
226
|
(...args: Parameters<T>) => void
|
|
217
227
|
/**
|
|
218
|
-
*
|
|
219
|
-
* when it is not currently executing.
|
|
228
|
+
* 生成一个在执行期间忽略重复调用的节流函数。
|
|
220
229
|
*
|
|
221
230
|
* @example
|
|
222
231
|
* ```
|
|
223
232
|
* let called = 0
|
|
224
|
-
* const
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* // Expect:
|
|
233
|
+
* const exampleFn1 = functionThrottleSimple(() => { called += 1 })
|
|
234
|
+
* exampleFn1()
|
|
235
|
+
* exampleFn1()
|
|
236
|
+
* // Expect: 1
|
|
228
237
|
* const example1 = called
|
|
229
238
|
* ```
|
|
230
239
|
*/
|
|
@@ -256,23 +265,25 @@ export const functionThrottleSimple = <T extends AnyFunction>(
|
|
|
256
265
|
}
|
|
257
266
|
}
|
|
258
267
|
|
|
268
|
+
/**
|
|
269
|
+
* 表示返回 `Promise` 的时间节流函数类型。
|
|
270
|
+
*/
|
|
259
271
|
export type FunctionThrottleTime<T extends AnyFunction> =
|
|
260
272
|
(...args: Parameters<T>) => Promise<ReturnType<T>>
|
|
261
273
|
/**
|
|
262
|
-
*
|
|
263
|
-
* of the original function. The throttled function will ensure that the original
|
|
264
|
-
* function is invoked at most once per every specified milliseconds.
|
|
274
|
+
* 生成一个返回 `Promise` 的时间节流函数。
|
|
265
275
|
*
|
|
266
|
-
*
|
|
267
|
-
* can be invoked will be greater than the sum of the specified milliseconds and
|
|
268
|
-
* the execution time of the original function.
|
|
276
|
+
* 当 `strict` 为 `true` 时,节流窗口会同时考虑等待时长与目标函数执行时长。
|
|
269
277
|
*
|
|
270
278
|
* @example
|
|
271
279
|
* ```
|
|
272
|
-
* const
|
|
273
|
-
* const
|
|
274
|
-
* const
|
|
275
|
-
* // Expect:
|
|
280
|
+
* const exampleFn1 = throttleTime(async (value: number) => value * 2, 100)
|
|
281
|
+
* const example1 = exampleFn1(2)
|
|
282
|
+
* const example2 = exampleFn1(3)
|
|
283
|
+
* // Expect: example1 instanceof Promise
|
|
284
|
+
* const example3 = example1 instanceof Promise
|
|
285
|
+
* // Expect: example2 instanceof Promise
|
|
286
|
+
* const example4 = example2 instanceof Promise
|
|
276
287
|
* ```
|
|
277
288
|
*/
|
|
278
289
|
export const throttleTime = <T extends AnyFunction>(
|
|
@@ -323,18 +334,23 @@ export const throttleTime = <T extends AnyFunction>(
|
|
|
323
334
|
}
|
|
324
335
|
}
|
|
325
336
|
|
|
337
|
+
/**
|
|
338
|
+
* 表示合并并发调用的节流函数类型。
|
|
339
|
+
*/
|
|
326
340
|
export type FunctionThrottle<T extends AnyFunction> =
|
|
327
341
|
(...args: Parameters<T>) => Promise<ReturnType<T>>
|
|
328
342
|
/**
|
|
329
|
-
*
|
|
330
|
-
* of the original function and coalesces concurrent calls.
|
|
343
|
+
* 生成一个返回 `Promise` 的节流函数,并合并并发调用。
|
|
331
344
|
*
|
|
332
345
|
* @example
|
|
333
346
|
* ```
|
|
334
|
-
* const
|
|
335
|
-
* const
|
|
336
|
-
* const
|
|
337
|
-
* // Expect:
|
|
347
|
+
* const exampleFn1 = functionThrottle(async () => "ok")
|
|
348
|
+
* const example1 = exampleFn1()
|
|
349
|
+
* const example2 = exampleFn1()
|
|
350
|
+
* // Expect: example1 instanceof Promise
|
|
351
|
+
* const example3 = example1 instanceof Promise
|
|
352
|
+
* // Expect: example2 instanceof Promise
|
|
353
|
+
* const example4 = example2 instanceof Promise
|
|
338
354
|
* ```
|
|
339
355
|
*/
|
|
340
356
|
export const functionThrottle = <T extends AnyFunction>(
|
|
@@ -409,6 +425,9 @@ export const functionPipe = <Fns extends AnyFunction[]>(
|
|
|
409
425
|
return functionCompose(...fns.toReversed()) as FunctionPipeAll<Fns>
|
|
410
426
|
}
|
|
411
427
|
|
|
428
|
+
/**
|
|
429
|
+
* 表示带参数缓存能力的记忆化函数类型。
|
|
430
|
+
*/
|
|
412
431
|
export type FunctionMemorized<T extends AnyFunction> =
|
|
413
432
|
(...args: Parameters<T>) => ReturnType<T>
|
|
414
433
|
// oxlint-disable-next-line no-explicit-any
|
|
@@ -434,7 +453,7 @@ const defaultMemorizeHasher = (...args: any[]): string => JSON.stringify(args)
|
|
|
434
453
|
*/
|
|
435
454
|
export const functionMemorize = <T extends AnyFunction>(
|
|
436
455
|
fn: T,
|
|
437
|
-
hashFunction?: (...args: Parameters<T>) => unknown
|
|
456
|
+
hashFunction?: (...args: Parameters<T>) => unknown | undefined
|
|
438
457
|
): FunctionMemorized<T> => {
|
|
439
458
|
const cache = new Map()
|
|
440
459
|
const hasher = hashFunction ?? defaultMemorizeHasher
|