Skip to content

Instantly share code, notes, and snippets.

View lydemann's full-sized avatar

Christian Lüdemann lydemann

View GitHub Profile
@lydemann
lydemann / exponential-backoff-retry.spec.ts
Created November 16, 2022 16:30
exponential-backoff-retry.spec.ts
import { fakeAsync, tick } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import {
exponentialBackoffRetry,
RETRY_COUNT,
RETRY_TIME,
} from './exponential-backoff-retry';
describe('exponentialBackoffRetry', () => {
@lydemann
lydemann / exponential-backoff-retry.ts
Created November 16, 2022 16:30
exponential-backoff-retry.ts
import { Observable, pipe, range, throwError, zip, of, timer } from 'rxjs';
import { map, mergeMap, retryWhen } from 'rxjs/operators';
export const RETRY_COUNT = 3;
export const RETRY_TIME = 500;
export function exponentialBackoffRetry<T>(
maxTries: number = RETRY_COUNT,
delay: number = RETRY_TIME,
) {
@lydemann
lydemann / server.ts
Created November 18, 2021 18:23
server.ts
import { ApolloServer, AuthenticationError } from 'apollo-server-express';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
import * as express from 'express';
import admin from 'firebase-admin';
import { AuthIdentity, RequestContext } from './auth-identity';
import { resolvers } from './resolvers';
import { typeDefs } from './schema';
/* Async verification with user token */
@lydemann
lydemann / server.ts
Created November 18, 2021 18:22
server.ts
import { ApolloServer, AuthenticationError } from 'apollo-server-express';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
import * as express from 'express';
import admin from 'firebase-admin';
import { AuthIdentity, RequestContext } from './auth-identity';
import { resolvers } from './resolvers';
import { typeDefs } from './schema';
/* Async verification with user token */
@lydemann
lydemann / main.ts
Created November 18, 2021 18:19
main.ts
import { region } from 'firebase-functions';
import { gqlServer } from './app/server';
import { environment } from './environments/environment';
const dotenvJSON = require('dotenv-json');
if (environment.production) {
dotenvJSON({ path: __dirname + '/assets/env.json' });
} else {
dotenvJSON({ path: __dirname + '/assets/env.local.json' });
}
@lydemann
lydemann / course.component.ts
Created May 14, 2021 11:55
course.component.ts
ngOnDestroy(): void {
this.renderer.removeChild(this.elementRef.nativeElement, this.styleElement);
this.destroy$.next();
this.destroy$.complete();
}
ngOnInit() {
switchMap((courseId) =>
this.courseFacade.getCourseCustomStyling(courseId)
)
@lydemann
lydemann / router.selectors.ts
Created March 16, 2021 08:52
router.selectors.ts
const getFlattenedParentRouteParams = (route: ActivatedRouteSnapshot) => {
if (!route.parent) {
return route.params;
}
const parentRouteParams = getFlattenedParentRouteParams(route.parent);
return {
...route.params,
...parentRouteParams,
};
@lydemann
lydemann / course-facade.service.ts
Last active March 23, 2021 16:38
course-facade.service.ts
editCourseSubmitted(editedCourse: Course) {
return this.apollo.mutate<Course>({
mutation: EDIT_COURSE_MUTATION,
variables: {
id: editedCourse.id,
name: editedCourse.name,
description: editedCourse.description,
} as Course,
});
}
@lydemann
lydemann / prevent-ghost-clicks.directive.ts
Created March 15, 2021 10:35
prevent-ghost-clicks.directive.ts
import { Directive, ElementRef, HostListener, OnInit } from '@angular/core';
import 'hammerjs';
/*
Preventing clicks after a HammerJS press (long press) event.
Inspired from HammerJS ghost click helper: https://gist.github.com/jtangelder/361052976f044200ea17
*/
@Directive({
selector: '[preventGhostClicks]',
})
@lydemann
lydemann / graphql-helpers.ts
Last active March 23, 2021 16:28
graphql-helpers.ts
export function removeFromCache<ReadQueryResponseT>(
toRemove: EntityObject,
readQuery: DocumentNode,
cache: ApolloCache<any>,
entityName: keyof ReadQueryResponseT
) {
const existingEntities = cache.readQuery<
Record<keyof ReadQueryResponseT, EntityObject[]>
>({
query: readQuery,