Skip to content

Commit 83df3bd

Browse files
committed
v2.0
1 parent 5762863 commit 83df3bd

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,7 @@
16211621
filePath = '',
16221622
data = null,
16231623
removeUnusedKeys = true,
1624+
allowFileReadWrite = true,
16241625
onDataChange = null,
16251626
onFileChange = null
16261627
} = {}) {
@@ -1634,6 +1635,7 @@
16341635

16351636
var isInitData = !fs.existsSync(filePath),
16361637
isFileProcessing = false,
1638+
isWrited = false,
16371639
writeTimeout,
16381640
lastModifiedTime = Date.now();
16391641

@@ -1642,6 +1644,7 @@
16421644

16431645
fs.watchFile(filePath, (curr, prev) => {
16441646

1647+
if (isWrited) { isWrited = false; return; }
16451648
if (lastModifiedTime > curr.mtimeMs) { return; }
16461649

16471650
readFileSync();
@@ -1650,6 +1653,7 @@
16501653
data.on('-change', (event) => {
16511654

16521655
writeFileSync();
1656+
isWrited = true;
16531657

16541658
// I am alive.
16551659
return true;
@@ -1658,6 +1662,13 @@
16581662
if (isInitData) { Promise.resolve().then(writeFileSync); }
16591663
else { readFileSync(); }
16601664

1665+
Object.defineProperty(data, '_allowFileReadWrite', {
1666+
get: () => allowFileReadWrite,
1667+
set: (val) => { allowFileReadWrite = Boolean(val); },
1668+
enumerable: false,
1669+
configurable: false
1670+
});
1671+
16611672
return data;
16621673

16631674

@@ -1677,7 +1688,7 @@
16771688
function readFileSync() {
16781689

16791690
if (!enableFileReadWrite || !fs.existsSync(filePath)) { return; }
1680-
1691+
16811692
var str = fs.readFileSync(
16821693
filePath,
16831694
{ encoding: 'utf8' }
@@ -1714,7 +1725,7 @@
17141725
}
17151726
function writeFileSync() {
17161727

1717-
if (!enableFileReadWrite) { return; }
1728+
if (!enableFileReadWrite || !allowFileReadWrite) { return; }
17181729

17191730
clearTimeout(writeTimeout);
17201731

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "data-context",
33
"description": "Watch data changes in the browser and node.js",
4-
"version": "2.0.0-beta.25",
4+
"version": "2.0.0-beta.26",
55
"license": "MIT",
66
"keywords": [
77
"on",

0 commit comments

Comments
 (0)