blob: 6e10a0703be92c5dfa7d0691b05a16b1efc709e3 [file] [log] [blame]
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +02001import {arrayBufferToBase64} from './common.js';
2import * as pb from './proto/main_pb.js';
3
4export const kWorkflowsDataKey = 'workflowsData';
5
6export default class WorkflowsStorage {
Adrià Vilanova Martínez0caffdd2022-10-17 19:40:35 +02007 static watch(callback, asProtobuf = false) {
8 // Function which will be called when the watcher is initialized and every
9 // time the workflows storage changes.
10 const callOnChanged = () => {
11 this.getAll(asProtobuf).then(workflows => callback(workflows));
12 };
13
14 chrome.storage.onChanged.addListener((changes, areaName) => {
15 if (areaName == 'local' && changes[kWorkflowsDataKey]) callOnChanged();
16 });
17
18 callOnChanged();
19 }
20
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020021 static getAll(asProtobuf = false) {
22 return new Promise(res => {
23 chrome.storage.local.get(kWorkflowsDataKey, items => {
24 const workflows = items[kWorkflowsDataKey];
25 if (!Array.isArray(workflows)) return res([]);
26 if (!asProtobuf) return res(workflows);
27
28 workflows.map(w => {
29 w.proto = pb.workflows.Workflow.deserializeBinary(w?.data);
30 delete w.data;
31 });
32 return res(workflows);
33 });
34 });
35 }
36
37 static get(uuid, asProtobuf = false) {
38 return this.getAll(asProtobuf).then(workflows => {
39 for (const w of workflows) {
40 if (w.uuid == uuid) return w;
41 }
42 return null;
43 });
44 }
45
46 static exists(uuid) {
47 return this.get(uuid).then(w => w !== null);
48 }
49
50 static addRaw(base64Workflow) {
51 const w = {
52 uuid: self.crypto.randomUUID(),
53 data: base64Workflow,
54 };
55 return this.getAll().then(workflows => {
56 workflows.push(w);
57 const items = {};
58 items[kWorkflowsDataKey] = workflows;
59 chrome.storage.local.set(items);
60 });
61 }
62
63 static add(workflow) {
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020064 return this._proto2Base64(workflow).then(data => {
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020065 return this.addRaw(data);
66 });
67 }
68
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020069 static updateRaw(uuid, base64Workflow) {
70 return this.getAll().then(workflows => {
71 workflows.map(w => {
72 if (w.uuid !== uuid) return w;
73 w.data = base64Workflow;
74 return w;
75 });
76 const items = {};
77 items[kWorkflowsDataKey] = workflows;
78 chrome.storage.local.set(items);
79 });
80 }
81
82 static update(uuid, workflow) {
83 return this._proto2Base64(workflow).then(data => {
84 return this.updateRaw(uuid, data);
85 });
86 }
87
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020088 static remove(uuid) {
89 return this.getAll().then(workflows => {
90 const items = {};
91 items[kWorkflowsDataKey] = workflows.filter(w => w.uuid != uuid);
92 chrome.storage.local.set(items);
93 });
94 }
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020095
96 static _proto2Base64(workflow) {
97 const binaryWorkflow = workflow.serializeBinary();
98 return arrayBufferToBase64(binaryWorkflow);
99 }
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +0200100}