@@ -1459,7 +1459,7 @@ dop.core.error = {
14591459    // Remote rejects 
14601460    reject_remote : { 
14611461        OBJECT_NOT_FOUND : 1 , 
1462-         1 : 'Remote object not found or not permissions to be subscribed ' , 
1462+         1 : 'Remote object not found or not permissions to use it ' , 
14631463        SUBSCRIPTION_NOT_FOUND : 2 , 
14641464        2 : 'Subscription not found to unsubscribe this object' , 
14651465        FUNCTION_NOT_FOUND : 3 , 
@@ -1989,7 +1989,7 @@ dop.core.emitObservers = function(mutations) {
19891989        subobject  =  mutation . object ; 
19901990        object_dop  =  dop . getObjectDop ( subobject ) ; 
19911991
1992-         if  ( ! mutationsWithSubscribers  /* && dop.data.object_data [object_dop[0]].nodes > 0*/ ) 
1992+         if  ( ! mutationsWithSubscribers  &&  isObject ( dop . data . object [ object_dop [ 0 ] ] ) ) 
19931993            mutationsWithSubscribers  =  true ; 
19941994
19951995        // Emiting mutations to observerProperties 
@@ -2060,8 +2060,9 @@ dop.core.injectMutationInAction = function(action, mutation, isUnaction) {
20602060            var  swaps  =  mutation . swaps . slice ( 0 ) ; 
20612061            if  ( isUnaction ) 
20622062                swaps . reverse ( ) ; 
2063-             var  tochange  =  ( swaps [ 0 ] > 0 )  ? 0  : 1 ; 
2064-             swaps [ tochange ]  =  swaps [ tochange ] * - 1 ; 
2063+             // var tochange = (swaps[0]>0) ? 0 : 1; 
2064+             // swaps[tochange] = swaps[tochange]*-1; 
2065+             swaps . unshift ( 0 ) ;  // 0 mean swap 
20652066            mutations . push ( swaps ) ; 
20662067        } 
20672068
@@ -2074,14 +2075,16 @@ dop.core.injectMutationInAction = function(action, mutation, isUnaction) {
20742075            } 
20752076            else 
20762077                splice  =  mutation . splice . slice ( 0 ) ; 
2077-                 
2078+             
2079+             splice . unshift ( 1 ) ;  // 1 mean splice 
20782080            mutations . push ( splice ) ; 
20792081        } 
20802082
20812083        // set 
20822084        else 
2083-             mutations . push ( [ prop ,  1 ,  value ] ) ; 
2085+             mutations . push ( [ 1 ,   prop ,  1 ,  value ] ) ; 
20842086
2087+         // We have to update the length of the array in case that is lower than before 
20852088        if  ( isUnaction  &&  mutation . length !== undefined  &&  mutation . length !== mutation . object . length ) 
20862089            action . length  =  mutation . length ; 
20872090    } 
@@ -2214,21 +2217,24 @@ dop.core.setActionMutator = function(destiny, prop, value, typeofValue, path) {
22142217            var  mutations  =  value [ dop . cons . DOP ] , 
22152218                mutation , 
22162219                index = 0 , 
2217-                 total = mutations . length ; 
2220+                 total = mutations . length , 
2221+                 typeArrayMutation ; 
22182222
22192223            // if (typeofDestiny!='array') 
22202224            //     dop.set(destiny, prop, []); 
22212225
22222226            for  ( ; index < total ;  ++ index )  { 
2223-                 mutation  =  mutations [ index ] ; 
2224-                 // swaps 
2225-                 if  ( mutation [ 0 ] < 0  ||  mutation [ 1 ] < 0 )  { 
2226-                     mutation  =  mutation . slice ( 0 ) ; 
2227-                     ( mutation [ 0 ] < 0 )  ? mutation [ 0 ]  =  mutation [ 0 ] * - 1  : mutation [ 1 ]  =  mutation [ 1 ] * - 1 ; 
2227+                 typeArrayMutation  =  mutations [ index ] [ 0 ] ;  // 0=swaps 1=splices 
2228+                 mutation  =  mutations [ index ] . slice ( 1 ) ; 
2229+                 // swap 
2230+                 if  ( typeArrayMutation === 0 )  { 
2231+                     // mutation = mutation.slice(0); 
2232+                     // (mutation[0]<0) ? mutation[0] = mutation[0]*-1 : mutation[1] = mutation[1]*-1; 
22282233                    dop . core . swap ( destiny [ prop ] ,  mutation ) ; 
22292234                } 
22302235                // set 
22312236                else  { 
2237+                     // We have to update the length of the array in case that is lower than before 
22322238                    if  ( destiny [ prop ] . length < mutation [ 0 ] ) 
22332239                        dop . getObjectTarget ( destiny [ prop ] ) . length  =  mutation [ 0 ] ; 
22342240                    // set 
@@ -2447,15 +2453,18 @@ dop.core.decode = function(property, value, node, undefineds) {
24472453//////////  src/core/protocol/emitNodes.js 
24482454
24492455dop . core . emitNodes  =  function ( action )  { 
2450-     // var object_id, node_token, node; 
2451-     // for (object_id in action) { 
2452-     //     if (dop.data.object_data[object_id].nodes > 0) { 
2453-     //         for (node_token in dop.data.object_data[object_id].node) { 
2454-     //             node = dop.data.node[node_token]; 
2455-     //             dop.protocol.merge(node, object_id, action[object_id]); 
2456-     //         } 
2457-     //     } 
2458-     // } 
2456+     var  object_id ,  node_token ,  node ,  object_data ; 
2457+     for  ( object_id  in  action )  { 
2458+         if  ( isObject ( dop . data . object [ object_id ] ) )  { 
2459+             object_data  =  dop . data . object [ object_id ] ; 
2460+             for  ( node_token  in  object_data . node )  { 
2461+                 if  ( object_data . node [ node_token ] . subscriber === 1 )  { 
2462+                     node  =  dop . data . node [ node_token ] ; 
2463+                     dop . protocol . patch ( node ,  Number ( object_id ) ,  action [ object_id ] . action ) ; 
2464+                 } 
2465+             } 
2466+         } 
2467+     } 
24592468} ; 
24602469
24612470
@@ -2592,9 +2601,11 @@ dop.core.registerObjectToNode = function(node, object) {
25922601        object_data . nodes_total  +=  1 ; 
25932602        object_data . node [ node . token ]  =  { 
25942603            subscriber : 0 ,  // 0 or 1 || false true  
2595-             owner : 0 ,  // object_id_owner 
2596-             subscriber_version : 0 ,  
2597-             owner_version : 0 
2604+             owner : 0 ,  // object_id_owner || 0 === false 
2605+             version : 0 ,  // incremental integer for new patches 
2606+             pending : [ ] , 
2607+             applied_version : 0 ,  // last patch version applied correctly 
2608+             applied : { } 
25982609        } ; 
25992610    } 
26002611
@@ -2755,6 +2766,44 @@ dop.protocol._oncall = function(node, request_id, request, response) {
27552766
27562767
27572768
2769+ //////////  src/protocol/_onpatch.js 
2770+ 
2771+ dop . protocol . _onpatch  =  function ( node ,  request_id ,  request ,  response )  { 
2772+     var  rejection  =  response [ 0 ] , 
2773+         object_id  =  request [ 2 ] , 
2774+         object_node  =  dop . data . object [ object_id ] . node [ node . token ] , 
2775+         version  =  request [ 3 ] , 
2776+         pending_list  =  object_node . pending , 
2777+         promise  =  request . promise , 
2778+         index  =  0 , 
2779+         total  =  pending_list . length , 
2780+         version_item ; 
2781+ 
2782+ 
2783+     if  ( rejection  !==  undefined )  { 
2784+         if  ( rejection  ===  0 )  { 
2785+             for  ( ; index < total ;  index ++ )  { 
2786+                 version_item  =  pending_list [ index ] [ 0 ] ; 
2787+                 // Removing from pending because its been received correctly 
2788+                 if  ( version_item  >=  version )  { 
2789+                     if  ( version_item  ===  version ) 
2790+                         pending_list . splice ( index ,  1 ) ; 
2791+                     break ; 
2792+                 } 
2793+                 // Resending 
2794+                 else 
2795+                     dop . protocol . patchSend ( node ,  object_id ,  object_node ,  version_item ,  pending_list [ index ] [ 1 ] ) ; 
2796+             } 
2797+             promise . resolve ( response [ 1 ] ) ; 
2798+         } 
2799+         else 
2800+             promise . reject ( dop . core . getRejectError ( rejection ) ) ; 
2801+     } 
2802+ } ; 
2803+ 
2804+ 
2805+ 
2806+ 
27582807//////////  src/protocol/_onsubscribe.js 
27592808
27602809dop . protocol . _onsubscribe  =  function ( node ,  request_id ,  request ,  response )  { 
@@ -2938,7 +2987,7 @@ dop.protocol.instructions = {
29382987                        // [-1234, 0, <return>] 
29392988
29402989                        // Owner -> Subscriptor 
2941-     mutation : 5 ,         // [ 1234, <instruction>, <object_id>, <version>, <mutation >] 
2990+     patch : 5 ,             // [ 1234, <instruction>, <object_id>, <version>, <patch >] 
29422991                        // [-1234, 0] 
29432992} ; 
29442993
@@ -2949,25 +2998,11 @@ for (var instruction in dop.protocol.instructions)
29492998
29502999
29513000
2952- //////////  src/protocol/mutation.js 
2953- 
2954- dop . protocol . mutation  =  function ( node ,  object_id ,  action )  { 
2955-     
2956-     console . log ( node . token ,  object_id ,  action ) ; 
2957-     // node.send(JSON.stringify( 
2958-     //     dop.core.createRequest(node, dop.protocol.instructions.connect, token) 
2959-     //)); 
2960- 
2961- } ; 
2962- 
2963- 
2964- 
2965- 
29663001//////////  src/protocol/onbroadcast.js 
29673002
29683003dop . protocol . onbroadcast  =  function ( node ,  request_id ,  request )  { 
2969-     dop . protocol . onfunction ( node ,  request_id ,  request ,  function ( permission ,   object_id )  { 
2970-         return  permission . owner === object_id ; 
3004+     dop . protocol . onfunction ( node ,  request_id ,  request ,  node . owner [ request [ 1 ] ] ,   function ( permission )  { 
3005+         return  permission . owner === request [ 1 ] ; 
29713006    } ) ; 
29723007} ; 
29733008
@@ -2977,7 +3012,7 @@ dop.protocol.onbroadcast = function(node, request_id, request) {
29773012//////////  src/protocol/oncall.js 
29783013
29793014dop . protocol . oncall  =  function ( node ,  request_id ,  request )  { 
2980-     dop . protocol . onfunction ( node ,  request_id ,  request ,  function ( permission )  { 
3015+     dop . protocol . onfunction ( node ,  request_id ,  request ,  request [ 1 ] ,   function ( permission )  { 
29813016        return  permission . subscriber === 1 ; 
29823017    } ) ; 
29833018} 
@@ -2987,13 +3022,12 @@ dop.protocol.oncall = function(node, request_id, request) {
29873022
29883023//////////  src/protocol/onfunction.js 
29893024// Used by dop.protocol.oncall && dop.protocol.onbroadcast 
2990- dop . protocol . onfunction  =  function ( node ,  request_id ,  request ,  validator )  { 
2991-     var  object_id  =  request [ 1 ] , 
2992-         path  =  request [ 2 ] , 
3025+ dop . protocol . onfunction  =  function ( node ,  request_id ,  request ,  object_id ,  validator )  { 
3026+     var  path  =  request [ 2 ] , 
29933027        params  =  request [ 3 ] , 
29943028        object_data  =  dop . data . object [ object_id ] ; 
29953029
2996-     if  ( isObject ( object_data )  &&  isObject ( object_data . node [ node . token ] )  &&  validator ( object_data . node [ node . token ] ,   object_id ) )  { 
3030+     if  ( isObject ( object_data )  &&  isObject ( object_data . node [ node . token ] )  &&  validator ( object_data . node [ node . token ] ) )  { 
29973031        var  functionName  =  path . pop ( ) , 
29983032            object  =  dop . util . get ( object_data . object ,  path ) , 
29993033            f  =  object [ functionName ] ; 
@@ -3027,6 +3061,44 @@ dop.protocol.onfunction = function(node, request_id, request, validator) {
30273061
30283062
30293063
3064+ //////////  src/protocol/onpatch.js 
3065+ 
3066+ dop . protocol . onpatch  =  function ( node ,  request_id ,  request )  { 
3067+     var  object_id_owner  =  request [ 1 ] , 
3068+         object_id  =  node . owner [ object_id_owner ] , 
3069+         version  =  request [ 2 ] , 
3070+         patch  =  request [ 3 ] , 
3071+         response  =  dop . core . createResponse ( request_id ) , 
3072+         object_data  =  dop . data . object [ object_id ] , 
3073+         object_node , 
3074+         collector ; 
3075+     
3076+     if  ( isObject ( object_data )  &&  isObject ( object_data . node [ node . token ] )  &&  object_data . node [ node . token ] . owner === object_id_owner )  { 
3077+         object_node  =  object_data . node [ node . token ] ; 
3078+         // Storing patch 
3079+         if  ( object_node . applied_version  <  version  &&  object_node . applied [ version ] === undefined )  { 
3080+             // Storing patch 
3081+             object_node . applied [ version ]  =  patch ; 
3082+             // Applying 
3083+             collector  =  dop . collectFirst ( ) ; 
3084+             while  ( object_node . applied [ object_node . applied_version + 1 ] )  { 
3085+                 object_node . applied_version  +=  1 ; 
3086+                 dop . core . setActionFunction ( object_data . object ,  object_node . applied [ object_node . applied_version ] ) ; 
3087+                 delete  object_node . applied [ object_node . applied_version ] ; 
3088+             } 
3089+             collector . emitAndDestroy ( ) ; 
3090+         } 
3091+         response . push ( 0 ) ; 
3092+     } 
3093+     else 
3094+         response . push ( dop . core . error . reject_remote . OBJECT_NOT_FOUND ) ; 
3095+     
3096+     dop . core . storeSendMessages ( node ,  response ) ; 
3097+ } ; 
3098+ 
3099+ 
3100+ 
3101+ 
30303102//////////  src/protocol/onsubscribe.js 
30313103
30323104dop . protocol . onsubscribe  =  function ( node ,  request_id ,  request )  { 
@@ -3104,6 +3176,25 @@ dop.protocol.onunsubscribe = function(node, request_id, request) {
31043176
31053177
31063178
3179+ //////////  src/protocol/patch.js 
3180+ 
3181+ dop . protocol . patch  =  function ( node ,  object_id ,  patch )  { 
3182+     var  object_node  =  dop . data . object [ object_id ] . node [ node . token ] , 
3183+         version  =  ++ object_node . version ; 
3184+     object_node . pending . push ( [ version ,  dop . util . merge ( { } ,  patch ) ] ) ;  // Making a copy because this object is exposed to the api users and can be mutated 
3185+     return  dop . protocol . patchSend ( node ,  object_id ,  object_node ,  version ,  patch ) ; 
3186+ } ; 
3187+ 
3188+ // Also used by dop.protocol._onpatch 
3189+ dop . protocol . patchSend  =  function ( node ,  object_id ,  object_node ,  version ,  patch )  { 
3190+     var  request  =  dop . core . createRequest (  node ,  dop . protocol . instructions . patch ,  object_id ,  version ,  patch ) ; 
3191+     dop . core . storeSendMessages ( node ,  request ,  dop . encodeFunction ) ; 
3192+     return  request . promise ; 
3193+ } ; 
3194+ 
3195+ 
3196+ 
3197+ 
31073198//////////  src/protocol/subscribe.js 
31083199
31093200dop . protocol . subscribe  =  function ( node ,  params )  { 
0 commit comments