Skip to content

Commit 2cf425f

Browse files
committed
FIX #2471 RxLocalDocument.$ emits to often
1 parent 9917426 commit 2cf425f

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
### X.X.X (comming soon)
44

5+
Bugfixes:
6+
- `RxLocalDocument.$` emitted to often on changes [#2471](https://github.com/pubkey/rxdb/issues/2471)
7+
58
Other:
6-
- Refactored GraphQL replication to run faster [#2524](https://github.com/pubkey/rxdb/pull/2524/) Thanks [@corinv](https://github.com/corinv)
9+
- Refactored GraphQL replication to run faster [#2524](https://github.com/pubkey/rxdb/pull/2524/)` Thanks [@corinv](https://github.com/corinv)
710

811
### 9.6.0 (7 September 2020)
912

src/plugins/local-documents.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import {
2121
newRxTypeError
2222
} from '../rx-error';
2323
import {
24-
clone, now, LOCAL_PREFIX
24+
clone,
25+
now,
26+
LOCAL_PREFIX
2527
} from '../util';
2628

2729
import type {
@@ -114,7 +116,9 @@ const RxLocalDocumentPrototype: any = {
114116
this: any,
115117
changeEvent: RxChangeEvent
116118
) {
117-
if (changeEvent.documentId !== this.primary) return;
119+
if (changeEvent.documentId !== this.primary) {
120+
return;
121+
}
118122
switch (changeEvent.operation) {
119123
case 'UPDATE':
120124
const newData = clone(changeEvent.documentData);
@@ -202,12 +206,10 @@ const RxLocalDocumentPrototype: any = {
202206
.then((res: any) => {
203207
const endTime = now();
204208
newData._rev = res.rev;
205-
this._dataSync$.next(newData);
206-
207209
const changeEvent = new RxChangeEvent(
208210
'UPDATE',
209211
this.id,
210-
clone(this._data),
212+
clone(newData),
211213
isRxDatabase(this.parent) ? this.parent.token : this.parent.database.token,
212214
isRxCollection(this.parent) ? this.parent.name : null,
213215
true,
@@ -294,8 +296,9 @@ RxLocalDocument.create = (id: string, data: any, parent: any) => {
294296
* throws if already exists
295297
*/
296298
function insertLocal(this: any, id: string, data: any): Promise<RxLocalDocument> {
297-
if (isRxCollection(this) && this._isInMemory)
299+
if (isRxCollection(this) && this._isInMemory) {
298300
return this._parentCollection.insertLocal(id, data);
301+
}
299302

300303
data = clone(data);
301304

@@ -326,15 +329,16 @@ function insertLocal(this: any, id: string, data: any): Promise<RxLocalDocument>
326329
* overwrites existing if exists
327330
*/
328331
function upsertLocal(this: any, id: string, data: any): Promise<RxLocalDocument> {
329-
if (isRxCollection(this) && this._isInMemory)
332+
if (isRxCollection(this) && this._isInMemory) {
330333
return this._parentCollection.upsertLocal(id, data);
334+
}
331335

332336
return this.getLocal(id)
333-
.then((existing: any) => {
337+
.then((existing: RxDocument) => {
334338
if (!existing) {
335339
// create new one
336-
const doc = this.insertLocal(id, data);
337-
return doc;
340+
const docPromise = this.insertLocal(id, data);
341+
return docPromise;
338342
} else {
339343
// update existing
340344
data._rev = existing._data._rev;

test/unit/local-documents.test.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import AsyncTestUtil from 'async-test-util';
2+
import AsyncTestUtil, { wait } from 'async-test-util';
33

44
import * as humansCollection from '../helper/humans-collection';
55
import * as schemas from '../helper/schemas';
@@ -103,17 +103,44 @@ config.parallel('local-documents.test.js', () => {
103103
c.database.destroy();
104104
});
105105
it('should update when exists', async () => {
106-
const c = await humansCollection.create();
106+
const c = await humansCollection.create(0);
107107
await c.upsertLocal('foobar', {
108108
foo: 'bar'
109109
});
110110
const doc = await c.upsertLocal('foobar', {
111111
foo: 'bar2'
112112
});
113+
await wait(1000);
113114
assert.ok(doc);
114115
assert.strictEqual(doc.get('foo'), 'bar2');
115116
c.database.destroy();
116117
});
118+
/**
119+
* @link https://github.com/pubkey/rxdb/issues/2471
120+
*/
121+
it('should invoke subscription once', async () => {
122+
const c = await humansCollection.create();
123+
const emitted: any[] = [];
124+
const doc = await c.upsertLocal('foobar', {
125+
foo: 'barOne',
126+
});
127+
await wait(50);
128+
const docSub = doc.$.subscribe(x => {
129+
emitted.push(x);
130+
});
131+
await c.upsertLocal('foobar', {
132+
foo: 'barTwo',
133+
});
134+
135+
assert.strictEqual(emitted.length, 2);
136+
// first 'barOne' is emitted because.$ is a BehaviorSubject
137+
assert.strictEqual(emitted[0].foo, 'barOne');
138+
// second after the change, barTwo is emitted
139+
assert.strictEqual(emitted[1].foo, 'barTwo');
140+
141+
docSub.unsubscribe();
142+
c.database.destroy();
143+
});
117144
});
118145
describe('negative', () => { });
119146
});

0 commit comments

Comments
 (0)