SketchAPI icon indicating copy to clipboard operation
SketchAPI copied to clipboard

Document.save not retaining ID and firing callback immediately

Open christianklotz opened this issue 5 years ago • 5 comments

Summary

  1. document.save() doesn't change the ID of the document and document.save(path, {saveMode: Save}, callback) where path is the same path of the current document does change the ID of the document which is unexpected

  2. document.save(callback) doesn't change the document ID which is okay, but the callback fires immediately, not after the document is being saved

Also there is no way to call document.save() by passing a callback and preserve the document ID.

Steps to Reproduce

  1. Call document.save(path, {saveMode: Save}, callback)
  2. Call document.save(callback)

Expected Results

  1. Both functions should NOT change the document ID
  2. The callback should be called after the document has been saved

Actual Results

  1. First function does not change the document ID, second function changes the document ID
  2. Callback is fired right after the function has been called and before the document has been saved.

Additional Information

Sketch version: 55.1 macOS version: 10.14.5 Issue reported via: https://app.frontapp.com/open/msg_2qyurpd

christianklotz avatar Jun 14 '19 12:06 christianklotz

I don't really understand the steps to repro.

If I run this:

const sketch = require('sketch')

const document = new sketch.Document()

console.log(document.id)

document.save('~/Desktop/test.sketch', {saveMode: sketch.Document.SaveMode.Save}, () => {
  console.log(document.id)
  document.save(() => {
    console.log(document.id)
  })
})

it prints

'8931F8A1-CF7D-49B5-B37C-D4A2D8F47F99'
'42BD8A70-A5EB-4FC9-BAA1-F90EA8D104EF'
'42BD8A70-A5EB-4FC9-BAA1-F90EA8D104EF'
Script executed in 0.922959s

which is expected because the ID of an unsaved document is temporary and will be set (and fixed) when saving the document. Maybe that's the cause of the confusion here?

mathieudutour avatar Jun 19 '19 21:06 mathieudutour

Hey Mathieu,

All of the above described scenarios are done on an already existing document and not on a newly created document. It's my understanding that when saving a document with { saveMode: Save } the document ID should not change.

const sketch = require('sketch/dom')

const document = sketch.getSelectedDocument();
const documentPath = document.path;
console.log(document.id)

document.save(documentPath, {saveMode: sketch.Document.SaveMode.Save}, () => {
  console.log(document.id)
})
'08BFEB99-CE7A-4A54-9119-09D6D06D34CC'
'520FD332-D7E2-47B2-B18A-DA9BA4979137'

The above example should be equivalent to just calling document.save() but with a callback. It should not change the document ID when doing so.

document.save(() => {
    console.log(document.id)
  })
'08BFEB99-CE7A-4A54-9119-09D6D06D34CC'
'08BFEB99-CE7A-4A54-9119-09D6D06D34CC'

This time the document ID doesn't change which is expected BUT the callback is executed before the document has been saved.

dmstoykov avatar Jun 25 '19 07:06 dmstoykov

For 1. new sketch.Document() does create a new document and as said before, it’s ID is temporary before being saved.

For 2., I’ll check about the callback but it’s a separate issue

mathieudutour avatar Jun 25 '19 09:06 mathieudutour

Sorry about the miss-understanding about 1. I updated the code example above to reflect the description. I replaced new sketch.Document() with sketch.getSelectedDocument().

dmstoykov avatar Jun 25 '19 13:06 dmstoykov

Then I can't reproduce:

  • open an existing document, make sure its saved
  • run the script
  • the 2 same IDs are logged

mathieudutour avatar Jun 26 '19 09:06 mathieudutour