Skip to content

Commit 4a53e9a

Browse files
authored
Revert "feat: db8 error handling (#6995)" (#6999)
This reverts commit f6c9909.
1 parent f6c9909 commit 4a53e9a

File tree

4 files changed

+54
-100
lines changed

4 files changed

+54
-100
lines changed

dist/types/plugins/dev-mode/error-messages.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export declare const ERROR_MESSAGES: {
4343
DB4: string;
4444
DB5: string;
4545
DB6: string;
46+
DB8: string;
4647
DB9: string;
4748
DB11: string;
4849
DB12: string;

src/plugins/dev-mode/error-messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export const ERROR_MESSAGES = {
7070
DB5: 'RxDatabase.addCollections(): collection-name not allowed',
7171
DB6: 'RxDatabase.addCollections(): another instance created this collection with a different schema. Read this https://rxdb.info/questions-answers.html?console=qa#cant-change-the-schema ',
7272
// removed in 13.0.0 (now part of the encryption plugin) DB7: 'RxDatabase.addCollections(): schema encrypted but no password given',
73-
// removed in xxxx (new instances wait for old instances to close) DB8: 'createRxDatabase(): A RxDatabase with the same name and adapter already exists.\n' +
74-
// 'Make sure to use this combination only once or set ignoreDuplicate to true if you do this intentional-\n' +
75-
// 'This often happens in react projects with hot reload that reloads the code without reloading the process.',
73+
DB8: 'createRxDatabase(): A RxDatabase with the same name and adapter already exists.\n' +
74+
'Make sure to use this combination only once or set ignoreDuplicate to true if you do this intentional-\n' +
75+
'This often happens in react projects with hot reload that reloads the code without reloading the process.',
7676
DB9: 'ignoreDuplicate is only allowed in dev-mode and must never be used in production',
7777
// removed in 14.0.0 - PouchDB RxStorage is removed - DB9: 'createRxDatabase(): Adapter not added. Use addPouchPlugin(require(\'pouchdb-adapter-[adaptername]\'));',
7878
// removed in 14.0.0 - PouchDB RxStorage is removed DB10: 'createRxDatabase(): To use leveldown-adapters, you have to add the leveldb-plugin. Use addPouchPlugin(require(\'pouchdb-adapter-leveldb\'));',

src/rx-database.ts

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ import type { RxMigrationState } from './plugins/migration-schema/index.ts';
8585
import type { RxReactivityFactory } from './types/plugins/reactivity.d.ts';
8686
import { rxChangeEventBulkToRxChangeEvents } from './rx-change-event.ts';
8787

88-
const DATABASE_CLOSED_PROMISE_MAP: Map<string, Set<Promise<void>>> = new Map();
88+
/**
89+
* stores the used database names+storage names
90+
* so we can throw when the same database is created more then once.
91+
*/
92+
const USED_DATABASE_NAMES: Set<string> = new Set();
8993

9094
let DB_COUNT = 0;
9195

@@ -126,11 +130,7 @@ export class RxDatabaseBase<
126130
public readonly hashFunction: HashFunction,
127131
public readonly cleanupPolicy?: Partial<RxCleanupPolicy>,
128132
public readonly allowSlowCount?: boolean,
129-
public readonly reactivity?: RxReactivityFactory<any>,
130-
/**
131-
* Function invoked when the database instance is considered closed
132-
*/
133-
public readonly onClosed?: () => void,
133+
public readonly reactivity?: RxReactivityFactory<any>
134134
) {
135135
DB_COUNT++;
136136

@@ -493,9 +493,6 @@ export class RxDatabaseBase<
493493
* we should generate the property list on build time.
494494
*/
495495
if (this.name === 'pseudoInstance') {
496-
if (this.onClosed) {
497-
this.onClosed();
498-
}
499496
return PROMISE_RESOLVE_FALSE;
500497
}
501498

@@ -512,7 +509,8 @@ export class RxDatabaseBase<
512509
))
513510
// close internal storage instances
514511
.then(() => this.internalStore.close())
515-
.then(() => this.onClosed && this.onClosed())
512+
// remove combination from USED_COMBINATIONS-map
513+
.then(() => USED_DATABASE_NAMES.delete(this.storage.name + '|' + this.name))
516514
.then(() => true);
517515
}
518516

@@ -537,26 +535,23 @@ export class RxDatabaseBase<
537535
}
538536

