Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(@contentlayer): serialize DocumentTypeMapState
  • Loading branch information
tstelzer committed Jul 7, 2022
commit a0bff262c86f0f5894c59f4304565b18bd7b06e3
5 changes: 2 additions & 3 deletions examples/gatsbydocs/pages/[...id].tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { DocumentTypes } from 'contentlayer/generated'
import { allDocuments } from 'contentlayer/generated'
import Head from 'next/head'
import type { FC } from 'react'

import { allDocuments } from 'contentlayer/generated'
import type { DocumentTypes } from 'contentlayer/generated'

export const getStaticPaths = () => {
const paths = allDocuments.map((_) => `/${_._raw.flattenedPath}`)

Expand Down
3 changes: 1 addition & 2 deletions examples/gatsbydocs/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { allDocuments } from 'contentlayer/generated'
import type { InferGetStaticPropsType } from 'next'
import type { FC } from 'react'

import { allDocuments } from 'contentlayer/generated'

export const getStaticProps = () => {
const docs = allDocuments.map((_) => ({ path: _._raw.flattenedPath, title: _.title }))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,47 @@ import type { PosixFilePath } from '@contentlayer/utils'
import type { Has } from '@contentlayer/utils/effect'
import { HashMap, O, pipe, State, T, Tagged } from '@contentlayer/utils/effect'

type DocumentTypeName = string
export type DocumentTypeName = string

export class DocumentTypeMap extends Tagged('@local/DocumentTypeMap')<{
readonly map: HashMap.HashMap<DocumentTypeName, PosixFilePath[]>
readonly map: HashMap.HashMap<DocumentTypeName, PosixFilePath[]>
}> {
static init = () => new DocumentTypeMap({ map: HashMap.make() })
static init = () => new DocumentTypeMap({ map: HashMap.make() })

add = (documentTypeName: DocumentTypeName, filePath: PosixFilePath) => {
const oldPaths = pipe(
HashMap.get_(this.map, documentTypeName),
O.getOrElse(() => []),
)
add = (documentTypeName: DocumentTypeName, filePath: PosixFilePath) => {
const oldPaths = pipe(
HashMap.get_(this.map, documentTypeName),
O.getOrElse(() => []),
)

return new DocumentTypeMap({
map: HashMap.set_(this.map, documentTypeName, [...oldPaths, filePath]),
})
}
return new DocumentTypeMap({
map: HashMap.set_(this.map, documentTypeName, [...oldPaths, filePath]),
})
}

getFilePaths = (documentTypeName: DocumentTypeName): O.Option<PosixFilePath[]> =>
HashMap.get_(this.map, documentTypeName)
getFilePaths = (documentTypeName: DocumentTypeName): O.Option<PosixFilePath[]> =>
HashMap.get_(this.map, documentTypeName)
}

/**
* This state is needed for certain kinds of error handling (e.g. to check if singleton documents have been provided)
*/
* This state is needed for certain kinds of error handling (e.g. to check if singleton documents have been provided)
*/
export const DocumentTypeMapState = State.State<DocumentTypeMap>(DocumentTypeMap._tag)

export const serialize = () => pipe(
DocumentTypeMapState.get,
// FIXME: unsafe
T.chain(map => T.succeedWith(() => JSON.stringify(map))),
);

export function provideFromSerialized(serialized: string) {
return pipe(
// FIXME: unsafe
T.succeedWith<DocumentTypeMap>(() => JSON.parse(serialized)),
T.map(map => T.provideSomeLayer(DocumentTypeMapState.Live(new DocumentTypeMap(map))))
)
}

export const provideDocumentTypeMapState = T.provideSomeLayer(DocumentTypeMapState.Live(DocumentTypeMap.init()))

export type HasDocumentTypeMapState = Has<State.State<DocumentTypeMap>>
Loading