Skip to content

Commit

Permalink
feat: skipWrites option
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 2, 2024
1 parent 52334b5 commit f62dec3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface MoquerieInstanceData {
resources: string
}
}
skipWrites: boolean
}

export interface CreateMoquerieInstanceOptions {
Expand All @@ -45,8 +46,14 @@ export interface CreateMoquerieInstanceOptions {
cwd: string
/**
* Whether to watch for changes in context, settings...
*
* @default true
*/
watching?: boolean
/**
* Don't write database changes to disk.
*/
skipWrites?: boolean
}

export async function createMoquerieInstance(options: CreateMoquerieInstanceOptions): Promise<MoquerieInstance> {
Expand All @@ -71,6 +78,7 @@ export async function createMoquerieInstance(options: CreateMoquerieInstanceOpti
resources: '',
},
},
skipWrites: options.skipWrites ?? false,
}

function onDestroy(cb: () => Awaitable<void>) {
Expand Down
40 changes: 34 additions & 6 deletions packages/core/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
})

async function writeManifest() {
if (mq.data.skipWrites) {
return
}

if (options.manifest?.write) {
return options.manifest.write(folder, manifest)
}
Expand All @@ -194,6 +198,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
}

async function writeFile(item: TData) {
if (mq.data.skipWrites) {
return
}

const manifestData = manifest.files[item.id]
if (!manifestData) {
throw new Error(`Invalid storage file ${item.id} not found in manifest`)
Expand Down Expand Up @@ -279,6 +287,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
const pendingRemovalFiles = new Set<string>()

async function removeFiles() {
if (mq.data.skipWrites) {
return
}

pendingRemovalFiles.forEach((file) => {
const resolvedPath = getFilePath(file)
if (fs.existsSync(resolvedPath)) {
Expand All @@ -292,6 +304,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
}

function removeFile(file: string) {
if (mq.data.skipWrites) {
return
}

pendingRemovalFiles.add(file)
queueRemoveFiles()
}
Expand All @@ -300,12 +316,16 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta

// @TODO better system for reloading files using watch for example

const refreshInterval = setInterval(async () => {
if (!writeQueue.size) {
manifest = await readManifest()
await load()
}
}, 5000)
let refreshInterval: NodeJS.Timeout

if (!mq.data.skipWrites && mq.data.watching) {
refreshInterval = setInterval(async () => {
if (!writeQueue.size) {
manifest = await readManifest()
await load()
}
}, 5000)
}

// Data access

Expand Down Expand Up @@ -344,6 +364,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
data[index] = item
}

if (mq.data.skipWrites) {
return
}

// Deduplicate filenames
let filename = getFilename(item)
if (options.deduplicateFiles !== false) {
Expand Down Expand Up @@ -373,6 +397,10 @@ export async function useStorage<TData extends { id: string }>(mq: MoquerieInsta
const index = data.findIndex(i => i.id === id)
if (index !== -1) {
data.splice(index, 1)

if (mq.data.skipWrites) {
return
}
const file = manifest.files[id]
if (file) {
removeFile(file)
Expand Down

0 comments on commit f62dec3

Please sign in to comment.