Skip to content

Commit

Permalink
another braindump
Browse files Browse the repository at this point in the history
  • Loading branch information
RuneKR committed Oct 28, 2021
1 parent 00cec4b commit 38f2a77
Show file tree
Hide file tree
Showing 26 changed files with 324 additions and 212 deletions.
16 changes: 0 additions & 16 deletions packages/component/AuthenticatedRoute/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/connector/readme.md

This file was deleted.

4 changes: 0 additions & 4 deletions packages/connector/useDependencies/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ type Fetchers<T> = { [P in keyof T]?: () => Promise<T[P]> };
type Promised<T> = { [P in keyof T]?: Promise<T[P]> };

export class DependencyManager<SingletonMap extends object> {
#factories: Fetchers<SingletonMap> = {};
#singletons: Promised<SingletonMap> = {};
private factories: Fetchers<SingletonMap> = {};
private singletons: Promised<SingletonMap> = {};
register<K extends keyof SingletonMap>(
serviceName: K,
factory: () => Promise<SingletonMap[K]>
) {
this.#factories[serviceName] = factory;
this.factories[serviceName] = factory;
}
resolve<K extends keyof SingletonMap>(
serviceName: K
): Promise<SingletonMap[K]> {
if (!this.#singletons[serviceName]) {
this.#singletons[serviceName] = this.#factories[serviceName]();
if (!this.singletons[serviceName]) {
// handle grazefully
this.singletons[serviceName] = this.factories[serviceName]!();
}

return this.#singletons[serviceName];
return this.singletons[serviceName]!;
}
}
File renamed without changes.
14 changes: 14 additions & 0 deletions packages/core/_framework/FeatureRegistry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface LayoutConfig<T extends Object> {
path: string;
params?: T;
}

export class FeatureRegistry<R> {
private routes: Array<LayoutConfig<any>> = [];
register<T>(layoutConfig: LayoutConfig<T>, handler: (params: T) => R) {
this.routes.push(layoutConfig);
}
getRoutes() {
return this.routes;
}
}
22 changes: 22 additions & 0 deletions packages/core/_framework/Navigation/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LayoutConfig } from "../FeatureRegistry";
import { Navigation } from ".";

const navigation = new Navigation();

const service_propertyDetail_scoreAdd: LayoutConfig<{
propertyPreference: number;
}> = {
path: ":propertyPreference",
};

navigation.push(
service_propertyDetail_scoreAdd,
{
propertyPreference: 123,
},
{ root: "Hello" }
);

navigation.back(service_propertyDetail_scoreAdd, {
propertyPreference: 123,
});
72 changes: 72 additions & 0 deletions packages/core/_framework/Navigation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { LayoutConfig } from "../FeatureRegistry";
import { createBrowserHistory } from "history";
import * as pathToRegexp from "path-to-regexp";

export type LayoutParams<T> = T extends LayoutConfig<infer R> ? R : T;

export class Navigation {
public history = createBrowserHistory();
private root?: {
title?: string;
stackReference?: number;
};
constructor() {
this.history.listen((update: any) => {
if (this.root?.stackReference === undefined) {
return;
}

switch (update.action) {
case "PUSH":
this.root.stackReference--;
break;
case "POP": {
this.root.stackReference++;
break;
}
default:
}
});
}
rootTitle(defaultTitle: string): string | undefined {
return this.root?.title ?? defaultTitle;
}
push<T extends LayoutConfig<any>>(
feature: T,
params: LayoutParams<T>,
options?: {
root?: string;
}
) {
const { _unsafe, ...safeParams } = params;

const url = pathToRegexp.compile(feature.path)(safeParams);

if (options?.root) {
this.root = {
title: options.root,
stackReference: 1,
};
}

this.history.push(url, _unsafe);
}
goRoot<T extends LayoutConfig<any>>(feature: T, params: LayoutParams<T>) {
if (this.root?.stackReference !== undefined) {
this.history.go(this.root.stackReference);
} else {
const { _unsafe, ...safeParams } = params;
const url = pathToRegexp.compile(feature.path)(safeParams);
this.history.replace(url, _unsafe);
}
}
back<T extends LayoutConfig<any>>(feature: T, params: LayoutParams<T>) {
if (document.referrer.length && document.referrer === document.location.hostname) {
this.history.goBack();
} else {
const { _unsafe, ...safeParams } = params;
const url = pathToRegexp.compile(feature.path)(safeParams);
this.history.replace(url, _unsafe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export class ObservableState<T extends object> {
mutateSet(valueSet: Partial<T>) {
this.value = { ...this.value, ...valueSet };
for (let key of Object.keys(valueSet)) {
this.listeners[key]?.forEach((listener) => listener(this.value[key]));
// due to lack of type inference i have to manually cast
this.listeners[key as keyof T]?.forEach((listener) =>
listener(this.value[key as keyof T])
);
}
}
}
File renamed without changes.
24 changes: 0 additions & 24 deletions packages/core/appShell/index.ts

This file was deleted.

11 changes: 11 additions & 0 deletions packages/core/appShellContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ObservableState } from "../_framework/ObservableState";

export interface AppShellContext {
title: string;
container: "none" | "main";
}

export const appShellContext = new ObservableState<AppShellContext>({
title: "Loading",
container: "none",
});
13 changes: 13 additions & 0 deletions packages/core/appShellEvents/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EventHub } from "../_framework/EventHub";

const DISPLAY_SNACKBAR = "DISPLAY_SNACKBAR";

export interface DisplaySnackbarEventDetail {
message: string;
}

interface AppShellEventMap {
[DISPLAY_SNACKBAR]: DisplaySnackbarEventDetail;
}

export const appshellEventHub = new EventHub<AppShellEventMap>();
22 changes: 0 additions & 22 deletions packages/core/history/index.test.ts

This file was deleted.

93 changes: 0 additions & 93 deletions packages/core/history/index.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/core/navigation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Navigation } from "../_framework/Navigation";

export const navigation = new Navigation();
27 changes: 3 additions & 24 deletions packages/core/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
type GuardedPromise = () => Promise<any>;
import { FeatureRegistry } from "../_framework/FeatureRegistry";

class GuardError extends Error {
public type: "UNAUTHENTICATED" | "ONBOARDING_REQUIRED";
constructor(message: string) {
super(message);
}
}

interface LazyLoad {
(): Promise<any>;
}

export const routes = [];

export function registerFeature(paths: Array<string>, lazyLoad: LazyLoad) {

}

export function guardOnboarded(guardedPromise: GuardedPromise) {
// lookup
Promise.resolve()
.then(guardedPromise)
.catch((e: GuardError) => {});
}
// could update to react only here?
export const registy = new FeatureRegistry<any>();
3 changes: 3 additions & 0 deletions packages/core/service/Authorization/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Authorization {
authenticated: boolean;
}
7 changes: 7 additions & 0 deletions packages/core/service/PersonalProfile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PersonalProfile {
/**
* Group which this profile belongs to
*/
groupCode: number;
onboardingCompleted: boolean;
}
Loading

0 comments on commit 38f2a77

Please sign in to comment.