Skip to content

Commit

Permalink
build: ✅ add MDX linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Feb 16, 2024
1 parent 249446e commit 9fc9ecf
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 70 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
extends: [
"plugin:astro/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:mdx/recommended",
"prettier",
],
overrides: [
Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
"postcss-preset-env": "^9.3.0",
"prettier": "^3.2.5",
"prettier-plugin-astro": "^0.13.0",
"remark-frontmatter": "^5.0.0",
"remark-lint-maximum-line-length": "^3.1.3",
"remark-lint-ordered-list-marker-value": "^3.1.2",
"remark-mdx": "^3.0.1",
"remark-preset-lint-consistent": "^5.1.2",
"remark-preset-lint-markdown-style-guide": "^5.1.3",
"remark-preset-lint-recommended": "^6.1.3",
"sharp": "^0.33.2",
Expand All @@ -80,10 +84,17 @@
"remarkConfig": {
"plugins": [
"remark-mdx",
"remark-frontmatter",
"remark-preset-lint-consistent",
"remark-preset-lint-recommended",
"remark-preset-lint-markdown-style-guide",
[
"remark-lint-maximum-line-length",
false
],
[
"remark-lint-ordered-list-marker-value",
"ordered"
]
]
}
Expand Down
88 changes: 88 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -456,79 +456,77 @@ Web 服务器可能收到大量请求,如果某个请求超时,那么可能

接受 `CancellationToken` 的方法可以有多种方法结束自身的执行:

- 使用 `token.ThrowIfCancellationRequested()` 抛出 `OperationCanceledException`
异常,强制退出方法;
- 在循环操作中监听 `token.IsCancellationRequested` 属性,如果结果为 `true`,则退
出循环;
-`token` 传给内层嵌套的异步操作。
- 使用 `token.ThrowIfCancellationRequested()` 抛出 `OperationCanceledException` 异常,强制退出方法
- 在循环操作中监听 `token.IsCancellationRequested` 属性,如果结果为 `true`,则退出循环
-`token` 传给内层嵌套的异步操作

示例:

1. 抛出异常:

```csharp
CancellationTokenSource tokenSource = new CancellationTokenSource();
Task t = Task.Delay(1000, tokenSource.Token);
tokenSource.CancelAfter(500);
try
{
await t;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
```

输出:

```plaintext frame="terminal"
System.Threading.Tasks.TaskCanceledException: A task was canceled.
```

2. 循环监听

```csharp
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
token.Register(
() =>
{
Console.WriteLine("Action canceled");
}
);

// 自旋循环等待,可以替代 Thread.Sleep 进行等待操作
SpinWait sw = new SpinWait();
Task task = Task.Run(
() =>
{
while (!token.IsCancellationRequested)
{
Console.Write("Running...\n");
sw.SpinOnce();
}
},
token
);
tokenSource.CancelAfter(10);
await task;
```

可能的输出:

```plaintext frame="terminal"
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Action canceled
```
1. 抛出异常:

```csharp
CancellationTokenSource tokenSource = new CancellationTokenSource();
Task t = Task.Delay(1000, tokenSource.Token);
tokenSource.CancelAfter(500);
try
{
await t;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
```

输出:

```plaintext frame="terminal"
System.Threading.Tasks.TaskCanceledException: A task was canceled.
```

2. 循环监听

```csharp
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
token.Register(
() =>
{
Console.WriteLine("Action canceled");
}
);

// 自旋循环等待,可以替代 Thread.Sleep 进行等待操作
SpinWait sw = new SpinWait();
Task task = Task.Run(
() =>
{
while (!token.IsCancellationRequested)
{
Console.Write("Running...\n");
sw.SpinOnce();
}
},
token
);
tokenSource.CancelAfter(10);
await task;
```

可能的输出:

```plaintext frame="terminal"
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Action canceled
```

## 总结

Expand Down

0 comments on commit 9fc9ecf

Please sign in to comment.