/**
 * @license
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CLASS({
  package: 'foam.core.dao',
  name: 'Sync',
  properties: [
    {
      name: 'local',
      hidden: true
    },
    {
      name: 'remote',
      hidden: true
    },
    {
      name: 'localVersionProp',
      hidden: true,
    },
    {
      name: 'remoteVersionProp',
      hidden: true,
    },
    {
      name: 'deletedProp',
      hidden: true
    },
    {
      model_: 'IntProperty',
      name: 'syncs'
    },
    {
      model_: 'BooleanProperty',
      name: 'syncing',
      defaultValue: false
    },
    {
      model_: 'IntProperty',
      help: 'Number of objects to sync from client if client store is empty.  0 to sync them all',
      name: 'initialSyncWindow',
      defaultValue: 0
    },
    {
      model_: 'IntProperty',
      name: 'syncedFromServer',
    },
    {
      model_: 'IntProperty',
      name: 'syncedFromClient'
    },
    {
      model_: 'IntProperty',
      name: 'purgedFromClient'
    }
  ],
  actions: [
    {
      name: 'sync',
      isEnabled: function() { return ! this.syncing; },
      action: function() {
        this.syncing = true;
        this.syncs += 1;

        var self = this;

        aseq(
          apar(
            aseq(
              function(ret) {
                self.local.select(MAX(self.remoteVersionProp))(function(m) {
                  ret((m && m.max) || 0);
                });
              },
              function(ret, localRemote) {
                var remote = self.remote;
                if ( localRemote == 0 && self.initialSyncWindow > 0 ) {
                  remote = remote.limit(self.initialSyncWindow);
                }
                remote = remote.where(GT(self.remoteVersionProp, localRemote));
                remote.select(SEQ(self.local, COUNT()))(ret);
              }),
            aseq(
              function(ret) {
                self.remote.select(MAX(self.localVersionProp))(function(m) {
                  ret((m && m.max) || 0);
                });
              },
              apar(
                function(ret, remoteLocal) {
                  var local = self.local;
                  local = local.where(GT(self.localVersionProp, remoteLocal));
                  local.select(SEQ(self.remote, COUNT()))(ret);
                },
                function(ret, remoteLocal) {
                  var local = self.local;
                  local = local.where(
                    AND(
                      LTE(self.localVersionProp, remoteLocal),
                      EQ(self.deletedProp, true)));
                  local.removeAll(COUNT())(ret);
                }))),
          function(ret, downstream, upstream, purged) {
            self.syncedFromServer += downstream.args[1].count;
            self.syncedFromClient += upstream.args[1].count;
            self.purgedFromClient -= purged.count;
            ret();
          })(function() {
            self.syncing = false;
          });
      }
    }
  ],
});

@@ -1565,16 +1565,280 @@
  %7D%0A  %5D,%0A
+%0A  methods: %7B%0A    purge: function(ret, remoteLocal) %7B%0A      var local = this.local;%0A      local = local.where(%0A        AND(%0A          LTE(this.localVersionProp, remoteLocal),%0A          EQ(this.deletedProp, true)));%0A      local.removeAll(COUNT())(ret);%0A    %7D%0A  %7D,%0A%0A
   action
@@ -3159,324 +3159,29 @@
     
-function(ret, remoteLocal) %7B%0A                  var local = self.local;%0A                  local = local.where(%0A                    AND(%0A                      LTE(self.localVersionProp, remoteLocal),%0A                      EQ(self.deletedProp, true)));%0A                  local.removeAll(COUNT())(ret);%0A                %7D
+self.purge.bind(self)
 ))),

Make the purge strategy of the Sync component a template method.