feat: sync
This commit is contained in:
parent
70bf1d7d2b
commit
2c72a023e2
|
@ -14,11 +14,14 @@
|
|||
"files": [
|
||||
"index.js",
|
||||
"index.js.map",
|
||||
"server.js",
|
||||
"server.js.map",
|
||||
"test.js",
|
||||
"test.js.map"
|
||||
],
|
||||
"dependencies": {
|
||||
"@avocado/resource": "^2.0.0",
|
||||
"@latus/core": "^2.0.0",
|
||||
"@latus/react": "^2.0.0",
|
||||
"@persea/core": "^1.0.0",
|
||||
"autoprefixer": "^9.8.6",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import JsonResourceController from './resource-controllers/json';
|
||||
import {patchJsonResource} from './state/json';
|
||||
import reducer from './state/reducer';
|
||||
|
||||
export {default as useJsonPatcher} from './hooks/use-json-patcher';
|
||||
|
@ -8,6 +9,14 @@ export {JsonResourceController};
|
|||
|
||||
export default {
|
||||
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,
|
||||
'@persea/core/resource-controllers': () => [
|
||||
JsonResourceController,
|
||||
|
|
9
packages/json/src/server/index.js
Normal file
9
packages/json/src/server/index.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
import {decorateWithLatus} from '@latus/core';
|
||||
|
||||
export default {
|
||||
hooks: {
|
||||
'@latus/socket/packets.decorate': decorateWithLatus(
|
||||
require.context('./packets/decorators', false, /\.js$/),
|
||||
),
|
||||
},
|
||||
};
|
31
packages/json/src/server/packets/decorators/action.js
Normal file
31
packages/json/src/server/packets/decorators/action.js
Normal 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);
|
||||
}
|
||||
|
||||
};
|
Loading…
Reference in New Issue
Block a user