Skip to content

Instantly share code, notes, and snippets.

@oychao
Last active December 11, 2018 04:40
Show Gist options
  • Save oychao/81a2128ca2196182e8b6dcd83cd9a4dd to your computer and use it in GitHub Desktop.
Save oychao/81a2128ca2196182e8b6dcd83cd9a4dd to your computer and use it in GitHub Desktop.
learn react and vue
/**
* Test React's Transaction
* <pre>
* wrappers (injected at creation time)
* + +
* | |
* +-----------------|--------|--------------+
* | v | |
* | +---------------+ | |
* | +--| wrapper1 |---|----+ |
* | | +---------------+ v | |
* | | +-------------+ | |
* | | +----| wrapper2 |--------+ |
* | | | +-------------+ | | |
* | | | | | |
* | v v v v | wrapper
* | +---+ +---+ +---------+ +---+ +---+ | invariants
* perform(anyMethod) | | | | | | | | | | | | maintained
* +----------------->|-|---|-|---|-->|anyMethod|---|---|-|---|-|-------->
* | | | | | | | | | | | |
* | | | | | | | | | | | |
* | | | | | | | | | | | |
* | +---+ +---+ +---------+ +---+ +---+ |
* | initialize close |
* +-----------------------------------------+
* </pre>
*/
const Transaction = require('../Transaction.js');
const wrappers = [{
initialize() {
console.log(`wrapper 1: ${this.name} initialize`);
},
close() {
console.log(`wrapper 1: ${this.name} close`);
}
}, {
initialize() {
console.log(`wrapper 2: ${this.name} initialize`);
},
close() {
console.log(`wrapper 2: ${this.name} close`);
}
}];
function MyTransaction() {
this.reinitializeTransaction();
this.name = 'Ouyang';
}
Object.assign(MyTransaction.prototype, Transaction, {
getTransactionWrappers() {
return wrappers;
}
});
const myTransaction = new MyTransaction();
const ret = myTransaction.perform(function(a, b) {
console.log(`performing: ${this.name} is calculating: a + b = ${a + b}`);
return a + b;
}, myTransaction, 1, 2);
console.log(`the result is ${ret}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment