How to extend index from hooks?

Does any way exist to extend the index from appropriate hooks?

TL;DR

I am trying to extend index for pages with custom value. I dully hoped the following will work (intuitively):

```space-lua
event.listen {
  name = 'editor:pageSaved',
  run = function(e)
    e.testingVariable = e.lastModified
    print('Data', e.data)
    return e
  end
}
```

It seems the e is immutable or something because the print() call does not print the testingVariable at all.

You cannot extend page attributes, but you could create separate objects as part of the index process. The event to listen to is page:index.

You can then create new objects via the index.indexObjects API: API/index

OK, I will give it a try. But still, if the e in my example above is immutable, I should at least see some warning(s) in the console (which I don’t).

There isn’t really such a thing as immutable data in Lua

OOPS! My fault, there’s a typo in the code. :wink:

There isn’t really such a thing as immutable data in Lua

TL;DR

To explain more further… One can always play with table’s metamethods to gain poor man’s sort of “immutability” as some sort of “safety net” (if needed)…

function immutable(tbl)
  return setmetatable({}, {
    __index = tbl,
    __newindex = function(tbl, key, val)
      error('Cannot modify read-only table')
    end,
    __metatable = false
  })
end

local t = immutable { x = 1, y = 2, z = 3 } -- new "immutable" table `t`

It’s a nice trick but MUST NOT be considered as a “security feature” or something like that because the rawset() call can still be used to override this!

So, yes, there is simply no real immutability in Lua. :slight_smile: