Coder Social home page Coder Social logo

Initialize Change Function about simulacra HOT 5 CLOSED

gr0uch avatar gr0uch commented on June 19, 2024
Initialize Change Function

from simulacra.

Comments (5)

jakesower avatar jakesower commented on June 19, 2024 1

I've tried to do something decidedly unusual. The behavior isn't exactly what I expected, but it's logical. I can pass in some other value that means null for purposes of my logic that simulacra won't think of as a null value. Essentially my expectation was that I could "hijack" the change function to override the default way that simulacra treats null.

Thanks for your time! I'll just formulate my code differently to skirt the issue.

from simulacra.

gr0uch avatar gr0uch commented on June 19, 2024

Quote from the readme:

A change function can be determined to be an insert, mutate, or remove operation based on whether the value or previous value is null:

Value but not previous value: insert operation.
Value and previous value: mutate operation.
No value: remove operation.

You probably don't want the else clause, because that could be either an insert or update. In this case, if you want to check for insertion, it would be:

if (previousValue == null) { ... }

Is calling it the change function misleading? I wasn't too sure about the name, maybe it could be called the mapping function or something, it's only called change internally and in the docs.

from simulacra.

jakesower avatar jakesower commented on June 19, 2024

I apologize for the large amount of time that's passed since I said anything. To answer your question right away, I did not find the name of the change function to be misleading.

I've written a test that shows the error more clearly: https://github.com/jakesower/simulacra/commit/8ad5faefa40392a0833e088b16eea73db477fcde

It appears to be due to having a bound variable set to null at initialization, rather than what I initially suspected (and what you addressed). It's something of an edge case, so I intend to put in a fix and submit it as a PR.

Please let me know if the test is invalid for some reason, otherwise I'll send you a PR that makes the test pass in the coming hours/days.

from simulacra.

gr0uch avatar gr0uch commented on June 19, 2024

I think the test case is mistaken here: https://github.com/jakesower/simulacra/commit/8ad5faefa40392a0833e088b16eea73db477fcde#diff-910eb6f57886ca16c136101fb1699231R202

Since the value of change is initialized to null, no element is rendered initially. Consider that in terms of rendering, these states are the same:

{ simple: 'Simple', change: null }
{ simple: 'Simple', change: undefined }
{ simple: 'Simple' }

null and undefined values are considered to be either not renderable, or an intent to remove the corresponding element. This means that the element is non-existent for null values.

In the example on the homepage you can try:

state.details = null

and it will remove the entire element corresponding to the value.

from simulacra.

gr0uch avatar gr0uch commented on June 19, 2024

That sounds perfectly fine to do actually. I will close this now.

from simulacra.

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.