Skip to content

Commit

Permalink
📖 完善了文档
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Oct 10, 2024
1 parent 4217fed commit c63d472
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
6 changes: 5 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gaubee/util",
"version": "0.9.1",
"version": "0.9.2",
"tasks": {
"build": "deno run -A ./dnt.ts",
"npm": "deno task build && deno task pub-npm",
Expand Down Expand Up @@ -46,6 +46,10 @@
"LICENSE",
"README.md",
"src/**/*.ts"
],
"exclude": [
"src/**/*.bench.ts",
"src/**/*.test.ts"
]
},
"compilerOptions": {
Expand Down
3 changes: 3 additions & 0 deletions src/ag.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* AsyncGeneratorFunction
*/
export const AGF = (() => {
try {
Function(
Expand Down
20 changes: 20 additions & 0 deletions src/lrc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { map_get_or_put, map_get_or_put_async } from "./map.ts";

/**
* @module
* 一个极简的 Least Recently Used 缓存
* @example
* ```ts
* const lrc = new Lrc()
* function getTime() {
* lrc.getOrPut('time', ()=>{
* return Date.now()
* })
* }
* const time1 = getTime()
* // hit cache
* const time2 = getTime()
* time1 === time2
*
* // remove all cache
* lrc.clear(0)
*
* const time3 = getTime()
* time1 !== time3
* ```
*/
export class Lrc<K, V> {
constructor(
Expand Down
7 changes: 4 additions & 3 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// deno-lint-ignore-file no-explicit-any
/**
* 遍历 map 对象,将结果聚合 array 对象返回
*/
export const map_to_array = <K, V, R>(
map: Map<K, V>,
mapper: (value: V, key: K) => R,
Expand Down Expand Up @@ -58,8 +60,7 @@ export const map_get_or_put_async = async <
return map.get(key)!;
}
}
const locks: Map<any, Promise<void>> =
((map as any)[locks_keys] ??= new Map());
const locks: Map<any, Promise<void>> = ((map as any)[locks_keys] ??= new Map());
const lock = Promise.withResolvers<void>();
locks.set(key, lock.promise);
try {
Expand Down
17 changes: 17 additions & 0 deletions src/promise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { event_target_on } from "./event_target.ts";
import { func_wrap } from "./func.ts";

/**
* setTimeout/clearTimeout 的 promise 版本
* @example
* ```ts
* const d = delay(1000)
*
* await d; // is promise
*
* d.cancel(); // clearTimeout
*
* d.cancel('some reason'); // clearTimeout and reject promise
*
* const ac = new AbortController()
* const d = delay(1000, { signal: ac.signal }) // with AbortSignal
* ac.abort('some reason'); // clearTimeout and reject promise
* ```
*/
export const delay = (
ms: number,
options?: { signal?: AbortSignal | null },
Expand Down
3 changes: 2 additions & 1 deletion src/shared_flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export type SharedFlowFun<T> = (data: T) => unknown;
export type SharedFlowOff = () => boolean;
export type SharedFlowListenOptions = { key?: unknown; onDispose?: Func };
/**
* @module
* 个极简的事件监听, 支持异步错误捕捉
* 对流有着极好的支持,支持背压,因此你甚至可以把它当作一个流发射器
*
*
* @example
* ```ts
* const flow = new SharedFlow<number>();
Expand Down

0 comments on commit c63d472

Please sign in to comment.