Skip to content

Instantly share code, notes, and snippets.

View klemola's full-sized avatar

Matias Klemola klemola

View GitHub Profile
@klemola
klemola / update_gravity_visuals.rs
Created August 30, 2022 09:36
vetovoima - update gravity visuals
fn update_gravity_visuals(
mut visuals_query: Query<(&mut Path, &mut DrawMode, &mut GravityRing)>,
gravity_source: Res<GravitySource>,
) {
let level_bounds_radius_pixels = LEVEL_BOUNDS_RADIUS_METERS * PIXELS_PER_METER;
let gravity_source_radius_pixels = GRAVITY_SOURCE_RADIUS_METERS * PIXELS_PER_METER;
let min_radius = gravity_source_radius_pixels;
// fade to nothing just before the ring hits the terrain
let max_radius = level_bounds_radius_pixels;
export function main() {
let userRequest: RemoteData<User, Error> = Loading;
let translationsRequest: RemoteData<Translations, Error> = Loading;
let appState: AppState = Initializing;
fetchUser()
.then(response => {
userRequest = Success(response);
if (isSuccess(userRequest) && isSuccess(translationsRequest)) {
function render(appState: AppState) {
switch (appState.type) {
case "Initializing":
return "Loading...";
case "Ready":
const { username } = appState.user;
const { greeting } = appState.translations;
return `${greeting}, ${username}`;
interface Initializing {
type: "Initializing";
}
interface Ready {
type: "Ready";
user: User;
translations: Translations;
}
function render(
userRequest: RemoteData<User, Error>,
translationsRequest: RemoteData<Translations, Error>
) {
if (isLoading(userRequest) || isLoading(translationsRequest)) {
return "Loading...";
} else if (isSuccess(userRequest) && isSuccess(translationsRequest)) {
const { username } = userRequest.data;
const { greeting } = translationsRequest.data;
return `${greeting}, ${username}`;
export function isNotAsked<T, E>(
remoteData: RemoteData<T, E>
): remoteData is NotAsked {
return remoteData.type === "NotAsked";
}
export function isLoading<T, E>(
remoteData: RemoteData<T, E>
): remoteData is Loading {
return remoteData.type === "Loading";
export function main() {
let userRequest: Data = { type: "Loading" };
fetchData()
.then(response => {
userRequest = { type: "Success", data: response };
})
.catch(error => {
userRequest = { type: "Failure", error };
});
interface User {
username: string;
email: string;
}
type Translations = { [key: string]: string };
export function main() {
let userRequest: RemoteData<User, Error> = Loading;
let translationsRequest: RemoteData<Translations, Error> = Loading;
type Data =
| {
type: "Loading";
}
| {
type: "Success";
data: User;
}
| {
type: "Failure";
interface NotAsked {
type: "NotAsked";
}
interface Loading {
type: "Loading";
}
interface Success<T> {
type: "Success";