-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Svelte 5 Runes mode #51
Open
kvangrae
wants to merge
8
commits into
metonym:master
Choose a base branch
from
kvangrae:runes-mode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−66
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e697f2
update deps to svelte 5 and vite 4
12696a0
migrate to svelte 5 runes mode
5cfeaef
update CHANGELOG
3ec2f88
move clearInterval into the return of $effect
bacf24d
remove unnecessary liveUpdate state variable
055d015
use $state instead of $derived
d73004b
11:24:32 PM [vite-plugin-svelte] WARNING: The following packages have…
e9653bb
remove exports as it's dynamically generated
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 +1,69 @@ | ||
<script> | ||
// @ts-check | ||
|
||
/** | ||
* Original timestamp | ||
* @type {import("dayjs").ConfigType} | ||
*/ | ||
export let timestamp = new Date().toISOString(); | ||
const { | ||
/** | ||
* Original timestamp | ||
* @type {import("dayjs").ConfigType} | ||
*/ | ||
timestamp = new Date().toISOString(), | ||
|
||
/** | ||
* Timestamp format for display. | ||
* It's also used as a title in the `relative` mode | ||
* @type {import("dayjs").OptionType} | ||
* @example "YYYY-MM-DD" | ||
*/ | ||
export let format = "MMM DD, YYYY"; | ||
/** | ||
* Timestamp format for display. | ||
* It's also used as a title in the `relative` mode | ||
* @type {import("dayjs").OptionType} | ||
* @example "YYYY-MM-DD" | ||
*/ | ||
format = "MMM DD, YYYY", | ||
|
||
/** | ||
* Set to `true` to display the relative time from the provided `timestamp`. | ||
* The value is displayed in a human-readable, relative format (e.g., "4 days ago", "Last week") | ||
* @type {boolean} | ||
*/ | ||
export let relative = false; | ||
/** | ||
* Set to `true` to display the relative time from the provided `timestamp`. | ||
* The value is displayed in a human-readable, relative format (e.g., "4 days ago", "Last week") | ||
* @type {boolean} | ||
*/ | ||
relative = false, | ||
|
||
/** | ||
* Set to `true` to update the relative time at 60 second interval. | ||
* Pass in a number (ms) to specify the interval length | ||
* @type {boolean | number} | ||
*/ | ||
export let live = false; | ||
|
||
/** | ||
* Formatted timestamp. | ||
* Result of invoking `dayjs().format()` | ||
* @type {string} | ||
*/ | ||
export let formatted = ""; | ||
/** | ||
* Set to `true` to update the relative time at 60 second interval. | ||
* Pass in a number (ms) to specify the interval length | ||
* @type {boolean | number} | ||
*/ | ||
live = false, | ||
...rest | ||
} = $props(); | ||
|
||
import { dayjs } from "./dayjs"; | ||
import { onMount } from "svelte"; | ||
|
||
/** @type {undefined | NodeJS.Timeout} */ | ||
let interval = undefined; | ||
|
||
const DEFAULT_INTERVAL = 60 * 1_000; | ||
|
||
onMount(() => { | ||
$effect(() => { | ||
/** @type {undefined | NodeJS.Timeout} */ | ||
let interval; | ||
if (relative && live !== false) { | ||
interval = setInterval( | ||
() => { | ||
formatted = dayjs(timestamp).from(); | ||
}, | ||
Math.abs(typeof live === "number" ? live : DEFAULT_INTERVAL), | ||
); | ||
} | ||
return () => clearInterval(interval); | ||
}); | ||
|
||
$: if (relative && live !== false) { | ||
interval = setInterval( | ||
() => { | ||
formatted = dayjs(timestamp).from(); | ||
}, | ||
Math.abs(typeof live === "number" ? live : DEFAULT_INTERVAL), | ||
); | ||
} | ||
$: formatted = relative | ||
? dayjs(timestamp).from() | ||
: dayjs(timestamp).format(format); | ||
$: title = relative ? dayjs(timestamp).format(format) : undefined; | ||
/** | ||
* Formatted timestamp. | ||
* Result of invoking `dayjs().format()` | ||
* @type {string} | ||
*/ | ||
let formatted = $derived( | ||
relative ? dayjs(timestamp).from() : dayjs(timestamp).format(format) | ||
); | ||
|
||
const title = $derived( | ||
relative ? dayjs(timestamp).format(format) : undefined, | ||
); | ||
</script> | ||
|
||
<time {title} {...$$restProps} datetime={timestamp}> | ||
<time {title} {...rest} datetime={timestamp}> | ||
{formatted} | ||
</time> |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait how does this even work. If this is setting formatted, but formatted is derived. Guessing this one takes over the derived instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call. it does work, but i agree, it doesn't makes sense for me to update a derived value. i'm thinking
formatted
should be a$state
instead of a$derived
. if that makes sense, let me know and i will update.but, in this instance, the derived will only be updated if
relative
,live
, ortimestamp
ever change, which, most likely, they won't change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either is fine, I think that if we update it the initial state needs to be set in the effect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i found that in
$effect
any values that are read asynchronously, for example in asetInterval
, will not be tracked.https://svelte.dev/docs/svelte/$effect#Understanding-dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, it doesn't look like it is recommended to set the initial state inside of the
$effect
. my intellij complains with the following:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with what i found out above, i'm thinking to either keep it the way it is or change
formatted
to use$state
instead of$derived
. let me know your thoughts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably using state would be good, whatever keeps it simple and easy to understand
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done