-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/inline-mod]: Implements new
lazyValue
utility
- Loading branch information
Showing
7 changed files
with
177 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@inox-tools/inline-mod": minor | ||
"@inox-tools/aik-mod": patch | ||
--- | ||
|
||
Implements new `lazyValue` utility |
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
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,99 @@ | ||
import { expect, test } from 'vitest'; | ||
import { inspectInlineMod } from '../src/inlining.js'; | ||
import { asyncFactory, factory, lazyValue } from '../src/index.js'; | ||
|
||
test('factory values', async () => { | ||
let callCount = 0; | ||
|
||
const factoryValue = factory(() => { | ||
callCount++; | ||
|
||
return { | ||
value: 'foo', | ||
}; | ||
}); | ||
|
||
const modInfo = await inspectInlineMod({ | ||
defaultExport: factoryValue, | ||
}); | ||
|
||
expect(modInfo.text).toEqualIgnoringWhitespace(` | ||
function __f0() { | ||
return (function() { | ||
const callCount = 0; | ||
return () => { | ||
callCount++; | ||
return { | ||
value: "foo" | ||
}; | ||
}; | ||
}).apply(undefined, undefined).apply(this, arguments); | ||
} | ||
const __defaultExport = __f0(); | ||
export default __defaultExport; | ||
`); | ||
|
||
expect(callCount).toBe(0); | ||
|
||
expect(factoryValue.value).toEqual('foo'); | ||
|
||
expect(callCount).toBe(1); | ||
|
||
factoryValue.value = 'bar'; | ||
expect(factoryValue.value).toEqual('bar'); | ||
|
||
expect(callCount).toBe(1); | ||
}); | ||
|
||
test('async factory values', async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- Used to test serialization | ||
let callCount = 0; | ||
|
||
const factoryValue = asyncFactory(() => { | ||
callCount++; | ||
|
||
return Promise.resolve({ | ||
value: 'foo', | ||
}); | ||
}); | ||
|
||
const modInfo = await inspectInlineMod({ | ||
defaultExport: factoryValue, | ||
}); | ||
|
||
expect(modInfo.text).toEqualIgnoringWhitespace(` | ||
function __f0() { | ||
return (function() { | ||
const callCount = 0; | ||
return () => { | ||
callCount++; | ||
return Promise.resolve({ | ||
value: "foo" | ||
}); | ||
}; | ||
}).apply(undefined, undefined).apply(this, arguments); | ||
} | ||
const __defaultExport = await __f0(); | ||
export default __defaultExport; | ||
`); | ||
}); | ||
|
||
test('lazy values', async () => { | ||
const value = lazyValue<string>(); | ||
|
||
setTimeout(() => { | ||
value.resolve('foo'); | ||
}, 500); | ||
|
||
const modInfo = await inspectInlineMod({ | ||
defaultExport: value, | ||
}); | ||
|
||
expect(modInfo.text).toEqualIgnoringWhitespace(` | ||
export default "foo"; | ||
`); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "../../tsconfig.base.json" | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"lib": ["ESNext"] | ||
} | ||
} |