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): make documentTypeMap explicit in makeCacheItemFr…
…omFilePath
  • Loading branch information
tstelzer committed Jul 8, 2022
commit d814ae1a4f6a7f5dc1a9e6896a12a4b8c6a93b4d
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export const runTest = async ({
previousCache: undefined,
contentTypeMap,
}),
// TODO: DocumentTypeMap is now explicitly returned from
// makeCacheItemFromFilePath in [1]. Verify that it is tested somewhere
// else, as we're throwing it away here.
T.map((_) => _.tuple[0]),
These.effectToEither,
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,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'
import type { Has, Option, These } from '@contentlayer/utils/effect'
import { HashMap, O, pipe, State, T, Tagged, Tuple } from '@contentlayer/utils/effect'

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(
export const serialize = () =>
pipe(
DocumentTypeMapState.get,
// FIXME: unsafe
T.chain(map => T.succeedWith(() => JSON.stringify(map))),
);
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))))
)
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()))
Expand Down
Loading