feat: sync

This commit is contained in:
cha0s 2021-01-27 16:28:47 -06:00
parent 70bf1d7d2b
commit 2c72a023e2
4 changed files with 52 additions and 0 deletions

View File

@ -14,11 +14,14 @@
"files": [ "files": [
"index.js", "index.js",
"index.js.map", "index.js.map",
"server.js",
"server.js.map",
"test.js", "test.js",
"test.js.map" "test.js.map"
], ],
"dependencies": { "dependencies": {
"@avocado/resource": "^2.0.0", "@avocado/resource": "^2.0.0",
"@latus/core": "^2.0.0",
"@latus/react": "^2.0.0", "@latus/react": "^2.0.0",
"@persea/core": "^1.0.0", "@persea/core": "^1.0.0",
"autoprefixer": "^9.8.6", "autoprefixer": "^9.8.6",

View File

@ -1,4 +1,5 @@
import JsonResourceController from './resource-controllers/json'; import JsonResourceController from './resource-controllers/json';
import {patchJsonResource} from './state/json';
import reducer from './state/reducer'; import reducer from './state/reducer';
export {default as useJsonPatcher} from './hooks/use-json-patcher'; export {default as useJsonPatcher} from './hooks/use-json-patcher';
@ -8,6 +9,14 @@ export {JsonResourceController};
export default { export default {
hooks: { hooks: {
'@latus/redux/effects': (latus) => {
const withSocket = (fn) => (...args) => fn(...args.concat(latus.get('%socket')));
return {
[patchJsonResource]: withSocket((store, action, socket) => {
socket.send(['Action', action]);
}),
};
},
'@latus/redux/reducers': () => reducer, '@latus/redux/reducers': () => reducer,
'@persea/core/resource-controllers': () => [ '@persea/core/resource-controllers': () => [
JsonResourceController, JsonResourceController,

View File

@ -0,0 +1,9 @@
import {decorateWithLatus} from '@latus/core';
export default {
hooks: {
'@latus/socket/packets.decorate': decorateWithLatus(
require.context('./packets/decorators', false, /\.js$/),
),
},
};

View File

@ -0,0 +1,31 @@
import fs from 'fs';
import {join} from 'path';
import {promisify} from 'util';
import {applyPatch} from 'fast-json-patch';
import {patchJsonResource} from '../../../state/json';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
export default (Action) => class ProjectAction extends Action {
static async respond(packet, socket) {
const {data: {type, payload}} = packet;
switch (type) {
case patchJsonResource.toString(): {
const {patch, project, uri} = payload;
const path = join(process.cwd(), 'projects', project, uri);
const buffer = await readFile(path);
const json = JSON.parse(buffer.toString('utf8'));
applyPatch(json, patch);
writeFile(path, JSON.stringify(json, null, 2));
break;
}
default:
}
return super.respond(packet, socket);
}
};