[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Cycler ... cycle.js revisited Re-factoring and enhancing an old stand-by

Simon Blackwell

3/1/2016 5:23:00 PM

AnyWhichWay has published a refactoring and enhancement of cycle.js for managing circular JSON structures.

Cycler enhances cycle.js by:

- the utilization of newer JavaScript constructs such as Map,
- ensuring objects are restored as instances of their original kind rather than just POJOs
- providing support for proper handling of classes derived from Array
- distinct packaging for NodeJS and the browser including pre-minified code
the addition of unit tests

https://github.com/anywhich...
10 Answers

Scott Sauyet

3/2/2016 3:03:00 AM

0

Simon Blackwell wrote:
> AnyWhichWay has published a refactoring and enhancement of cycle.js for
> managing circular JSON structures.
> [ ... ]
> https://github.com/anywhich...

So no one else needs to look, I traced down the lineage under discussion here,
and found that this is a refactoring of something called `cycle` [1], which
seems to be a variant of part of Douglas Crockford's JSON implementation [2]
based on JSONPath [3].

Disappointing to me was that this has nothing to do with Cycle.js, [4] the
first web framework in years to actually look interesting to me.

I did not investigate code quality.


[1]: <https://github.com/dscape...
[2]: <https://github.com/douglascrockford/J...
[3]: <http://goeshttp://goessner.net/articles/Jso...
[4]: <http://cycle.j...

-- Scott

Simon Blackwell

3/2/2016 1:42:00 PM

0

On Tuesday, March 1, 2016 at 10:03:12 PM UTC-5, Scott Sauyet wrote:
> Simon Blackwell wrote:
> > AnyWhichWay has published a refactoring and enhancement of cycle.js for
> > managing circular JSON structures.
> > [ ... ]
> > https://github.com/anywhich...
>
> So no one else needs to look, I traced down the lineage under discussion here,
> and found that this is a refactoring of something called `cycle` [1], which
> seems to be a variant of part of Douglas Crockford's JSON implementation [2]
> based on JSONPath [3].
>
> Disappointing to me was that this has nothing to do with Cycle.js, [4] the
> first web framework in years to actually look interesting to me.
>
> I did not investigate code quality.
>
>
> [1]: <https://github.com/dscape...
> [2]: <https://github.com/douglascrockford/J...
> [3]: <http://goeshttp://goessner.net/articles/Jso...
> [4]: <http://cycle.j...
>
> -- Scott

Indeed Cycle.js.org provides a very interesting framework, although very new rather than an "old standby" and far broader in scope than the old cycle.js for decycling JSON. I will make a separate post regarding cycle.js.org since I think it brings up some interesting questions.

Michael Haufe (\"TNO\")

3/3/2016 1:33:00 AM

0

On Tuesday, March 1, 2016 at 9:03:12 PM UTC-6, Scott Sauyet wrote:

> Disappointing to me was that this has nothing to do with Cycle.js, [4] the
> first web framework in years to actually look interesting to me.

You might be interested in this (apparently) abandoned data format:

<http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/slide1...

Combine with another abandoned concept from JavaScript 1.8.4:

<https://developer.mozilla.org/en-US/docs/Archive/Web/Sharp_variables_in_Java...

I made a token attempt to get this into the language proper, but it didn't go anywhere sadly:

<https://mail.mozilla.org/pipermail/es-discuss/2011-May/01474...

I'll ping him again to see if there is any possibility of reviving this.

Michael Haufe (\"TNO\")

3/3/2016 1:37:00 AM

0

On Wednesday, March 2, 2016 at 7:32:45 PM UTC-6, Michael Haufe (TNO) wrote:

> I'll ping him again to see if there is any possibility of reviving this.

nope...

<https://twitter.com/BrendanEich/status/705204428878...

"@mlhaufe Dead and not even zombiesign."

Scott Sauyet

3/3/2016 3:26:00 AM

0

Michael Haufe (TNO) wrote:
> Scott Sauyet wrote:
>
>> Disappointing to me was that this has nothing to do with Cycle.js, [4] the
>> first web framework in years to actually look interesting to me.
>
> You might be interested in this (apparently) abandoned data format:
>
> <http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/slide1...

It could be interesting. Cycle.js takes its name from the fact that it
deals with sources of and sinks for data, connecting them into cycles.
It is not related to the OP's original point.


> Combine with another abandoned concept from JavaScript 1.8.4:
>
> <https://developer.mozilla.org/en-US/docs/Archive/Web/Sharp_variables_in_Java...

I do remember these, and I remember thinking they might be useful if
they caught on, but I never did any pure Mozilla development in which
I might have taken advantage of them.

Perhaps it's too bad they didn't make it, but I don't feel their
loss particularly strongly.

-- Scott

ram

3/3/2016 4:06:00 AM

0

Scott Sauyet <scott.sauyet@gmail.com> writes:
>> Combine with another abandoned concept from JavaScript 1.8.4:
>> <https://developer.mozilla.org/en-US/docs/Archive/Web/Sharp_variables_in_Java...

In C I can

#include <stdio.h>

int main( void )
{ struct s { char * val; struct s * next; } a = { "alpha", &a };
printf( "%s\n", a.val );
printf( "%s\n", a.next->val );
printf( "%s\n", a.next->next->val );
printf( "%s\n", a.next->next->next->val ); }

alpha
alpha
alpha
alpha

, but in JavaScript I can't

|< b ={ val : "alpha", next : b };
|X ReferenceError: b is not defined

. So this might be a point where C is more »permissive« than
JavaScript (while usually JavaScript tries to be more
permissive than C, e.g., automatic semicolon insertion and
some such).

In C, the scope of an identifier begins at the start of its
initializer, IIRC. Therefore, one can already use it in its
initializer.

Simon Blackwell

3/3/2016 12:49:00 PM

0

On Wednesday, March 2, 2016 at 8:32:45 PM UTC-5, Michael Haufe (TNO) wrote:
> On Tuesday, March 1, 2016 at 9:03:12 PM UTC-6, Scott Sauyet wrote:
>
> > Disappointing to me was that this has nothing to do with Cycle.js, [4] the
> > first web framework in years to actually look interesting to me.
>
> You might be interested in this (apparently) abandoned data format:
>
> <http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/slide1...
>
> Combine with another abandoned concept from JavaScript 1.8.4:
>
> <https://developer.mozilla.org/en-US/docs/Archive/Web/Sharp_variables_in_Java...
>
> I made a token attempt to get this into the language proper, but it didn't go anywhere sadly:
>
> <https://mail.mozilla.org/pipermail/es-discuss/2011-May/01474...
>
> I'll ping him again to see if there is any possibility of reviving this.

Thanks for the historical links!

Quite interesting, particularly http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/sl.... This seems like it would be relatively straightforward to support using $1, $2, $3, etc. in place of the &1, &2, &3, etc. references. However, it would loose the class restoration semantics incorporated into cycler.js.

We have mucked around a good bit with serialization and restoration semantics and cycle.js gave us the inspiration for the approach used in cycler.js. This is the most elegant and least intrusive method we have found to date. Is anyone aware of another method not tied to a specific database or schema definition mechanism?

Michael Haufe (\"TNO\")

3/3/2016 2:45:00 PM

0

On Thursday, March 3, 2016 at 6:48:41 AM UTC-6, Simon Blackwell wrote:

> Quite interesting, particularly http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/sl.... This seems like it would be relatively straightforward to support using $1, $2, $3, etc. in place of the &1, &2, &3, etc. references. However, it would loose the class restoration semantics incorporated into cycler.js.

Serialize to a string and deserialize it. You have to do that with JSON anyway.

> We have mucked around a good bit with serialization and restoration semantics and cycle.js gave us the inspiration for the approach used in cycler.js. This is the most elegant and least intrusive method we have found to date. Is anyone aware of another method not tied to a specific database or schema definition mechanism?

A database is irrelevant when it comes to (de)serialization. IRT schemas, that's up to you, they can be either intrinsic in the serialization (like XML+XSD), or extrinsic like JSON.

Another possible approach is to use YAML as it supports non-hierarchical data as well

Simon Blackwell

3/4/2016 6:20:00 PM

0

On Thursday, March 3, 2016 at 9:45:20 AM UTC-5, Michael Haufe (TNO) wrote:
> On Thursday, March 3, 2016 at 6:48:41 AM UTC-6, Simon Blackwell wrote:
>
> > Quite interesting, particularly http://wayback.archive.org/web/20150827182315/http://www.dcs.bbk.ac.uk/~ptw/teaching/ssd/sl.... This seems like it would be relatively straightforward to support using $1, $2, $3, etc. in place of the &1, &2, &3, etc. references. However, it would loose the class restoration semantics incorporated into cycler.js.
>
> Serialize to a string and deserialize it. You have to do that with JSON anyway.
>
> > We have mucked around a good bit with serialization and restoration semantics and cycle.js gave us the inspiration for the approach used in cycler..js. This is the most elegant and least intrusive method we have found to date. Is anyone aware of another method not tied to a specific database or schema definition mechanism?
>
> A database is irrelevant when it comes to (de)serialization. IRT schemas, that's up to you, they can be either intrinsic in the serialization (like XML+XSD), or extrinsic like JSON.
>
> Another possible approach is to use YAML as it supports non-hierarchical data as well

I suppose I was not clear in my request. A shortcoming with JSON is that natively it only supports de-serializing to Objects or Arrays. You have to provide a reviver function to get alternate behavior but JSON provides no native way of capturing the semantics required to revive objects into their original classes. And, if only standard serialized JSON is sent over to a server there is nothing to tell the server how to restore it with semantics. What we implemented in cycler.js is a light weight schemaless way of passing semantics. If the JavaScript class definitions are the same on the client and server, with cycler you are essentially done (the class defintions essentially act as schema). We are wondering about other light weight schemaless approaches. We explored adding a special metadata sub object to all serialized JSON objects, it just seemed a little less clean than what we ended up with in cycler.js since there was then a tendency to start "polluting" the metadata sub-object with all kinds of potentially application specific stuff, e.g. version numbers, date/time stamps, etc.

Scott Sauyet

3/5/2016 3:18:00 AM

0

Simon Blackwell wrote:
> I suppose I was not clear in my request. A shortcoming with JSON is that
> natively it only supports de-serializing to Objects or Arrays. You have to
> provide a reviver function to get alternate behavior but JSON provides no
> native way of capturing the semantics required to revive objects into their
> original classes. And, if only standard serialized JSON is sent over to a
> server there is nothing to tell the server how to restore it with semantics.

Ok, this makes much more sense than what you originally posted. On USENET
it is traditional to give enough information in the post that a user does
not have to visit external resources to research the topic. URLs are fine,
but the content posted should stand by itself. This post gives enough
information that I was interested in checking out the library.


> What we implemented in cycler.js is a light weight schemaless way of passing
> semantics. If the JavaScript class definitions are the same on the client
> and server, with cycler you are essentially done (the class defintions
> essentially act as schema).

I'm afraid I'm not particularly impressed. There is mention in the
documentation of Douglas Crockford, and if I'm not mistaken, he is
currently suggesting a form of OOP in Javascript that would not work
with your tool.

I don't do a great deal of OOP Javascript myself, but when I do, I
also prefer a similar style, one which chooses strong encapsulation
over shared prototype methods. It's very useful for dealing with
immutable structures. Here is is a simple immutable point constructor
function:

var Point = function(_x, _y) {
this.x = function() {return _x;};
this.y = function() {return _y;};
this.distance = function(point) {
var dx = point.x() - _x;
var dy = point.y() - _y;
return Math.sqrt(dx * dx + dy * dy);
};
this.move = function(dx, dy) {
return new Point(_x + dx, _y + dy);
};
this.toString = function() {
return 'Point(' + _x + ', ' + _y + ')';
}
};

It would be used like this:

var p0 = new Point(1, 6);
p0.toString(); //=> Point(1, 6)
var p1 = p0.move(3, 4);
p1.toString(); //=> Point(4, 10)
p0.distance(p1); //=> 5

But when I use Cycle and simulate passing this over the wire (via
JSON methods), I get this behavior:

var points = Cycler.retrocycle(JSON.parse(
JSON.stringify(Cycler.decycle([p1, p2]))
));
points[0].toString(); //=> "[object Object]"
points[0].constructor; //=> function Point()
points[0].x(); //=> Type Error: points[0].x is not a function


This did not surprise me. I would have been quite surprised in fact
if you'd managed to make this work, as the internal data of a Point
is quite well encapsulated. There should be no way for your code to
access that closure. The advantage of the reviver paradigm in JSON,
as obnoxious as it is, is that it forces the user to consider this
up front in working with data that actually needs to be serialized.

What did surprise me, though, is that Cycle seems to have a problem
even with basic arrays:

points instanceof Array; //=> true

(so it says, but...)

points.map(function() {return "foo";}); //=> []

The data returned is not actually a proper array. This has nothing
to do with our Points:

var baz = Cycler.retrocycle(JSON.parse(
JSON.stringify(Cycler.decycle({name: 'a', vals: ['foo', 'bar']}))
));
baz.name; //=> 'a' // good!
baz.vals.map(function(val) {return val;}); //=> [] // uh oh!


> We are wondering about other light weight
> schemaless approaches. We explored adding a special metadata sub object to
> all serialized JSON objects, it just seemed a little less clean than what
> we ended up with in cycler.js since there was then a tendency to start
> "polluting" the metadata sub-object with all kinds of potentially application
> specific stuff, e.g. version numbers, date/time stamps, etc.

Although I would welcome one, I'd be surprised to find a useful generic
version of this concept. I think the difficulties are fundamental.