Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 1 | import {arrayBufferToBase64} from './common.js'; |
| 2 | import * as pb from './proto/main_pb.js'; |
| 3 | |
| 4 | export const kWorkflowsDataKey = 'workflowsData'; |
| 5 | |
| 6 | export default class WorkflowsStorage { |
Adrià Vilanova Martínez | 0caffdd | 2022-10-17 19:40:35 +0200 | [diff] [blame] | 7 | 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ínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 21 | 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ínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 28 | 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ínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 35 | this.convertRawListToProtobuf(workflows); |
Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 36 | 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ínez | 8803b6c | 2022-10-17 00:36:38 +0200 | [diff] [blame] | 68 | return this._proto2Base64(workflow).then(data => { |
Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 69 | return this.addRaw(data); |
| 70 | }); |
| 71 | } |
| 72 | |
Adrià Vilanova Martínez | 8803b6c | 2022-10-17 00:36:38 +0200 | [diff] [blame] | 73 | 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ínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 92 | 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ínez | 8803b6c | 2022-10-17 00:36:38 +0200 | [diff] [blame] | 99 | |
| 100 | static _proto2Base64(workflow) { |
| 101 | const binaryWorkflow = workflow.serializeBinary(); |
| 102 | return arrayBufferToBase64(binaryWorkflow); |
| 103 | } |
Adrià Vilanova Martínez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 104 | } |