Help with onWillSave


#1

Hi guys,

I am trying to use buffer.onWillSave(), from the atom documentation (https://atom.io/docs/api/v1.24.0/TextBuffer#instance-onWillSave) to try and force atom to carry out a function first, and then save.

The description says that the function will wait for any promises to be resolved before the Editor saves. However, in my case, the Editor saves regardless of the pending status of the promise.

Is there a particular/correct way I should be implementing my promise for it to work with onWillSave()? It could very well be that the error is on my part. Can anyone give me an example of using Promises with onWillSave()?

Any help would be appreciated.
Thanks.


#2

What does your current code look like? I’m guessing you’ve forgotten / not realised it needs to be an argument to a disposables instance. For example,

const { CompositeDisposable } = require("atom");
this.disposables = new CompositeDisposable();
this.disposables.add(
  // add it here
)

#4

It is already an argument to a disposable instance.

@disposables.add editor.buffer.onWillSave (event) =>
myfunction() // this returns a promise

What I basically want to do is to make the editor wait a certain time before saving. A function that resolves in period defined by setTimout would be good enough. However, I can’t seem to get onWillSave to wait. Do you know of the proper syntax to use here?

I apologize if the question is too dumb, but I could not find any examples with properly using Promises with the Atom API.


#5

Here is a full working example that can be pasted into your init.js file.

const { CompositeDisposable } = require("atom");

let disposables = new CompositeDisposable();
let buffer = atom.workspace.getActiveTextEditor().getBuffer();

disposables.add(
  buffer.onWillSave(() => {
    return new Promise((resolve) => {
      console.log("waiting to save");
      setTimeout(() => { resolve(console.log("about to save")); }, 1000);
    });
  }),
  buffer.onDidSave(() => {
    console.log("saved");
  })
);

Edit: I don’t use CoffeeScript, so you’ll have to translate yourself if you need to. I guarantee the JS version works though, I was just testing it now.


#6

The above will only work for the active text editor caught by let buffer .... It’s a starting point though, and you can subscribe to the creation of new buffers and add the function that way.


#7

Thanks for the reply!

I tried it out, and this is similar to what I have been trying so far. However, for me, the sequence still comes out as follows when I look at the console when performing a save.

waiting to save
saved
about to save

I should add that this is not in my .init file, but in a package.


#8

But I also tried it out in the init.js file, and the result is the same.


#9

I can’t offer any further help then. Every time I save, using the script I pasted above verbatim, I got the sequence

  1. waiting to save

  2. A 1 second delay

  3. about to save

  4. saved

This was with Atom version 1.24.0

If you are using an older version of Atom, it should not be a problem (because the onWillSave method is evidently defined).


#10

Ok, thank you, this was very helpful in itself. I have no idea why it doesn’t work for me.

I will try running the script on a clean atom install. Perhaps some of my other packages are causing a problem.


#11

Here is an improved version of the same script (this one attaches itself to all open TextEditor instances)

const { CompositeDisposable } = require("atom");

let disposables = new CompositeDisposable();

disposables.add(
  atom.workspace.observeTextEditors((editor) => {
    let buffer = editor.getBuffer();
    disposables.add(
      buffer.onWillSave(() => {
        return new Promise((resolve) => {
          console.log("waiting to save");
          setTimeout(() => { resolve(console.log("about to save")) }, 1000);
        });
      }),
      buffer.onDidSave(() => {
        console.log("saved");
      })
    );
  })
);

#12

Well, my bad, it turns out the only problem was that I was using an older version of atom (1.20) instead of the current one, 1.24, as you were.

The onWillSave() function only added the Promise functionality from version 1.21.

Thanks a lot for your help, Aerijo! Is there anyway I can upvote or give points or something to your answer?


#13

Not that I know of. There is only the like button. I appreciate the gesture though.