Skip to content

Commit

Permalink
feat(release): this.
Browse files Browse the repository at this point in the history
this.: fetch a deepstream list that is read-only, any array mutation will not
persist locally after browser refresh
  • Loading branch information
ecerroni committed Aug 27, 2016
1 parent a5f3892 commit 1bcf66a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/process-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function processMethod(methods) {
var value = methods[key]
var method = process(value)
if (!method) {
console.log("can not accept method \"" + key + "\"", value)
0
throw "can not accept method \"" + key + "\""
} else {
result[key] = method
Expand Down
135 changes: 97 additions & 38 deletions src/vdc-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function vDs(params){
var ds = deepstream(params.connectionUrl, params.options);
ds.on('connectionStateChanged', function(connectionState) {
// will be called with 'CLOSED' once the connection is successfully closed.
//console.log('connectionStateChanged', connectionState)
})
return ds
}
Expand Down Expand Up @@ -70,29 +69,93 @@ function _arrayObjectIndexOf(myArray, property, searchTerm) {
*/
function _vdsList(list, ds) {
var subscribedList = ds.record.getList(list);
var listArr = []
//console.log('I have subscribed you to the >> '+ list + ' List')
gloList.push({listName: list, listData: subscribedList});

console.log('# OF LIST SUBSCRIBED TO ', gloList.length, gloList.map(function(ln){return ln.listName}))
0

//console.log('List Names: ', gloList.map(function(ln){return ln.listName}))
return _arrayObjectIndexOf(gloList, 'listName', list);
}

function vueListDiscard (listName) {
var ds = this.$ds
//var listToFetch = params.listName;
//var key = params.syncData

var list = ds.record.getList(listName);
console.log('ready to get rid of ', listName, list)
/*list.unsubscribe(function(){
console.log('unsubscribed from ', listName)
})*/
0

list.discard()

}


function vueListFetchReadOnly (listName) {

var ds = this.$ds
var listToFetch = listName;
var key = this._callingKey
var list = gloList[_vdsList(listToFetch, ds)].listData;
var self = this // This is Vue sync caller
var data = [];

var k = key

list.whenReady(function onListReady() {
list.getEntries().forEach(function (entry, itemIdx) {
var item = ds.record.getRecord(entry);
item.subscribe(function (data) {
0
var keyExists = Object.keys(self.$$syncDict).indexOf(k) > -1 ? Object.keys(self.$$syncDict).indexOf(k) : -1;
var update = null;

if (keyExists > -1) {
var idx = -1
update = self.$data[k].find(function (item, index) {
idx = index

return idx == itemIdx
})

if (update) {
0
self.$data[k].$set(idx, data)
}
}
})// end item.subscribe

item.whenReady(function () {
data.safePush(item.get())
})
})
0
})


data._list = listToFetch
data._key = key
var self = this

data.safePush = function (arr) {
return this.__proto__.push.apply(this, arguments);
}

data.$safeSet = function (idx, toUpdate) {
0
if (idx >= data.length) {
data.length = Number(idx) + 1
}

data.splice(idx, 1, toUpdate)[0]
0
}//end data.$set

data.$remove = function (toUpdate) {
0
gloList[_arrayObjectIndexOf(gloList, 'listName', data._list)].listData.removeEntry(toUpdate)
return this.__proto__.$remove.apply(this, arguments);
}//end data.$set

return data
}

/**
*
* @param params
Expand All @@ -109,14 +172,12 @@ function vueListFetch (listName) {

var k = key

// console.log('connectivity ', ds.getConnectionState())

// this.$on('beforeDestroy', function(){console.log('now destroy the list')})
list.whenReady(function onListReady() {
list.getEntries().forEach(function (entry, index) {
var item = ds.record.getRecord(entry);
item.subscribe(function (data) {
console.log('[DS] EVENT: {SUBSCRIBE} ITEM [', data._uid, ']')
0
var keyExists = Object.keys(self.$$syncDict).indexOf(k) > -1 ? Object.keys(self.$$syncDict).indexOf(k) : -1;
var update = null;

Expand All @@ -128,7 +189,7 @@ function vueListFetch (listName) {
})

if (update) {
console.log('[DS] FROM {SUBSCRIBE} HAVE ITEM [', update._uid, '] TO INSERT @ ', idx)
0
self.$data[k].$set(idx, data)
}
}
Expand All @@ -138,13 +199,13 @@ function vueListFetch (listName) {
data.safePush(item.get())
})
})
console.log('[DS] FETCH LIST ENTRIES FOR /'+listName+'/: ', list.getEntries())
0
})

list.on('entry-added', function (recordChanged) {
var item = ds.record.getRecord(recordChanged)
item.subscribe(function (data) {
console.log('[DS] EVENT: {SUBSCRIBE} ITEM (LIST) [', data._uid, ']')
0
var keyExists = Object.keys(self.$$syncDict).indexOf(k) > -1 ? Object.keys(self.$$syncDict).indexOf(k) : -1;
var update = null;
if (keyExists > -1) {
Expand All @@ -155,7 +216,7 @@ function vueListFetch (listName) {
})

if (update) {
console.log('[DS] FROM {SUBSCRIBE} FOUND ITEM [', update._uid, '] TO UPDATE @ ', idx)
0
self.$data[k].$set(idx, data)
}
}
Expand All @@ -173,7 +234,7 @@ function vueListFetch (listName) {
})

if (!update) self.$data[k].$safeSet(self.$data[k].length, obj)
console.log('[DS] EVENT: {ENTRY-ADDED} (LIST) | ITEM[', obj._uid, ']')
0
})
}) // end list.on.entryAdded

Expand All @@ -184,7 +245,7 @@ function vueListFetch (listName) {
return item._uid == recordChanged;
})
remove ? self.$data[k].$remove(remove) : null;
if (remove) console.log('[DS] EVENT: {ENTRY-REMOVED} (LIST) | ITEM[', recordChanged, ']')
if (remove) 0
}
})// end list.on.entryRemoved

Expand All @@ -193,7 +254,7 @@ function vueListFetch (listName) {
var self = this
data.push = function (arr) {
var pushMessage = '[CONNECTOR] Push intercepted | Checking... [ FRESH UID ]'
console.log(pushMessage)
0
var uid = ds.getUid();
var record = data._key + '/' + uid;
var timestamp = Date.now()
Expand All @@ -217,7 +278,7 @@ function vueListFetch (listName) {
gloList[_arrayObjectIndexOf(gloList, 'listName', data._list)].listData.addEntry(record)
}

console.log('[CONNECTOR] Pushed! Complete', [data._list, record, newEntry])
0
return this.__proto__.push.apply(this, arguments);
}//end data.push

Expand All @@ -226,13 +287,13 @@ function vueListFetch (listName) {
}

data.$set = function (idx, toUpdate) {
console.log('[CONNECTOR] $set intercepted | Checking... [', toUpdate._uid, '] @', idx)
0
if (idx >= data.length) {
data.length = Number(idx) + 1
}

data.splice(idx, 1, toUpdate)[0]
console.log('[CONNECTOR] $set complete | Commit... [', toUpdate._uid, ']')
0

var updateRecord = ds.record.getRecord(toUpdate._uid)
updateRecord.whenReady(function () {
Expand All @@ -241,17 +302,17 @@ function vueListFetch (listName) {
}//end data.$set

data.$safeSet = function (idx, toUpdate) {
console.log('[CONNECTOR] $safeSet intercepted | Checking... [', toUpdate._uid, '] @', idx)
0
if (idx >= data.length) {
data.length = Number(idx) + 1
}

data.splice(idx, 1, toUpdate)[0]
console.log('[CONNECTOR] $safeSet complete | Commit... [', toUpdate._uid, ']')
0
}//end data.$set

data.$remove = function (toUpdate) {
console.log('[CONNECTOR] $remove intercepted | Deleting... [', toUpdate._uid, '] ')
0
gloList[_arrayObjectIndexOf(gloList, 'listName', data._list)].listData.removeEntry(toUpdate._uid)
return this.__proto__.$remove.apply(this, arguments);
}//end data.$set
Expand Down Expand Up @@ -310,13 +371,14 @@ function vueRecordCreate(recordName, defVal, genId) {
}

entry.set(objData);
//console.log('inner entry ', entry, record, recordName)

return entry
} else {
console.log('Either something went wrong during "create" or object data are missing')
0
}

})
return record
}

/**
Expand All @@ -331,14 +393,14 @@ function vueRecordFetch(record) {
var item = ds.record.getRecord(record);

item.subscribe(function(value){
console.log('[DS] EVENT: {SUBSCRIBE} ITEM (SINGLE ITEM) ', value)
0

var keyExists = Object.keys(self.$$syncDict).indexOf(k) > -1 ? Object.keys(self.$$syncDict).indexOf(k) : -1;

if (keyExists > -1) {
for (var key in value) {
if (Object.hasOwnProperty(key)) {
console.log(key);self.$set(key, value[key])
self.$set(key, value[key])
}
}
}
Expand All @@ -362,9 +424,6 @@ function vueRecordFetch(record) {
function vueRecordFieldFetch(record, field) {
var k = this._callingKey
var ds = this.$ds
//var getList = this.$ds != null ? ds.getConnectionState() : 'WAIT'
//console.log('get list ', getList, ds.getConnectionState())
//if (getList !== 'WAIT') {

var item = ds.record.getRecord(record);
var self = this
Expand All @@ -374,7 +433,7 @@ function vueRecordFieldFetch(record, field) {
var keyExists = Object.keys(self.$$syncDict).indexOf(k) > -1 ? Object.keys(self.$$syncDict).indexOf(k) : -1;
if ((keyExists > -1) /*&& (self.$data[k])*/) {
self.$set(k, value)
console.log('[DS] EVENT: {SUBSCRIBE} FIELD ["' + field + '"]', 'IN RECORD (SINGLE ITEM): ', value)
0
}
;
});
Expand All @@ -389,7 +448,7 @@ function vueRecordFieldFetch(record, field) {

this.$on('ds-sync', function (newVal, keySent) {
if (k == keySent) {
console.log('[CONNECTOR] v-model change interecepted | Checking...', 'record[', record, '] field[', field, '] value[', newVal, ']')
0
var rec = ds.record.getRecord(record)

rec.whenReady(function () {
Expand All @@ -401,15 +460,15 @@ function vueRecordFieldFetch(record, field) {
})//end this.$on


// }
return data
return data
}

module.exports = {
vDs: vDs,
vueRecordFieldFetch: vueRecordFieldFetch,
vueRecordFetch: vueRecordFetch,
vueListFetch: vueListFetch,
vueListFetchReadOnly: vueListFetchReadOnly,
vueListDiscard: vueListDiscard,
vdsLogin: vdsLogin,
vdsLogout: vdsLogout,
Expand Down
2 changes: 2 additions & 0 deletions src/vdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ exports.install = function (Vue, options){
Vue.config.optionMergeStrategies.sync = Vue.config.optionMergeStrategies.computed

p.$dsListFetch = builtInDeepstream['vueListFetch']
p.$dsListFetchReadOnly = builtInDeepstream['vueListFetchReadOnly']
p.$dsConnect = builtInDeepstream['vDs']
p.$dsLogin = builtInDeepstream['vdsLogin']
p.$dsLogout = builtInDeepstream['vdsLogout']
p.$dsRecordFieldFetch = builtInDeepstream['vueRecordFieldFetch']
p.$dsRecordCreate = builtInDeepstream['vueRecordCreate']
p.$dsRecordFetch = builtInDeepstream['vueRecordFetch']

Vue.filter('ds-sync', {
read: function(value, key) {
Expand Down

0 comments on commit 1bcf66a

Please sign in to comment.