增加了获取随机数的注入

This commit is contained in:
Xu Chang 2022-03-29 20:21:10 +08:00
parent ae86d5707c
commit 4c7c6c7635
3 changed files with 29 additions and 18 deletions

3
lib/context.d.ts vendored
View File

@ -5,7 +5,8 @@ export declare class Context<ED extends EntityDict> implements ContextInterface<
rowStore: TreeStore<ED>;
uuid?: string;
opRecords: OpRecord<ED>[];
constructor(store: TreeStore<ED>);
getRandomNumber: (length: number) => Promise<Uint8Array>;
constructor(store: TreeStore<ED>, getRandomNumber: (length: number) => Promise<Uint8Array>);
on(event: 'commit' | 'rollback', callback: (context: ContextInterface<ED>) => Promise<void>): void;
begin(options?: object): Promise<void>;
commit(): Promise<void>;

View File

@ -10,27 +10,32 @@ class Context {
rowStore;
uuid;
opRecords;
constructor(store) {
getRandomNumber; // 在不同的环境下取随机数的实现
constructor(store, getRandomNumber) {
this.rowStore = store;
this.opRecords = [];
this.getRandomNumber = getRandomNumber;
}
on(event, callback) {
throw new Error('not implemented here!');
}
async begin(options) {
(0, assert_1.default)(!this.uuid);
this.uuid = (0, uuid_1.v4)();
const random = await this.getRandomNumber(16);
this.uuid = (0, uuid_1.v4)({ random });
this.rowStore.begin(this.uuid);
}
async commit() {
(0, assert_1.default)(this.uuid);
this.rowStore.commit(this.uuid);
this.uuid = undefined;
if (this.uuid) {
this.rowStore.commit(this.uuid);
this.uuid = undefined;
}
}
async rollback() {
(0, assert_1.default)(this.uuid);
this.rowStore.rollback(this.uuid);
this.uuid = undefined;
if (this.uuid) {
this.rowStore.rollback(this.uuid);
this.uuid = undefined;
}
}
}
exports.Context = Context;

View File

@ -1,17 +1,19 @@
import assert from 'assert';
import { v4 } from 'uuid';
import { Context as ContextInterface } from 'oak-domain/lib/types/Context';
import { EntityDef, EntityDict, OperationResult, OpRecord } from 'oak-domain/lib/types/Entity';
import { EntityDict, OperationResult, OpRecord } from 'oak-domain/lib/types/Entity';
import TreeStore from './store';
export class Context<ED extends EntityDict> implements ContextInterface<ED> {
rowStore: TreeStore<ED>;
uuid?: string;
opRecords: OpRecord<ED>[];
getRandomNumber: (length: number) => Promise<Uint8Array>; // 在不同的环境下取随机数的实现
constructor(store: TreeStore<ED>) {
constructor(store: TreeStore<ED>, getRandomNumber: (length: number) => Promise<Uint8Array>) {
this.rowStore = store;
this.opRecords = [];
this.getRandomNumber = getRandomNumber;
}
on(event: 'commit' | 'rollback', callback: (context: ContextInterface<ED>) => Promise<void>): void {
@ -20,17 +22,20 @@ export class Context<ED extends EntityDict> implements ContextInterface<ED> {
async begin(options?: object): Promise<void> {
assert(!this.uuid);
this.uuid = v4();
const random = await this.getRandomNumber(16);
this.uuid = v4({ random });
this.rowStore.begin(this.uuid);
}
async commit(): Promise<void> {
assert(this.uuid);
this.rowStore.commit(this.uuid!);
this.uuid = undefined;
if (this.uuid) {
this.rowStore.commit(this.uuid!);
this.uuid = undefined;
}
}
async rollback(): Promise<void> {
assert(this.uuid);
this.rowStore.rollback(this.uuid!);
this.uuid = undefined;
if(this.uuid) {
this.rowStore.rollback(this.uuid!);
this.uuid = undefined;
}
}
}