Last active
March 31, 2022 12:18
-
-
Save vendethiel/cb188183e631ba40dfc085081b7e53a8 to your computer and use it in GitHub Desktop.
Once
This file contains hidden or 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
const once = (function () { | |
const fns = []; | |
let i = 0; | |
return function (fn) { | |
const cur = i++; | |
fns[cur] = fn; | |
return function () { | |
if (fns[cur]) { | |
fns[cur].apply(null, arguments); | |
fns[cur] = null; | |
} | |
} | |
} | |
})(); | |
const query = once(sendRequest); | |
function MyComponent() { | |
query(); | |
} |
This file contains hidden or 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
once = let fns = [], i = 0 | |
-> | |
cur = i++ | |
fns[cur] = it | |
-> | |
fns[cur]? ... | |
fns[cur] = void |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment