-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathcounter.ts
More file actions
170 lines (145 loc) · 4.44 KB
/
counter.ts
File metadata and controls
170 lines (145 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { assert } from "@fluidframework/core-utils/internal";
import type {
IChannelAttributes,
IFluidDataStoreRuntime,
IChannelStorageService,
} from "@fluidframework/datastore-definitions/internal";
import { MessageType } from "@fluidframework/driver-definitions/internal";
import { readAndParse } from "@fluidframework/driver-utils/internal";
import type {
ISummaryTreeWithStats,
IRuntimeMessageCollection,
IRuntimeMessagesContent,
ISequencedMessageEnvelope,
} from "@fluidframework/runtime-definitions/internal";
import type { IFluidSerializer } from "@fluidframework/shared-object-base/internal";
import {
SharedObject,
createSingleBlobSummary,
} from "@fluidframework/shared-object-base/internal";
import type { ISharedCounter, ISharedCounterEvents } from "./interfaces.js";
/**
* Describes the operation (op) format for incrementing the {@link SharedCounter}.
*/
interface IIncrementOperation {
type: "increment";
incrementAmount: number;
}
/**
* @remarks Used in snapshotting.
*/
interface ICounterSnapshotFormat {
/**
* The value of the counter.
*/
value: number;
}
const snapshotFileName = "header";
/**
* {@inheritDoc ISharedCounter}
* @legacy @beta
*/
export class SharedCounter
extends SharedObject<ISharedCounterEvents>
implements ISharedCounter
{
public constructor(
id: string,
runtime: IFluidDataStoreRuntime,
attributes: IChannelAttributes,
) {
super(id, runtime, attributes, "fluid_counter_");
}
private _value: number = 0;
/**
* {@inheritDoc ISharedCounter.value}
*/
public get value(): number {
return this._value;
}
/**
* {@inheritDoc ISharedCounter.increment}
*/
public increment(incrementAmount: number): void {
// Incrementing by floating point numbers will be eventually inconsistent, since the order in which the
// increments are applied affects the result. A more-robust solution would be required to support this.
if (incrementAmount % 1 !== 0) {
throw new Error("Must increment by a whole number");
}
const op: IIncrementOperation = {
type: "increment",
incrementAmount,
};
this.incrementCore(incrementAmount);
this.submitLocalMessage(op);
}
private incrementCore(incrementAmount: number): void {
this._value += incrementAmount;
this.emit("incremented", incrementAmount, this._value);
}
/**
* Create a summary for the counter.
*
* @returns The summary of the current state of the counter.
*/
protected summarizeCore(serializer: IFluidSerializer): ISummaryTreeWithStats {
// Get a serializable form of data
const content: ICounterSnapshotFormat = {
value: this.value,
};
// And then construct the summary for it
return createSingleBlobSummary(snapshotFileName, JSON.stringify(content));
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
*/
protected async loadCore(storage: IChannelStorageService): Promise<void> {
const content = await readAndParse<ICounterSnapshotFormat>(storage, snapshotFileName);
this._value = content.value;
}
/**
* Called when the object has disconnected from the delta stream.
*/
protected onDisconnect(): void {}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.processMessagesCore}
*/
protected processMessagesCore(messagesCollection: IRuntimeMessageCollection): void {
const { envelope, local, messagesContent } = messagesCollection;
for (const messageContent of messagesContent) {
this.processMessage(envelope, messageContent, local);
}
}
private processMessage(
messageEnvelope: ISequencedMessageEnvelope,
messageContent: IRuntimeMessagesContent,
local: boolean,
): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (messageEnvelope.type === MessageType.Operation && !local) {
const op = messageContent.contents as IIncrementOperation;
switch (op.type) {
case "increment": {
this.incrementCore(op.incrementAmount);
break;
}
default: {
throw new Error("Unknown operation");
}
}
}
}
/**
* {@inheritdoc @fluidframework/shared-object-base#SharedObjectCore.applyStashedOp}
*/
protected applyStashedOp(op: unknown): void {
const counterOp = op as IIncrementOperation;
// TODO: Clean up error code linter violations repo-wide.
assert(counterOp.type === "increment", 0x3ec /* Op type is not increment */);
this.increment(counterOp.incrementAmount);
}
}