Skip to content

Commit

Permalink
refactor: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jan 22, 2025
1 parent 6580327 commit bebfc8f
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 322 deletions.
8 changes: 4 additions & 4 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"arrowParens": "avoid",
"endOfLine": "auto",
"semi": true,
"singleQuote": false,
"endOfLine": "auto",
"arrowParens": "avoid"
"tabWidth": 2,
"trailingComma": "es5"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"scripts": {
"prepublishOnly": "npm run build",
"lint": "eslint && prettier --check .",
"lint:fix": "eslint --fix && prettier --write .",
"docs": "typedoc --options typedoc.json",
"test": "vitest",
"test:coverage": "vitest --coverage --coverage.include=src/**",
Expand Down
15 changes: 0 additions & 15 deletions scripts/gzip.mjs

This file was deleted.

15 changes: 7 additions & 8 deletions src/abortable.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in type doc
import type { DisposableStore } from "./disposable-store";
import type {
DisposableDisposer,
DisposableType,
import { type DisposableStore } from "./disposable-store";
import {
type DisposableDisposer,
type DisposableType,
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in type doc
Disposer,
type Disposer,
} from "./interface";

import { dispose, isFn } from "./utils";

/**
Expand All @@ -15,14 +14,14 @@ import { dispose, isFn } from "./utils";
*/
interface AbortableDisposable {
(): any;
dispose: (this: void) => any;
abortable: (onDispose: () => void) => void;
dispose: (this: void) => any;
}

interface AbortableDisposableImpl extends AbortableDisposable {
abortable: (onDispose?: () => void) => void;
/** deps */
_o?: (() => any) | null | void;
abortable: (onDispose?: () => void) => void;
}

/**
Expand Down
163 changes: 81 additions & 82 deletions src/disposable-map.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type {
DisposableDisposer,
DisposableType,
import { isAbortable } from "./abortable";
import {
type DisposableDisposer,
type DisposableType,
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in type doc
IDisposable,
type IDisposable,
} from "./interface";

import { isAbortable } from "./abortable";
import { dispose } from "./utils";

/**
Expand All @@ -30,26 +29,29 @@ export interface DisposableMap<TKey = any> extends DisposableDisposer {
(): void;

/**
* Returns an iterable of keys in the map
* Flush and clear all of the {@link Disposer}s and {@link IDisposable}s in the Map.
*/
keys(): IterableIterator<TKey>;
dispose(): void;

/**
* Get the number of {@link DisposableType}s in the Map.
* Invoke the {@link DisposableType} and remove it from the Map at the specific key.
*
* @param key Map key of the {@link DisposableType}. Flush all if omitted.
*/
size(): number;
flush(key?: TKey): void;

/**
* Set a {@link DisposableType} to the Map at the specific key.
*
* Adding {@link DisposableType} to the same key will first invoke(`flush`) the previous {@link DisposableType} at that key.
* Check if a {@link DisposableType} at the specific key is in the Map.
*
* @param key Key for the {@link DisposableType}. Adding with same key will first invoke(`flush`) the previous {@link DisposableType}.
* @param disposable A {@link DisposableType} .
* @returns The same {@link DisposableType} .
* @param key Map key of the {@link DisposableType}.
* @returns `true` if exists, otherwise `false`.
*/
set<T extends DisposableType>(key: TKey, disposable: T): T;
has(key: TKey): boolean;

/**
* Returns an iterable of keys in the map
*/
keys(): IterableIterator<TKey>;
/**
* Invoke the executor function and add the returned {@link DisposableType} to the Map at the specific key.
*
Expand All @@ -60,6 +62,7 @@ export interface DisposableMap<TKey = any> extends DisposableDisposer {
* @returns The returned {@link DisposableType}.
*/
make<T extends DisposableType>(key: TKey, executor: () => T): T;

/**
* Invoke the executor function and add the returned {@link DisposableType} to the Map at the specific key.
*
Expand All @@ -73,7 +76,7 @@ export interface DisposableMap<TKey = any> extends DisposableDisposer {
*/
make<T extends DisposableType>(
key: TKey,
executor: () => T | null | undefined | void
executor: () => null | T | undefined | void
): T | void;

/**
Expand All @@ -85,76 +88,20 @@ export interface DisposableMap<TKey = any> extends DisposableDisposer {
remove(key: TKey): DisposableType | undefined;

/**
* Check if a {@link DisposableType} at the specific key is in the Map.
* Set a {@link DisposableType} to the Map at the specific key.
*
* @param key Map key of the {@link DisposableType}.
* @returns `true` if exists, otherwise `false`.
*/
has(key: TKey): boolean;

/**
* Invoke the {@link DisposableType} and remove it from the Map at the specific key.
* Adding {@link DisposableType} to the same key will first invoke(`flush`) the previous {@link DisposableType} at that key.
*
* @param key Map key of the {@link DisposableType}. Flush all if omitted.
* @param key Key for the {@link DisposableType}. Adding with same key will first invoke(`flush`) the previous {@link DisposableType}.
* @param disposable A {@link DisposableType} .
* @returns The same {@link DisposableType} .
*/
flush(key?: TKey): void;
set<T extends DisposableType>(key: TKey, disposable: T): T;

/**
* Flush and clear all of the {@link Disposer}s and {@link IDisposable}s in the Map.
* Get the number of {@link DisposableType}s in the Map.
*/
dispose(): void;
}

function keys<K>(this: DisposableMap<K>): IterableIterator<K> {
return (this._disposables_ || []).keys() as IterableIterator<K>;
}

function size<K>(this: DisposableMap<K>): number {
return this._disposables_?.size || 0;
}

function has<K>(this: DisposableMap<K>, key: K): boolean {
return this._disposables_?.has(key) || false;
}

function set<T extends DisposableType, K>(
this: DisposableMap<K>,
key: K,
disposable: T
): T {
this.flush(key);
if (isAbortable(disposable)) {
disposable.abortable(() => this.remove(key));
}
(this._disposables_ ??= new Map()).set(key, disposable);
return disposable;
}

function make<T extends DisposableType, K>(
this: DisposableMap<K>,
key: K,
executor: () => T | null
): T | void {
const disposable = executor();
if (disposable) {
return this.set(key, disposable);
}
}

function remove<K>(this: DisposableMap<K>, key: K): DisposableType | undefined {
const disposable = this._disposables_?.get(key);
if (disposable) {
this._disposables_?.delete(key);
return disposable;
}
}

function flush<K>(this: DisposableMap<K>, key?: K): void {
if (key == null) {
this.dispose();
} else {
dispose(this.remove(key));
}
size(): number;
}

/**
Expand Down Expand Up @@ -201,3 +148,55 @@ export function disposableMap<TKey = any>(): DisposableMap<TKey> {
disposer.flush = flush;
return disposer;
}

function flush<K>(this: DisposableMap<K>, key?: K): void {
if (key == null) {
this.dispose();
} else {
dispose(this.remove(key));
}
}

function has<K>(this: DisposableMap<K>, key: K): boolean {
return this._disposables_?.has(key) || false;
}

function keys<K>(this: DisposableMap<K>): IterableIterator<K> {
return (this._disposables_ || []).keys() as IterableIterator<K>;
}

function make<T extends DisposableType, K>(
this: DisposableMap<K>,
key: K,
executor: () => null | T
): T | void {
const disposable = executor();
if (disposable) {
return this.set(key, disposable);
}
}

function remove<K>(this: DisposableMap<K>, key: K): DisposableType | undefined {
const disposable = this._disposables_?.get(key);
if (disposable) {
this._disposables_?.delete(key);
return disposable;
}
}

function set<T extends DisposableType, K>(
this: DisposableMap<K>,
key: K,
disposable: T
): T {
this.flush(key);
if (isAbortable(disposable)) {
disposable.abortable(() => this.remove(key));
}
(this._disposables_ ??= new Map()).set(key, disposable);
return disposable;
}

function size<K>(this: DisposableMap<K>): number {
return this._disposables_?.size || 0;
}
Loading

0 comments on commit bebfc8f

Please sign in to comment.