rotaryengine.sys.mjs (2902B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { 6 Store, 7 SyncEngine, 8 LegacyTracker, 9 } from "resource://services-sync/engines.sys.mjs"; 10 11 import { CryptoWrapper } from "resource://services-sync/record.sys.mjs"; 12 import { SerializableSet, Utils } from "resource://services-sync/util.sys.mjs"; 13 14 /* 15 * A fake engine implementation. 16 * This is used all over the place. 17 * 18 * Complete with record, store, and tracker implementations. 19 */ 20 21 export function RotaryRecord(collection, id) { 22 CryptoWrapper.call(this, collection, id); 23 } 24 25 RotaryRecord.prototype = {}; 26 Object.setPrototypeOf(RotaryRecord.prototype, CryptoWrapper.prototype); 27 Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]); 28 29 export function RotaryStore(name, engine) { 30 Store.call(this, name, engine); 31 this.items = {}; 32 } 33 34 RotaryStore.prototype = { 35 async create(record) { 36 this.items[record.id] = record.denomination; 37 }, 38 39 async remove(record) { 40 delete this.items[record.id]; 41 }, 42 43 async update(record) { 44 this.items[record.id] = record.denomination; 45 }, 46 47 async itemExists(id) { 48 return id in this.items; 49 }, 50 51 async createRecord(id, collection) { 52 let record = new RotaryRecord(collection, id); 53 54 if (!(id in this.items)) { 55 record.deleted = true; 56 return record; 57 } 58 59 record.denomination = this.items[id] || "Data for new record: " + id; 60 return record; 61 }, 62 63 async changeItemID(oldID, newID) { 64 if (oldID in this.items) { 65 this.items[newID] = this.items[oldID]; 66 } 67 68 delete this.items[oldID]; 69 }, 70 71 async getAllIDs() { 72 let ids = {}; 73 for (let id in this.items) { 74 ids[id] = true; 75 } 76 return ids; 77 }, 78 79 async wipe() { 80 this.items = {}; 81 }, 82 }; 83 84 Object.setPrototypeOf(RotaryStore.prototype, Store.prototype); 85 86 export function RotaryTracker(name, engine) { 87 LegacyTracker.call(this, name, engine); 88 } 89 90 RotaryTracker.prototype = {}; 91 Object.setPrototypeOf(RotaryTracker.prototype, LegacyTracker.prototype); 92 93 export function RotaryEngine(service) { 94 SyncEngine.call(this, "Rotary", service); 95 // Ensure that the engine starts with a clean slate. 96 this.toFetch = new SerializableSet(); 97 this.previousFailed = new SerializableSet(); 98 } 99 100 RotaryEngine.prototype = { 101 _storeObj: RotaryStore, 102 _trackerObj: RotaryTracker, 103 _recordObj: RotaryRecord, 104 105 async _findDupe(item) { 106 // This is a Special Value® used for testing proper reconciling on dupe 107 // detection. 108 if (item.id == "DUPE_INCOMING") { 109 return "DUPE_LOCAL"; 110 } 111 112 for (let [id, value] of Object.entries(this._store.items)) { 113 if (item.denomination == value) { 114 return id; 115 } 116 } 117 return null; 118 }, 119 }; 120 Object.setPrototypeOf(RotaryEngine.prototype, SyncEngine.prototype);