services/sync/modules-testing/rotaryengine.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial