Skip to content

Commit 7591d91

Browse files
committed
feat(notion): add polling system
1 parent f34af86 commit 7591d91

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

examples/node-script-notion/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"name": "node-script-notion-example",
33
"private": true,
44
"scripts": {
5-
"start": "contentlayer build && node --experimental-json-modules my-script.mjs"
5+
"start": "contentlayer build && node --experimental-json-modules my-script.mjs",
6+
"dev": "contentlayer dev"
67
},
78
"dependencies": {
89
"@notionhq/client": "^2.2.3",
910
"contentlayer": "latest"
1011
}
11-
}
12+
}

packages/@contentlayer/source-notion/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentlayer-source-notion",
3-
"version": "0.0.1-alpha.25",
3+
"version": "0.0.1-alpha.26",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/source-notion/src/fetchData/fetchAllDocuments.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,35 @@ export const fetchAllDocuments = ({ databaseTypeDefs, previousCache, schemaDef,
2323
S.chain((pages) =>
2424
pipe(
2525
S.effect(
26-
T.forEachParN_(pages, os.cpus().length, (page) =>
27-
makeCacheItem({
28-
page,
29-
documentTypeDef: schemaDef.documentTypeDefMap[databaseTypeDef.name]!,
30-
databaseTypeDef,
31-
previousCache,
32-
options,
33-
}),
26+
pipe(
27+
T.forEachParN_(pages, os.cpus().length, (page) =>
28+
makeCacheItem({
29+
page,
30+
documentTypeDef: schemaDef.documentTypeDefMap[databaseTypeDef.name]!,
31+
databaseTypeDef,
32+
previousCache,
33+
options,
34+
}),
35+
),
3436
),
3537
),
3638
OT.withStreamSpan('@contentlayer/source-notion/fetchData:makeCacheItems'),
3739
),
3840
),
3941
S.runCollect,
40-
T.chain((chunks) => T.reduce_(chunks, [] as DataCache.CacheItem[], (z, a) => T.succeed([...z, ...a]))),
42+
T.chain((chunks) =>
43+
T.reduce_(chunks, [] as { fromCache: boolean; cacheItem: DataCache.CacheItem }[], (z, a) =>
44+
T.succeed([...z, ...a]),
45+
),
46+
),
4147
),
4248
),
43-
T.map((chunks) => Chunk.reduce_(chunks, [] as DataCache.CacheItem[], (z, a) => [...z, ...a])),
44-
T.map((documents) => ({ cacheItemsMap: Object.fromEntries(documents.map((_) => [_.document._id, _])) })),
49+
T.map((chunks) =>
50+
Chunk.reduce_(chunks, [] as { fromCache: boolean; cacheItem: DataCache.CacheItem }[], (z, a) => [...z, ...a]),
51+
),
52+
T.map((documents) => ({
53+
cacheItemsMap: Object.fromEntries(documents.map((_) => [_.cacheItem.document._id, _.cacheItem])),
54+
})),
4555
OT.withSpan('@contentlayer/source-notion/fetchData:fetchAllDocuments'),
4656
T.mapError((error) => new core.SourceFetchDataError({ error, alreadyHandled: false })),
4757
)

packages/@contentlayer/source-notion/src/fetchData/makeCacheItem.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type * as core from '@contentlayer/core'
2-
import { hashObject } from '@contentlayer/utils'
32
import { OT, pipe, T } from '@contentlayer/utils/effect'
43
import type { PageObjectResponse } from '@notionhq/client/build/src/api-endpoints'
54

@@ -27,7 +26,7 @@ export const makeCacheItem = ({ databaseTypeDef, documentTypeDef, previousCache,
2726
previousCache.cacheItemsMap[page.id]!.hasWarnings === false
2827
) {
2928
const cacheItem = previousCache.cacheItemsMap[page.id]!
30-
return cacheItem
29+
return { cacheItem, fromCache: true }
3130
}
3231

3332
const document = yield* $(makeDocument({ documentTypeDef, databaseTypeDef, page, options }))
@@ -38,10 +37,13 @@ export const makeCacheItem = ({ databaseTypeDef, documentTypeDef, previousCache,
3837
})
3938

4039
return {
41-
document,
42-
documentHash,
43-
hasWarnings: false,
44-
documentTypeName: documentTypeDef.name,
40+
cacheItem: {
41+
document,
42+
documentHash,
43+
hasWarnings: false,
44+
documentTypeName: documentTypeDef.name,
45+
},
46+
fromCache: false,
4547
}
4648
}),
4749
OT.withSpan('@contentlayer/source-notion/fetchData:makeCacheItem'),

packages/@contentlayer/source-notion/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as core from '@contentlayer/core'
22
import { processArgs } from '@contentlayer/core'
3-
import { pipe, S, T } from '@contentlayer/utils/effect'
3+
import { pipe, S, SC, T } from '@contentlayer/utils/effect'
44
import { NotionRenderer } from '@notion-render/client'
55
import * as notion from '@notionhq/client'
66

@@ -17,13 +17,15 @@ export const makeSource: core.MakeSourcePlugin<PluginOptions & core.PartialArgs>
1717
const {
1818
options,
1919
extensions,
20-
restArgs: { databaseTypes, ...rest },
20+
restArgs: { databaseTypes, dev, ...rest },
2121
} = await processArgs(args, sourceKey)
2222

2323
const databaseTypeDefs = (Array.isArray(databaseTypes) ? databaseTypes : Object.values(databaseTypes)).map((_) =>
2424
_.def(),
2525
)
2626

27+
const polling = dev && dev.polling === false ? false : dev?.polling ?? 5_000
28+
2729
const client =
2830
rest.client instanceof notion.Client
2931
? rest.client
@@ -59,6 +61,7 @@ export const makeSource: core.MakeSourcePlugin<PluginOptions & core.PartialArgs>
5961
T.provideService(NotionRendererTag)(renderer),
6062
),
6163
),
64+
S.repeatSchedule<unknown, void | number>(polling ? SC.spaced(polling) : SC.stop),
6265
),
6366
}
6467
}

packages/@contentlayer/source-notion/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export type PluginOptions = {
88
client?: ConstructorParameters<typeof notion.Client>[0] | notion.Client
99
renderer?: ConstructorParameters<typeof NotionRenderer>[0] | NotionRenderer
1010
databaseTypes: DatabaseTypes
11+
dev?: {
12+
polling: false | number
13+
}
1114
}
1215

1316
export type FieldDef = core.FieldDef & { propertyKey?: string }

0 commit comments

Comments
 (0)