feat: replaceResource

This commit is contained in:
cha0s 2022-03-20 14:17:02 -05:00
parent 8eda46cf4f
commit 5a44a0dfae
4 changed files with 23 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import {patchJsonResource} from '@avocado/resource-persea';
import {patchJsonResource, replaceResource} from '@avocado/resource-persea';
import {Hooks} from '@flecks/core';
import Persea from './components/persea';
@ -21,6 +21,9 @@ export default {
[patchJsonResource]: withSocket((store, action, socket) => {
socket.send(['Action', action]);
}),
[replaceResource]: withSocket((store, action, socket) => {
socket.send(['Action', action]);
}),
};
},
'@flecks/redux.slices': () => ({

View File

@ -11,6 +11,7 @@ export default {
[Hooks]: {
'@flecks/core.starting': (flecks) => {
flecks.set('$persea/core.patches', []);
flecks.set('$persea/core.replacements', []);
startFlush(flecks);
},
'@flecks/db/server.models': Flecks.provide(require.context('./models', false, /\.js$/)),

View File

@ -1,4 +1,4 @@
import {patchJsonResource} from '@avocado/resource-persea';
import {patchJsonResource, replaceResource} from '@avocado/resource-persea';
export default (Action, flecks) => class ProjectAction extends Action {
@ -9,6 +9,10 @@ export default (Action, flecks) => class ProjectAction extends Action {
flecks.get('$persea/core.patches').push(payload);
break;
}
case replaceResource.toString(): {
flecks.get('$persea/core.replacements').push(payload);
break;
}
default:
}
return super.respond(packet, socket);

View File

@ -42,6 +42,19 @@ const startFlush = (flecks) => {
setTimeout(flushPatches, 0);
};
setTimeout(flushPatches, 0);
const flushReplacements = async () => {
const replacements = flecks.get('$persea/core.replacements');
if (replacements.length > 0) {
while (replacements.length > 0) {
const {value, project, uri} = replacements.shift();
const path = join(process.cwd(), 'projects', project, uri);
// eslint-disable-next-line no-await-in-loop
await writeFile(path, value);
}
}
setTimeout(flushReplacements, 0);
};
setTimeout(flushReplacements, 0);
};
export default startFlush;