Skip to content

Commit e5bd961

Browse files
committed
Make setters async again
1 parent 5fd5bda commit e5bd961

File tree

1 file changed

+59
-65
lines changed

1 file changed

+59
-65
lines changed

src/worker.js

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -296,35 +296,32 @@ Worker.prototype.save = function save(filename) {
296296
/* ----- SET / GET ----- */
297297

298298
Worker.prototype.set = function set(opt) {
299-
// Set properties immediately.
300-
// NOTE: Don't need to worry about race conditions (with .then) because
301-
// each promise is unique. So anything set here will override the
302-
// prototype, even if an earlier .then resolves after this.
299+
// Set properties within the promise chain.
303300
// TODO: Test null/undefined input to this function.
304301
// TODO: Implement ordered pairs?
305-
for (var key in opt) {
306-
if (key in Worker.template.prop) {
307-
// Set pre-defined properties.
308-
this.prop[key] = opt[key];
309-
} else {
310-
switch (key) {
311-
case 'margin':
312-
this.setMargin(opt.margin);
313-
break;
314-
case 'jsPDF':
315-
this.opt.jsPDF = opt.jsPDF;
316-
case 'pageSize':
317-
this.setPageSize(opt.pageSize);
318-
break;
319-
default:
320-
// Set any other properties in opt.
321-
this.opt[key] = opt[key];
302+
return this.then(async function set_main() {
303+
for (var key in opt) {
304+
if (key in Worker.template.prop) {
305+
// Set pre-defined properties.
306+
this.prop[key] = opt[key];
307+
} else {
308+
switch (key) {
309+
case 'margin':
310+
await this.setMargin(opt.margin);
311+
break;
312+
case 'jsPDF':
313+
// Include jsPDF here because it must also update pageSize.
314+
this.opt.jsPDF = opt.jsPDF;
315+
case 'pageSize':
316+
await this.setPageSize(opt.pageSize);
317+
break;
318+
default:
319+
// Set any other properties in opt.
320+
this.opt[key] = opt[key];
321+
}
322322
}
323323
}
324-
}
325-
326-
// Return this for command chaining.
327-
return this;
324+
});
328325
};
329326

330327
Worker.prototype.get = function get(key, cbk) {
@@ -336,55 +333,52 @@ Worker.prototype.get = function get(key, cbk) {
336333
};
337334

338335
Worker.prototype.setMargin = function setMargin(margin) {
339-
// Parse the margin property.
340-
switch (objType(margin)) {
341-
case 'number':
342-
margin = [margin, margin, margin, margin];
343-
case 'array':
344-
if (margin.length === 2) {
345-
margin = [margin[0], margin[1], margin[0], margin[1]];
346-
}
347-
if (margin.length === 4) {
348-
break;
349-
}
350-
default:
351-
return this.error('Invalid margin array.');
352-
}
353-
this.opt.margin = margin;
354-
355-
// Update pageSize with the new margin.
356-
this.setPageSize();
336+
return this.then(function setMargin_main() {
337+
// Parse the margin property.
338+
switch (objType(margin)) {
339+
case 'number':
340+
margin = [margin, margin, margin, margin];
341+
case 'array':
342+
if (margin.length === 2) {
343+
margin = [margin[0], margin[1], margin[0], margin[1]];
344+
}
345+
if (margin.length === 4) {
346+
break;
347+
}
348+
default:
349+
return this.error('Invalid margin array.');
350+
}
357351

358-
// Return this for command chaining.
359-
return this;
352+
// Set the margin property, then update pageSize.
353+
this.opt.margin = margin;
354+
}).then(this.setPageSize);
360355
}
361356

362357
Worker.prototype.setPageSize = function setPageSize(pageSize) {
363358
function toPx(val, k) {
364359
return Math.floor(val * k / 72 * 96);
365360
}
366361

367-
// Retrieve page-size based on jsPDF settings, if not explicitly provided.
368-
pageSize = pageSize || jsPDF.getPageSize(this.opt.jsPDF);
369-
370-
// Add 'inner' field if not present.
371-
if (!pageSize.hasOwnProperty('inner')) {
372-
pageSize.inner = {
373-
width: pageSize.width - this.opt.margin[1] - this.opt.margin[3],
374-
height: pageSize.height - this.opt.margin[0] - this.opt.margin[2]
375-
};
376-
pageSize.inner.px = {
377-
width: toPx(pageSize.inner.width, pageSize.k),
378-
height: toPx(pageSize.inner.height, pageSize.k)
379-
};
380-
pageSize.inner.ratio = pageSize.inner.height / pageSize.inner.width;
381-
}
382-
383-
// Attach pageSize to this.
384-
this.prop.pageSize = pageSize;
362+
return this.then(function setPageSize_main() {
363+
// Retrieve page-size based on jsPDF settings, if not explicitly provided.
364+
pageSize = pageSize || jsPDF.getPageSize(this.opt.jsPDF);
365+
366+
// Add 'inner' field if not present.
367+
if (!pageSize.hasOwnProperty('inner')) {
368+
pageSize.inner = {
369+
width: pageSize.width - this.opt.margin[1] - this.opt.margin[3],
370+
height: pageSize.height - this.opt.margin[0] - this.opt.margin[2]
371+
};
372+
pageSize.inner.px = {
373+
width: toPx(pageSize.inner.width, pageSize.k),
374+
height: toPx(pageSize.inner.height, pageSize.k)
375+
};
376+
pageSize.inner.ratio = pageSize.inner.height / pageSize.inner.width;
377+
}
385378

386-
// Return this for command chaining.
387-
return this;
379+
// Attach pageSize to this.
380+
this.prop.pageSize = pageSize;
381+
});
388382
}
389383

390384
Worker.prototype.setProgress = function setProgress(val, state, n, stack) {

0 commit comments

Comments
 (0)