extra-lazy
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -24,3 +24,3 @@ (function (global, factory) { | ||
/*! ***************************************************************************** | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
@@ -58,5 +58,35 @@ | ||
const contexts = []; | ||
function withLazyStatic(fn) { | ||
const cache = []; | ||
return (...args) => { | ||
contexts.push({ cache, index: 0 }); | ||
try { | ||
return fn(...args); | ||
} | ||
finally { | ||
contexts.pop(); | ||
} | ||
}; | ||
} | ||
function lazyStatic(getter) { | ||
if (contexts.length) { | ||
const context = contexts[contexts.length - 1]; | ||
if (context.index === context.cache.length) { | ||
context.cache.push(getter()); | ||
} | ||
const result = context.cache[context.index]; | ||
context.index++; | ||
return result; | ||
} | ||
else { | ||
throw new Error('lazyStatic can only be called in the function wrapped by withlazyStatic.'); | ||
} | ||
} | ||
exports.lazy = lazy; | ||
exports.lazyAsyncFunction = lazyAsyncFunction; | ||
exports.lazyFunction = lazyFunction; | ||
exports.lazyStatic = lazyStatic; | ||
exports.withLazyStatic = withLazyStatic; | ||
@@ -63,0 +93,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,17 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).ExtraLazy={})}(this,(function(n){"use strict";function t(n){let t,e=!1;return function(){return e||(t=n(),e=!0),t}} | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function e(n,t,e,o){return new(e||(e=Promise))((function(i,u){function c(n){try{r(o.next(n))}catch(n){u(n)}}function f(n){try{r(o.throw(n))}catch(n){u(n)}}function r(n){var t;n.done?i(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(c,f)}r((o=o.apply(n,t||[])).next())}))}n.lazy=t,n.lazyAsyncFunction=function(n){const o=t(n);return(...n)=>e(this,void 0,void 0,(function*(){return(yield o())(...n)}))},n.lazyFunction=function(n){const e=t(n);return(...n)=>e()(...n)},Object.defineProperty(n,"__esModule",{value:!0})})); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).ExtraLazy={})}(this,(function(n){"use strict";function t(n){let t,e=!1;return function(){return e||(t=n(),e=!0),t}}function e(n,t,e,c){return new(e||(e=Promise))((function(i,o){function u(n){try{a(c.next(n))}catch(n){o(n)}}function r(n){try{a(c.throw(n))}catch(n){o(n)}}function a(n){var t;n.done?i(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(u,r)}a((c=c.apply(n,t||[])).next())}))}const c=[];n.lazy=t,n.lazyAsyncFunction=function(n){const c=t(n);return(...n)=>e(this,void 0,void 0,(function*(){return(yield c())(...n)}))},n.lazyFunction=function(n){const e=t(n);return(...n)=>e()(...n)},n.lazyStatic=function(n){if(c.length){const t=c[c.length-1];t.index===t.cache.length&&t.cache.push(n());const e=t.cache[t.index];return t.index++,e}throw new Error("lazyStatic can only be called in the function wrapped by withlazyStatic.")},n.withLazyStatic=function(n){const t=[];return(...e)=>{c.push({cache:t,index:0});try{return n(...e)}finally{c.pop()}}},Object.defineProperty(n,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.min.js.map |
@@ -32,5 +32,35 @@ (function (global, factory) { | ||
const contexts = []; | ||
function withLazyStatic(fn) { | ||
const cache = []; | ||
return (...args) => { | ||
contexts.push({ cache, index: 0 }); | ||
try { | ||
return fn(...args); | ||
} | ||
finally { | ||
contexts.pop(); | ||
} | ||
}; | ||
} | ||
function lazyStatic(getter) { | ||
if (contexts.length) { | ||
const context = contexts[contexts.length - 1]; | ||
if (context.index === context.cache.length) { | ||
context.cache.push(getter()); | ||
} | ||
const result = context.cache[context.index]; | ||
context.index++; | ||
return result; | ||
} | ||
else { | ||
throw new Error('lazyStatic can only be called in the function wrapped by withlazyStatic.'); | ||
} | ||
} | ||
exports.lazy = lazy; | ||
exports.lazyAsyncFunction = lazyAsyncFunction; | ||
exports.lazyFunction = lazyFunction; | ||
exports.lazyStatic = lazyStatic; | ||
exports.withLazyStatic = withLazyStatic; | ||
@@ -37,0 +67,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,2 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).ExtraLazy={})}(this,(function(n){"use strict";function e(n){let e,t=!1;return function(){return t||(e=n(),t=!0),e}}n.lazy=e,n.lazyAsyncFunction=function(n){const t=e(n);return async(...n)=>(await t())(...n)},n.lazyFunction=function(n){const t=e(n);return(...n)=>t()(...n)},Object.defineProperty(n,"__esModule",{value:!0})})); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).ExtraLazy={})}(this,(function(n){"use strict";function t(n){let t,e=!1;return function(){return e||(t=n(),e=!0),t}}const e=[];n.lazy=t,n.lazyAsyncFunction=function(n){const e=t(n);return async(...n)=>(await e())(...n)},n.lazyFunction=function(n){const e=t(n);return(...n)=>e()(...n)},n.lazyStatic=function(n){if(e.length){const t=e[e.length-1];t.index===t.cache.length&&t.cache.push(n());const c=t.cache[t.index];return t.index++,c}throw new Error("lazyStatic can only be called in the function wrapped by withlazyStatic.")},n.withLazyStatic=function(n){const t=[];return(...c)=>{e.push({cache:t,index:0});try{return n(...c)}finally{e.pop()}}},Object.defineProperty(n,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.min.js.map |
export * from './lazy'; | ||
export * from './lazy-function'; | ||
export * from './lazy-async-function'; | ||
export * from './lazy-static'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./lazy-async-function"), exports); | ||
__exportStar(require("./lazy-static"), exports); | ||
//# sourceMappingURL=index.js.map |
export * from './lazy'; | ||
export * from './lazy-function'; | ||
export * from './lazy-async-function'; | ||
export * from './lazy-static'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./lazy-async-function"), exports); | ||
__exportStar(require("./lazy-static"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "extra-lazy", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -42,13 +42,13 @@ "keywords": [], | ||
"devDependencies": { | ||
"@commitlint/cli": "^16.2.3", | ||
"@commitlint/config-conventional": "^16.2.1", | ||
"@rollup/plugin-commonjs": "^21.0.3", | ||
"@commitlint/cli": "^17.0.3", | ||
"@commitlint/config-conventional": "^17.0.3", | ||
"@rollup/plugin-commonjs": "^22.0.2", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^13.1.3", | ||
"@rollup/plugin-node-resolve": "^13.3.0", | ||
"@rollup/plugin-replace": "^4.0.0", | ||
"@rollup/plugin-typescript": "^8.3.1", | ||
"@rollup/plugin-typescript": "^8.3.4", | ||
"@types/jest": "^27.4.1", | ||
"@typescript-eslint/eslint-plugin": "^5.18.0", | ||
"@typescript-eslint/parser": "^5.18.0", | ||
"eslint": "^8.12.0", | ||
"@typescript-eslint/eslint-plugin": "^5.33.0", | ||
"@typescript-eslint/parser": "^5.33.0", | ||
"eslint": "^8.21.0", | ||
"husky": "4", | ||
@@ -58,13 +58,14 @@ "jest": "^27.5.1", | ||
"npm-run-all": "^4.1.5", | ||
"return-style": "^1.0.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.70.1", | ||
"rollup": "^2.77.2", | ||
"rollup-plugin-analyzer": "^4.0.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"standard-version": "^9.3.1", | ||
"standard-version": "^9.5.0", | ||
"ts-jest": "^27.1.4", | ||
"tscpaths": "^0.0.9", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.6.3" | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -50,1 +50,24 @@ # extra-lazy | ||
Create a async function lazily. | ||
### lazyStatic | ||
```ts | ||
function lazyStatic<T>(getter: () => T): T | ||
/** | ||
* @param fn | ||
* The function must satisfy the following conditions, it's like React hooks very much: | ||
* - The function should not be an async function, | ||
* it is impossible to ensure that `lazyStatic` works correctly in asynchronous flows. | ||
* - `lazyStatic` calls should not be in loops or branches. | ||
*/ | ||
function withLazyStatic<Result, Args extends any[]>( | ||
fn: (...args: Args) => Result | ||
): (...args: Args) => Result | ||
``` | ||
```ts | ||
const fn = withLazyStatic((text: string) => lazyStatic(() => text)) | ||
fn('hello') // 'hello' | ||
fn('world') // 'hello' | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114959
49
493
73
25