Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active November 30, 2023 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artalar/cfca839afed55fa5c29666666f1b4ba6 to your computer and use it in GitHub Desktop.
Save artalar/cfca839afed55fa5c29666666f1b4ba6 to your computer and use it in GitHub Desktop.
// BASE
// debounce
const onChange = debounce((ctx, event) => {
inputAtom(ctx, event.currentTarget.value);
}, 500);
// concurrent
const onChange = concurrent(async (ctx, event) => {
await ctx.schedule(() => sleep(500));
inputAtom(ctx, event.currentTarget.value);
});
// PREPARATION MAPPING
// debounce
const handleChange = debounce((ctx, value) => {
inputAtom(ctx, value);
}, 500);
const onChange = (ctx, event) => {
handleChange(ctx, event.currentTarget.value);
};
// concurrent
const onChange = concurrent(async (ctx, event) => {
const { value } = event.currentTarget;
await ctx.schedule(() => sleep(500));
inputAtom(ctx, value);
});
// CONDITIONS
// debounce
const handleChange = (ctx, value) => {
inputAtom(ctx, value);
};
const handleDebounceChange = debounce(handleChange, 500);
const onChange = (ctx, event) => {
const { value } = event.currentTarget;
if (Math.random() > 0.5) handleChange(ctx, value);
else handleDebounceChange(ctx, value);
};
// concurrent
const onChange = concurrent(async (ctx, event) => {
const { value } = event.currentTarget;
if (Math.random() > 0.5) await ctx.schedule(() => sleep(500));
inputAtom(ctx, value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment