@fluidframework/counter 2.0.0-rc.1.0.0 → 2.0.0-rc.1.0.2

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 CHANGED
@@ -1,94 +1,167 @@
1
- # @fluidframework/cell
1
+ # @fluidframework/counter
2
2
 
3
- The `SharedCell` Distributed Data Structure (DDS) stores a single, shared value that can be edited or deleted.
4
-
5
- <!-- AUTO-GENERATED-CONTENT:START (README_INSTALLATION_SECTION:includeHeading=TRUE) -->
3
+ <!-- AUTO-GENERATED-CONTENT:START (README_DEPENDENCY_GUIDELINES_SECTION:includeHeading=TRUE) -->
6
4
 
7
5
  <!-- prettier-ignore-start -->
8
6
  <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
9
7
 
10
- ## Installation
11
-
12
- To get started, install the package by running the following command:
8
+ ## Using Fluid Framework libraries
13
9
 
14
- ```bash
15
- npm i @fluidframework/counter
16
- ```
10
+ When taking a dependency on a Fluid Framework library, we recommend using a `^` (caret) version range, such as `^1.3.4`.
11
+ While Fluid Framework libraries may use different ranges with interdependencies between other Fluid Framework libraries,
12
+ library consumers should always prefer `^`.
17
13
 
18
14
  <!-- prettier-ignore-end -->
19
15
 
20
16
  <!-- AUTO-GENERATED-CONTENT:END -->
21
17
 
22
- <!-- AUTO-GENERATED-CONTENT:START (README_DEPENDENCY_GUIDELINES_SECTION:includeHeading=TRUE) -->
18
+ ## Introduction
23
19
 
24
- <!-- prettier-ignore-start -->
25
- <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
20
+ The `SharedCounter` distributed data structure (DDS) is used to store an integer counter value that can be modified by multiple clients simultaneously.
21
+ The data structure affords incrementing and decrementing the shared value via its `increment` method. Decrements are done by providing a negative value.
26
22
 
27
- ## Using Fluid Framework libraries
23
+ The `SharedCounter` is a specialized [Optimistic DDS][].
24
+ It operates on communicated _deltas_ (amounts by which the shared value should be incremented or decremented), rather than direct changes to the shared value.
25
+ 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)).
28
26
 
29
- When taking a dependency on a Fluid Framework library, we recommend using a `^` (caret) version range, such as `^1.3.4`.
30
- While Fluid Framework libraries may use different ranges with interdependencies between other Fluid Framework libraries,
31
- library consumers should always prefer `^`.
27
+ Note that the `SharedCounter` only operates on integer values.
32
28
 
33
- <!-- prettier-ignore-end -->
29
+ ### Why a specialized DDS?
34
30
 
35
- <!-- AUTO-GENERATED-CONTENT:END -->
31
+ You may be asking yourself, why not just store the shared integer value directly in another DDS like a [SharedMap][]?
32
+ Why incur the overhead of another runtime type?
36
33
 
37
- <!-- AUTO-GENERATED-CONTENT:START (README_API_DOCS_SECTION:includeHeading=TRUE) -->
34
+ 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.
35
+ For a semantic data type like a counter, this can result in undesirable behavior.
38
36
 
39
- <!-- prettier-ignore-start -->
37
+ #### SharedMap Example
40
38
 
41
- <!-- This section is automatically generated. To update it, make the appropriate changes to docs/md-magic.config.js or the embedded content, then run 'npm run build:md-magic' in the docs folder. -->
39
+ Let's illustrate the issue with an example.
42
40
 
43
- ## API Documentation
41
+ Consider a polling widget.
42
+ The widget displays a list of options and allows users to click a checkbox to vote for a given option.
43
+ Next to each option in the list, a live counter is displayed that shows the number of votes for that item.
44
44
 
45
- API documentation for **@fluidframework/cell** is available at <https://fluidframework.com/docs/api/v1/cell>.
45
+ Whenever a user checks an option, all users should see the counter corresponding to that option increment by 1.
46
46
 