539537
/**
540-
* gets the name key for the passed database name and storage.
538+
* checks if an instance with same name and storage already exists
539+
* @throws {RxError} if used
541540
*/
542-
function getDatabaseNameKey(
541+
function throwIfDatabaseNameUsed(
543542
name: string,
544543
storage: RxStorage<any, any>
545544
) {
546-
return storage.name + '|' + name;
547-
}
548-
549-
/**
550-
* ponyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
551-
*/
552-
function createPromiseWithResolvers<T>() {
553-
let resolve!: (value: T | PromiseLike<T>) => void;
554-
let reject!: (reason?: any) => void;
555-
const promise = new Promise<T>((res, rej) => {
556-
resolve = res;
557-
reject = rej;
558-
});
559-
return { promise, resolve, reject };
545+
const key = storage.name + '|' + name;
546+
if (!USED_DATABASE_NAMES.has(key)) {
547+
return;
548+
} else {
549+
throw newRxError('DB8', {
550+
name,
551+
storage: storage.name,
552+
link: 'https://rxdb.info/rx-database.html#ignoreduplicate'
553+
});
554+
}
560555
}
561556

562557
/**
@@ -621,47 +616,39 @@ export function createRxDatabase<
621616
options,
622617
localDocuments
623618
});
624-
625-
if (ignoreDuplicate) {
619+
// check if combination already used
620+
if (!ignoreDuplicate) {
621+
throwIfDatabaseNameUsed(name, storage);
622+
} else {
626623
if (!overwritable.isDevMode()) {
627624
throw newRxError('DB9', {
628625
database: name
629626
});
630627
}
631628
}
629+
USED_DATABASE_NAMES.add(storage.name + '|' + name);
632630

633-
const closedPromiseWithResolvers = createPromiseWithResolvers<void>();
634-
const databaseNameKey = getDatabaseNameKey(name, storage);
635631
const databaseInstanceToken = randomToken(10);
636-
const databaseClosedPromiseMapEntry = DATABASE_CLOSED_PROMISE_MAP.get(databaseNameKey) || new Set<Promise<void>>();
637-
const databaseClosedPromise = closedPromiseWithResolvers.promise.then(() => {
638-
databaseClosedPromiseMapEntry.delete(databaseClosedPromise);
639-
});
640-
const databaseClosedPromisesToAwait = ignoreDuplicate ? [] : Array.from(databaseClosedPromiseMapEntry);
641632

642-
DATABASE_CLOSED_PROMISE_MAP.set(databaseNameKey, databaseClosedPromiseMapEntry);
643-
databaseClosedPromiseMapEntry.add(databaseClosedPromise);
644-
645-
return Promise.all(databaseClosedPromisesToAwait).then(() => {
646-
return createRxDatabaseStorageInstance<
647-
Internals,
648-
InstanceCreationOptions
649-
>(
650-
databaseInstanceToken,
651-
storage,
652-
name,
653-
instanceCreationOptions as any,
654-
multiInstance,
655-
password
656-
)
633+
return createRxDatabaseStorageInstance<
634+
Internals,
635+
InstanceCreationOptions
636+
>(
637+
databaseInstanceToken,
638+
storage,
639+
name,
640+
instanceCreationOptions as any,
641+
multiInstance,
642+
password
643+
)
657644
/**
658645
* Creating the internal store might fail
659646
* if some RxStorage wrapper is used that does some checks
660647
* and then throw.
661648
* In that case we have to properly clean up the database.
662649
*/
663650
.catch(err => {
664-
closedPromiseWithResolvers.resolve();
651+
USED_DATABASE_NAMES.delete(storage.name + '|' + name);
665652
throw err;
666653
})
667654
.then(storageInstance => {
@@ -678,8 +665,7 @@ export function createRxDatabase<
678665
hashFunction,
679666
cleanupPolicy,
680667
allowSlowCount,
681-
reactivity,
682-
closedPromiseWithResolvers.resolve
668+
reactivity
683669
) as any;
684670

685671
return runAsyncPluginHooks('createRxDatabase', {
@@ -697,7 +683,6 @@ export function createRxDatabase<
697683
}
698684
}).then(() => rxDatabase);
699685
});
700-
});
701686
}
702687

703688
/**

test/unit/rx-database.test.ts

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -212,51 +212,19 @@ describeParallel('rx-database.test.ts', () => {
212212
});
213213
it('do not allow 2 databases with same name and storage', async () => {
214214
const name = randomToken(10);
215-
const storage = config.storage.getStorage();
216-
217-
let db1: RxDatabase | undefined;
218-
let db2: RxDatabase | undefined;
219-
220-
const db1Promise = createRxDatabase({
221-
name,
222-
storage,
223-
}).then((db1PromiseValue) => {
224-
db1 = db1PromiseValue;
225-
assert.ok(db2 === undefined);
226-
227-
setTimeout(() => {
228-
db1PromiseValue.close();
229-
}, 500);
230-
231-
return db1PromiseValue;
232-
});
233-
const db2Promise = createRxDatabase({
234-
name,
235-
storage,
236-
}).then((db2PromiseValue) => {
237-
db2 = db2PromiseValue;
238-
assert.ok(db1?.closed);
239-
240-
return db2PromiseValue;
241-
});
242-
243-
244-
const resolvedDb1 = await db1Promise;
245-
const resolvedDb2 = await db2Promise;
246-
247-
assert.ok(resolvedDb1.closed);
248-
assert.ok(!resolvedDb2.closed);
249-
250-
resolvedDb2.close();
251-
252-
const db3 = await createRxDatabase({
215+
const db = await createRxDatabase({
253216
name,
254-
storage,
217+
storage: config.storage.getStorage()
255218
});
256-
257-
assert.ok(!db3.closed);
258-
259-
db3.close();
219+
await AsyncTestUtil.assertThrows(
220+
() => createRxDatabase({
221+
name,
222+
storage: config.storage.getStorage()
223+
}),
224+
'RxError',
225+
'ignoreDuplicate'
226+
);
227+
db.close();
260228
});
261229
});
262230
});

0 commit comments

Comments
 (0)