Skip to content

Commit

Permalink
♻️ Use new OperationalContext-based classes for handling `Datasourc…
Browse files Browse the repository at this point in the history
…e` operations, getting rid of callbacks
  • Loading branch information
skerit committed Mar 22, 2024
1 parent d04e114 commit 5adfdb4
Show file tree
Hide file tree
Showing 29 changed files with 1,674 additions and 1,090 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Make `Datasource#toApp()` work without a callback
* Print a warning when a stage takes over 10 seconds to finish
* Add `OperationalContext` class for keeping track of complex operations
* Use new `OperationalContext`-based classes for handling `Datasource` operations, getting rid of callbacks

## 1.4.0-alpha.3 (2024-02-25)

Expand Down
4 changes: 1 addition & 3 deletions lib/app/behaviour/publishable_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
* @since 0.1.0
* @version 0.3.0
*/
var Publish = Function.inherits('Alchemy.Behaviour', function PublishableBehaviour(model, options) {
PublishableBehaviour.super.call(this, model, options);
});
var Publish = Function.inherits('Alchemy.Behaviour', 'PublishableBehaviour');

/**
* Listen to attachments to schema's
Expand Down
55 changes: 35 additions & 20 deletions lib/app/behaviour/revision_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Revision.setStatic(function getRevisionModel(model) {
this.schema.remove('updated');

this.addField('record_id', 'ObjectId');
this.addField('revision', 'Number');
this.addField('revision', 'Integer');
this.addField('delta', 'Object');

if (Classes.Alchemy.Model.User) {
Expand Down Expand Up @@ -100,7 +100,7 @@ Revision.setStatic(function attached(schema, new_options) {
const context = schema.model_class;

// Add the revision number to the main model
context.addField('__r', 'Number', {
context.addField('__r', 'Integer', {
title: 'Revision',
});

Expand Down Expand Up @@ -217,30 +217,42 @@ Revision.setMethod(function beforeSave(record, options, creating) {
return;
}

let that = this,
next = this.wait('series');
let that = this;

// Find the original record
Model.get(this.model.model_name).findById(record.$pk, async function gotRecord(err, result) {
let pledge = new Swift();

if (result) {
// Find the original record
Model.get(this.model.model_name).findById(record.$pk, function gotRecord(err, result) {

// Get the original data
let ori = await that.model.convertRecordToDatasourceFormat(result);
if (err || !result) {
return pledge.resolve();
}

// Store the original data in a weakmap for later
revision_before.set(options, ori);
let conversion;

// Increase the revision count by 1
if (ori.__r) {
main.__r = ori.__r+1;
} else {
main.__r = 1;
}
try {
conversion = that.model.convertRecordToDatasourceFormat(result);
} catch (err) {
return pledge.reject(err);
}

next();
pledge.resolve(Swift.waterfall(
conversion,
ori => {
// Store the original data in a weakmap for later
revision_before.set(options, ori);

// Increase the revision count by 1
if (ori.__r) {
main.__r = ori.__r+1;
} else {
main.__r = 1;
}
}
));
});

return pledge;
});

/**
Expand All @@ -266,9 +278,10 @@ Revision.setMethod(function afterSave(record, options, created) {
}

let doc = this.model.createDocument(record);
let next = this.wait();
const that = this;

let pledge = new Swift();

// Find the complete saved item
Model.get(this.model.model_name).findByPk(doc.$pk, async function gotRecord(err, result) {

Expand Down Expand Up @@ -312,6 +325,8 @@ Revision.setMethod(function afterSave(record, options, created) {
}
}

next();
pledge.resolve();
});

return pledge;
});
16 changes: 8 additions & 8 deletions lib/app/behaviour/sluggable_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ Sluggable.setMethod(async function beforeSave(data, options, creating) {
has_new_value,
old_record,
new_value,
old_value,
next;
old_value;

// Get the actual record data
if (data[that.model.name]) {
Expand All @@ -170,9 +169,6 @@ Sluggable.setMethod(async function beforeSave(data, options, creating) {
has_new_value = true;
}

// Let other event callbacks wait for this one
next = this.wait('series');

if (!creating) {
old_record = await this.model.findById(data._id);

Expand All @@ -186,7 +182,7 @@ Sluggable.setMethod(async function beforeSave(data, options, creating) {
// and we're not explicitly setting a new slug value,
// then do nothing
if (!creating && old_value && !has_new_value) {
return next();
return;
}

let new_data = {};
Expand All @@ -198,19 +194,23 @@ Sluggable.setMethod(async function beforeSave(data, options, creating) {
Object.assign(new_data, data);
}

let pledge = new Swift();

// Try creating a new slug
that.createSlug(new_data, new_value, function createdSlug(err, result) {

if (err) {
return next(err);
return pledge.reject(err);
}

if (!Object.isEmpty(result)) {
data[that.target_field.name] = result;
}

next();
pledge.resolve();
});

return pledge;
});

/**
Expand Down
Loading

0 comments on commit 5adfdb4

Please sign in to comment.