mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
c17a1980a2
This simplifies stores to just be storage interaction, they no longer react to events. Controllers now call services and awaits the result from the call. When the service calls are returned the database is updated. This simplifies testing dramatically, cause you know that your state is updated when returned from a call, rather than hoping the store has picked up the event (which really was a command) and reacted to it. Events are still emitted from eventStore, so other parts of the app can react to events as they're being sent out. As part of the move to services, we now also emit an application-created event when we see a new client application. Fixes: #685 Fixes: #595
145 lines
4.3 KiB
JavaScript
145 lines
4.3 KiB
JavaScript
const { stateSchema } = require('./state-schema');
|
|
const {
|
|
FEATURE_IMPORT,
|
|
DROP_FEATURES,
|
|
STRATEGY_IMPORT,
|
|
DROP_STRATEGIES,
|
|
} = require('../event-type');
|
|
|
|
const {
|
|
readFile,
|
|
parseFile,
|
|
filterExisitng,
|
|
filterEqual,
|
|
} = require('./state-util');
|
|
|
|
class StateService {
|
|
constructor(stores, { getLogger }) {
|
|
this.eventStore = stores.eventStore;
|
|
this.toggleStore = stores.featureToggleStore;
|
|
this.strategyStore = stores.strategyStore;
|
|
this.logger = getLogger('services/state-service.js');
|
|
}
|
|
|
|
importFile({ file, dropBeforeImport, userName, keepExisting }) {
|
|
return readFile(file)
|
|
.then(data => parseFile(file, data))
|
|
.then(data =>
|
|
this.import({ data, userName, dropBeforeImport, keepExisting }),
|
|
);
|
|
}
|
|
|
|
async import({ data, userName, dropBeforeImport, keepExisting }) {
|
|
const importData = await stateSchema.validateAsync(data);
|
|
|
|
if (importData.features) {
|
|
await this.importFeatures({
|
|
features: data.features,
|
|
userName,
|
|
dropBeforeImport,
|
|
keepExisting,
|
|
});
|
|
}
|
|
|
|
if (importData.strategies) {
|
|
await this.importStrategies({
|
|
strategies: data.strategies,
|
|
userName,
|
|
dropBeforeImport,
|
|
keepExisting,
|
|
});
|
|
}
|
|
}
|
|
|
|
async importFeatures({
|
|
features,
|
|
userName,
|
|
dropBeforeImport,
|
|
keepExisting,
|
|
}) {
|
|
this.logger.info(`Importing ${features.length} feature toggles`);
|
|
const oldToggles = dropBeforeImport
|
|
? []
|
|
: await this.toggleStore.getFeatures();
|
|
|
|
if (dropBeforeImport) {
|
|
this.logger.info(`Dropping existing feature toggles`);
|
|
await this.toggleStore.dropFeatures();
|
|
await this.eventStore.store({
|
|
type: DROP_FEATURES,
|
|
createdBy: userName,
|
|
data: { name: 'all-features' },
|
|
});
|
|
}
|
|
|
|
await Promise.all(
|
|
features
|
|
.filter(filterExisitng(keepExisting, oldToggles))
|
|
.filter(filterEqual(oldToggles))
|
|
.map(feature =>
|
|
this.toggleStore.importFeature(feature).then(() =>
|
|
this.eventStore.store({
|
|
type: FEATURE_IMPORT,
|
|
createdBy: userName,
|
|
data: feature,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
async importStrategies({
|
|
strategies,
|
|
userName,
|
|
dropBeforeImport,
|
|
keepExisting,
|
|
}) {
|
|
this.logger.info(`Importing ${strategies.length} strategies`);
|
|
const oldStrategies = dropBeforeImport
|
|
? []
|
|
: await this.strategyStore.getStrategies();
|
|
|
|
if (dropBeforeImport) {
|
|
this.logger.info(`Dropping existing strategies`);
|
|
await this.strategyStore.dropStrategies();
|
|
await this.eventStore.store({
|
|
type: DROP_STRATEGIES,
|
|
createdBy: userName,
|
|
data: { name: 'all-strategies' },
|
|
});
|
|
}
|
|
|
|
await Promise.all(
|
|
strategies
|
|
.filter(filterExisitng(keepExisting, oldStrategies))
|
|
.filter(filterEqual(oldStrategies))
|
|
.map(strategy =>
|
|
this.strategyStore.importStrategy(strategy).then(() => {
|
|
this.eventStore.store({
|
|
type: STRATEGY_IMPORT,
|
|
createdBy: userName,
|
|
data: strategy,
|
|
});
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
async export({ includeFeatureToggles = true, includeStrategies = true }) {
|
|
return Promise.all([
|
|
includeFeatureToggles
|
|
? this.toggleStore.getFeatures()
|
|
: Promise.resolve(),
|
|
includeStrategies
|
|
? this.strategyStore.getEditableStrategies()
|
|
: Promise.resolve(),
|
|
]).then(([features, strategies]) => ({
|
|
version: 1,
|
|
features,
|
|
strategies,
|
|
}));
|
|
}
|
|
}
|
|
|
|
module.exports = StateService;
|