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

Feature/rename destroy to close #6602

Open
wants to merge 15 commits into
base: release/16.0.0
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
- CHANGE (internal) remove `conflictResultionTasks()` and `resolveConflictResultionTask()` from the RxStorage interface.
- FIX (GraphQL replication) datapath must be equivalent for pull and push [#6019](https://github.com/pubkey/rxdb/pull/6019)
- CHANGE `ignoreDuplicate: true` on `createRxDatabase()` must only be allowed in dev-mode.
- RENAME
- `.destroy()` to `.close()`
- `.onDestroy()` to `.onClose()`
- `postDestroyRxCollection` to `postCloseRxCollection`
- `preDestroyRxDatabase` to `preCloseRxDatabase`
-
<!-- ADD new changes here! -->

<!-- /CHANGELOG NEWEST -->
Expand Down
2 changes: 1 addition & 1 deletion docs-src/docs/migration-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ myDatabase.addCollections({

## autoMigrate

By default, the migration automatically happens when the collection is created. Calling `RxDatabase.addRxCollections()` returns only when the migration has finished.
By default, the migration automatically happens when the collection is created. Calling `RxDatabase.addCollections()` returns only when the migration has finished.
If you have lots of data or the migrationStrategies take a long time, it might be better to start the migration 'by hand' and show the migration-state to the user as a loading-bar.

```javascript
Expand Down
12 changes: 6 additions & 6 deletions docs-src/docs/rx-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,19 @@ await myCollection.remove();
// collection is now removed and can be re-created
```

### destroy()
Destroys the collection's object instance. This is to free up memory and stop all observers and replications.
### close()
Removes the collection's object instance from the [RxDatabase](./rx-database.md). This is to free up memory and stop all observers and replications. It will not delete the collections data. When you create the collection again with `database.addCollections()`, the newly added collection will still have all data.
```js
await myCollection.destroy();
await myCollection.close();
```

### onDestroy / onRemove()
With these you can add a function that is run when the collection was destroyed or removed.
### onClose / onRemove()
With these you can add a function that is run when the collection was closed or removed.
This works even across multiple browser tabs so you can detect when another tab removes the collection
and you application can behave accordingly.

```js
await myCollection.onDestroy(() => console.log('I am destroyed'));
await myCollection.onClose(() => console.log('I am closed'));
await myCollection.onRemove(() => console.log('I am removed'));
```

Expand Down
9 changes: 5 additions & 4 deletions docs-src/docs/rx-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ myDatabase.requestIdlePromise(1000 /* time in ms */).then(() => {

```

### destroy()
Destroys the databases object-instance. This is to free up memory and stop all observers and replications.
Returns a `Promise` that resolves when the database is destroyed.
### close()
Closes the databases object-instance. This is to free up memory and stop all observers and replications.
Returns a `Promise` that resolves when the database is closed.
Closing a database will not remove the databases data. When you create the database again with `createRxDatabase()`, all data will still be there.
```javascript
await myDatabase.destroy();
await myDatabase.close();
```

### remove()
Expand Down
4 changes: 2 additions & 2 deletions docs-src/docs/rx-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ const pipeline = await emailCollection.addPipeline({

You can await the idleness of a pipeline with `await myRxPipeline.awaitIdle()`. This will await a promise that resolved when the pipeline has processed all documents and is not running anymore.

### destroy()
### close()

`await myRxPipeline.destroy()` stops the pipeline so that is no longer doing stuff. This is automatically called when the RxCollection or RxDatabase of the pipeline is destroyed.
`await myRxPipeline.close()` stops the pipeline so that is no longer doing stuff. This is automatically called when the RxCollection or RxDatabase of the pipeline is closed.

### remove()

Expand Down
69 changes: 27 additions & 42 deletions docs-src/docs/transactions-conflicts-revisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,54 +63,39 @@ When a document is send to the backend and the backend detected a conflict (by c

## Custom conflict handler

A conflict handler is a JavaScript function that has two tasks:
- Detect if a conflict exists
- Solve existing conflicts
A conflict handler is an object with two JavaScript functions:
- Detect if two document states are equal
- Solve existing conflicts

Because the conflict handler also is used for conflict detection, it will run many times on pull-, push- and write operations of RxDB. Most of the time it will detect that there is no conflict and then return.

Lets have a look at the default conflict handler of RxDB to learn how to create a custom one:
Lets have a look at the [default conflict handler](https://github.com/pubkey/rxdb/blob/master/src/replication-protocol/default-conflict-handler.ts) of RxDB to learn how to create a custom one:

```ts
export const defaultConflictHandler: RxConflictHandler<any> = function (
/**
* The conflict handler gets 3 input properties:
* - assumedMasterState: The state of the document that is assumed to be on the master branch
* - newDocumentState: The new document state of the fork branch (=client) that RxDB want to write to the master
* - realMasterState: The real master state of the document
*/
i: RxConflictHandlerInput<any>
): Promise<RxConflictHandlerOutput<any>> {
/**
* Here we detect if a conflict exists in the first place.
* If there is no conflict, we return isEqual=true.
* If there is a conflict, return isEqual=false.
* In the default handler we do a deepEqual check,
* but in your custom conflict handler you probably want
* to compare specific properties of the document, like the updatedAt time,
* for better performance because deepEqual() is expensive.
*/
if (deepEqual(
i.newDocumentState,
i.realMasterState
)) {
return Promise.resolve({
isEqual: true
});
import { deepEqual } from 'rxdb/plugins/utils';
export const defaultConflictHandler: RxConflictHandler<any> = {
isEqual(a, b) {
/**
* isEqual() is used to detect conflicts or to detect if a
* document has to be pushed to the remote.
* If the documents are deep equal,
* we have no conflict.
* Because deepEqual is CPU expensive, on your custom conflict handler you might only
* check some properties, like the updatedAt time or revisions
* for better performance.
*/
return deepEqual(a, b);
},
resolve(i) {
/**
* The default conflict handler will always
* drop the fork state and use the master state instead.
*
* In your custom conflict handler you likely want to merge properties
* of the realMasterState and the newDocumentState instead.
*/
return i.realMasterState;
}

/**
* If a conflict exists, we have to resolve it.
* The default conflict handler will always
* drop the fork state and use the master state instead.
*
* In your custom conflict handler you likely want to merge properties
* of the realMasterState and the newDocumentState instead.
*/
return Promise.resolve({
isEqual: false,
documentData: i.realMasterState
});
};
```

Expand Down
2 changes: 1 addition & 1 deletion docs-src/docs/tutorials/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ console.log(amount);
/**
* clean up
*/
myDatabase.destroy();
myDatabase.close();
```


Expand Down
2 changes: 1 addition & 1 deletion examples/electron/test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = (function () {
const readData = await attachment.getStringData();
assert.strictEqual(readData, dataString);

await db.destroy();
await db.close();
}());
};
return runTests;
Expand Down
Loading
Loading