Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
let request = Orbit.globals.indexedDB.deleteDatabase(this.dbName);
request.onerror = (/* event */) => {
// console.error('error deleting indexedDB', this.dbName);
reject(request.error);
};
request.onsuccess = (/* event */) => {
// console.log('success deleting indexedDB', this.dbName);
resolve();
};
});
}
return new Promise((resolve, reject) => {
if (this._db) {
resolve(this._db);
} else {
let request = Orbit.globals.indexedDB.open(this.dbName, this.dbVersion);
request.onerror = (/* event */) => {
// console.error('error opening indexedDB', this.dbName);
reject(request.error);
};
request.onsuccess = (/* event */) => {
// console.log('success opening indexedDB', this.dbName);
const db = (this._db = request.result);
resolve(db);
};
request.onupgradeneeded = (event: any) => {
// console.log('indexedDB upgrade needed');
const db = (this._db = event.target.result);
if (event && event.oldVersion > 0) {
fetch(url: string, customSettings?: FetchSettings): Promise {
let settings = this.initFetchSettings(customSettings);
let fullUrl = url;
if (settings.params) {
fullUrl = this.urlBuilder.appendQueryParams(fullUrl, settings.params);
delete settings.params;
}
let fetchFn = (Orbit as any).fetch || Orbit.globals.fetch;
// console.log('fetch', fullUrl, settings, 'polyfill', fetchFn.polyfill);
if (settings.timeout) {
let timeout = settings.timeout;
delete settings.timeout;
return new Promise((resolve, reject) => {
let timedOut: boolean;
let timer = Orbit.globals.setTimeout(() => {
timedOut = true;
reject(new NetworkError(`No fetch response within ${timeout}ms.`));
}, timeout);
fetchFn(fullUrl, settings)
const operations = [];
const typeFilter = expression.type;
for (let key in Orbit.globals.localStorage) {
if (key.indexOf(source.namespace + source.delimiter) === 0) {
let typesMatch = isNone(typeFilter);
if (!typesMatch) {
let fragments = key.split(source.delimiter);
let type = fragments[1];
typesMatch = (typeFilter === type);
}
if (typesMatch) {
let record = JSON.parse(Orbit.globals.localStorage.getItem(key));
if (source.keyMap) {
source.keyMap.pushRecord(record);
}
operations.push({
op: 'addRecord',
record
});
}
}
}
return Orbit.Promise.resolve([buildTransform(operations)]);
},
findRecords(source: LocalStorageSource, expression: FindRecords): Promise {
const operations = [];
const typeFilter = expression.type;
for (let key in Orbit.globals.localStorage) {
if (key.indexOf(source.namespace + source.delimiter) === 0) {
let typesMatch = isNone(typeFilter);
if (!typesMatch) {
let fragments = key.split(source.delimiter);
let type = fragments[1];
typesMatch = (typeFilter === type);
}
if (typesMatch) {
let record = JSON.parse(Orbit.globals.localStorage.getItem(key));
if (source.keyMap) {
source.keyMap.pushRecord(record);
}
.then((response: any) => {
Orbit.globals.clearTimeout(timer);
if (!timedOut) {
return this.handleFetchResponse(response);
}
})
.then(resolve, reject);
getRecordSync(identity: RecordIdentity): Record | undefined {
const key = this.getKeyForRecord(identity);
const item: string | undefined = Orbit.globals.localStorage.getItem(key);
if (item) {
const record: Record = JSON.parse(item);
if (this._keyMap) {
this._keyMap.pushRecord(record);
}
return record;
}
return undefined;
}
removeRecordSync(recordIdentity: RecordIdentity): Record {
const record = this.getRecordSync(recordIdentity);
if (record) {
const key = this.getKeyForRecord(record);
Orbit.globals.localStorage.removeItem(key);
return record;
} else {
return null;
}
}