Skip to content

Commit

Permalink
fix(examples): add type hints, minor refactor (various)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 22, 2019
1 parent 33fbd7f commit a70aa1d
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 971 deletions.
45 changes: 31 additions & 14 deletions examples/commit-table-ssr/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const COMMITS_URL =
process.env.NODE_ENV === "production" ? "./commits.json" : "/commits";

// UI root component
const app = (state) => [
const app = (state: any) => [
"div",
[header, ctx.repo.name],
[stats, state],
[repoTable, state.commits]
];

// stats container component
const stats = (ctx: AppContext, state) => [
const stats = (ctx: AppContext, state: any) => [
"div",
ctx.ui.stats.root,
["div.tl", ctx.ui.stats.col, [searchFilter, state]],
Expand All @@ -51,7 +51,10 @@ const stats = (ctx: AppContext, state) => [
];

// search filter input component
const searchFilter = (ctx: AppContext, state) => [
const searchFilter = (
ctx: AppContext,
state: { commits: Commit[]; search: string }
) => [
"div",
"Filter:",
[
Expand All @@ -61,15 +64,21 @@ const searchFilter = (ctx: AppContext, state) => [
type: "text",
value: state.search,
// emit changes on `search` stream
oninput: (e) => search.next(e.target.value.toLowerCase())
oninput: (e: any) => search.next(e.target.value.toLowerCase())
}
],
`(${state.commits.length} commits)`
];

// transformation function to filter commits with search string
// doesn't apply filter if search term is empty
const filterCommits = ({ commits, search }) => ({
const filterCommits = ({
commits,
search
}: {
commits: Commit[];
search: string;
}) => ({
search,
commits: search
? commits.filter((x) => x.msg.toLowerCase().indexOf(search) !== -1)
Expand All @@ -78,17 +87,25 @@ const filterCommits = ({ commits, search }) => ({

// transformation function to compute stats of filtered commits
// uses `resolve-map` package to execute given functions in dependency order
const computeStats = (state) =>
const computeStats = (state: any) =>
resolveMap({
...state,
adds: ({ commits }) =>
transduce(map((x: Commit) => x.add || 0), add(), commits),
dels: ({ commits }) =>
transduce(map((x: Commit) => x.del || 0), add(), commits),
authors: ({ commits }) =>
adds: ({ commits }: any) =>
transduce(
map((x: Commit) => x.add || 0),
add(),
commits
),
dels: ({ commits }: any) =>
transduce(
map((x: Commit) => x.del || 0),
add(),
commits
),
authors: ({ commits }: any) =>
transduce(pluck("author"), conj(), commits).size,
avgAdds: ({ commits, adds }) => (adds / commits.length) | 0,
avgDels: ({ commits, dels }) => (dels / commits.length) | 0
avgAdds: ({ commits, adds }: any) => (adds / commits.length) | 0,
avgDels: ({ commits, dels }: any) => (dels / commits.length) | 0
});

// error stream & handler
Expand Down Expand Up @@ -116,7 +133,7 @@ const commits = fromInterval(60 * 60 * 1000)
const search = stream<string>();

// stream combinator & transformation into UI / DOM update
sync({
sync<any, any>({
// streams to synchronize
src: {
commits,
Expand Down
2 changes: 1 addition & 1 deletion examples/commit-table-ssr/src/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface HTMLDoc {
/**
* This object will be passed to all component functions.
*/
ctx: AppContext;
ctx?: AppContext;
}

export interface HTMLHead {
Expand Down
3 changes: 1 addition & 2 deletions examples/commit-table-ssr/src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTMLDoc, AppContext } from "./api";
import { AppContext, HTMLDoc } from "./api";

export const DEFAULT_DOC: HTMLDoc = {
head: {
Expand All @@ -23,7 +23,6 @@ export const DEFAULT_DOC: HTMLDoc = {
styles: [],
title: ""
},
ctx: null,
body: []
};

Expand Down
17 changes: 8 additions & 9 deletions examples/commit-table-ssr/src/server/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ const parseLog = ([log]: string[]): Partial<Commit> => {
*
* @param log
*/
const parseStats = ([_, stats]: string[]): Partial<Commit> =>
const parseStats = ([_, stats]: string[]): Partial<Commit> | null =>
stats
? transduce(
map(([k, v]) => [k, parseInt(v)]),
map(([k, v]) => <[string, number]>[k, parseInt(v)]),
assocObj(),
zip(["files", "add", "del"], stats.split(","))
)
Expand All @@ -70,13 +70,12 @@ export const repoCommits = (repoPath: string) =>
// merge commits have only 1 line
// pick a random number for merge commits
// in case there're successive ones
partitionBy(
(x) =>
x.indexOf("~~Merge ") !== -1
? Math.random()
: x.length > 0
? 1
: 0
partitionBy((x) =>
x.indexOf("~~Merge ") !== -1
? Math.random()
: x.length > 0
? 1
: 0
),
// remove empty lines
filter((x) => x[0].length > 0),
Expand Down
15 changes: 8 additions & 7 deletions examples/commit-table-ssr/src/server/html.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Nullable } from "@thi.ng/api";
import { mergeDeepObj } from "@thi.ng/associative";
import { serialize } from "@thi.ng/hiccup";
import { map } from "@thi.ng/transducers";
Expand Down Expand Up @@ -25,19 +26,19 @@ export const html = (doc: HTMLDoc) => {
{ lang: doc.lang || "en" },
[
"head",
map((meta) => ["meta", meta], doc.head.meta),
map((s) => script(null, s), doc.head.scripts),
map((link) => ["link", link], doc.head.links),
map((css) => ["style", css], doc.head.styles),
["title", doc.head.title]
map((meta) => ["meta", meta], doc.head!.meta || []),
map((s) => script(null, s), doc.head!.scripts || []),
map((link) => ["link", link], doc.head!.links || []),
map((css) => ["style", css], doc.head!.styles || []),
["title", doc.head!.title || ""]
],
["body", doc.ctx.ui.body, ...doc.body]
["body", doc.ctx!.ui.body, ...doc.body]
],
doc.ctx
)}`;
};

export const script = (
_: AppContext,
_: Nullable<AppContext>,
script: { src: string; type?: string }
) => ["script", { type: "text/javascript", ...script }];
Loading

0 comments on commit a70aa1d

Please sign in to comment.