Effect CommunityEC
Effect Community5mo ago
2 replies
wayne

How to prevent `Atom.optimistic` from debouncing in `@effect-atom/atom-react`?

In @effect-atom/atom-react, how do I stop Atom.optimistic from debouncing? For example if I have the following code:
export const toggleTodoAtom = Atom.optimisticFn(allTodosAtom, {
    reducer: (todos, id: TodoId) =>
        Option.match(HashMap.get(todos, id), {
            onSome: (todo) => HashMap.set(todos, id, { ...todo, completed: !todo.completed }),
            onNone: () => todos,
        }),
    fn: (set) =>
        ApiClient.runtime.fn((id) =>
            Effect.gen(function* () {
                const todos = yield* Atom.get(allTodosAtom)
                const todo = HashMap.get(todos, id)
                if (Option.isSome(todo)) {
                    const client = yield* ApiClient
                    const updatedTodo = yield* client.todos.updateTodoCompleted({
                        path: { id },
                        payload: !todo.value.completed,
                    })
                    set(HashMap.set(todos, id, updatedTodo))
                }
            }),
        ),
})

If the server is slow, and I click quickly each call to updateTodoCompleted will cancel the previous.
I've tried to pipe an Effect.fork at the end of the
Effect.gen
but that results in nothing occuring at all ...
Was this page helpful?