|
1 | 1 | import { LiveObject, LiveObjectData } from './liveobject';
|
| 2 | +import { LiveObjects } from './liveobjects'; |
| 3 | +import { StateCounter, StateCounterOp, StateOperation, StateOperationAction } from './statemessage'; |
2 | 4 |
|
3 | 5 | export interface LiveCounterData extends LiveObjectData {
|
4 | 6 | data: number;
|
5 | 7 | }
|
6 | 8 |
|
7 | 9 | export class LiveCounter extends LiveObject<LiveCounterData> {
|
| 10 | + constructor( |
| 11 | + liveObjects: LiveObjects, |
| 12 | + private _created: boolean, |
| 13 | + initialData?: LiveCounterData | null, |
| 14 | + objectId?: string, |
| 15 | + ) { |
| 16 | + super(liveObjects, initialData, objectId); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Returns a {@link LiveCounter} instance with a 0 value. |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | + static zeroValue(liveobjects: LiveObjects, isCreated: boolean, objectId?: string): LiveCounter { |
| 25 | + return new LiveCounter(liveobjects, isCreated, null, objectId); |
| 26 | + } |
| 27 | + |
8 | 28 | value(): number {
|
9 | 29 | return this._dataRef.data;
|
10 | 30 | }
|
11 | 31 |
|
| 32 | + /** |
| 33 | + * @internal |
| 34 | + */ |
| 35 | + isCreated(): boolean { |
| 36 | + return this._created; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @internal |
| 41 | + */ |
| 42 | + setCreated(created: boolean): void { |
| 43 | + this._created = created; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @internal |
| 48 | + */ |
| 49 | + applyOperation(op: StateOperation): void { |
| 50 | + if (op.objectId !== this.getObjectId()) { |
| 51 | + throw new this._client.ErrorInfo( |
| 52 | + `Cannot apply state operation with objectId=${op.objectId}, to this LiveCounter with objectId=${this.getObjectId()}`, |
| 53 | + 50000, |
| 54 | + 500, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + switch (op.action) { |
| 59 | + case StateOperationAction.COUNTER_CREATE: |
| 60 | + this._applyCounterCreate(op.counter); |
| 61 | + break; |
| 62 | + |
| 63 | + case StateOperationAction.COUNTER_INC: |
| 64 | + if (this._client.Utils.isNil(op.counterOp)) { |
| 65 | + this._throwNoPayloadError(op); |
| 66 | + } else { |
| 67 | + this._applyCounterInc(op.counterOp); |
| 68 | + } |
| 69 | + break; |
| 70 | + |
| 71 | + default: |
| 72 | + throw new this._client.ErrorInfo( |
| 73 | + `Invalid ${op.action} op for LiveCounter objectId=${this.getObjectId()}`, |
| 74 | + 50000, |
| 75 | + 500, |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
12 | 80 | protected _getZeroValueData(): LiveCounterData {
|
13 | 81 | return { data: 0 };
|
14 | 82 | }
|
| 83 | + |
| 84 | + private _throwNoPayloadError(op: StateOperation): void { |
| 85 | + throw new this._client.ErrorInfo( |
| 86 | + `No payload found for ${op.action} op for LiveCounter objectId=${this.getObjectId()}`, |
| 87 | + 50000, |
| 88 | + 500, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + private _applyCounterCreate(op: StateCounter | undefined): void { |
| 93 | + if (this.isCreated()) { |
| 94 | + // skip COUNTER_CREATE op if this counter is already created |
| 95 | + this._client.Logger.logAction( |
| 96 | + this._client.logger, |
| 97 | + this._client.Logger.LOG_MICRO, |
| 98 | + 'LiveCounter._applyCounterCreate()', |
| 99 | + `skipping applying COUNTER_CREATE op on a counter instance as it is already created; objectId=${this._objectId}`, |
| 100 | + ); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (this._client.Utils.isNil(op)) { |
| 105 | + // if a counter object is missing for the COUNTER_CREATE op, the initial value is implicitly 0 in this case. |
| 106 | + // we need to SUM the initial value to the current value due to the reasons below, but since it's a 0, we can skip addition operation |
| 107 | + this.setCreated(true); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // note that it is intentional to SUM the incoming count from the create op. |
| 112 | + // if we get here, it means that current counter instance wasn't initialized from the COUNTER_CREATE op, |
| 113 | + // so it is missing the initial value that we're going to add now. |
| 114 | + this._dataRef.data += op.count ?? 0; |
| 115 | + this.setCreated(true); |
| 116 | + } |
| 117 | + |
| 118 | + private _applyCounterInc(op: StateCounterOp): void { |
| 119 | + this._dataRef.data += op.amount; |
| 120 | + } |
15 | 121 | }
|
0 commit comments