@fluidframework/counter 2.0.0-internal.1.1.0 → 2.0.0-internal.1.2.0.93071
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +159 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/package.json +10 -10
- package/src/packageVersion.ts +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,161 @@
|
|
|
1
1
|
# @fluidframework/counter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Introduction
|
|
4
|
+
|
|
5
|
+
The `SharedCounter` distributed data structure (DDS) is used to store an integer counter value that can be modified by multiple clients simultaneously.
|
|
6
|
+
The data structure affords incrementing and decrementing the shared value via its `increment` method. Decrements are done by providing a negative value.
|
|
7
|
+
|
|
8
|
+
The `SharedCounter` is a specialized [Optimistic DDS][].
|
|
9
|
+
It operates on communicated _deltas_ (amounts by which the shared value should be incremented or decremented), rather than direct changes to the shared value.
|
|
10
|
+
In this way, it avoids the pitfalls of DDSes with simpler merge strategies, in which one user's edit may clobber another's (see [below](#why-a-specialized-dds)).
|
|
11
|
+
|
|
12
|
+
Note that the `SharedCounter` only operates on integer values.
|
|
13
|
+
|
|
14
|
+
### Why a specialized DDS?
|
|
15
|
+
|
|
16
|
+
You may be asking yourself, why not just store the shared integer value directly in another DDS like a [SharedMap][]?
|
|
17
|
+
Why incur the overhead of another runtime type?
|
|
18
|
+
|
|
19
|
+
The key to the answer here is that DDSes with simpler merge strategies (like `SharedMap`) take a somewhat brute-force approach to merging concurrent edits.
|
|
20
|
+
For a semantic data type like a counter, this can result in undesirable behavior.
|
|
21
|
+
|
|
22
|
+
#### SharedMap Example
|
|
23
|
+
|
|
24
|
+
Let's illustrate the issue with an example.
|
|
25
|
+
|
|
26
|
+
Consider a polling widget.
|
|
27
|
+
The widget displays a list of options and allows users to click a checkbox to vote for a given option.
|
|
28
|
+
Next to each option in the list, a live counter is displayed that shows the number of votes for that item.
|
|
29
|
+
|
|
30
|
+
Whenever a user checks an option, all users should see the counter corresponding to that option increment by 1.
|
|
31
|
+
|
|
32
|
+
In this example, the application is storing its vote counts in a [SharedMap][], where the map keys are `strings` representing the IDs of the options, and the values are `numbers` representing the associated vote counts.
|
|
33
|
+
|
|
34
|
+
For simplicity, we will look at a scenario in which 2 users vote for the same option at around the same time.
|
|
35
|
+
|
|
36
|
+
Specifically, **User A** clicks the checkbox for option **Foo**, which currently has **0** votes.
|
|
37
|
+
The application then optimistically updates the vote count for that object by writing the updated counter value of **1** for option **Foo** to its `SharedMap`.
|
|
38
|
+
|
|
39
|
+
The value change operation (op) is then transmitted to the service to be sequenced and eventually sent to other users in the collaborative session.
|
|
40
|
+
|
|
41
|
+
At around the same time, **User B** clicks the checkbox for option **Foo**, which in their view currently has **0** votes.
|
|
42
|
+
Similarly to before, the application optimistically updates the associated counter value to **1**, and transmits its own update op.
|
|
43
|
+
|
|
44
|
+
The service receives the op from **User A** first, and sequences their op updating **Foo** to **1** as **op 0**. **User B**'s op is received second, and is sequenced as **op 1**.
|
|
45
|
+
|
|
46
|
+
Both users then receive acknowledgement of their update, and receive **op 0** and **op 1** to be applied in order.
|
|
47
|
+
Both clients apply **op 0** by setting **Foo** to **1**.
|
|
48
|
+
Then both clients apply **op 1** by setting **Foo** to **1**.
|
|
49
|
+
|
|
50
|
+
But this isn't right.
|
|
51
|
+
Two different users voted for option **Foo**, but the counter now displays **1**.
|
|
52
|
+
|
|
53
|
+
`SharedCounter` solves this problem by expressing its operations in terms of *increments* and *decrements* rather than as direct value updates.
|
|
54
|
+
|
|
55
|
+
So for the scenario above, if the system was using `SharedCounter`s to represent the vote counts, **User A** would submit an op *incrementing* **Foo** by **+1**, rather than updating the value of **Foo** from **0** to **1**.
|
|
56
|
+
At around the same time, **User B** would submit their own **+1** op for **Foo**.
|
|
57
|
+
|
|
58
|
+
Assuming the same sequencing, both users first apply **op 0** and increment their counter for **Foo** by **+1** (from **0** to **1**).
|
|
59
|
+
Next, they both apply **op 1** and increment their counter for **Foo** by **+1** a second time (from **1** to **2**).
|
|
60
|
+
|
|
61
|
+
Now both users see the right vote count for `Foo`!
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
The `SharedCounter` object provides a simple API surface for managing a shared integer whose value may be incremented and decremented by collaborators.
|
|
66
|
+
|
|
67
|
+
A new `SharedCounter` value will be initialized with its value set to `0`.
|
|
68
|
+
If you wish to initialize the counter to a different value, you may [modify the value](#incrementing--decrementing-the-value) before attaching the container, or before storing it in another shared object like a `SharedMap`.
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
The package containing the `SharedCounter` library is [@fluidframework/shared-counter](https://www.npmjs.com/package/@fluidframework/counter).
|
|
73
|
+
|
|
74
|
+
To get started, run the following from a terminal in your repository:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install @fluidframework/shared-counter
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Creation
|
|
81
|
+
|
|
82
|
+
The workflow for creating a `SharedCounter` is effectively the same as many of our other DDSes.
|
|
83
|
+
For an example of how to create one, please see our workflow examples for [SharedMap creation][].
|
|
84
|
+
|
|
85
|
+
### Incrementing / decrementing the value
|
|
86
|
+
|
|
87
|
+
Once you have created your `SharedCounter`, you can change its value using the [increment][] method.
|
|
88
|
+
This method accepts a positive or negative *integer* to be applied to the shared value.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
sharedCounter.increment(3); // Adds 3 to the current value
|
|
93
|
+
sharedCounter.increment(-5); // Subtracts 5 from the current value
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `incremented` event
|
|
97
|
+
|
|
98
|
+
The [incremented][] event is sent when a client in the collaborative session changes the counter value via the `increment` method.
|
|
99
|
+
|
|
100
|
+
Signature:
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
(event: "incremented", listener: (incrementAmount: number, newValue: number) => void)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
By listening to this event, you can receive and apply the changes coming from other collaborators.
|
|
107
|
+
Consider the following code example for configuring a Counter widget:
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
const sharedCounter = container.initialObjects.sharedCounter;
|
|
111
|
+
let counterValue = sharedCounter.value;
|
|
112
|
+
|
|
113
|
+
const incrementButton = document.createElement('button');
|
|
114
|
+
button.textContent = "Increment";
|
|
115
|
+
const decrementButton = document.createElement('button');
|
|
116
|
+
button.textContent = "Decrement";
|
|
117
|
+
|
|
118
|
+
// Increment / decrement shared counter value when the corresponding button is clicked
|
|
119
|
+
incrementButton.addEventListener('click', () => sharedCounter.increment(1));
|
|
120
|
+
decrementButton.addEventListener('click', () => sharedCounter.increment(-1));
|
|
121
|
+
|
|
122
|
+
const counterValueLabel = document.createElement('label');
|
|
123
|
+
counterValueLabel.textContent = `${counterValue}`;
|
|
124
|
+
|
|
125
|
+
// This function will be called each time the shared counter value is incremented
|
|
126
|
+
// (including increments from this client).
|
|
127
|
+
// Update the local counter value and the corresponding label being displayed in the widget.
|
|
128
|
+
const updateCounterValueLabel = (delta) => {
|
|
129
|
+
counterValue += delta;
|
|
130
|
+
counterValueLabel.textContent = `${counterValue}`;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Register to be notified when the counter is incremented
|
|
134
|
+
sharedCounter.on("incremented", updateCounterValueLabel);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
In the code above, whenever a user presses either the Increment or Decrement button, the `sharedCounter.increment` is called with +/- 1.
|
|
138
|
+
This causes the `incremented` event to be sent to all of the clients who have this container open.
|
|
139
|
+
|
|
140
|
+
Since `updateCounterValueLabel` is listening for all `incremented` events, the view will always refresh with the appropriate updated value any time a collaborator increments or decrements the counter value.
|
|
141
|
+
|
|
142
|
+
<!-- AUTO-GENERATED-CONTENT:START (README_API_DOCS_SECTION:includeHeading=TRUE) -->
|
|
143
|
+
## API Documentation
|
|
144
|
+
|
|
145
|
+
API documentation for **@fluidframework/counter** is available at <https://fluidframework.com/docs/apis/counter>.
|
|
146
|
+
<!-- AUTO-GENERATED-CONTENT:END -->
|
|
147
|
+
|
|
148
|
+
<!-- AUTO-GENERATED-CONTENT:START (README_TRADEMARK_SECTION:includeHeading=TRUE) -->
|
|
149
|
+
## Trademark
|
|
150
|
+
|
|
151
|
+
This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.
|
|
152
|
+
Use of these trademarks or logos must follow Microsoft's [Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
|
|
153
|
+
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
|
|
154
|
+
<!-- AUTO-GENERATED-CONTENT:END -->
|
|
155
|
+
|
|
156
|
+
<!-- Links -->
|
|
157
|
+
[increment]: https://fluidframework.com/docs/apis/counter/isharedcounter-interface#increment-methodsignature
|
|
158
|
+
[incremented]: https://fluidframework.com/docs/apis/counter/isharedcounterevents-interface#_call_-callsignature
|
|
159
|
+
[Optimistic DDS]: https://fluidframework.com/docs/build/dds/#optimistic-data-structures
|
|
160
|
+
[SharedMap]: https://fluidframework.com/docs/data-structures/map
|
|
161
|
+
[SharedMap creation]: https://fluidframework.com/docs/data-structures/map/#creation
|
package/dist/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/counter";
|
|
8
|
-
export declare const pkgVersion = "2.0.0-internal.1.
|
|
8
|
+
export declare const pkgVersion = "2.0.0-internal.1.2.0.93071";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.d.ts","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,OAAO,4BAA4B,CAAC;AACjD,eAAO,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"packageVersion.d.ts","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,OAAO,4BAA4B,CAAC;AACjD,eAAO,MAAM,UAAU,+BAA+B,CAAC"}
|
package/dist/packageVersion.js
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.pkgVersion = exports.pkgName = void 0;
|
|
10
10
|
exports.pkgName = "@fluidframework/counter";
|
|
11
|
-
exports.pkgVersion = "2.0.0-internal.1.
|
|
11
|
+
exports.pkgVersion = "2.0.0-internal.1.2.0.93071";
|
|
12
12
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,yBAAyB,CAAC;AACpC,QAAA,UAAU,GAAG,
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,yBAAyB,CAAC;AACpC,QAAA,UAAU,GAAG,4BAA4B,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/counter\";\nexport const pkgVersion = \"2.0.0-internal.1.2.0.93071\";\n"]}
|
package/lib/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/counter";
|
|
8
|
-
export declare const pkgVersion = "2.0.0-internal.1.
|
|
8
|
+
export declare const pkgVersion = "2.0.0-internal.1.2.0.93071";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.d.ts","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,OAAO,4BAA4B,CAAC;AACjD,eAAO,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"packageVersion.d.ts","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,OAAO,4BAA4B,CAAC;AACjD,eAAO,MAAM,UAAU,+BAA+B,CAAC"}
|
package/lib/packageVersion.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export const pkgName = "@fluidframework/counter";
|
|
8
|
-
export const pkgVersion = "2.0.0-internal.1.
|
|
8
|
+
export const pkgVersion = "2.0.0-internal.1.2.0.93071";
|
|
9
9
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,4BAA4B,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/counter\";\nexport const pkgVersion = \"2.0.0-internal.1.2.0.93071\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/counter",
|
|
3
|
-
"version": "2.0.0-internal.1.
|
|
3
|
+
"version": "2.0.0-internal.1.2.0.93071",
|
|
4
4
|
"description": "Counter DDS",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -59,20 +59,20 @@
|
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@fluidframework/common-utils": "^1.0.0",
|
|
62
|
-
"@fluidframework/core-interfaces": "
|
|
63
|
-
"@fluidframework/datastore-definitions": "
|
|
64
|
-
"@fluidframework/driver-utils": "
|
|
62
|
+
"@fluidframework/core-interfaces": "2.0.0-internal.1.2.0.93071",
|
|
63
|
+
"@fluidframework/datastore-definitions": "2.0.0-internal.1.2.0.93071",
|
|
64
|
+
"@fluidframework/driver-utils": "2.0.0-internal.1.2.0.93071",
|
|
65
65
|
"@fluidframework/protocol-definitions": "^1.0.0",
|
|
66
|
-
"@fluidframework/runtime-definitions": "
|
|
67
|
-
"@fluidframework/shared-object-base": "
|
|
66
|
+
"@fluidframework/runtime-definitions": "2.0.0-internal.1.2.0.93071",
|
|
67
|
+
"@fluidframework/shared-object-base": "2.0.0-internal.1.2.0.93071"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@fluidframework/build-common": "^0.
|
|
71
|
-
"@fluidframework/build-tools": "^0.
|
|
70
|
+
"@fluidframework/build-common": "^1.0.0",
|
|
71
|
+
"@fluidframework/build-tools": "^0.4.4000",
|
|
72
72
|
"@fluidframework/counter-previous": "npm:@fluidframework/counter@^1.0.0",
|
|
73
73
|
"@fluidframework/eslint-config-fluid": "^0.28.2000",
|
|
74
|
-
"@fluidframework/mocha-test-setup": "
|
|
75
|
-
"@fluidframework/test-runtime-utils": "
|
|
74
|
+
"@fluidframework/mocha-test-setup": "2.0.0-internal.1.2.0.93071",
|
|
75
|
+
"@fluidframework/test-runtime-utils": "2.0.0-internal.1.2.0.93071",
|
|
76
76
|
"@microsoft/api-extractor": "^7.22.2",
|
|
77
77
|
"@rushstack/eslint-config": "^2.5.1",
|
|
78
78
|
"@types/mocha": "^9.1.1",
|
package/src/packageVersion.ts
CHANGED