Skip to content

Commit

Permalink
Merge pull request #552 from Nozbe/idb-fixes2
Browse files Browse the repository at this point in the history
IDB fixes
  • Loading branch information
radex authored Nov 6, 2019
2 parents c1fc0ab + 57c41d0 commit 5f7dc29
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ This is a **massive** new update to WatermelonDB! 🍉
- [LokiJS] Introduces new `new LokiJSAdapter({ ..., useWebWorker: false })` option. Before, web workers
were always used with `LokiJSAdapter`. Although web workers may have some performance benefits, disabling them
may lead to lower memory consumption, lower latency, and easier debugging. YMMV.

- [LokiJS] Added `onIndexedDBVersionChange` option to `LokiJSAdapter`. This is a callback that's called
when internal IDB version changed (most likely the database was deleted in another browser tab).
Pass a callback to force log out in this copy of the app as well. Note that this only works when
using incrementalIDB and not using web workers
- [Model] Add `Model._dangerouslySetRawWithoutMarkingColumnChange()` method. You probably shouldn't use it,
but if you know what you're doing and want to live-update records from server without marking record as updated,
this is useful
Expand Down
28 changes: 17 additions & 11 deletions docs-master/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ npm install @nozbe/with-observables
yarn add --dev @babel/plugin-proposal-decorators
```
or

```bash
npm install -D @babel/plugin-proposal-decorators
2. Add ES6 decorators support to your `.babelrc` file:
```json
{
Expand All @@ -38,9 +38,9 @@ npm install @nozbe/with-observables
### iOS (React Native)
1. **Set up Babel config in your project**
See instructions above ⬆️
2. **Add Swift support to your Xcode project**:
- Open `ios/YourAppName.xcodeproj` in Xcode
- Right-click on **Your App Name** in the Project Navigator on the left, and click **New File…**
Expand All @@ -59,21 +59,21 @@ npm install @nozbe/with-observables
1. Open your project in Xcode, right click on **Libraries** in the Project Navigator on the left and click **Add Files to "Your Project Name"**. Look under `node_modules/@nozbe/watermelondb/native/ios` and select `WatermelonDB.xcodeproj`
2. Go to Project settings (top item in the Project navigator on the left), select your app name under **Targets****Build Phases****Link Binary With Libraries**, and add `libWatermelonDB.a`
For more information about linking libraries manually, [see React Native documentation](https://facebook.github.io/react-native/docs/linking-libraries-ios).
**Using CocoaPods**
[Please contribute!](https://github.com/Nozbe/WatermelonDB/issues/279)
Note that Xcode 9.4 and a deployment target of at least iOS 9.0 is required (although Xcode 10 and iOS 11.0 are recommended).
### Android (React Native)
1. **Set up Babel config in your project**
See instructions above ⬆️
1. In `android/settings.gradle`, add:
```gradle
Expand Down Expand Up @@ -141,11 +141,11 @@ This guide assumes you use Webpack as your bundler.
yarn add --dev worker-loader
```
or
```bash
npm install -D worker-loader
```
2. And add this to Webpack configuration:
```js
// webpack.config.js
Expand Down Expand Up @@ -241,6 +241,12 @@ import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs'
const adapter = new LokiJSAdapter({
schema,
// useWebWorker: false,
// experimentalUseIncrementalIndexedDB: true,
// onIndexedDBVersionChange: () => {
// // database was deleted in another browser tab
// services.forceLogOut()
// },
})
// The rest is the same!
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nozbe/watermelondb",
"description": "Build powerful React Native and React web apps that scale from hundreds to tens of thousands of records and remain fast",
"version": "0.15.0-8",
"version": "0.15.0-10",
"scripts": {
"build": "NODE_ENV=production node ./scripts/make.js",
"dev": "NODE_ENV=development node ./scripts/make.js",
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"lokijs": "git+https://github.com/Nozbe/LokiJS.git#f9f6f73c4bfe1d6df27c99d7a5e18f730754e904",
"lokijs": "git+https://github.com/Nozbe/LokiJS.git#384b80f",
"rambdax": "2.15.0",
"rxjs": "^6.2.2",
"rxjs-compat": "^6.3.2",
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/lokijs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export type LokiAdapterOptions = $Exact<{
// may lead to lower memory consumption, lower latency, and easier debugging
useWebWorker?: boolean,
experimentalUseIncrementalIndexedDB?: boolean,
// Called when internal IDB version changed (most likely the database was deleted in another browser tab)
// Pass a callback to force log out in this copy of the app as well
// Note that this only works when using incrementalIDB and not using web workers
onIndexedDBVersionChange?: () => void,
// -- internal --
_testLokiAdapter?: LokiMemoryAdapter,
}>
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/lokijs/worker/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export default class LokiExecutor {

experimentalUseIncrementalIndexedDB: boolean

onIndexedDBVersionChange: ?(() => void)

_testLokiAdapter: ?LokiMemoryAdapter

cachedRecords: Map<TableName<any>, Set<RecordId>> = new Map()
Expand All @@ -45,6 +47,7 @@ export default class LokiExecutor {
this.schema = schema
this.migrations = migrations
this.experimentalUseIncrementalIndexedDB = options.experimentalUseIncrementalIndexedDB || false
this.onIndexedDBVersionChange = options.onIndexedDBVersionChange
this._testLokiAdapter = _testLokiAdapter
}

Expand Down Expand Up @@ -236,6 +239,7 @@ export default class LokiExecutor {
this.dbName,
this._testLokiAdapter,
this.experimentalUseIncrementalIndexedDB,
this.onIndexedDBVersionChange,
)

logger.log('[DB][Worker] Database loaded')
Expand Down
24 changes: 19 additions & 5 deletions src/adapters/lokijs/worker/lokiExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,36 @@ const isIDBAvailable = () => {
}

// in Firefox private mode, IDB will be available, but will fail to open
const db = indexedDB.open('WatermelonIDBChecker')
db.onsuccess = () => resolve(true)
db.onerror = () => resolve(false)
const checkRequest: IDBOpenDBRequest = indexedDB.open('WatermelonIDBChecker')
checkRequest.onsuccess = e => {
const db: IDBDatabase = e.target.result
db.close()
resolve(true)
}
checkRequest.onerror = () => {
resolve(false)
}
checkRequest.onblocked = () => {
// eslint-disable-next-line no-console
console.error('WatermelonIDBChecker call is blocked')
}
})
}

async function getLokiAdapter(
name: ?string,
adapter: ?LokiMemoryAdapter,
useIncrementalIDB: boolean,
onIndexedDBVersionChange: ?(() => void),
): mixed {
if (adapter) {
return adapter
} else if (await isIDBAvailable()) {
if (useIncrementalIDB) {
const IncrementalIDBAdapter = require('lokijs/src/incremental-indexeddb-adapter')
return new IncrementalIDBAdapter()
return new IncrementalIDBAdapter({
onversionchange: onIndexedDBVersionChange,
})
}
const LokiIndexedAdapter = require('lokijs/src/loki-indexed-adapter')
return new LokiIndexedAdapter(name)
Expand All @@ -42,9 +55,10 @@ export async function newLoki(
name: ?string,
adapter: ?LokiMemoryAdapter,
useIncrementalIDB: boolean,
onIndexedDBVersionChange: ?(() => void),
): Loki {
const loki = new Loki(name, {
adapter: await getLokiAdapter(name, adapter, useIncrementalIDB),
adapter: await getLokiAdapter(name, adapter, useIncrementalIDB, onIndexedDBVersionChange),
autosave: true,
autosaveInterval: 250,
verbose: true,
Expand Down

0 comments on commit 5f7dc29

Please # to comment.