Skip to content

Commit c31e804

Browse files
committed
react-api: add rebounce option to save file
1 parent 55630b6 commit c31e804

File tree

8 files changed

+58
-18
lines changed

8 files changed

+58
-18
lines changed

.changeset/beige-pumas-run.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pulse-editor/shared-utils": patch
3+
"@pulse-editor/react-api": patch
4+
---
5+
6+
Add rebounce option for useFile hook's saveFile

.changeset/pre.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"afraid-moments-punch",
1313
"angry-llamas-smash",
1414
"beige-pandas-rhyme",
15+
"beige-pumas-run",
1516
"bumpy-parents-pull",
1617
"calm-rivers-march",
1718
"chatty-trains-beam",

npm-packages/react-api/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @pulse-editor/react-api
22

3+
## 0.1.1-beta.57
4+
5+
### Patch Changes
6+
7+
- Add rebounce option for useFile hook's saveFile
8+
- Updated dependencies
9+
- @pulse-editor/shared-utils@0.1.1-beta.57
10+
311
## 0.1.1-beta.56
412

513
### Patch Changes

npm-packages/react-api/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pulse-editor/react-api",
3-
"version": "0.1.1-beta.56",
3+
"version": "0.1.1-beta.57",
44
"main": "dist/main.js",
55
"files": [
66
"dist"
@@ -37,8 +37,11 @@
3737
"typescript-eslint": "^8.30.1"
3838
},
3939
"peerDependencies": {
40-
"@pulse-editor/shared-utils": "0.1.1-beta.56",
40+
"@pulse-editor/shared-utils": "0.1.1-beta.57",
4141
"react": "^19.0.0",
4242
"react-dom": "^19.0.0"
43+
},
44+
"dependencies": {
45+
"use-debounce": "^10.0.6"
4346
}
4447
}

npm-packages/react-api/src/hooks/editor/use-file.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { IMCMessage, IMCMessageTypeEnum } from "@pulse-editor/shared-utils";
22
import { useCallback, useEffect, useState } from "react";
3+
import { useDebouncedCallback } from "use-debounce";
34
import useIMC from "../imc/use-imc";
45

5-
export default function useFile(uri: string | undefined) {
6+
/**
7+
*
8+
* @param uri The file URI to read/write
9+
* @param debounce Debounce time in ms for write operations
10+
* @returns
11+
*/
12+
export default function useFile(uri: string | undefined, debounce = 0) {
613
const [file, setFile] = useState<File | undefined>(undefined);
714

815
const receiverHandlerMap = new Map<
@@ -20,26 +27,32 @@ export default function useFile(uri: string | undefined) {
2027

2128
const { imc, isReady } = useIMC(receiverHandlerMap, "file");
2229

30+
const sendFileDebounced = useDebouncedCallback(
31+
async (newFile: File) => {
32+
if (!isReady || !uri) return;
33+
await imc?.sendMessage(IMCMessageTypeEnum.PlatformWriteFile, {
34+
uri,
35+
file: newFile,
36+
});
37+
},
38+
debounce,
39+
{ maxWait: debounce * 2 },
40+
);
41+
2342
const saveFile = useCallback(
24-
(fileContent: string) => {
25-
if (!uri) return;
26-
else if (!file) return;
43+
async (fileContent: string) => {
44+
if (!uri || !file) return;
2745

28-
// Update file content
2946
const newFile = new File([fileContent], file.name, {
3047
type: file.type,
3148
lastModified: Date.now(),
3249
});
3350
setFile(newFile);
3451

35-
if (isReady && uri) {
36-
imc?.sendMessage(IMCMessageTypeEnum.PlatformWriteFile, {
37-
uri,
38-
file: newFile,
39-
});
40-
}
52+
// ✅ This now waits until the debounced write actually finishes
53+
await sendFileDebounced(newFile);
4154
},
42-
[uri, file, isReady],
55+
[file, uri, sendFileDebounced],
4356
);
4457

4558
// Read file when uri changes

npm-packages/shared-utils/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @pulse-editor/shared-utils
22

3+
## 0.1.1-beta.57
4+
5+
### Patch Changes
6+
7+
- Add rebounce option for useFile hook's saveFile
8+
39
## 0.1.1-beta.56
410

511
### Patch Changes

npm-packages/shared-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pulse-editor/shared-utils",
3-
"version": "0.1.1-beta.56",
3+
"version": "0.1.1-beta.57",
44
"main": "dist/main.js",
55
"files": [
66
"dist"

package-lock.json

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)