-
Notifications
You must be signed in to change notification settings - Fork 6
/
plugins.ts
33 lines (30 loc) · 963 Bytes
/
plugins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Subject } from 'rxjs';
import { RxCollection, RxPlugin } from 'rxdb';
import { RxDatabaseBase } from 'rxdb/dist/types/rx-database';
type CollectionRecord = Record<string, RxCollection>;
export type RxDatabaseBaseExtended<
Internals = any,
Options = any
> = RxDatabaseBase<Internals, Options> & {
newCollections$?: Subject<CollectionRecord>;
};
/**
* Extends RxDB prototype with a newCollections$ property: a stream emitting any
* new collections added via addCollections().
*/
export const observeNewCollections: RxPlugin = {
name: 'new-collection-observer',
rxdb: true,
prototypes: {
RxDatabase: (proto: RxDatabaseBaseExtended) => {
const newCollections$ = new Subject<CollectionRecord>();
proto.newCollections$ = newCollections$;
const orig = proto.addCollections;
proto.addCollections = async function (...args) {
const col = await orig.apply(this, args);
newCollections$.next(col);
return col;
};
},
},
};