Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove & other promise returning functions #833

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/data-migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ class OldCollection {
/**
* @return {Promise}
*/
countAllUndeleted() {
async countAllUndeleted() {
return PouchDB.countAllUndeleted(this.pouchdb);
}

getBatch(batchSize) {
async getBatch(batchSize) {
return PouchDB
.getBatch(this.pouchdb, batchSize)
.then(docs => docs
Expand Down Expand Up @@ -264,7 +264,7 @@ class OldCollection {
* deletes this.pouchdb and removes it from the database.collectionsCollection
* @return {Promise}
*/
delete() {
async delete() {
return this
.pouchdb.destroy()
.then(() => this.database.removeCollectionDoc(this.dataMigrator.name, this.schema));
Expand Down Expand Up @@ -328,7 +328,7 @@ class OldCollection {
* get an array with OldCollection-instances from all existing old pouchdb-instance
* @return {Promise<OldCollection[]>}
*/
export function _getOldCollections(dataMigrator) {
export async function _getOldCollections(dataMigrator) {
return Promise
.all(
dataMigrator.currentSchema.previousVersions
Expand All @@ -346,8 +346,8 @@ export function _getOldCollections(dataMigrator) {
* returns true if a migration is needed
* @return {Promise<boolean>}
*/
export function mustMigrate(dataMigrator) {
if (dataMigrator.currentSchema.version === 0) return Promise.resolve(false);
export async function mustMigrate(dataMigrator) {
if (dataMigrator.currentSchema.version === 0) return false;
return _getOldCollections(dataMigrator)
.then(oldCols => {
if (oldCols.length === 0) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function runPluginHooks(hookKey, obj) {
/**
* @return {Promise}
*/
export function runAsyncPluginHooks(hookKey, obj) {
export async function runAsyncPluginHooks(hookKey, obj) {
return Promise.all(
HOOKS[hookKey].map(fun => fun(obj))
);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function ensureSchemaSupportsAttachments(doc) {
}
}

function resyncRxDocument(doc) {
async function resyncRxDocument(doc) {
return doc.collection.pouch.get(doc.primary).then(docData => {
const data = doc.collection._handleFromPouch(docData);
const changeEvent = RxChangeEvent.create(
Expand Down Expand Up @@ -65,7 +65,7 @@ export const blobBufferUtil = {
}
return blobBuffer;
},
toString(blobBuffer) {
async toString(blobBuffer) {
if (blobBuffer instanceof Buffer) {
// node
return nextTick()
Expand Down Expand Up @@ -165,7 +165,7 @@ function shouldEncrypt(doc) {
/**
* @return {Promise}
*/
export function putAttachment({
export async function putAttachment({
id,
data,
type = 'text/plain'
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/in-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class InMemoryRxCollection extends RxCollection.RxCollection {
* in the parent collection
* @return {Promise}
*/
awaitPersistence() {
if (this._nonPersistentRevisions.size === 0) return Promise.resolve();
async awaitPersistence() {
if (this._nonPersistentRevisions.size === 0) return;
return this._nonPersistentRevisionsSubject.pipe(
filter(() => this._nonPersistentRevisions.size === 0),
first()
Expand Down Expand Up @@ -216,7 +216,7 @@ function toCleanSchema(rxSchema) {
* @param {RxCollection} toCollection
* @return {Promise<{}[]>} Promise that resolves with an array of the docs data
*/
export function replicateExistingDocuments(fromCollection, toCollection) {
export async function replicateExistingDocuments(fromCollection, toCollection) {
return fromCollection.pouch.allDocs({
attachments: false,
include_docs: true
Expand Down Expand Up @@ -247,7 +247,7 @@ export function replicateExistingDocuments(fromCollection, toCollection) {
* @param {PouchDB} pouch
* @return {Promise<void>}
*/
export function setIndexes(schema, pouch) {
export async function setIndexes(schema, pouch) {
return Promise.all(
schema.indexes
.map(indexAr => {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/json-dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RxChangeEvent from '../rx-change-event';
/**
* @return {Promise}
*/
const dumpRxDatabase = function (decrypted = false, collections = null) {
const dumpRxDatabase = async function (decrypted = false, collections = null) {
const json = {
name: this.name,
instanceToken: this.token,
Expand Down Expand Up @@ -40,7 +40,7 @@ const dumpRxDatabase = function (decrypted = false, collections = null) {
});
};

const importDumpRxDatabase = function (dump) {
const importDumpRxDatabase = async function (dump) {
/**
* collections must be created before the import
* because we do not know about the other collection-settings here
Expand All @@ -60,7 +60,7 @@ const importDumpRxDatabase = function (dump) {
);
};

const dumpRxCollection = function (decrypted = false) {
const dumpRxCollection = async function (decrypted = false) {
const encrypted = !decrypted;

const json = {
Expand Down Expand Up @@ -91,7 +91,7 @@ const dumpRxCollection = function (decrypted = false) {
/**
* @return {Promise}
*/
const importDumpRxCollection = function (exportedJSON) {
const importDumpRxCollection = async function (exportedJSON) {
// check schemaHash
if (exportedJSON.schemaHash !== this.schema.hash) {
throw RxError.newRxError('JD2', {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LeaderElector {
/**
* @return {Promise} promise which resolve when the instance becomes leader
*/
waitForLeadership() {
async waitForLeadership() {
return this.elector.awaitLeadership().then(() => {
this.isLeader = true;
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/local-documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const RxLocalDocumentPrototype = {
/**
* @return {Promise}
*/
remove() {
async remove() {
const removeId = LOCAL_PREFIX + this.id;
return this.parentPouch.remove(removeId, this._data._rev)
.then(() => {
Expand Down Expand Up @@ -263,7 +263,7 @@ const _getPouchByParent = parent => {
* throws if already exists
* @return {Promise<RxLocalDocument>}
*/
const insertLocal = function (id, data) {
const insertLocal = async function (id, data) {
if (RxCollection.isInstanceOf(this) && this._isInMemory)
return this._parentCollection.insertLocal(id, data);

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/watch-for-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export function watchForChanges() {
* @param {*} change
* @return {Promise<boolean>}
*/
function _handleSingleChange(collection, change) {
if (change.id.charAt(0) === '_') return Promise.resolve(false); // do not handle changes of internal docs
async function _handleSingleChange(collection, change) {
if (change.id.charAt(0) === '_') return false; // do not handle changes of internal docs

// wait 2 ticks and 20 ms to give the internal event-handling time to run
return promiseWait(20)
Expand All @@ -65,7 +65,7 @@ function _handleSingleChange(collection, change) {
.then(() => {
const docData = change.doc;
// already handled by internal event-stream
if (collection._changeEventBuffer.hasChangeWithRevision(docData._rev)) return Promise.resolve(false);
if (collection._changeEventBuffer.hasChangeWithRevision(docData._rev)) return false;

const cE = RxChangeEvent.fromPouchChange(docData, collection);

Expand Down
4 changes: 2 additions & 2 deletions src/pouch-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import RxError from './rx-error';
* @param {PouchDB} pouchdb instance
* @return {Promise<number>} number of documents
*/
PouchDB.countAllUndeleted = function(pouchdb) {
PouchDB.countAllUndeleted = async function(pouchdb) {
return pouchdb
.allDocs({
include_docs: false,
Expand All @@ -43,7 +43,7 @@ PouchDB.countAllUndeleted = function(pouchdb) {
* @param {number} limit
* @return {Promise<{}[]>} array with documents
*/
PouchDB.getBatch = function(pouchdb, limit) {
PouchDB.getBatch = async function(pouchdb, limit) {
if (limit <= 1) {
throw RxError.newRxError('P1', {
limit
Expand Down
24 changes: 12 additions & 12 deletions src/rx-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class RxCollection {

_applyHookFunctions(this);
}
prepare() {
async prepare() {
this.pouch = this.database._spawnPouchDB(this.name, this.schema.version, this._pouchSettings);

if (this.schema.doKeyCompression()) {
Expand Down Expand Up @@ -250,7 +250,7 @@ export class RxCollection {
* @param {[type]} key [description]
* @return {[type]} [description]
*/
_pouchGet(key) {
async _pouchGet(key) {
return this
.pouch
.get(key)
Expand All @@ -264,7 +264,7 @@ export class RxCollection {
* @param {?boolean} noDecrypt if true, decryption will not be made
* @return {Object[]} array with documents-data
*/
_pouchFind(rxQuery, limit, noDecrypt = false) {
async _pouchFind(rxQuery, limit, noDecrypt = false) {
const compressedQueryJSON = rxQuery.keyCompress();
if (limit) compressedQueryJSON.limit = limit;

Expand Down Expand Up @@ -352,7 +352,7 @@ export class RxCollection {
* @param {RxDocument} doc which was created
* @return {Promise<RxDocument>}
*/
insert(json) {
async insert(json) {
// inserting a temporary-document
let tempDoc = null;
if (RxDocument.isInstanceOf(json)) {
Expand Down Expand Up @@ -413,7 +413,7 @@ export class RxCollection {
* same as insert but overwrites existing document with same primary
* @return {Promise<RxDocument>}
*/
upsert(json) {
async upsert(json) {
json = clone(json);
const primary = json[this.schema.primaryPath];
if (!primary) {
Expand Down Expand Up @@ -441,7 +441,7 @@ export class RxCollection {
* @param {object} json
* @return {Promise}
*/
atomicUpsert(json) {
async atomicUpsert(json) {
json = clone(json);
const primary = json[this.schema.primaryPath];
if (!primary) {
Expand Down Expand Up @@ -606,9 +606,9 @@ export class RxCollection {
/**
* @return {Promise<void>}
*/
_runHooks(when, key, data, instance) {
async _runHooks(when, key, data, instance) {
const hooks = this.getHooks(when, key);
if (!hooks) return Promise.resolve();
if (!hooks) return;

// run parallel: false
const tasks = hooks.series.map(hook => () => hook(data, instance));
Expand Down Expand Up @@ -798,7 +798,7 @@ const checkOrmMethods = function(statics) {
/**
* @return {Promise}
*/
function _atomicUpsertUpdate(doc, json) {
async function _atomicUpsertUpdate(doc, json) {
return doc.atomicUpdate(innerDoc => {
json._rev = innerDoc._rev;
innerDoc._data = json;
Expand All @@ -812,7 +812,7 @@ function _atomicUpsertUpdate(doc, json) {
* @param {any} json
* @return {Promise<{ doc: RxDocument, inserted: boolean}>} promise that resolves with new doc and flag if inserted
*/
function _atomicUpsertEnsureRxDocumentExists(rxCollection, primary, json) {
async function _atomicUpsertEnsureRxDocumentExists(rxCollection, primary, json) {
return rxCollection.findOne(primary).exec()
.then(doc => {
if (!doc) {
Expand Down Expand Up @@ -848,7 +848,7 @@ export function getDocumentOrmPrototype(rxCollection) {
/**
* creates the indexes in the pouchdb
*/
function _prepareCreateIndexes(rxCollection, spawnedPouchPromise) {
async function _prepareCreateIndexes(rxCollection, spawnedPouchPromise) {
return Promise.all(
rxCollection.schema.indexes
.map(indexAr => {
Expand Down Expand Up @@ -881,7 +881,7 @@ function _prepareCreateIndexes(rxCollection, spawnedPouchPromise) {
* @param {?Object} [migrationStrategies={}]
* @return {Promise<RxCollection>} promise with collection
*/
export function create({
export async function create({
database,
name,
schema,
Expand Down
22 changes: 11 additions & 11 deletions src/rx-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class RxDatabase {
);
}

dangerousRemoveCollectionInfo() {
async dangerousRemoveCollectionInfo() {
const colPouch = this._collectionsPouch;
return colPouch.allDocs()
.then(docsRes => {
Expand Down Expand Up @@ -102,8 +102,8 @@ export class RxDatabase {
/**
* @return {Promise}
*/
waitForLeadership() {
if (!this.multiInstance) return Promise.resolve(true);
async waitForLeadership() {
if (!this.multiInstance) return true;
return this.leaderElector.waitForLeadership();
}

Expand Down Expand Up @@ -135,9 +135,9 @@ export class RxDatabase {

/**
* removes the collection-doc from this._collectionsPouch
* @return {Promise}
* }
*/
removeCollectionDoc(name, schema) {
async removeCollectionDoc(name, schema) {
const docId = _collectionNamePrimary(name, schema);
return this
._collectionsPouch
Expand Down Expand Up @@ -351,7 +351,7 @@ export class RxDatabase {
* deletes the database and its stored data
* @return {Promise}
*/
remove() {
async remove() {
return this
.destroy()
.then(() => removeDatabase(this.name, this.adapter));
Expand Down Expand Up @@ -472,7 +472,7 @@ export async function _ensureStorageTokenExists(rxDatabase) {
* @param {RxChangeEvent} changeEvent
* @return {Promise<boolean>}
*/
export function writeToSocket(rxDatabase, changeEvent) {
export async function writeToSocket(rxDatabase, changeEvent) {
if (
rxDatabase.multiInstance &&
!changeEvent.isIntern() &&
Expand All @@ -488,7 +488,7 @@ export function writeToSocket(rxDatabase, changeEvent) {
};
return rxDatabase.broadcastChannel.postMessage(sendOverChannel);
} else
return Promise.resolve(false);
return false;
}

/**
Expand All @@ -506,7 +506,7 @@ export function _collectionNamePrimary(name, schema) {
* @param {string} collectionName
* @return {Promise<string[]>} resolves all known collection-versions
*/
export function _removeAllOfCollection(rxDatabase, collectionName) {
export async function _removeAllOfCollection(rxDatabase, collectionName) {

return rxDatabase.lockedRun(
() => rxDatabase._collectionsPouch.allDocs({
Expand Down Expand Up @@ -577,7 +577,7 @@ async function prepare(rxDatabase) {
}


export function create({
export async function create({
name,
adapter,
password,
Expand Down Expand Up @@ -720,7 +720,7 @@ export async function removeDatabase(databaseName, adapter) {
* check is the given adapter can be used
* @return {Promise}
*/
export function checkAdapter(adapter) {
export async function checkAdapter(adapter) {
return overwritable.checkAdapter(adapter);
}

Expand Down
Loading