| 1 |
define(['dojo/store/Memory', 'dojo/store/Cache', 'dojo/when', 'dojo/aspect', 'dojo/_base/lang'], |
| 2 |
function(Memory, Cache, when, aspect, lang){ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
var defaultTransactionLogStore; |
| 13 |
var stores = {}; |
| 14 |
var nextStoreId = 1; |
| 15 |
return function(options){ |
| 16 |
options = options || {}; |
| 17 |
var masterStore = options.masterStore; |
| 18 |
var cachingStore = options.cachingStore; |
| 19 |
var storeId = masterStore.id || masterStore.storeName || masterStore.name || (masterStore.id = nextStoreId++); |
| 20 |
if(storeId){ |
| 21 |
stores[storeId] = masterStore; |
| 22 |
} |
| 23 |
var transactionLogStore = options.transactionLogStore || defaultTransactionLogStore || |
| 24 |
(defaultTransactionLogStore = new Memory()); |
| 25 |
var autoCommit = true; |
| 26 |
function addToLog(method){ |
| 27 |
return function execute(target, options){ |
| 28 |
var transactionStore = this; |
| 29 |
if(autoCommit){ |
| 30 |
|
| 31 |
var result = masterStore[method](target, options); |
| 32 |
when(result, null, function(e){ |
| 33 |
if(transactionStore.errorHandler(e)){ |
| 34 |
|
| 35 |
autoCommit = false; |
| 36 |
options.error = e; |
| 37 |
execute.call(transactionStore, target, options); |
| 38 |
autoCommit = true; |
| 39 |
} |
| 40 |
}); |
| 41 |
return result; |
| 42 |
}else{ |
| 43 |
|
| 44 |
var previousId = method === 'remove' ? target : transactionStore.getIdentity(target); |
| 45 |
if(previousId !== undefined){ |
| 46 |
var previous = cachingStore.get(previousId); |
| 47 |
} |
| 48 |
return when(previous, function(previous){ |
| 49 |
return when(transactionLogStore.add({ |
| 50 |
objectId: previousId, |
| 51 |
method: method, |
| 52 |
target: target, |
| 53 |
previous: previous, |
| 54 |
options: options, |
| 55 |
storeId: storeId |
| 56 |
}), function(){ |
| 57 |
return target; |
| 58 |
}); |
| 59 |
}); |
| 60 |
} |
| 61 |
}; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
aspect.before(masterStore, 'notify', function(object, existingId){ |
| 66 |
if(object){ |
| 67 |
cachingStore.put(object); |
| 68 |
}else{ |
| 69 |
cachingStore.remove(existingId); |
| 70 |
} |
| 71 |
}); |
| 72 |
return new Cache(lang.delegate(masterStore, { |
| 73 |
put: addToLog('put'), |
| 74 |
add: addToLog('add'), |
| 75 |
remove: addToLog('remove'), |
| 76 |
errorHandler: function(error){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
console.error(error); |
| 82 |
return true; |
| 83 |
}, |
| 84 |
commit: function(){ |
| 85 |
|
| 86 |
autoCommit = true; |
| 87 |
var transactionStore = this; |
| 88 |
|
| 89 |
return transactionLogStore.query({}).map(function(action){ |
| 90 |
|
| 91 |
var method = action.method; |
| 92 |
var store = stores[action.storeId]; |
| 93 |
var target = action.target; |
| 94 |
var result; |
| 95 |
try{ |
| 96 |
|
| 97 |
result = store[method](target, action.options); |
| 98 |
}catch(e){ |
| 99 |
result = transactionStore.errorHandler(e); |
| 100 |
if(result === true){ |
| 101 |
|
| 102 |
return e; |
| 103 |
}else if(result === false){ |
| 104 |
|
| 105 |
if(method === 'add'){ |
| 106 |
cachingStore.remove(action.objectId); |
| 107 |
}else{ |
| 108 |
cachingStore.put(target); |
| 109 |
} |
| 110 |
store.notify && store.notify(method === 'add' ? null : action.previous, |
| 111 |
method === 'remove' ? undefined : action.objectId); |
| 112 |
} |
| 113 |
result = e; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
transactionLogStore.remove(action.id); |
| 118 |
return result; |
| 119 |
}); |
| 120 |
}, |
| 121 |
transaction: function(){ |
| 122 |
|
| 123 |
autoCommit = false; |
| 124 |
var transactionStore = this; |
| 125 |
return { |
| 126 |
commit: function(){ |
| 127 |
return transactionStore.commit(); |
| 128 |
} |
| 129 |
}; |
| 130 |
} |
| 131 |
}), cachingStore, options); |
| 132 |
}; |
| 133 |
}); |