-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # package.json
- Loading branch information
Showing
17 changed files
with
266 additions
and
61,456 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,54 @@ | ||
// See https://dev.to/jdgamble555/using-sharable-runes-with-typescript-in-svelte5-5hcp | ||
|
||
import { writable, readable } from 'svelte/store'; | ||
import { useSharedStore } from './use-shared.svelte'; | ||
|
||
export const rune = <T>(initialValue?: T) => { | ||
let _state = $state<T | undefined>(initialValue); | ||
return { | ||
get value(): T | undefined { | ||
return _state; | ||
}, | ||
set value(v: T) { | ||
_state = v; | ||
} | ||
}; | ||
}; | ||
|
||
// writable store context | ||
export const useWritable = <T>(name: string, initialValue?: T) => | ||
useSharedStore(name, writable, initialValue); | ||
|
||
// readable store context | ||
export const useReadable = <T>(name: string, initialValue?: T) => | ||
useSharedStore(name, readable, initialValue); | ||
|
||
// Shared rune context | ||
export const useState = <T>(name: string, initialValue?: T) => | ||
useSharedStore(name, rune, initialValue); | ||
|
||
/* | ||
// Example of tracking Firebase user state | ||
// Usage: | ||
// <script> | ||
// import { useUser } from '$lib/utils/state.svelte'; | ||
// const user = useUser(); | ||
// </script> | ||
const _useUser = (defaultUser: UserType | null = null) => { | ||
const user = rune(defaultUser); | ||
const unsubscribe = onIdTokenChanged( | ||
auth, | ||
(_user: User | null) => { | ||
if (!_user) { | ||
user.value = null; | ||
return; | ||
} | ||
const { displayName, photoURL, uid, email } = _user; | ||
user.value = { displayName, photoURL, uid, email }; | ||
}); | ||
onDestroy(unsubscribe); | ||
return user; | ||
}; | ||
export const useUser = (defaultUser: UserType | null = null) => | ||
useSharedStore('user', _useUser, defaultUser); | ||
*/ |
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,16 @@ | ||
// See https://dev.to/jdgamble555/using-sharable-runes-with-typescript-in-svelte5-5hcp | ||
|
||
import { getContext, hasContext, setContext } from 'svelte'; | ||
|
||
export const useSharedStore = <T, A>(name: string, fn: (value?: A) => T, initialValue?: A) => { | ||
if (hasContext(name)) { | ||
return getContext<T>(name); | ||
} | ||
if (initialValue === undefined) { | ||
throw new Error( | ||
`Readable store "${name}" is not found, or is being created without initialValue.` | ||
); | ||
} | ||
const _value = fn(initialValue); | ||
return setContext(name, _value); | ||
}; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.