Skip to content

Commit

Permalink
fix: commit states to avoid memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Apr 24, 2023
1 parent cc11f8d commit 751000d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ module.exports = internals.State = class {

this.mainstay.restore();
}

commit() {

if (this.mainstay.shadow) {
this.mainstay.shadow.override(this.path, this._snapshot);
this._snapshot = undefined;
}

this.mainstay.commit();
}
};


Expand Down
2 changes: 2 additions & 0 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = Any.extend({
const result = item.schema.$_validate(value, localState, prefs);
if (!result.errors) {
matched.push(result.value);
localState.commit();
}
else {
failed.push(result.errors);
Expand Down Expand Up @@ -113,6 +114,7 @@ module.exports = Any.extend({

const result = item.schema.$_validate(value, localState, prefs);
if (!result.errors) {
localState.commit();
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ module.exports = Any.extend({
requiredChecks[j] = res;

if (!res.errors) {
localState.commit();
value[i] = res.value;
isValid = true;
internals.fastSplice(requireds, j);
Expand Down Expand Up @@ -272,6 +273,7 @@ module.exports = Any.extend({

res = inclusion.$_validate(item, localState, prefs);
if (!res.errors) {
localState.commit();
if (inclusion._flags.result === 'strip') {
internals.fastSplice(value, i);
--i;
Expand Down
5 changes: 5 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ internals.Mainstay = class {
this.externals = snapshot.externals;
this.warnings = snapshot.warnings;
}

commit() {

this._snapshots.pop();
}
};


Expand Down

0 comments on commit 751000d

Please sign in to comment.