搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫mattrltrent/use_optimistic

使用樂觀

🪝 React 的 useOptimistic 鈎子的移植版本。

使用樂觀 專案封面
星標
6
分支
0
最近推送(UTC)
2024年2月27日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

Flutter hook: useOptimistic 🪝

An easy-to-use hook to optimistically update generic state and then resolve it later (async or sync) by accept, acceptAs, or reject-ing.


Documentation
  • Full example of using this hook to manage integer state.
  • GitHub repo found here.
  • Package found here.

Documentation is largely found in the package itself via doc-comments (i.e. hover over package functions in your editor and it'll tell you what they do). First, however, view the above full example to get started.

Simple usage

demo image

Initialize the hook:

  final UseOptimistic<int> useOptimistic = UseOptimistic<int>(initialState: 0)

Ensure your widget listens to its state changes:

@override
void initState() {
  super.initState();
  useOptimistic.addListener(() => setState(() => debugPrint("state changed to: ${useOptimistic.state}")));
}

Ensure the hook will be disposed when your widget is:

@override
void dispose() {
  super.dispose();
  useOptimistic.dispose();
}

Update your state optimistically:

TextButton(
  onPressed: () async {

    Resolver r = useOptimistic.fn(
      1, // the [newValue] to be passed to functions below
      todo: (currentState, newValue) => currentState + newValue,
      undo: (currentState, oldValue) => currentState - oldValue,
    );

    // simulating an API call
    await Future.delayed(const Duration(seconds: 1));

    // three mutually exclusive ways to deal with result
    r.acceptAs(2); // [undo] original function, then [todo] with new value
    r.accept(); // accept the original optimistic update
    r.reject(); // reject the original optimistic update and [undo] original function

  },
  child: const Text("optimistically add 1"),
),

You can call useOptimistic.fn( ... ) multiple times with different todo and undo functions and it'll execute the proper todo/undo associated with fn at the moment you called it. This means you can call multiple separate useOptimistic.fn( ... )s safely together. It does not mean you can have a single fn like this (pseudo-code): useOptimistic.fn( if x todo: () => {...} else todo: () => {...} ) that has conditionally rendered todo/undo functions.

Listen to the state:

Text("current value: ${useOptimistic.state}"),
Extra
  • The package is always open to improvements, [suggestions](mailto:me@matthewtrent.me), and additions!
  • I'll look through GitHub PRs and Issues as soon as I can.

analytics

Matthew Trent GitHub avatar
GitHub 作者

Matthew Trent ↗

SWE @ Sorce (YC F25) | MS CS @ Columbia | Prev. @ Probe, Magi, Confesi (Founder) | Honors BS CS w/ Distinction

Sorce (YC F25)San Francisco, CA
語言C++CMakeDartHTMLSwiftCKotlinObjective-C

分類

此儲存庫發佈的套件

使用的依賴

依賴清單 4 項

  • flutter{"sdk":"flutter"}
  • uuid^4.3.3
  • flutter_test開發依賴{"sdk":"flutter"}
  • flutter_lints開發依賴^3.0.0

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