-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix placeholders not being replaced in certain circular circumstances
- Loading branch information
Showing
3 changed files
with
50 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,7 +233,7 @@ class RootValue { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.0.0 | ||
* @version 2.0.0 | ||
* @version 2.0.3 | ||
* | ||
* @param {Object} original_holder The object that holds the value | ||
* @param {String} key The key this value is held under | ||
|
@@ -254,7 +254,19 @@ class RootValue { | |
result = value; | ||
} else { | ||
result = this.createReviverValueWrapper(original_holder, key, value, parent); | ||
|
||
result = result.undriedValue(new_holder, key); | ||
|
||
if (result && result instanceof Placeholder) { | ||
if (result.done) { | ||
result = result.result; | ||
} else { | ||
result.waiters.push({ | ||
holder: new_holder, | ||
key : key, | ||
}); | ||
} | ||
} | ||
} | ||
} else { | ||
result = value; | ||
|
@@ -856,13 +868,30 @@ class RefValue extends Value { | |
} | ||
} | ||
|
||
/** | ||
* Represent a value that will be replaced later | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.0.3 | ||
* @version 2.0.3 | ||
*/ | ||
class Placeholder { | ||
constructor(holder, key) { | ||
this.holder = holder; | ||
this.key = key; | ||
this.result = undefined; | ||
this.done = false; | ||
this.waiters = []; | ||
} | ||
} | ||
|
||
/** | ||
* Represent a value that needs reviving using registered undriers | ||
* (Only used during undrying) | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.0.0 | ||
* @version 2.0.1 | ||
* @version 2.0.3 | ||
*/ | ||
class UndrierValue extends Value { | ||
|
||
|
@@ -874,9 +903,9 @@ class UndrierValue extends Value { | |
this.placeholders = []; | ||
} | ||
|
||
let placeholder = Symbol(); | ||
let placeholder = new Placeholder(holder, key); | ||
|
||
this.placeholders.push([holder, key, placeholder]); | ||
this.placeholders.push(placeholder); | ||
|
||
return placeholder; | ||
} | ||
|
@@ -895,16 +924,15 @@ class UndrierValue extends Value { | |
if (this.placeholders) { | ||
let placeholder, | ||
holder, | ||
entry, | ||
key; | ||
|
||
while (this.placeholders.length) { | ||
|
||
entry = this.placeholders.shift(); | ||
|
||
holder = entry[0]; | ||
key = entry[1]; | ||
placeholder = entry[2]; | ||
placeholder = this.placeholders.shift(); | ||
placeholder.done = true; | ||
placeholder.result = result; | ||
holder = placeholder.holder; | ||
key = placeholder.key; | ||
|
||
holder[key] = result; | ||
|
||
|
@@ -924,6 +952,13 @@ class UndrierValue extends Value { | |
walk(holder, fixer); | ||
} | ||
} | ||
|
||
if (placeholder.waiters.length) { | ||
while (placeholder.waiters.length) { | ||
let waiter = placeholder.waiters.shift(); | ||
waiter.holder[waiter.key] = result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "json-dry", | ||
"description": "Don't repeat yourself, JSON: Add support for (circular) references, class instances, ...", | ||
"version": "2.0.2", | ||
"version": "2.0.3-alpha", | ||
"author": "Jelle De Loecker <[email protected]>", | ||
"keywords": [ | ||
"json", | ||
|