47
- <!-- prettier-ignore-end -->
47
+ 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.
48
48
 
49
- <!-- AUTO-GENERATED-CONTENT:END -->
49
+ For simplicity, we will look at a scenario in which 2 users vote for the same option at around the same time.
50
50
 
51
- <!-- AUTO-GENERATED-CONTENT:START (README_CONTRIBUTION_GUIDELINES_SECTION:includeHeading=TRUE) -->
51
+ Specifically, **User A** clicks the checkbox for option **Foo**, which currently has **0** votes.
52
+ 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`.
52
53
 
53
- <!-- prettier-ignore-start -->
54
- <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
54
+ The value change operation (op) is then transmitted to the service to be sequenced and eventually sent to other users in the collaborative session.
55
55
 
56
- ## Contribution Guidelines
56
+ At around the same time, **User B** clicks the checkbox for option **Foo**, which in their view currently has **0** votes.
57
+ Similarly to before, the application optimistically updates the associated counter value to **1**, and transmits its own update op.
57
58
 
58
- There are many ways to [contribute](https://github.com/microsoft/FluidFramework/blob/main/CONTRIBUTING.md) to Fluid.
59
+ 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**.
59
60
 
60
- - Participate in Q&A in our [GitHub Discussions](https://github.com/microsoft/FluidFramework/discussions).
61
- - [Submit bugs](https://github.com/microsoft/FluidFramework/issues) and help us verify fixes as they are checked in.
62
- - Review the [source code changes](https://github.com/microsoft/FluidFramework/pulls).
63
- - [Contribute bug fixes](https://github.com/microsoft/FluidFramework/blob/main/CONTRIBUTING.md).
61
+ Both users then receive acknowledgement of their update, and receive **op 0** and **op 1** to be applied in order.
62
+ Both clients apply **op 0** by setting **Foo** to **1**.
63
+ Then both clients apply **op 1** by setting **Foo** to **1**.
64
64
 
65
- Detailed instructions for working in the repo can be found in the [Wiki](https://github.com/microsoft/FluidFramework/wiki).
65
+ But this isn't right.
66
+ Two different users voted for option **Foo**, but the counter now displays **1**.
66
67
 
67
- This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
68
- For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
68
+ `SharedCounter` solves this problem by expressing its operations in terms of _increments_ and _decrements_ rather than as direct value updates.
69
69
 
70
- This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.
71
- Use of these trademarks or logos must follow Microsoft’s [Trademark & Brand Guidelines](https://www.microsoft.com/trademarks).
72
- Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
70
+ 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**.
71
+ At around the same time, **User B** would submit their own **+1** op for **Foo**.
73
72
 
74
- <!-- prettier-ignore-end -->
73
+ Assuming the same sequencing, both users first apply **op 0** and increment their counter for **Foo** by **+1** (from **0** to **1**).
74
+ Next, they both apply **op 1** and increment their counter for **Foo** by **+1** a second time (from **1** to **2**).
75
75
 
76
- <!-- AUTO-GENERATED-CONTENT:END -->
76
+ Now both users see the right vote count for `Foo`!
77
77
 
78
- <!-- AUTO-GENERATED-CONTENT:START (README_HELP_SECTION:includeHeading=TRUE) -->
78
+ ## Usage
79
79
 
80
- <!-- prettier-ignore-start -->
81
- <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
80
+ The `SharedCounter` object provides a simple API surface for managing a shared integer whose value may be incremented and decremented by collaborators.
82
81
 
83
- ## Help
82
+ A new `SharedCounter` value will be initialized with its value set to `0`.
83
+ 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`.
84
84
 
