Skip to content

Commit

Permalink
Add syncInterval to be explicit about when to (not) sync
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Jan 16, 2024
1 parent 2ade739 commit 97c41d2
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 22 deletions.
37 changes: 21 additions & 16 deletions apps/web/src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import {Entry} from 'alinea/core'
export async function GET(request: Request) {
const searchTerm = new URL(request.url).searchParams.get('query')
if (!searchTerm) return Response.json([])
const matches = await cms.in(cms.workspaces.main.pages).find(
Entry()
.select({
title: Entry.title,
url: Entry.url,
snippet: alinea.snippet(),
parents({parents}) {
return parents().select({
id: Entry.entryId,
title: Entry.title
})
}
})
.search(searchTerm)
.take(25)
)
const matches = await cms
.in(cms.workspaces.main.pages)
.syncInterval(0)
.find(
Entry()
.select({
title: Entry.title,
url: Entry.url,
snippet: alinea.snippet(),
parents({parents}) {
return parents().select({
id: Entry.entryId,
title: Entry.title
})
}
})
.search(...searchTerm.split(' '))
.take(25)
)
return Response.json(matches)
}

export const runtime = 'edge'
11 changes: 7 additions & 4 deletions src/backend/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ export class Handler implements Resolver {

resolve = async (params: Connection.ResolveParams) => {
const {resolveDefaults} = this.options
await this.periodicSync()
return this.resolver.resolve({...resolveDefaults, ...params})
const resolveParams = {...resolveDefaults, ...params}
const {syncInterval} = resolveParams
await this.periodicSync(syncInterval)
return this.resolver.resolve(resolveParams)
}

protected previewAuth(): Connection.Context {
Expand Down Expand Up @@ -110,9 +112,10 @@ export class Handler implements Resolver {
return {...entry, ...entryData, path: entry.path}
}

async periodicSync() {
async periodicSync(syncInterval = 5) {
if (syncInterval === 0) return
const now = Date.now()
if (now - this.lastSync < 5_000) return
if (now - this.lastSync < syncInterval * 1000) return
this.lastSync = now
try {
await this.syncPending()
Expand Down
4 changes: 3 additions & 1 deletion src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export interface Config {
preview?: string | ComponentType<{entry: Entry; previewToken: string}>
/** Every edit will pass through a draft phase before being published */
enableDrafts?: boolean
/** The interval in seconds at which the frontend will poll for updates */
syncInterval?: number

/**
publicDir?: string
publicDir?: string
dashboardFile?: string
handlerUrl?:
*/
Expand Down
1 change: 1 addition & 0 deletions src/core/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export namespace Connection {
selection: Selection
location?: Array<string>
locale?: string
syncInterval?: number
}
export type MediaUploadParams = {
buffer: ArrayBuffer
Expand Down
21 changes: 21 additions & 0 deletions src/core/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ import {seralizeLocation, serializeSelection} from './pages/Serialize.js'
export type Location = Root | Workspace | PageSeed

export interface GraphRealmApi {
/** Filter results by location */
in(location: Location): GraphRealmApi
/** Filter results by locale */
locale(locale: string): GraphRealmApi
/** Find a single entry or null */
maybeGet<S extends Projection | Type>(
select: S
): Promise<Projection.InferOne<S> | null>
/** Find a single entry */
get<S extends Projection | Type>(select: S): Promise<Projection.InferOne<S>>
/** Find a set of entries */
find<S extends Projection | Type>(select: S): Promise<Selection.Infer<S>>
/** The time in seconds to poll for updates to content */
syncInterval(interval: number): GraphRealmApi
/** The amount of results found */
count(cursor: Cursor.Find<any>): Promise<number>
}

Expand All @@ -41,6 +49,19 @@ export class GraphRealm implements GraphRealmApi {
this.targets = Schema.targets(config.schema)
}

syncInterval(interval: number) {
return new GraphRealm(
this.config,
params => {
return this.resolve({
...params,
syncInterval: interval
})
},
this.origin
)
}

in(location: Location): GraphRealmApi {
// Should this reset locale?
return new GraphRealm(this.config, this.resolve, {...this.origin, location})
Expand Down
1 change: 1 addition & 0 deletions src/core/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface PreviewUpdate {
export interface ResolveDefaults {
realm?: Realm
preview?: PreviewUpdate
syncInterval?: number
}

export interface Resolver {
Expand Down
1 change: 1 addition & 0 deletions src/core/driver/DefaultDriver.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class DefaultDriver extends CMS {
config: this.config,
url: devUrl,
resolveDefaults: {
syncInterval: this.config.syncInterval ?? 60,
realm: Realm.Published
}
})
Expand Down
1 change: 1 addition & 0 deletions src/core/driver/NextDriver.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class NextDriver extends DefaultDriver implements NextApi {
const [draftStatus] = outcome(() => draftMode())
const isDraft = draftStatus?.isEnabled
const resolveDefaults: ResolveDefaults = {
syncInterval: this.config.syncInterval ?? 60,
realm: Realm.Published
}
if (isDraft) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/UIStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface UIStoryProps extends PropsWithChildren<{}> {

export function UIStory({fullWidth, fullHeight, children}: UIStoryProps) {
return (
<DashboardProvider dev config={example} client={client}>
<DashboardProvider dev config={example.config} client={client}>
<Viewport attachToBody>
<FieldToolbar.Provider>
<div
Expand Down

0 comments on commit 97c41d2

Please sign in to comment.