Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 46x 46x 8x 8x 8x 8x 8x 46x 4x 4x 4x 46x 2x 2x 46x 1x 1x 46x 8x 8x 8x 46x 1x 1x 46x 1x 1x 46x 12x 12x 46x 8x 8x 8x 46x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 46x 46x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 1x 1x 1x 13x 20x 20x 20x 66x 238x 238x 66x 20x 20x 20x 20x 1x | import { Option, none, some } from 'fp-ts/Option';
import { isEqual } from 'lodash';
import { BehaviorSubject, distinctUntilChanged, map, Observable } from 'rxjs';
import { Configuration } from '../configuration';
import { Action, ActionType } from './actions';
export enum PageKeyType {
StdDoc,
LocalDoc,
}
const getInitialState = (): State => ({
configuration: none,
errors: [],
loadedPage: false,
pageKey: {
type: PageKeyType.LocalDoc,
val: some('index.html'),
},
parsedDoc: none,
rawDoc: none,
userHistory: {
paths: [],
cursor: 0,
},
});
let stateSubject = new BehaviorSubject<State>(getInitialState());
const updateInternal = <T>(state: State, { payload, actionType }: Action<T>) => {
switch (actionType) {
case ActionType.Batch: {
return (payload as any).reduce(
(stateAcc: State, currAct: Action<unknown>) => updateInternal(stateAcc, currAct),
state
);
}
case ActionType.PushError: {
const newErrors = payload as any;
return { ...state, errors: [...state.errors, ...newErrors] };
}
case ActionType.ResetError: {
return { ...state, errors: [] };
}
case ActionType.Reset: {
return getInitialState();
}
case ActionType.SetConfig: {
const configuration = payload as any;
return { ...state, configuration };
}
case ActionType.SetHistoryCursor: {
return { ...state, userHistory: { paths: state.userHistory.paths, cursor: payload } };
}
case ActionType.SetPageKey: {
return { ...state, pageKey: payload as any };
}
case ActionType.SetParsedDoc: {
return { ...state, parsedDoc: payload as any };
}
case ActionType.SetRawDoc: {
const rawDoc = payload as any;
return { ...state, loadedPage: true, parsedDoc: none, rawDoc };
}
case ActionType.SetUserHistory: {
const currCursor = state.userHistory.cursor;
const newHistory = [...state.userHistory.paths.slice(0, currCursor + 1), ...(payload as any)];
return {
...state,
userHistory: {
paths: newHistory,
cursor: newHistory.length - 1,
},
};
}
}
};
export enum StateKey {
configuration = 'configuration',
errors = 'errors',
loadedPage = 'loadedPage',
pageKey = 'pageKey',
parsedDoc = 'parsedDoc',
rawDoc = 'rawDoc',
userHistory = 'userHistory',
}
export type State = {
[StateKey.configuration]: Option<Configuration>;
[StateKey.errors]: string[];
[StateKey.loadedPage]: boolean;
[StateKey.pageKey]: PageKey;
[StateKey.parsedDoc]: Option<string>;
[StateKey.rawDoc]: Option<string>;
[StateKey.userHistory]: {
paths: { docsPath: Option<string>; key: PageKey }[];
cursor: number;
};
};
export type PageKey = {
val: Option<string>;
type: PageKeyType;
};
export const update = <T>(action: Action<T>) => {
const state = stateSubject.getValue();
const newState = updateInternal(state, action);
if (newState) {
stateSubject.next(newState);
}
};
// this will be take a list of top level selectors until something else is needed
export const slice = <T extends StateKey>(selectors: T[]) =>
new Observable<Pick<State, T>>((sub) => {
stateSubject
.pipe(
map((state) => {
return selectors.reduce((newState, key) => {
newState[key] = state[key];
return newState;
}, {} as Pick<State, T>);
}),
distinctUntilChanged(isEqual)
)
.subscribe(sub);
});
|