Don't dispatch create-event on startup

I have my plugin listen to the create-event. I noticed, that the create-event is fired on startup for the file, that Obsidian opens automatically, even if the note existed previously!

Steps to reproduce

  1. Create todays daily note
  2. Set Obsidian to open todays daily note on startup
  3. Add some event-listener to the create-event
  4. Restart Obsidian

Expected result

No create-event dispatched (because creation happened beforehand)

Actual result

create-event dispatched

Environment

OS: Arch Linux
Obsidian Version: v1.0.3

Let me know if I can help in any way. If this won’t/ can’t be fixed: Why does this behaviour exist in the first place? I’d argue that dispatching the create-event even if nothing is newly created is unintuitive.

1 Like

I have a similar problem

I created a plugin that keeps track of all the files I have in my vault, and I want to do changes to my plugin data when files are created/renamed/deleted, and when I do a “create” event - it triggers on every file in my vault every time I open Obsidian, where I expect it to trigger ONLY if I add a new file through drag and drop or creating a new note

It happens because in the startup Obsidian loads the files in the vault, and newly loaded files are treated as “created” at that time. It sounds strange but that’s how it works.

To prevent your create event handler from being invoked at the startup, put it inside Workspace.onLayoutReady, like so:

this.app.workspace.onLayoutReady(() => {
    this.registerEvent(
        this.app.vault.on('create', (file) => {
            // do some stuff
        })
    )
})