-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from jrTilak/feat/once
feat: add once method
- Loading branch information
Showing
10 changed files
with
190 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
desc: Returns a new function that can be called only once. | ||
--- | ||
|
||
The `once` function is used to create a new function that can be called only once. After the first call, the function will always return undefined without executing the original function. | ||
|
||
This is useful when you want to ensure that a function is called only once, regardless of how many times it is called. For example, a subscribe button on a website should only be clicked once, and the function should not be executed again if the button is clicked multiple times. |
26 changes: 26 additions & 0 deletions
26
src/www/src/registry/functions/functional/once/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import once from "."; | ||
|
||
describe("once", () => { | ||
it("calls the provided function only once", () => { | ||
const fn = vi.fn((x: number) => x + 1); | ||
const onceFn = once(() => fn(1)); | ||
const result1 = onceFn(); | ||
const result2 = onceFn(); | ||
const result3 = onceFn(); | ||
expect(result1).toBe(2); | ||
expect(result2).toBe(undefined); | ||
expect(result3).toBe(undefined); | ||
}); | ||
|
||
it("should also accept functions with arguments", () => { | ||
const fn = vi.fn((x: number) => x + 1); | ||
const onceFn = once(fn); | ||
const result1 = onceFn(1); | ||
const result2 = onceFn(2); | ||
const result3 = onceFn(3); | ||
expect(result1).toBe(2); | ||
expect(result2).toBe(undefined); | ||
expect(result3).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Creates a function that can only be called once. Subsequent calls to the function will return undefined. | ||
* | ||
* @param fn - The function to be called once. | ||
* @returns A new function that can only be called once. | ||
*/ | ||
const once = <T, S extends any[]>( | ||
fn: (...args: S) => T | ||
): ((...args: S) => T | undefined) => { | ||
let isCalled = false; | ||
return (...args: S): T | undefined => { | ||
if (!isCalled) { | ||
isCalled = true; | ||
return fn(...args); | ||
} | ||
return undefined; | ||
}; | ||
}; | ||
|
||
export default once; |
11 changes: 11 additions & 0 deletions
11
src/www/src/registry/functions/functional/once/once.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import once from "."; | ||
|
||
const subscribe = once(() => { | ||
console.log("Subscribed"); | ||
}); | ||
|
||
const result = subscribe(); | ||
// Expected Output: Subscribed | ||
|
||
const result2 = subscribe(); | ||
// Expected Output: undefined : as the function has been called once already. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IRegistryFunctionPropTable } from "@/types/registry.types"; | ||
|
||
const Props: IRegistryFunctionPropTable[] = [ | ||
{ | ||
title: "function", | ||
required: true, | ||
defaultValue: undefined, | ||
propDesc: "The function to be called", | ||
type: "Function", | ||
}, | ||
]; | ||
|
||
export default Props; |