| 1 function Command() { 2 if (!Command.NextID) Command.NextID = 0; 3 this.id = ++Command.NextID; 4 // unsynchronized API 5 this.doit = function(){ alert("DOIT called"); } 6 this.undo = function(){ alert("UNDO called"); } 7 this.redo = function(){ this.doit(); } 8 // synchronized API 9 this.sDoIt = function(){ new Mutex(this,"doit"); } 10 this.sUnDo = function(){ new Mutex(this,"undo"); } 11 this.sReDo = function(){ new Mutex(this,"redo"); } 12 } |
| 1 function Mutex( cmdObject, methodName ) { 2 // define static field and method 3 if (!Mutex.Wait) Mutex.Wait = new Map(); 4 Mutex.SLICE = function( cmdID, startID ) { 5 Mutex.Wait.get(cmdID).attempt( Mutex.Wait.get(startID) ); 6 } 7 // define instance method 8 this.attempt = function( start ) { 9 for (var j=start; j; j=Mutex.Wait.next(j.c.id)) { 10 if (j.enter 11 || (j.number && (j.number < this.number || 12 (j.number == this.number 13 && j.c.id < this.c.id)))) 14 return setTimeout 15 ("Mutex.SLICE("+this.c.id+","+j.c.id+")",10); 16 } 17 //run with exclusive access 18 this.c[ this.methodID ](); 19 //release exclusive access 20 this.number = 0; 21 Mutex.Wait.remove( this.c.id ); 22 } 23 // constructor logic 24 this.c = cmdObject; 25 this.methodID = methodName; 26 //(enter and number are "false" here) 27 Mutex.Wait.add( this.c.id, this ); 28 this.enter = true; 29 this.number = (new Date()).getTime(); 30 this.enter = false; 31 this.attempt( Mutex.Wait.first() ); 32 } |
| function Map() { this.map = new Object(); // Map API this.add = function( k,o ){ this.map[k] = o; } this.remove = function( k ){ delete this.map[k]; } this.get = function( k ){ return k==null ? null : this.map[k]; } this.first = function(){ return this.get( this.nextKey() ); } this.next = function( k ){ return this.get( this.nextKey(k) ); } this.nextKey = function( k ){ for (i in this.map) { if ( !k ) return i; if (k==i) k=null; /*tricky*/ } return null; } } |