Skip to content

Commit 44b8343

Browse files
committed
feat(async-stack): expose StackItem.msToTimeout as a public rune
1 parent eaa9f2a commit 44b8343

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

.changeset/two-meals-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@svelte-put/async-stack': minor
3+
---
4+
5+
make `StackItem.msToTimeout` public

packages/async-stack/src/stack-item.svelte.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export class StackItem<
1111
/** @default 'idle' */
1212
state: StackItemState;
1313
config: Required<StackItemInstanceConfig<string, UserComponent>>;
14+
/**
15+
* milliseconds until the item is timeouted,
16+
* 0 means the item has already timeouted or has no timeouted
17+
* (check config.timeout for more information)
18+
*/
19+
msToTimeout: number;
1420
/**
1521
* a promise that resolves when the item is resolved,
1622
* by timing out or via the .resolve method

packages/async-stack/src/stack-item.svelte.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ export class StackItem {
1111
/** @type {Required<import('./types.package').StackItemInstanceConfig<string, UserComponent>>} */
1212
config;
1313

14+
/** @type {number} */
15+
// eslint-disable-next-line no-undef
16+
msToTimeout = $state(0);
17+
1418
#internals = {
15-
msToTimeout: 0,
1619
/** @type {ReturnType<typeof setTimeout> | undefined} */
1720
timeoutId: undefined,
1821
lastStartedTime: new Date().getTime(),
@@ -30,29 +33,29 @@ export class StackItem {
3033
*/
3134
constructor(config) {
3235
this.config = config;
33-
this.#internals.msToTimeout = config.timeout;
36+
this.msToTimeout = config.timeout;
3437

3538
this.resolution = new Promise((resolve) => {
3639
this.#internals.resolve = resolve;
3740
});
3841
}
3942

4043
resume = () => {
41-
if (this.#internals.msToTimeout <= 0 || this.state === 'elapsing') return;
44+
if (this.msToTimeout <= 0 || this.state === 'elapsing') return;
4245
clearTimeout(this.#internals.timeoutId);
4346
this.#internals.lastStartedTime = new Date().getTime();
4447
this.#internals.timeoutId = setTimeout(async () => {
45-
this.#internals.msToTimeout = 0;
48+
this.msToTimeout = 0;
4649
await this.resolve();
4750
this.state = 'timeout';
48-
}, this.#internals.msToTimeout);
51+
}, this.msToTimeout);
4952
this.state = 'elapsing';
5053
};
5154

5255
pause = () => {
5356
if (this.state === 'paused' || this.state === 'idle') return;
5457
clearTimeout(this.#internals.timeoutId);
55-
this.#internals.msToTimeout -= new Date().getTime() - this.#internals.lastStartedTime;
58+
this.msToTimeout -= new Date().getTime() - this.#internals.lastStartedTime;
5659
this.state = 'paused';
5760
};
5861

0 commit comments

Comments
 (0)