Debian Codegraph @

dojox/store/transaction.js

raw symbols SCIP
1 define(['dojo/store/Memory', 'dojo/store/Cache', 'dojo/when', 'dojo/aspect', 'dojo/_base/lang'],
2 function(Memory, Cache, when, aspect, lang){
3 // summary:
4 // This is a transaction managing store. When transaction is started, by calling store.transaction(),
5 // all request operations (put, add, remove), are entered into a transaction log. When commit() is
6 // called, the actions in the log are then committed. This store relies on three other stores,
7 // extending the dojo/store/Cache:
8 // * masterStore - like with Cache, this store is the ultimate authority, and is usually connected to the backend
9 // * cachingStore - This store includes local data, including changes within the current transaction
10 // * transactionLogStore - This store holds the log of actions that need to be committed to the masterStore on the next commit
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 // immediately perform the action
31 var result = masterStore[method](target, options);
32 when(result, null, function(e){
33 if(transactionStore.errorHandler(e)){
34 // failed, and the errorHandler has signaled that it should be requeued
35 autoCommit = false;
36 options.error = e;
37 execute.call(transactionStore, target, options);
38 autoCommit = true;
39 }
40 });
41 return result;
42 }else{
43 // add to the transaction log
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 // we need to listen for any notifications from the master store, and propagate these to the caching store
64 // this arguably should actually be done in dojo/store/Cache
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 // this is called whenever an error occurs when attempting to commit transactional actions
78 // this can return true to indicate that the action should be reattempted in the next commit
79 // this can return false to indicate that the action should be reverted
80 // if this returns undefined, no action will be taken
81 console.error(error);
82 return true;
83 },
84 commit: function(){
85 // commit everything in the transaction log
86 autoCommit = true;
87 var transactionStore = this;
88 // query for everything in the log
89 return transactionLogStore.query({}).map(function(action){
90 //var options = action.options || {};
91 var method = action.method;
92 var store = stores[action.storeId];
93 var target = action.target;
94 var result;
95 try{
96 // execute the queued action
97 result = store[method](target, action.options);
98 }catch(e){
99 result = transactionStore.errorHandler(e);
100 if(result === true){
101 // don't remove it from the log, let it be executed again
102 return e;
103 }else if(result === false){
104 // revert, by sending out a notification and updating the caching store
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 // fired it, can remove now
116 // TODO: handle async
117 transactionLogStore.remove(action.id);
118 return result;
119 });
120 },
121 transaction: function(){
122 // start a new transaction (by just turning off autoCommit)
123 autoCommit = false;
124 var transactionStore = this;
125 return {
126 commit: function(){
127 return transactionStore.commit();
128 }
129 };
130 }
131 }), cachingStore, options);
132 };
133 });