Skip to content

Instantly share code, notes, and snippets.

@m93a
Last active May 18, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m93a/b883de753f39c583bc14ba10bca778a0 to your computer and use it in GitHub Desktop.
Save m93a/b883de753f39c583bc14ba10bca778a0 to your computer and use it in GitHub Desktop.
Code snippets for Svelte projects
<script lang="ts">
let foo = 1;
let lastFoo: typeof foo;
$: {
fooChanged(foo, lastFoo);
lastFoo = foo;
}
function fooChanged(current: typeof foo, previous?: typeof foo) {
console.log(current, previous);
}
</script>
<button on:click={() => foo++}>Click me!</button>
{
"Change Listener Pattern (Typed)": {
"scope": "typescript",
"prefix": "change",
"body": [
"let last${1/(.*)/${1:/capitalize}/}: typeof $1;",
"\\$: {",
"\t$1Changed($1, last${1/(.*)/${1:/capitalize}/});",
"\tlast${1/(.*)/${1:/capitalize}/} = $1;",
"}",
"",
"function $1Changed(current: typeof $1, previous?: typeof $1) {",
"\t$2",
"}"
]
},
"Change Listener Pattern (Untyped)": {
"scope": "javascript",
"prefix": "change",
"body": [
"let last${1/(.*)/${1:/capitalize}/};",
"\\$: {",
"\t$1Changed($1, last${1/(.*)/${1:/capitalize}/});",
"\tlast${1/(.*)/${1:/capitalize}/} = $1;",
"}",
"",
"function $1Changed(current, previous) {",
"\t$2",
"}"
]
},
"Change Listener Pattern (Store, Typed)": {
"scope": "typescript",
"prefix": "store-change",
"body": [
"let last${1/(.*)/${1:/capitalize}/}: typeof \\$$1;",
"\\$: {",
"\t$1Changed(\\$$1, last${1/(.*)/${1:/capitalize}/});",
"\tlast${1/(.*)/${1:/capitalize}/} = \\$$1;",
"}",
"",
"function $1Changed(current: typeof \\$$1, previous?: typeof \\$$1) {",
"\t$2",
"}"
]
},
"Change Listener Pattern (Store, Untyped)": {
"scope": "javascript",
"prefix": "store-change",
"body": [
"let last${1/(.*)/${1:/capitalize}/};",
"\\$: {",
"\t$1Changed(\\$$1, last${1/(.*)/${1:/capitalize}/});",
"\tlast${1/(.*)/${1:/capitalize}/} = \\$$1;",
"}",
"",
"function $1Changed(current, previous) {",
"\t$2",
"}"
]
},
"Script (Typed)": {
"scope": "svelte",
"prefix": "ts",
"body": [
"<script lang=\"ts\">",
"\t$1",
"</script>"
]
},
"Script (Untyped)": {
"scope": "svelte",
"prefix": "js",
"body": [
"<script>",
"\t$1",
"</script>"
]
},
"Script (Module, Typed)": {
"scope": "svelte",
"prefix": "module-ts",
"body": [
"<script lang=\"ts\" context=\"module\">",
"\t$1",
"</script>"
]
},
"Script (Module, Untyped)": {
"scope": "svelte",
"prefix": "module-js",
"body": [
"<script context=\"module\">",
"\t$1",
"</script>"
]
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment