foam-framework
Version:
MVC metaprogramming framework
159 lines (142 loc) • 4.1 kB
JavaScript
/*
* @license
* Copyright 2015 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({
name: 'AbstractFileDAO',
package: 'foam.core.dao',
extends: 'AbstractDAO',
properties: [
{
name: 'model',
type: 'Model',
requred: true
},
{
name: 'filename',
label: 'Storage file name',
type: 'String',
defaultValueFn: function() {
return this.model.plural;
}
},
{
name: 'type',
label: 'Filesystem Type',
type: 'String',
view: { factory_: 'foam.ui.ChoiceView', choices: ['Persistent', 'Temporary'] },
defaultValue: 'Persistent'
}
],
methods: {
init: function() {
this.SUPER();
var self = this;
var withEntry = amemo(aseq(
function(ret) {
window.webkitStorageInfo.requestQuota(
self.type === 'Persistent' ? 1 : 0,
1024 * 1024 * 200, // 200 MB should be fine.
function() { ret(1024 * 1024 * 200); },
console.error.bind(console));
},
function(ret, quota) {
window.requestFileSystem(
self.type === 'Persistent' ? 1 : 0,
quota, /* expected size*/
ret,
console.error.bind(console));
},
function(ret, filesystem) {
filesystem.root.getFile(
self.filename,
{ create: true },
ret,
console.error.bind(console));
}));
this.withWriter = amemo(aseq(
withEntry,
function(ret, entry) {
entry.createWriter(ret, console.error.bind(console));
})),
this.withStorage = amemo(aseq(
withEntry,
function(ret, entry) {
entry.file(ret, console.error.bind(console));
},
function(ret, file) {
var reader = new FileReader();
var storage = {};
reader.onerror = console.error.bind(console);
reader.onloadend = function() {
self.parseContents_(ret, reader.result, storage);
};
this.readFile_(reader, file);
}));
},
put: function(obj, sink) {
var self = this;
this.withStorage(function(s) {
s.put(obj, {
__proto__: sink,
put: function() {
sink && sink.put && sink.put(obj);
self.notify_('put', [obj]);
self.update_('put', obj);
}
});
});
},
find: function(key, sink) {
this.withStorage(function(s) {
s.find(key, sink);
});
},
remove: function(obj, sink) {
var self = this;
this.withStorage(function(s) {
s.remove(obj, {
__proto__: sink,
remove: function(obj) {
self.__proto__.remove && self.__proto__.remove(obj);
self.notify_('remove', [obj]);
self.update_('remove', obj);
}
});
});
},
removeAll: function(sink, options) {
var self = this;
var future = afuture();
this.withStorage(function(s) {
var fut = s.removeAll({
__proto__: sink,
remove: function(obj) {
self.__proto__.remove && self.__proto__.remove(obj);
self.notify_('remove', [obj]);
self.update_('remove', obj);
}
}, options);
fut(future.set);
});
return future.get;
},
select: function(sink, options) {
this.withStorage(function(s) {
s.select(sink, options);
});
}
}
});