Maybe there’s some way of doing this, not sure. But if you’ve got some function that returns HTML, and use it like [fn()] in your HTML, then use update(), that function will be called again, right? There’s no way of blocking that, as far as I know–which could do things like reset state and such.

Not even sure how you’d implement such a feature. Maybe pass an argument to the function telling it it’s updating or something? Or mark the code as only-run-once somehow? [:code run once]? Not sure… Could be useful for certain plug-ins though to avoid users of the plugin accidentally messing it up by using update().

  • VioneT@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    1 month ago

    Most of the time, you can create a variable on the window object, then check that variable again if the content has already been added.

    yourFunction() =>
      if (!window.yourFunctionHasBeenRan) {
        // do things
        window.yourFunctionHasBeenRan = true
      }
      return ""
    
    • wthit56@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 month ago

      Ah, that’s true. You’d still have to return what was returned before though I guess. Which would be difficult because there’s no way of matching up a run to a re-run from the same exact source.

      • VioneT@lemmy.worldM
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 month ago

        You can also probably just set the value you want to retrieve again to a window variable as well, then just return window.savedValue instead of returning nothing.

        yourFunction() =>
          if (!window.yourFunctionHasBeenRan) {
            // do things
            window.savedValue = 'theValue'
            window.yourFunctionHasBeenRan = true
          }
          return window.savedValue
        
        • wthit56@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 month ago

          Right. Unless it’s the kind of plugin you have more than once in your HTML. Like some formatting thing or wrapper. Then you need to store every return from the function, and where it came from, and be able to cross reference the two–see what I mean?

          • VioneT@lemmy.worldM
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 month ago

            Yup, a good example of it is the tap-plugin, different outputs but is remembered by the page.

            • wthit56@lemmy.worldOP
              link
              fedilink
              English
              arrow-up
              1
              ·
              1 month ago

              Ooh, an interesting plugin! It doesn’t work in the way I’m talking about at all though.

              The function would need to be able to generate some return value when called. But always return that same return value for that source call every time, so that when update() is called on an element that contains code that calls the function… it’s as if that code block was not re-evaluated at all.

              That tap plugin specifically does not use update() at all. And specially does generate new values when it’s interacted with. I’m not sure how it’s relevant. Maybe you could point me to the part you think is doing what I’m talking about?

              • VioneT@lemmy.worldM
                link
                fedilink
                English
                arrow-up
                2
                ·
                1 month ago

                Maybe the locker-plugin might be a better example. It essentially stores the ‘locked’ value, so when update() is used, it would only return the ‘locked’ value.

                • wthit56@lemmy.worldOP
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  1 month ago

                  Ah I see, that seems to work about as expected. Only annoyance is, the user of the plugin has to come up with a unique name and track those, make sure they don’t use the same name twice or whatever.

                  This method would work, which is cool. I still feel like it would be a useful feature for the engine anyway. If only to save on processing those code blocks that the user only wants to “run once.”