dsh-plugin-inspiration-workbench 0.9.0 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/host/index.js CHANGED
@@ -31,7 +31,7 @@ function dataFilePath() {
31
31
  return join(home, 'inspiration-workbench-layout.json');
32
32
  }
33
33
 
34
- function createStore() {
34
+ export function createStore(filePath = dataFilePath()) {
35
35
  let layout = null;
36
36
  let updatedAt = 0;
37
37
  let rev = 0;
@@ -39,6 +39,8 @@ function createStore() {
39
39
  /** Named board snapshots: [{ id, name, savedAt, layout }] */
40
40
  let canvases = [];
41
41
  let activeCanvas = null;
42
+ let tasks = [];
43
+ let flowEvents = [];
42
44
  const listeners = new Set();
43
45
  let saveTimer = null;
44
46
  let loading = null;
@@ -48,15 +50,17 @@ function createStore() {
48
50
  if (!loading) {
49
51
  loading = (async () => {
50
52
  try {
51
- const text = await fs.readFile(dataFilePath(), 'utf8');
53
+ const text = await fs.readFile(filePath, 'utf8');
52
54
  const parsed = JSON.parse(text);
53
- if (parsed && typeof parsed === 'object' && parsed.layout) {
54
- layout = parsed.layout;
55
+ if (parsed && typeof parsed === 'object' && parsed.layout) layout = parsed.layout;
56
+ if (parsed && typeof parsed === 'object') {
55
57
  updatedAt = Number(parsed.updatedAt) || 0;
56
58
  rev = Number(parsed.rev) || 0;
57
59
  }
58
60
  if (parsed && Array.isArray(parsed.canvases)) canvases = parsed.canvases;
59
61
  if (parsed && typeof parsed.activeCanvas === 'string') activeCanvas = parsed.activeCanvas;
62
+ if (parsed && Array.isArray(parsed.tasks)) tasks = parsed.tasks.slice(-200);
63
+ if (parsed && Array.isArray(parsed.flowEvents)) flowEvents = parsed.flowEvents.slice(-500);
60
64
  } catch {
61
65
  /* first run, or the document is unreadable: start empty */
62
66
  }
@@ -89,15 +93,31 @@ function createStore() {
89
93
  return { rev, updatedAt };
90
94
  }
91
95
 
96
+ function taskState() {
97
+ return { tasks, flowEvents };
98
+ }
99
+
100
+ async function putTasks(nextTasks, nextFlowEvents, sourceClientId) {
101
+ await ensureLoaded();
102
+ tasks = Array.isArray(nextTasks) ? nextTasks.slice(-200) : [];
103
+ flowEvents = Array.isArray(nextFlowEvents) ? nextFlowEvents.slice(-500) : [];
104
+ clientId = typeof sourceClientId === 'string' ? sourceClientId : '';
105
+ updatedAt = Date.now();
106
+ rev += 1;
107
+ scheduleSave();
108
+ broadcast();
109
+ return { rev, updatedAt };
110
+ }
111
+
92
112
  function scheduleSave() {
93
113
  if (saveTimer) return;
94
114
  saveTimer = setTimeout(async () => {
95
115
  saveTimer = null;
96
116
  try {
97
- await fs.mkdir(join(dataFilePath(), '..'), { recursive: true });
117
+ await fs.mkdir(join(filePath, '..'), { recursive: true });
98
118
  await fs.writeFile(
99
- dataFilePath(),
100
- JSON.stringify({ rev, updatedAt, layout, canvases, activeCanvas }),
119
+ filePath,
120
+ JSON.stringify({ rev, updatedAt, layout, canvases, activeCanvas, tasks, flowEvents }),
101
121
  'utf8'
102
122
  );
103
123
  } catch {
@@ -136,6 +156,8 @@ function createStore() {
136
156
  active: () => activeCanvas,
137
157
  put,
138
158
  putCanvases,
159
+ taskState,
160
+ putTasks,
139
161
  subscribe(res) {
140
162
  listeners.add(res);
141
163
  return () => listeners.delete(res);
@@ -268,6 +290,46 @@ export function apply(ctx) {
268
290
  return;
269
291
  }
270
292
 
293
+ if (route === '/tasks' || route === '/tasks/') {
294
+ if (req.method === 'GET') {
295
+ res.setHeader('content-type', 'application/json');
296
+ res.setHeader('cache-control', 'no-store');
297
+ store
298
+ .ensureLoaded()
299
+ .then(() => {
300
+ const current = store.snapshot();
301
+ const state = store.taskState();
302
+ res.end(
303
+ JSON.stringify({
304
+ rev: current.rev,
305
+ updatedAt: current.updatedAt,
306
+ tasks: state.tasks,
307
+ flowEvents: state.flowEvents,
308
+ })
309
+ );
310
+ })
311
+ .catch(() => res.writeHead(500).end());
312
+ return;
313
+ }
314
+ if (req.method === 'PUT' || req.method === 'POST') {
315
+ readBody(req, MAX_BODY_BYTES)
316
+ .then(async (text) => {
317
+ const parsed = JSON.parse(text);
318
+ if (!parsed || !Array.isArray(parsed.tasks) || !Array.isArray(parsed.flowEvents)) {
319
+ res.writeHead(400).end('bad tasks');
320
+ return;
321
+ }
322
+ const result = await store.putTasks(parsed.tasks, parsed.flowEvents, parsed.clientId);
323
+ res.setHeader('content-type', 'application/json');
324
+ res.end(JSON.stringify(result));
325
+ })
326
+ .catch(() => res.writeHead(400).end('bad request'));
327
+ return;
328
+ }
329
+ res.writeHead(405, { allow: 'GET, PUT' }).end();
330
+ return;
331
+ }
332
+
271
333
  if (route === '/events' || route === '/events/') {
272
334
  if (req.method !== 'GET') {
273
335
  res.writeHead(405, { allow: 'GET' }).end();