85
- Not finding what you're looking for in this README? Check out our [GitHub
86
- Wiki](https://github.com/microsoft/FluidFramework/wiki) or [fluidframework.com](https://fluidframework.com/docs/).
85
+ ## Installation
87
86
 
88
- Still not finding what you're looking for? Please [file an
89
- issue](https://github.com/microsoft/FluidFramework/wiki/Submitting-Bugs-and-Feature-Requests).
87
+ The package containing the `SharedCounter` library is [@fluidframework/shared-counter](https://www.npmjs.com/package/@fluidframework/counter).
90
88
 
91
- Thank you!
89
+ To get started, run the following from a terminal in your repository:
90
+
91
+ ```bash
92
+ npm install @fluidframework/shared-counter
93
+ ```
94
+
95
+ ### Creation
96
+
97
+ The workflow for creating a `SharedCounter` is effectively the same as many of our other DDSes.
98
+ For an example of how to create one, please see our workflow examples for [SharedMap creation][].
99
+
100
+ ### Incrementing / decrementing the value
101
+
102
+ Once you have created your `SharedCounter`, you can change its value using the [increment][] method.
103
+ This method accepts a positive or negative _integer_ to be applied to the shared value.
104
+
105
+ ```javascript
106
+ sharedCounter.increment(3); // Adds 3 to the current value
107
+ sharedCounter.increment(-5); // Subtracts 5 from the current value
108
+ ```
109
+
110
+ ### `incremented` event
111
+
112
+ The [incremented][] event is sent when a client in the collaborative session changes the counter value via the `increment` method.
113
+
114
+ Signature:
115
+
116
+ ```javascript
117
+ (event: "incremented", listener: (incrementAmount: number, newValue: number) => void)
118
+ ```
119
+
120
+ By listening to this event, you can receive and apply the changes coming from other collaborators.
121
+ Consider the following code example for configuring a Counter widget:
122
+
123
+ ```javascript
124
+ const sharedCounter = container.initialObjects.sharedCounter;
125
+ let counterValue = sharedCounter.value;
126
+
127
+ const incrementButton = document.createElement("button");
128
+ button.textContent = "Increment";
129
+ const decrementButton = document.createElement("button");
130
+ button.textContent = "Decrement";
131
+
132
+ // Increment / decrement shared counter value when the corresponding button is clicked
133
+ incrementButton.addEventListener("click", () => sharedCounter.increment(1));
134
+ decrementButton.addEventListener("click", () => sharedCounter.increment(-1));
135
+
136
+ const counterValueLabel = document.createElement("label");
137
+ counterValueLabel.textContent = `${counterValue}`;
138
+
139
+ // This function will be called each time the shared counter value is incremented
140
+ // (including increments from this client).
141
+ // Update the local counter value and the corresponding label being displayed in the widget.
142
+ const updateCounterValueLabel = (delta) => {
143
+ counterValue += delta;
144
+ counterValueLabel.textContent = `${counterValue}`;
145
+ };
146
+
147
+ // Register to be notified when the counter is incremented
148
+ sharedCounter.on("incremented", updateCounterValueLabel);
149
+ ```
150
+
151
+ In the code above, whenever a user presses either the Increment or Decrement button, the `sharedCounter.increment` is called with +/- 1.
152
+ This causes the `incremented` event to be sent to all of the clients who have this container open.
153
+
154
+ 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.
155
+
156
+ <!-- AUTO-GENERATED-CONTENT:START (README_API_DOCS_SECTION:includeHeading=TRUE) -->
157
+
158
+ <!-- prettier-ignore-start -->
159
+
160
+ <!-- This section is automatically generated. To update it, make the appropriate changes to docs/md-magic.config.js or the embedded content, then run 'npm run build:md-magic' in the docs folder. -->
161
+
162
+ ## API Documentation
163
+
164
+ API documentation for **@fluidframework/counter** is available at <https://fluidframework.com/docs/apis/counter>.
92
165
 
93
166
  <!-- prettier-ignore-end -->
94
167
 
@@ -110,3 +183,11 @@ Use of Microsoft trademarks or logos in modified versions of this project must n
110
183
  <!-- prettier-ignore-end -->
111
184
 
112
185
  <!-- AUTO-GENERATED-CONTENT:END -->
186
+
187
+ <!-- Links -->
188
+
189
+ [increment]: https://fluidframework.com/docs/apis/counter/isharedcounter-interface#increment-methodsignature
190
+ [incremented]: https://fluidframework.com/docs/apis/counter/isharedcounterevents-interface#_call_-callsignature
191
+ [optimistic dds]: https://fluidframework.com/docs/build/dds/#optimistic-data-structures
192
+ [sharedmap]: https://fluidframework.com/docs/data-structures/map
193
+ [sharedmap creation]: https://fluidframework.com/docs/data-structures/map/#creation
@@ -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-rc.1.0.0";
8
+ export declare const pkgVersion = "2.0.0-rc.1.0.2";
9
9
  //# sourceMappingURL=packageVersion.d.ts.map
@@ -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-rc.1.0.0";
11
+ exports.pkgVersion = "2.0.0-rc.1.0.2";
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,gBAAgB,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-rc.1.0.0\";\n"]}
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,gBAAgB,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-rc.1.0.2\";\n"]}
@@ -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-rc.1.0.0";
8
+ export declare const pkgVersion = "2.0.0-rc.1.0.2";
9
9
  //# sourceMappingURL=packageVersion.d.mts.map
