From 5a44a0dfae9dc4d08bbc715bc42480759239d295 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 20 Mar 2022 14:17:02 -0500 Subject: [PATCH] feat: replaceResource --- packages/core/src/index.js | 5 ++++- packages/core/src/server/index.js | 1 + .../core/src/server/packets/decorators/action.js | 6 +++++- packages/core/src/server/start-flush.js | 13 +++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/core/src/index.js b/packages/core/src/index.js index c35a636..412ba7d 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -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': () => ({ diff --git a/packages/core/src/server/index.js b/packages/core/src/server/index.js index 11928f9..4d60f0e 100644 --- a/packages/core/src/server/index.js +++ b/packages/core/src/server/index.js @@ -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$/)), diff --git a/packages/core/src/server/packets/decorators/action.js b/packages/core/src/server/packets/decorators/action.js index fb9988a..1cc5e20 100644 --- a/packages/core/src/server/packets/decorators/action.js +++ b/packages/core/src/server/packets/decorators/action.js @@ -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); diff --git a/packages/core/src/server/start-flush.js b/packages/core/src/server/start-flush.js index 936ee03..18322cf 100644 --- a/packages/core/src/server/start-flush.js +++ b/packages/core/src/server/start-flush.js @@ -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;