Skip to content

Commit 22ae50b

Browse files
committed
v2.0
1 parent 26f0ed3 commit 22ae50b

5 files changed

Lines changed: 30 additions & 13 deletions

File tree

README.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ <h3 id="reviver"><em>Reviver</em></h3>
437437
or <a href="#createdatacontextdata-propertyname-parent"><code>createDataContext</code></a> or <code>null</code></p>
438438
<h2 id="license">License</h2>
439439
<p>This project is licensed under the MIT License.</p>
440-
<p>Copyright © 2024 Manuel Lõhmus</p>
440+
<p>Copyright © Manuel Lõhmus</p>
441441
<p><a href="https://www.paypal.com/donate?hosted_button_id=TMZURGNWWYUBW"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Donate" /></a></p>
442442
<p>Donations are welcome and will go towards further development of this project.</p>
443443
<br>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ or [`createDataContext`](#createdatacontextdata-propertyname-parent) or `null`
423423

424424
This project is licensed under the MIT License.
425425

426-
Copyright &copy; 2024 Manuel Lõhmus
426+
Copyright &copy; Manuel Lõhmus
427427

428428
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/donate?hosted_button_id=TMZURGNWWYUBW)
429429

index.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/** Copyright (c) Manuel Lõhmus (MIT License). */
32

43
"use strict";
@@ -211,7 +210,7 @@
211210
delete emitToParent.timeout;
212211

213212
_this.emit("-change", { eventName: "-change", target: _this });
214-
}, 0, this);
213+
}, 10, this);
215214
}
216215

217216
if ((eventName === '-' || eventName === '-change') &&
@@ -469,10 +468,10 @@
469468
}
470469

471470
target.emitToParent("-", { eventName, target, propertyPath: [property], oldValue, newValue });
471+
target.emit(property, { eventName, target, propertyPath: [property], oldValue, newValue });
472472

473473
setModified(target, property);
474474

475-
target.emit(property, { eventName, target, propertyPath: [property], oldValue, newValue });
476475
target.emitToParent("-change", { eventName: "-change", target });
477476

478477
return ret;
@@ -674,12 +673,21 @@
674673

675674
if (it.current === '\r' && it.following === '\r') {
676675

677-
if (!def._isDataContext) { def = createDataContext(def); }
676+
if (!def._isDataContext) {
677+
678+
def = createDataContext(def);
679+
def._isModified = true;
680+
}
681+
if (_typeof(val) === 'array') {
682+
683+
val.length = 0;
684+
}
678685
if (val?._events) { def._events = val._events; }
679686
if (val?._propertyName && val._parent) {
680687

681688
def._parent = val._parent;
682689
def._propertyName = val._propertyName;
690+
val._parent[val._propertyName] = def;
683691
}
684692

685693
it.next();
@@ -960,7 +968,7 @@
960968
obj[k] = v;
961969

962970
if (!v?._isDataContext && obj !== val && obj[k] === val?.[k]
963-
|| v?._isDataContext && !v._modified.length) {
971+
|| v?._isDataContext && !v.isChanged) {
964972

965973
if (obj?._isDataContext) {
966974

@@ -1402,6 +1410,8 @@
14021410
? Object.values(value._modified).sort()
14031411
: Object.keys(value);
14041412

1413+
if (type === "array") { keys = keys.filter(function (k) { return Number.isInteger(Number(k)) }); }
1414+
14051415
// write emty object
14061416
if (!keys.length) {
14071417

@@ -1657,12 +1667,18 @@
16571667

16581668
var isInitData = !fs.existsSync(filePath),
16591669
isFileProcessing = false,
1660-
writeTimeout;
1670+
writeTimeout,
1671+
lastModifiedTime = Date.now();
16611672

16621673
if (!data) { data = createDataContext({}); }
16631674
if (!data._isDataContext) { data = createDataContext(data); }
16641675

1665-
fs.watchFile(filePath, (curr, prev) => { readFileSync(); });
1676+
fs.watchFile(filePath, (curr, prev) => {
1677+
1678+
if (lastModifiedTime > curr.mtimeMs) { return; }
1679+
1680+
readFileSync();
1681+
});
16661682

16671683
data.on('-change', (event) => {
16681684

@@ -1738,7 +1754,7 @@
17381754
writeTimeout = setTimeout(function () {
17391755

17401756
wait(write);
1741-
}, 500);
1757+
}, 5000);
17421758

17431759

17441760
function write() {
@@ -1755,6 +1771,7 @@
17551771
fs.writeFileSync(filePath, strJson, { encoding: 'utf8', flag: 'w', flush: true });
17561772

17571773
//data.resetChanges();
1774+
lastModifiedTime = Date.now();
17581775
isFileProcessing = false;
17591776

17601777
if (onDataChange) {

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.20",
4+
"version": "2.0.0-beta.21",
55
"license": "MIT",
66
"keywords": [
77
"on",

0 commit comments

Comments
 (0)