Coder Social home page Coder Social logo

Comments (2)

adamhaile avatar adamhaile commented on June 1, 2024

Thanks for the report, and thanks @shimaore for the PRs!

Yeah, the example used to work, because it used to be the case that all data signals got a .toJSON() method so that they could be JSONized transparently. That was removed a version back, though, because it turned out that it caused a noticeable performance regression in some cases. It made a lot of codepaths polymorphic: sometimes they got a plain function, sometimes a function (a data signal) that had a toJSON() method added.

Instead, in my own code, I now explicitly specify signals that I want to be jsonable by using a little jsonable(<signal>) utility which adds the .toJSON() method just to those signals:

// add a .toJSON() to a signal so that it will serialize its value to JSON
export function jsonable<T extends () => any>(obs : T) : T {
    (obs as any).toJSON = toJSON;
    return obs;
}

function toJSON(this : () => any) {
    var json = this();
    // if we shadowed the value's own toJSON, call it now
    if (json && json.toJSON) json = json.toJSON();
    return json;
}

So, for instance, the old code would work if we did:

var Todo = t => ({               // our Todo constructor
       title: jsonable(S.data(t.title)),   // properties are data signals
       done: jsonable(S.data(t.done))
    }),
    todos = jsonable(SArray([])),          // our array of todos

Sorry I hadn't updated the README and for the trouble that gave you, but thanks for bringing it to my attention!

from s.

jlapointe avatar jlapointe commented on June 1, 2024

Ahh I see, thanks for the solution!

from s.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.