blob: 3fc6f95fd88618ba56955d6f99fcc3da60aca569 [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ínez96ae96f2022-10-17 23:50:36 +020021 static convertRawListToProtobuf(workflows) {
22 workflows.forEach(w => {
23 w.proto = pb.workflows.Workflow.deserializeBinary(w?.data);
24 delete w.data;
25 });
26 }
27
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020028 static getAll(asProtobuf = false) {
29 return new Promise(res => {
30 chrome.storage.local.get(kWorkflowsDataKey, items => {
31 const workflows = items[kWorkflowsDataKey];
32 if (!Array.isArray(workflows)) return res([]);
33 if (!asProtobuf) return res(workflows);
34
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020035 this.convertRawListToProtobuf(workflows);
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020036 return res(workflows);
37 });
38 });
39 }
40
41 static get(uuid, asProtobuf = false) {
42 return this.getAll(asProtobuf).then(workflows => {
43 for (const w of workflows) {
44 if (w.uuid == uuid) return w;
45 }
46 return null;
47 });
48 }
49
50 static exists(uuid) {
51 return this.get(uuid).then(w => w !== null);
52 }
53
54 static addRaw(base64Workflow) {
55 const w = {
56 uuid: self.crypto.randomUUID(),
57 data: base64Workflow,
58 };
59 return this.getAll().then(workflows => {
60 workflows.push(w);
61 const items = {};
62 items[kWorkflowsDataKey] = workflows;
63 chrome.storage.local.set(items);
64 });
65 }
66
67 static add(workflow) {
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020068 return this._proto2Base64(workflow).then(data => {
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020069 return this.addRaw(data);
70 });
71 }
72
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020073 static updateRaw(uuid, base64Workflow) {
74 return this.getAll().then(workflows => {
75 workflows.map(w => {
76 if (w.uuid !== uuid) return w;
77 w.data = base64Workflow;
78 return w;
79 });
80 const items = {};
81 items[kWorkflowsDataKey] = workflows;
82 chrome.storage.local.set(items);
83 });
84 }
85
86 static update(uuid, workflow) {
87 return this._proto2Base64(workflow).then(data => {
88 return this.updateRaw(uuid, data);
89 });
90 }
91
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +020092 static remove(uuid) {
93 return this.getAll().then(workflows => {
94 const items = {};
95 items[kWorkflowsDataKey] = workflows.filter(w => w.uuid != uuid);
96 chrome.storage.local.set(items);
97 });
98 }
Adrià Vilanova Martínez8803b6c2022-10-17 00:36:38 +020099
100 static _proto2Base64(workflow) {
101 const binaryWorkflow = workflow.serializeBinary();
102 return arrayBufferToBase64(binaryWorkflow);
103 }
Adrià Vilanova Martínez7f1e8ea2022-10-14 15:50:11 +0200104}