Refactor syncable update function.
This commit is contained in:
parent
2ee9b7dbc4
commit
e3e0f4d3f7
@ -8,3 +8,4 @@ export * from '@app/_helpers/http-getter';
|
|||||||
export * from '@app/_helpers/mock-backend';
|
export * from '@app/_helpers/mock-backend';
|
||||||
export * from '@app/_helpers/read-csv';
|
export * from '@app/_helpers/read-csv';
|
||||||
export * from '@app/_helpers/schema-validation';
|
export * from '@app/_helpers/schema-validation';
|
||||||
|
export * from '@app/_helpers/sync';
|
||||||
|
31
src/app/_helpers/sync.ts
Normal file
31
src/app/_helpers/sync.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { ArgPair, Syncable } from 'cic-client-meta';
|
||||||
|
import * as Automerge from 'automerge';
|
||||||
|
|
||||||
|
export function updateSyncable(
|
||||||
|
changes: Array<ArgPair>,
|
||||||
|
changesDescription: string,
|
||||||
|
syncable: Syncable
|
||||||
|
): any {
|
||||||
|
syncable.m = Automerge.change(syncable.m, changesDescription, (m) => {
|
||||||
|
changes.forEach((c) => {
|
||||||
|
const path = c.k.split('.');
|
||||||
|
let target = m['data'];
|
||||||
|
while (path.length > 1) {
|
||||||
|
const part = path.shift();
|
||||||
|
target = target[part];
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
target[path[0]] = c.v;
|
||||||
|
} catch (e) {
|
||||||
|
if (
|
||||||
|
!e.message.includes(
|
||||||
|
'Cannot assign an object that already belongs to an Automerge document.'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
m['timestamp'] = Math.floor(Date.now() / 1000);
|
||||||
|
});
|
||||||
|
}
|
@ -12,7 +12,7 @@ import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
|
|||||||
import { RegistryService } from '@app/_services/registry.service';
|
import { RegistryService } from '@app/_services/registry.service';
|
||||||
import { CICRegistry } from '@cicnet/cic-client';
|
import { CICRegistry } from '@cicnet/cic-client';
|
||||||
import { AuthService } from '@app/_services/auth.service';
|
import { AuthService } from '@app/_services/auth.service';
|
||||||
import { personValidation, vcardValidation } from '@app/_helpers';
|
import { personValidation, updateSyncable, vcardValidation } from '@app/_helpers';
|
||||||
import { add0x } from '@src/assets/js/ethtx/dist/hex';
|
import { add0x } from '@src/assets/js/ethtx/dist/hex';
|
||||||
const vCard = require('vcard-parser');
|
const vCard = require('vcard-parser');
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ export class UserService {
|
|||||||
for (const prop of Object.keys(accountInfo)) {
|
for (const prop of Object.keys(accountInfo)) {
|
||||||
update.push(new ArgPair(prop, accountInfo[prop]));
|
update.push(new ArgPair(prop, accountInfo[prop]));
|
||||||
}
|
}
|
||||||
syncableAccount.update(update, 'client-branch');
|
updateSyncable(update, 'client-branch', syncableAccount);
|
||||||
await personValidation(syncableAccount.m.data);
|
await personValidation(syncableAccount.m.data);
|
||||||
await this.updateMeta(syncableAccount, accountKey, this.headers);
|
await this.updateMeta(syncableAccount, accountKey, this.headers);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user