mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 21:33:24 +03:00
Updated collections event initialization
refs https://github.com/TryGhost/Team/issues/3169 - Ghost model events should not be crossing emitted to outside libraries. The wrapper code is a "glue code" that can still listen to model events but transforms them to specific format that is useful for the collections module - This hooks up the Ghost model events and transforms them into a "CollectionEvent" format.
This commit is contained in:
parent
49d831d971
commit
2eb7f7dd25
@ -1,6 +1,7 @@
|
|||||||
const {
|
const {
|
||||||
CollectionsService,
|
CollectionsService,
|
||||||
CollectionsRepositoryInMemory
|
CollectionsRepositoryInMemory,
|
||||||
|
CollectionResourceChangeEvent
|
||||||
} = require('@tryghost/collections');
|
} = require('@tryghost/collections');
|
||||||
const labs = require('../../../shared/labs');
|
const labs = require('../../../shared/labs');
|
||||||
|
|
||||||
@ -24,6 +25,9 @@ class CollectionsServiceWrapper {
|
|||||||
if (!labs.isSet('collections')) {
|
if (!labs.isSet('collections')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const events = require('../../lib/common/events');
|
||||||
|
|
||||||
const existingBuiltins = await this.api.getAll({filter: 'slug:featured'});
|
const existingBuiltins = await this.api.getAll({filter: 'slug:featured'});
|
||||||
|
|
||||||
if (!existingBuiltins.data.length) {
|
if (!existingBuiltins.data.length) {
|
||||||
@ -46,16 +50,23 @@ class CollectionsServiceWrapper {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const events = require('../../lib/common/events');
|
const ghostModelUpdateEvents = require('./update-events');
|
||||||
// @NOTE: these should be reworked to use the "Event" classes
|
|
||||||
// instead of Bookshelf model events
|
|
||||||
const updateEvents = require('./update-events');
|
|
||||||
|
|
||||||
// @NOTE: naive update implementation to keep things simple for the first version
|
const collectionListener = (event, data) => {
|
||||||
for (const event of updateEvents) {
|
const change = Object.assign({}, {
|
||||||
events.on(event, () => {
|
id: data.id,
|
||||||
this.api.updateAutomaticCollections();
|
resource: event.split('.')[0]
|
||||||
});
|
}, data._changed);
|
||||||
|
const collectionResourceChangeEvent = CollectionResourceChangeEvent.create(event, change);
|
||||||
|
// @NOTE: to avoid race conditions we need a queue here to make sure updates happen
|
||||||
|
// one by one and not in parallel
|
||||||
|
this.api.updateCollections(collectionResourceChangeEvent);
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const event of ghostModelUpdateEvents) {
|
||||||
|
if (!events.hasRegisteredListener(event, 'collectionListener')) {
|
||||||
|
events.on(event, data => collectionListener(event, data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user