When publishing a value with a circular reference, a 'cyclic object value' error is thrown
See original GitHub issueWhen publishing a value with a circular reference, a ‘cyclic object value’ error is thrown. This is because of the serialization that happens to fill topicCache
.
Code to reproduce:
const foo = {
id: "foo",
bar: ko.observable()
}
const bar = {
id: "bar",
foo: ko.observable()
}
ko.postbox.subscribe("foo", function() {
console.log("foo was published");
})
ko.postbox.publish("foo", foo); // console: foo was published
foo.bar(bar);
ko.postbox.publish("foo", foo); // console: foo was published
bar.foo(foo);
ko.postbox.publish("foo", foo); // TypeError: cyclic object value
Is this something that can be solved by the library, or is it something I need to solve in my code?
Issue Analytics
- State:
- Created 7 years ago
- Comments:14
Top Results From Across the Web
TypeError: cyclic object value - JavaScript - MDN Web Docs
The JavaScript exception "cyclic object value" occurs when object references were found in JSON. JSON.stringify() doesn't try to solve them ...
Read more >Serializing object that contains cyclic object value
Both work by first "decycling" the object, i.e., constructing another object (without any cyclic references) "containing the same ...
Read more >JavaScript TypeError - Cyclic object value - GeeksforGeeks
This JavaScript exception cyclic object value occurs if the references of objects were found in JSON. JSON.stringify() fails to solve them.
Read more >TypeError: cyclic object value
To serialize circular references you can use a library that supports them (e.g. cycle.js) or implement a solution by yourself, which will ...
Read more >Let's play around with the Deep Copy | Frontend Canteen |
It throws an error again. We call that error “cyclic object value” or “circular reference”. It will make an infinity recursion.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m currently using this workaround:
I’m not using
subscribeTo(...)
, orsubscribe(...)
withinitializeWithLatestValue
set totrue
, so I don’t think I’m breaking anything.Well eventually you might hit a point where you do need to serialize the object which includes a circular reference, and removing the serializer won’t help in that situation.
If that’s the case, you may want to look into Douglas Crockford’s cycle.js: https://github.com/douglascrockford/JSON-js
For KO postbox, you could easily create a wrapper around cycle.js and use it to serialize and deserialize your objects containing circular references.