@@ -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-rc.1.0.0";
8
+ export const pkgVersion = "2.0.0-rc.1.0.2";
9
9
  //# sourceMappingURL=packageVersion.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"packageVersion.mjs","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,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-rc.1.0.0\";\n"]}
1
+ {"version":3,"file":"packageVersion.mjs","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,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-rc.1.0.2\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/counter",
3
- "version": "2.0.0-rc.1.0.0",
3
+ "version": "2.0.0-rc.1.0.2",
4
4
  "description": "Counter DDS",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -87,13 +87,13 @@
87
87
  "temp-directory": "nyc/.nyc_output"
88
88
  },
89
89
  "dependencies": {
90
- "@fluidframework/core-interfaces": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
91
- "@fluidframework/core-utils": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
92
- "@fluidframework/datastore-definitions": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
93
- "@fluidframework/driver-utils": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
90
+ "@fluidframework/core-interfaces": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
91
+ "@fluidframework/core-utils": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
92
+ "@fluidframework/datastore-definitions": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
93
+ "@fluidframework/driver-utils": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
94
94
  "@fluidframework/protocol-definitions": "^3.1.0",
95
- "@fluidframework/runtime-definitions": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
96
- "@fluidframework/shared-object-base": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0"
95
+ "@fluidframework/runtime-definitions": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
96
+ "@fluidframework/shared-object-base": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@arethetypeswrong/cli": "^0.13.3",
@@ -102,8 +102,8 @@
102
102
  "@fluidframework/build-tools": "^0.29.0",
103
103
  "@fluidframework/counter-previous": "npm:@fluidframework/counter@2.0.0-internal.8.0.0",
104
104
  "@fluidframework/eslint-config-fluid": "^3.2.0",
105
- "@fluidframework/mocha-test-setup": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
106
- "@fluidframework/test-runtime-utils": ">=2.0.0-rc.1.0.0 <2.0.0-rc.1.1.0",
105
+ "@fluidframework/mocha-test-setup": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
106
+ "@fluidframework/test-runtime-utils": ">=2.0.0-rc.1.0.2 <2.0.0-rc.1.1.0",
107
107
  "@microsoft/api-extractor": "^7.38.3",
108
108
  "@types/mocha": "^9.1.1",
109
109
  "@types/node": "^18.19.0",
@@ -6,4 +6,4 @@
6
6
  */
7
7
 
8
8
  export const pkgName = "@fluidframework/counter";
9
- export const pkgVersion = "2.0.0-rc.1.0.0";
9
+ export const pkgVersion = "2.0.0-rc.1.0.2";