- Service Worker
- Indexdb
This commit is contained in:
@@ -295,7 +295,7 @@
|
||||
}
|
||||
|
||||
// HTTP methods whose capitalization should be normalized
|
||||
var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']
|
||||
var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH']
|
||||
|
||||
function normalizeMethod(method) {
|
||||
var upcased = method.toUpperCase()
|
||||
|
||||
24
src/js/globalenv.js
Normal file
24
src/js/globalenv.js
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
// importScripts('/statics/js/immortal-db.min.js');
|
||||
|
||||
// const cfgenv = {
|
||||
// website: 'http://localhost:8080',
|
||||
// serverweb: 'http://localhost:3000',
|
||||
// dbname: 'mydb3',
|
||||
// dbversion: 10,
|
||||
// }
|
||||
|
||||
/*
|
||||
async function clearAllDataImmortal(table) {
|
||||
console.log('clearAllDataImmortal', table)
|
||||
const db = ImmortalDB.ImmortalDB
|
||||
await db.remove(table)
|
||||
}
|
||||
|
||||
async function writeDataImmortal(table, datavalue) {
|
||||
console.log('writeDataImmortal', table, datavalue)
|
||||
const db = ImmortalDB.ImmortalDB
|
||||
await db.set(table, datavalue)
|
||||
}
|
||||
|
||||
*/
|
||||
311
src/js/idb.js
311
src/js/idb.js
@@ -1,311 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
(function() {
|
||||
function toArray(arr) {
|
||||
return Array.prototype.slice.call(arr);
|
||||
}
|
||||
|
||||
function promisifyRequest(request) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
request.onsuccess = function() {
|
||||
resolve(request.result);
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function promisifyRequestCall(obj, method, args) {
|
||||
var request;
|
||||
var p = new Promise(function(resolve, reject) {
|
||||
request = obj[method].apply(obj, args);
|
||||
promisifyRequest(request).then(resolve, reject);
|
||||
});
|
||||
|
||||
p.request = request;
|
||||
return p;
|
||||
}
|
||||
|
||||
function promisifyCursorRequestCall(obj, method, args) {
|
||||
var p = promisifyRequestCall(obj, method, args);
|
||||
return p.then(function(value) {
|
||||
if (!value) return;
|
||||
return new Cursor(value, p.request);
|
||||
});
|
||||
}
|
||||
|
||||
function proxyProperties(ProxyClass, targetProp, properties) {
|
||||
properties.forEach(function(prop) {
|
||||
Object.defineProperty(ProxyClass.prototype, prop, {
|
||||
get: function() {
|
||||
return this[targetProp][prop];
|
||||
},
|
||||
set: function(val) {
|
||||
this[targetProp][prop] = val;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function proxyRequestMethods(ProxyClass, targetProp, Constructor, properties) {
|
||||
properties.forEach(function(prop) {
|
||||
if (!(prop in Constructor.prototype)) return;
|
||||
ProxyClass.prototype[prop] = function() {
|
||||
return promisifyRequestCall(this[targetProp], prop, arguments);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function proxyMethods(ProxyClass, targetProp, Constructor, properties) {
|
||||
properties.forEach(function(prop) {
|
||||
if (!(prop in Constructor.prototype)) return;
|
||||
ProxyClass.prototype[prop] = function() {
|
||||
return this[targetProp][prop].apply(this[targetProp], arguments);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function proxyCursorRequestMethods(ProxyClass, targetProp, Constructor, properties) {
|
||||
properties.forEach(function(prop) {
|
||||
if (!(prop in Constructor.prototype)) return;
|
||||
ProxyClass.prototype[prop] = function() {
|
||||
return promisifyCursorRequestCall(this[targetProp], prop, arguments);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function Index(index) {
|
||||
this._index = index;
|
||||
}
|
||||
|
||||
proxyProperties(Index, '_index', [
|
||||
'name',
|
||||
'keyPath',
|
||||
'multiEntry',
|
||||
'unique'
|
||||
]);
|
||||
|
||||
proxyRequestMethods(Index, '_index', IDBIndex, [
|
||||
'get',
|
||||
'getKey',
|
||||
'getAll',
|
||||
'getAllKeys',
|
||||
'count'
|
||||
]);
|
||||
|
||||
proxyCursorRequestMethods(Index, '_index', IDBIndex, [
|
||||
'openCursor',
|
||||
'openKeyCursor'
|
||||
]);
|
||||
|
||||
function Cursor(cursor, request) {
|
||||
this._cursor = cursor;
|
||||
this._request = request;
|
||||
}
|
||||
|
||||
proxyProperties(Cursor, '_cursor', [
|
||||
'direction',
|
||||
'key',
|
||||
'primaryKey',
|
||||
'value'
|
||||
]);
|
||||
|
||||
proxyRequestMethods(Cursor, '_cursor', IDBCursor, [
|
||||
'update',
|
||||
'delete'
|
||||
]);
|
||||
|
||||
// proxy 'next' methods
|
||||
['advance', 'continue', 'continuePrimaryKey'].forEach(function(methodName) {
|
||||
if (!(methodName in IDBCursor.prototype)) return;
|
||||
Cursor.prototype[methodName] = function() {
|
||||
var cursor = this;
|
||||
var args = arguments;
|
||||
return Promise.resolve().then(function() {
|
||||
cursor._cursor[methodName].apply(cursor._cursor, args);
|
||||
return promisifyRequest(cursor._request).then(function(value) {
|
||||
if (!value) return;
|
||||
return new Cursor(value, cursor._request);
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
function ObjectStore(store) {
|
||||
this._store = store;
|
||||
}
|
||||
|
||||
ObjectStore.prototype.createIndex = function() {
|
||||
return new Index(this._store.createIndex.apply(this._store, arguments));
|
||||
};
|
||||
|
||||
ObjectStore.prototype.index = function() {
|
||||
return new Index(this._store.index.apply(this._store, arguments));
|
||||
};
|
||||
|
||||
proxyProperties(ObjectStore, '_store', [
|
||||
'name',
|
||||
'keyPath',
|
||||
'indexNames',
|
||||
'autoIncrement'
|
||||
]);
|
||||
|
||||
proxyRequestMethods(ObjectStore, '_store', IDBObjectStore, [
|
||||
'put',
|
||||
'add',
|
||||
'delete',
|
||||
'clear',
|
||||
'get',
|
||||
'getAll',
|
||||
'getKey',
|
||||
'getAllKeys',
|
||||
'count'
|
||||
]);
|
||||
|
||||
proxyCursorRequestMethods(ObjectStore, '_store', IDBObjectStore, [
|
||||
'openCursor',
|
||||
'openKeyCursor'
|
||||
]);
|
||||
|
||||
proxyMethods(ObjectStore, '_store', IDBObjectStore, [
|
||||
'deleteIndex'
|
||||
]);
|
||||
|
||||
function Transaction(idbTransaction) {
|
||||
this._tx = idbTransaction;
|
||||
this.complete = new Promise(function(resolve, reject) {
|
||||
idbTransaction.oncomplete = function() {
|
||||
resolve();
|
||||
};
|
||||
idbTransaction.onerror = function() {
|
||||
reject(idbTransaction.error);
|
||||
};
|
||||
idbTransaction.onabort = function() {
|
||||
reject(idbTransaction.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Transaction.prototype.objectStore = function() {
|
||||
return new ObjectStore(this._tx.objectStore.apply(this._tx, arguments));
|
||||
};
|
||||
|
||||
proxyProperties(Transaction, '_tx', [
|
||||
'objectStoreNames',
|
||||
'mode'
|
||||
]);
|
||||
|
||||
proxyMethods(Transaction, '_tx', IDBTransaction, [
|
||||
'abort'
|
||||
]);
|
||||
|
||||
function UpgradeDB(db, oldVersion, transaction) {
|
||||
this._db = db;
|
||||
this.oldVersion = oldVersion;
|
||||
this.transaction = new Transaction(transaction);
|
||||
}
|
||||
|
||||
UpgradeDB.prototype.createObjectStore = function() {
|
||||
return new ObjectStore(this._db.createObjectStore.apply(this._db, arguments));
|
||||
};
|
||||
|
||||
proxyProperties(UpgradeDB, '_db', [
|
||||
'name',
|
||||
'version',
|
||||
'objectStoreNames'
|
||||
]);
|
||||
|
||||
proxyMethods(UpgradeDB, '_db', IDBDatabase, [
|
||||
'deleteObjectStore',
|
||||
'close'
|
||||
]);
|
||||
|
||||
function DB(db) {
|
||||
this._db = db;
|
||||
}
|
||||
|
||||
DB.prototype.transaction = function() {
|
||||
return new Transaction(this._db.transaction.apply(this._db, arguments));
|
||||
};
|
||||
|
||||
proxyProperties(DB, '_db', [
|
||||
'name',
|
||||
'version',
|
||||
'objectStoreNames'
|
||||
]);
|
||||
|
||||
proxyMethods(DB, '_db', IDBDatabase, [
|
||||
'close'
|
||||
]);
|
||||
|
||||
// Add cursor iterators
|
||||
// TODO: remove this once browsers do the right thing with promises
|
||||
['openCursor', 'openKeyCursor'].forEach(function(funcName) {
|
||||
[ObjectStore, Index].forEach(function(Constructor) {
|
||||
Constructor.prototype[funcName.replace('open', 'iterate')] = function() {
|
||||
var args = toArray(arguments);
|
||||
var callback = args[args.length - 1];
|
||||
var nativeObject = this._store || this._index;
|
||||
var request = nativeObject[funcName].apply(nativeObject, args.slice(0, -1));
|
||||
request.onsuccess = function() {
|
||||
callback(request.result);
|
||||
};
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
// polyfill getAll
|
||||
[Index, ObjectStore].forEach(function(Constructor) {
|
||||
if (Constructor.prototype.getAll) return;
|
||||
Constructor.prototype.getAll = function(query, count) {
|
||||
var instance = this;
|
||||
var items = [];
|
||||
|
||||
return new Promise(function(resolve) {
|
||||
instance.iterateCursor(query, function(cursor) {
|
||||
if (!cursor) {
|
||||
resolve(items);
|
||||
return;
|
||||
}
|
||||
items.push(cursor.value);
|
||||
|
||||
if (count !== undefined && items.length == count) {
|
||||
resolve(items);
|
||||
return;
|
||||
}
|
||||
cursor.continue();
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
var exp = {
|
||||
open: function(name, version, upgradeCallback) {
|
||||
var p = promisifyRequestCall(indexedDB, 'open', [name, version]);
|
||||
var request = p.request;
|
||||
|
||||
request.onupgradeneeded = function(event) {
|
||||
if (upgradeCallback) {
|
||||
upgradeCallback(new UpgradeDB(request.result, event.oldVersion, request.transaction));
|
||||
}
|
||||
};
|
||||
|
||||
return p.then(function(db) {
|
||||
return new DB(db);
|
||||
});
|
||||
},
|
||||
delete: function(name) {
|
||||
return promisifyRequestCall(indexedDB, 'deleteDatabase', [name]);
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof module !== 'undefined') {
|
||||
module.exports = exp;
|
||||
module.exports.default = module.exports;
|
||||
}
|
||||
else {
|
||||
self.idb = exp;
|
||||
}
|
||||
}());
|
||||
8
src/js/immortal-db.min.js
vendored
Normal file
8
src/js/immortal-db.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
112
src/js/storage.js
Normal file
112
src/js/storage.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import { rescodes } from "../store/Modules/rescodes";
|
||||
|
||||
export let idbKeyval = (() => {
|
||||
let db;
|
||||
|
||||
function getDB() {
|
||||
if (!db) {
|
||||
console.log('CREO DB STORAGE JS !')
|
||||
db = new Promise((resolve, reject) => {
|
||||
const openreq = indexedDB.open('mydb3', 11);
|
||||
|
||||
openreq.onerror = () => {
|
||||
reject(openreq.error);
|
||||
};
|
||||
|
||||
openreq.onupgradeneeded = () => {
|
||||
// First time setup: create an empty object store
|
||||
openreq.result.createObjectStore('todos', { keyPath: '_id' });
|
||||
openreq.result.createObjectStore('sync_todos', { keyPath: '_id' });
|
||||
openreq.result.createObjectStore('delete_todos', { keyPath: '_id' });
|
||||
openreq.result.createObjectStore('config', { keyPath: '_id' });
|
||||
};
|
||||
|
||||
openreq.onsuccess = () => {
|
||||
resolve(openreq.result);
|
||||
};
|
||||
});
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
async function withStore(type, table, callback, ) {
|
||||
const db = await getDB();
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(table, type);
|
||||
transaction.oncomplete = () => resolve();
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
callback(transaction.objectStore(table));
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
async get(key) {
|
||||
let req;
|
||||
await withStore('readonly', 'keyval', store => {
|
||||
req = store.get(key);
|
||||
});
|
||||
return req.result;
|
||||
},
|
||||
async getdata(table, key) {
|
||||
let req;
|
||||
await withStore('readonly', table, store => {
|
||||
console.log('store', store, 'key', key)
|
||||
req = store.get(key);
|
||||
});
|
||||
console.log('RISFINALE!', req.result)
|
||||
return req.result;
|
||||
},
|
||||
async getalldata(table) {
|
||||
let req;
|
||||
await withStore('readonly', table, store => {
|
||||
req = store.getAll();
|
||||
});
|
||||
return req.result;
|
||||
},
|
||||
async set(key, value) {
|
||||
return await withStore('readwrite', 'keyval', store => {
|
||||
store.put(value, key);
|
||||
});
|
||||
},
|
||||
async setdata(table, valuekey) {
|
||||
|
||||
let value = []
|
||||
if (table === rescodes.DB.CMD_DELETE_TODOS) {
|
||||
value['_id'] = valuekey
|
||||
}else {
|
||||
value = valuekey
|
||||
}
|
||||
|
||||
console.log('setdata', table, value)
|
||||
|
||||
return await withStore('readwrite', table, store => {
|
||||
store.put(value);
|
||||
});
|
||||
},
|
||||
async delete(key) {
|
||||
return await withStore('readwrite', 'keyval', store => {
|
||||
store.delete(key);
|
||||
});
|
||||
},
|
||||
async deletedata(table, key) {
|
||||
return await withStore('readwrite', table, store => {
|
||||
store.delete(key);
|
||||
});
|
||||
},
|
||||
async clearalldata(table) {
|
||||
return await withStore('readwrite', table, store => {
|
||||
store.clear();
|
||||
});
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
// iOS add-to-homescreen is missing IDB, or at least it used to.
|
||||
// I haven't tested this in a while.
|
||||
if (!self.indexedDB) {
|
||||
idbKeyval = {
|
||||
get: key => Promise.resolve(localStorage.getItem(key)),
|
||||
set: (key, val) => Promise.resolve(localStorage.setItem(key, val)),
|
||||
delete: key => Promise.resolve(localStorage.removeItem(key))
|
||||
};
|
||||
}
|
||||
@@ -1,67 +1,57 @@
|
||||
console.log('utility.js')
|
||||
|
||||
var dbPromise = idb.open('mydb', 10, function (db) {
|
||||
console.log('OPEN MYDB')
|
||||
if (!db.objectStoreNames.contains('sync_todos')) {
|
||||
db.createObjectStore('sync_todos', { keyPath: 'id' });
|
||||
}
|
||||
if (!db.objectStoreNames.contains('todos')) {
|
||||
db.createObjectStore('todos', { keyPath: 'id' });
|
||||
}
|
||||
});
|
||||
// var dbPromise = idb.open('mydb1', 1, function (db) {
|
||||
// console.log('OPEN MYDB')
|
||||
// if (!db.objectStoreNames.contains('todos')) {
|
||||
// db.createObjectStore('todos', { keyPath: '_id' });
|
||||
// }
|
||||
// if (!db.objectStoreNames.contains('config')) {
|
||||
// db.createObjectStore('config', { keyPath: '_id' });
|
||||
// }
|
||||
// });
|
||||
|
||||
function writeData(st, data) {
|
||||
console.log('writeData', st, data);
|
||||
return dbPromise
|
||||
.then(function (db) {
|
||||
var tx = db.transaction(st, 'readwrite');
|
||||
var store = tx.objectStore(st);
|
||||
store.put(data);
|
||||
return tx.complete;
|
||||
});
|
||||
}
|
||||
|
||||
function readAllData(st) {
|
||||
console.log('readAllData', st);
|
||||
return dbPromise
|
||||
.then(function (db) {
|
||||
var tx = db.transaction(st, 'readonly');
|
||||
var store = tx.objectStore(st);
|
||||
return store.getAll();
|
||||
});
|
||||
}
|
||||
// function readAllData(st) {
|
||||
// console.log('readAllData', st);
|
||||
// return dbPromise
|
||||
// .then(function (db) {
|
||||
// var tx = db.transaction(st, 'readonly');
|
||||
// var store = tx.objectStore(st);
|
||||
// return store.getAll();
|
||||
// });
|
||||
// }
|
||||
|
||||
function clearAllData(st) {
|
||||
console.log('clearAllData', st);
|
||||
return dbPromise
|
||||
.then(function (db) {
|
||||
var tx = db.transaction(st, 'readwrite');
|
||||
var store = tx.objectStore(st);
|
||||
store.clear();
|
||||
return tx.complete;
|
||||
});
|
||||
}
|
||||
// function clearAllData(st) {
|
||||
// console.log('clearAllData', st);
|
||||
// return dbPromise
|
||||
// .then(function (db) {
|
||||
// var tx = db.transaction(st, 'readwrite');
|
||||
// var store = tx.objectStore(st);
|
||||
// store.clear();
|
||||
// return tx.complete;
|
||||
// });
|
||||
// }
|
||||
|
||||
function deleteItemFromData(st, id) {
|
||||
console.log('deleteItemFromData', st, 'ID:', id);
|
||||
dbPromise
|
||||
.then(function (db) {
|
||||
|
||||
var tx = db.transaction(st, 'readwrite');
|
||||
var store = tx.objectStore(st);
|
||||
|
||||
try {
|
||||
store.delete(id);
|
||||
return tx.complete;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.then(function (res) {
|
||||
if (res)
|
||||
console.log('Item deleted!');
|
||||
});
|
||||
}
|
||||
// function deleteItemFromData(st, id) {
|
||||
// console.log('deleteItemFromData', st, 'ID:', id);
|
||||
// dbPromise
|
||||
// .then(function (db) {
|
||||
//
|
||||
// var tx = db.transaction(st, 'readwrite');
|
||||
// var store = tx.objectStore(st);
|
||||
//
|
||||
// try {
|
||||
// store.delete(id);
|
||||
// return tx.complete;
|
||||
// } catch (e) {
|
||||
// return false;
|
||||
// }
|
||||
// })
|
||||
// .then(function (res) {
|
||||
// if (res)
|
||||
// console.log('Item deleted!');
|
||||
// });
|
||||
// }
|
||||
|
||||
function urlBase64ToUint8Array(base64String) {
|
||||
var padding = '='.repeat((4 - base64String.length % 4) % 4);
|
||||
|
||||
Reference in New Issue
Block a user