Skip to content

Commit

Permalink
fix(engine): data loss issue and improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 29, 2023
1 parent 200da18 commit 07f6877
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions packages/engine/demo/data-lost-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,56 @@ const logger = createLogger('AlwatrStore/Demo', true);
logger.banner('AlwatrStore/Demo');

const alwatrStore = new AlwatrStore({
rootPath: './db'
rootPath: './db',
defaultChangeDebounce: 250, // for demo
});

const docId = {
name: 'data-lost-test',
region: Region.Public,
}
const list = [];

for (let i = 0; i < 10; i++) {
/**
* @type
*/
const docId = {
name: 'profile',
region: Region.PerUser,
ownerId: `u${i}-hash-126789`,
};

if (!alwatrStore.exists(docId)) {
alwatrStore.defineStoreFile({
...docId,
type: StoreFileType.Collection,
extension: StoreFileExtension.Json,
});
}

if (!alwatrStore.exists(docId)) {
alwatrStore.defineStoreFile({
...docId,
type: StoreFileType.Collection,
extension: StoreFileExtension.Json,
});
list.push(await alwatrStore.collection(docId));
}

const collection = await alwatrStore.collection(docId);
function insertData() {
const collection = list[Math.floor(Math.random() * list.length)];

function insertData () {
const id = collection.append({
let itemId = collection.append({
name: (Math.random() * 10000).toString(36),
age: Math.floor(Math.random() * 80) + 10,
});

console.log('Inserted', id);

if (Math.random() < 0.01) {
console.log('Collection: %s, itemId:', collection.id, itemId);

itemId = list[0].append({
name: (Math.random() * 10000).toString(36),
age: 15,
})

console.log('Collection0, itemId:', itemId);

if (Math.random() < 0.001) {
throw new Error('unexpected_random_error');
}

setTimeout(insertData, 5);
setTimeout(insertData, 2);
}

insertData();

0 comments on commit 07f6877

Please sign in to comment.