@liveblocks/client 0.17.5 → 0.17.8

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2021 LIVEBLOCKS PTY LTD
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -29,22 +29,22 @@ npm install @liveblocks/client
29
29
 
30
30
  ## Documentation
31
31
 
32
- - [Read the documentation](https://liveblocks.io/docs) to start using
33
- Liveblocks.
34
- - Explore the
35
- [API Reference](https://liveblocks.io/docs/api-reference/liveblocks-client).
32
+ Read the [documentation](https://liveblocks.io/docs) for guides and API
33
+ references.
36
34
 
37
35
  ## Examples
38
36
 
39
- - Browse our gallery of collaborative UI patterns.
40
- [View examples gallery](https://liveblocks.io/examples)
41
- - Explore and clone any of our open-source examples.
42
- [View code examples](https://github.com/liveblocks/liveblocks/tree/main/examples)
37
+ Explore our [collaborative examples](https://liveblocks.io/examples) to help you
38
+ get started.
39
+
40
+ > All examples are open-source and live in this repository, within
41
+ > [`/examples`](../../examples).
43
42
 
44
43
  ## Releases
45
44
 
46
- For changelog, visit
47
- [https://github.com/liveblocks/liveblocks/releases](https://github.com/liveblocks/liveblocks/releases).
45
+ See the [latest changes](https://github.com/liveblocks/liveblocks/releases) or
46
+ learn more about
47
+ [upcoming releases](https://github.com/liveblocks/liveblocks/milestones).
48
48
 
49
49
  ## Community
50
50
 
package/index.js CHANGED
@@ -161,7 +161,7 @@ function makeStateMachine(state, context, mockedEffects) {
161
161
  _state$pausedHistory,
162
162
  historyItem
163
163
  )
164
- : state.undoStack.push(historyItem);
164
+ : (state.undoStack.push(historyItem), onHistoryChange());
165
165
  }
166
166
  function storageDispatch(ops, reverse, storageUpdates) {
167
167
  var _state$batch$ops, _state$batch$reverseO;
@@ -414,7 +414,7 @@ function makeStateMachine(state, context, mockedEffects) {
414
414
  var ws = WebSocketPolyfill || WebSocket;
415
415
  return function (token) {
416
416
  return new ws(
417
- liveblocksServer + "/?token=" + token + "&version=0.17.5"
417
+ liveblocksServer + "/?token=" + token + "&version=0.17.8"
418
418
  );
419
419
  };
420
420
  })(
@@ -514,6 +514,18 @@ function makeStateMachine(state, context, mockedEffects) {
514
514
  (0, _step8.value)({ connectionId: message.actor, event: message.event });
515
515
  }
516
516
  }
517
+ function onHistoryChange() {
518
+ for (
519
+ var _step9,
520
+ _iterator9 = Json._createForOfIteratorHelperLoose(
521
+ state.listeners.history
522
+ );
523
+ !(_step9 = _iterator9()).done;
524
+
525
+ ) {
526
+ (0, _step9.value)({ canUndo: canUndo(), canRedo: canRedo() });
527
+ }
528
+ }
517
529
  function onUserJoinedMessage(message) {
518
530
  return (
519
531
  (state.users[message.actor] = {
@@ -552,13 +564,13 @@ function makeStateMachine(state, context, mockedEffects) {
552
564
  : Json.compact([parseServerMessage(data)]));
553
565
  if (null !== messages && 0 !== messages.length) {
554
566
  for (
555
- var _step9,
567
+ var _step10,
556
568
  updates = { storageUpdates: new Map(), others: [] },
557
- _iterator9 = Json._createForOfIteratorHelperLoose(messages);
558
- !(_step9 = _iterator9()).done;
569
+ _iterator10 = Json._createForOfIteratorHelperLoose(messages);
570
+ !(_step10 = _iterator10()).done;
559
571
 
560
572
  ) {
561
- var message = _step9.value;
573
+ var message = _step10.value;
562
574
  switch (message.type) {
563
575
  case Json.ServerMsgCode.USER_JOINED:
564
576
  updates.others.push(onUserJoinedMessage(message));
@@ -615,15 +627,15 @@ function makeStateMachine(state, context, mockedEffects) {
615
627
  ) {
616
628
  updateConnection({ state: "failed" });
617
629
  for (
618
- var _step10,
630
+ var _step11,
619
631
  error = new LiveblocksError(event.reason, event.code),
620
- _iterator10 = Json._createForOfIteratorHelperLoose(
632
+ _iterator11 = Json._createForOfIteratorHelperLoose(
621
633
  state.listeners.error
622
634
  );
623
- !(_step10 = _iterator10()).done;
635
+ !(_step11 = _iterator11()).done;
624
636
 
625
637
  ) {
626
- (0, _step10.value)(error);
638
+ (0, _step11.value)(error);
627
639
  }
628
640
  var _delay = getRetryDelay(!0);
629
641
  state.numberOfRetry++,
@@ -659,14 +671,14 @@ function makeStateMachine(state, context, mockedEffects) {
659
671
  function updateConnection(connection) {
660
672
  state.connection = connection;
661
673
  for (
662
- var _step11,
663
- _iterator11 = Json._createForOfIteratorHelperLoose(
674
+ var _step12,
675
+ _iterator12 = Json._createForOfIteratorHelperLoose(
664
676
  state.listeners.connection
665
677
  );
666
- !(_step11 = _iterator11()).done;
678
+ !(_step12 = _iterator12()).done;
667
679
 
668
680
  ) {
669
- (0, _step11.value)(connection.state);
681
+ (0, _step12.value)(connection.state);
670
682
  }
671
683
  }
672
684
  function getRetryDelay(slow) {
@@ -757,14 +769,14 @@ function makeStateMachine(state, context, mockedEffects) {
757
769
  data: state.buffer.presence,
758
770
  });
759
771
  for (
760
- var _step12,
761
- _iterator12 = Json._createForOfIteratorHelperLoose(
772
+ var _step13,
773
+ _iterator13 = Json._createForOfIteratorHelperLoose(
762
774
  state.buffer.messages
763
775
  );
764
- !(_step12 = _iterator12()).done;
776
+ !(_step13 = _iterator13()).done;
765
777
 
766
778
  ) {
767
- var _event3 = _step12.value;
779
+ var _event3 = _step13.value;
768
780
  messages.push(_event3);
769
781
  }
770
782
  state.buffer.storageOperations.length > 0 &&
@@ -803,6 +815,12 @@ function makeStateMachine(state, context, mockedEffects) {
803
815
  }
804
816
  var _getInitialStatePromise = null,
805
817
  _getInitialStateResolver = null;
818
+ function canUndo() {
819
+ return state.undoStack.length > 0;
820
+ }
821
+ function canRedo() {
822
+ return state.redoStack.length > 0;
823
+ }
806
824
  return {
807
825
  onClose: onClose,
808
826
  onMessage: onMessage,
@@ -953,14 +971,16 @@ function makeStateMachine(state, context, mockedEffects) {
953
971
  if (null != historyItem) {
954
972
  state.isHistoryPaused = !1;
955
973
  var result = apply(historyItem, !0);
956
- notify(result.updates), state.redoStack.push(result.reverse);
974
+ notify(result.updates),
975
+ state.redoStack.push(result.reverse),
976
+ onHistoryChange();
957
977
  for (
958
- var _step13,
959
- _iterator13 = Json._createForOfIteratorHelperLoose(historyItem);
960
- !(_step13 = _iterator13()).done;
978
+ var _step14,
979
+ _iterator14 = Json._createForOfIteratorHelperLoose(historyItem);
980
+ !(_step14 = _iterator14()).done;
961
981
 
962
982
  ) {
963
- var op = _step13.value;
983
+ var op = _step14.value;
964
984
  "presence" !== op.type && state.buffer.storageOperations.push(op);
965
985
  }
966
986
  tryFlushing();
@@ -973,19 +993,23 @@ function makeStateMachine(state, context, mockedEffects) {
973
993
  if (null != historyItem) {
974
994
  state.isHistoryPaused = !1;
975
995
  var result = apply(historyItem, !0);
976
- notify(result.updates), state.undoStack.push(result.reverse);
996
+ notify(result.updates),
997
+ state.undoStack.push(result.reverse),
998
+ onHistoryChange();
977
999
  for (
978
- var _step14,
979
- _iterator14 = Json._createForOfIteratorHelperLoose(historyItem);
980
- !(_step14 = _iterator14()).done;
1000
+ var _step15,
1001
+ _iterator15 = Json._createForOfIteratorHelperLoose(historyItem);
1002
+ !(_step15 = _iterator15()).done;
981
1003
 
982
1004
  ) {
983
- var op = _step14.value;
1005
+ var op = _step15.value;
984
1006
  "presence" !== op.type && state.buffer.storageOperations.push(op);
985
1007
  }
986
1008
  tryFlushing();
987
1009
  }
988
1010
  },
1011
+ canUndo: canUndo,
1012
+ canRedo: canRedo,
989
1013
  pauseHistory: function () {
990
1014
  (state.pausedHistory = []), (state.isHistoryPaused = !0);
991
1015
  },
@@ -1058,6 +1082,7 @@ function createRoom(options, context) {
1058
1082
  error: [],
1059
1083
  connection: [],
1060
1084
  storage: [],
1085
+ history: [],
1061
1086
  },
1062
1087
  numberOfRetry: 0,
1063
1088
  lastFlushTime: 0,
@@ -1113,6 +1138,8 @@ function createRoom(options, context) {
1113
1138
  history: {
1114
1139
  undo: machine.undo,
1115
1140
  redo: machine.redo,
1141
+ canUndo: machine.canUndo,
1142
+ canRedo: machine.canRedo,
1116
1143
  pause: machine.pauseHistory,
1117
1144
  resume: machine.resumeHistory,
1118
1145
  },
package/index.mjs CHANGED
@@ -141,7 +141,7 @@ function makeStateMachine(state, context, mockedEffects) {
141
141
  state.undoStack.length >= 50 && state.undoStack.shift(),
142
142
  state.isHistoryPaused
143
143
  ? state.pausedHistory.unshift(...historyItem)
144
- : state.undoStack.push(historyItem);
144
+ : (state.undoStack.push(historyItem), onHistoryChange());
145
145
  }
146
146
  function storageDispatch(ops, reverse, storageUpdates) {
147
147
  state.isBatching
@@ -337,7 +337,7 @@ function makeStateMachine(state, context, mockedEffects) {
337
337
  );
338
338
  const ws = WebSocketPolyfill || WebSocket;
339
339
  return (token) =>
340
- new ws(`${liveblocksServer}/?token=${token}&version=0.17.5`);
340
+ new ws(`${liveblocksServer}/?token=${token}&version=0.17.8`);
341
341
  })(
342
342
  context.liveblocksServer,
343
343
  null !==
@@ -427,6 +427,10 @@ function makeStateMachine(state, context, mockedEffects) {
427
427
  for (const listener of state.listeners.event)
428
428
  listener({ connectionId: message.actor, event: message.event });
429
429
  }
430
+ function onHistoryChange() {
431
+ for (const listener of state.listeners.history)
432
+ listener({ canUndo: canUndo(), canRedo: canRedo() });
433
+ }
430
434
  function onUserJoinedMessage(message) {
431
435
  return (
432
436
  (state.users[message.actor] = {
@@ -655,6 +659,12 @@ function makeStateMachine(state, context, mockedEffects) {
655
659
  }
656
660
  let _getInitialStatePromise = null,
657
661
  _getInitialStateResolver = null;
662
+ function canUndo() {
663
+ return state.undoStack.length > 0;
664
+ }
665
+ function canRedo() {
666
+ return state.redoStack.length > 0;
667
+ }
658
668
  return {
659
669
  onClose: onClose,
660
670
  onMessage: onMessage,
@@ -795,7 +805,9 @@ function makeStateMachine(state, context, mockedEffects) {
795
805
  if (null == historyItem) return;
796
806
  state.isHistoryPaused = !1;
797
807
  const result = apply(historyItem, !0);
798
- notify(result.updates), state.redoStack.push(result.reverse);
808
+ notify(result.updates),
809
+ state.redoStack.push(result.reverse),
810
+ onHistoryChange();
799
811
  for (const op of historyItem)
800
812
  "presence" !== op.type && state.buffer.storageOperations.push(op);
801
813
  tryFlushing();
@@ -807,11 +819,15 @@ function makeStateMachine(state, context, mockedEffects) {
807
819
  if (null == historyItem) return;
808
820
  state.isHistoryPaused = !1;
809
821
  const result = apply(historyItem, !0);
810
- notify(result.updates), state.undoStack.push(result.reverse);
822
+ notify(result.updates),
823
+ state.undoStack.push(result.reverse),
824
+ onHistoryChange();
811
825
  for (const op of historyItem)
812
826
  "presence" !== op.type && state.buffer.storageOperations.push(op);
813
827
  tryFlushing();
814
828
  },
829
+ canUndo: canUndo,
830
+ canRedo: canRedo,
815
831
  pauseHistory: function () {
816
832
  (state.pausedHistory = []), (state.isHistoryPaused = !0);
817
833
  },
@@ -877,6 +893,7 @@ function createRoom(options, context) {
877
893
  error: [],
878
894
  connection: [],
879
895
  storage: [],
896
+ history: [],
880
897
  },
881
898
  numberOfRetry: 0,
882
899
  lastFlushTime: 0,
@@ -932,6 +949,8 @@ function createRoom(options, context) {
932
949
  history: {
933
950
  undo: machine.undo,
934
951
  redo: machine.redo,
952
+ canUndo: machine.canUndo,
953
+ canRedo: machine.canRedo,
935
954
  pause: machine.pauseHistory,
936
955
  resume: machine.resumeHistory,
937
956
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/client",
3
- "version": "0.17.5",
3
+ "version": "0.17.8",
4
4
  "description": "A client that lets you interact with Liveblocks servers.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -17,9 +17,6 @@
17
17
  "import": "./index.mjs"
18
18
  }
19
19
  },
20
- "files": [
21
- "**"
22
- ],
23
20
  "keywords": [
24
21
  "liveblocks",
25
22
  "multiplayer",
@@ -29,47 +26,7 @@
29
26
  "bugs": {
30
27
  "url": "https://github.com/liveblocks/liveblocks/issues"
31
28
  },
32
- "scripts": {
33
- "build": "rollup -c && cp ./package.json ./README.md ./lib",
34
- "format": "eslint --fix src/ test/ && prettier --write src/ test/",
35
- "lint": "eslint src/ test/",
36
- "test": "jest --watch --silent --verbose --config=./jest.config.js",
37
- "test-ci": "jest --silent --verbose",
38
- "test-e2e": "jest --silent --verbose --config=./jest.config.e2e.js"
39
- },
40
29
  "license": "Apache-2.0",
41
- "devDependencies": {
42
- "@babel/core": "^7.17.10",
43
- "@babel/preset-env": "^7.17.10",
44
- "@babel/preset-typescript": "^7.16.7",
45
- "@rollup/plugin-babel": "^5.3.1",
46
- "@rollup/plugin-node-resolve": "^13.1.3",
47
- "@rollup/plugin-replace": "^4.0.0",
48
- "@rollup/plugin-typescript": "^8.3.1",
49
- "@types/jest": "^26.0.24",
50
- "@types/node-fetch": "^2.6.1",
51
- "@types/ws": "^8.5.3",
52
- "@typescript-eslint/eslint-plugin": "^5.26.0",
53
- "@typescript-eslint/parser": "^5.26.0",
54
- "dotenv": "^16.0.0",
55
- "eslint": "^8.12.0",
56
- "eslint-plugin-import": "^2.26.0",
57
- "eslint-plugin-simple-import-sort": "^7.0.0",
58
- "jest": "^28.0.3",
59
- "jest-each": "^27.5.1",
60
- "jest-environment-jsdom": "^28.1.0",
61
- "msw": "^0.39.1",
62
- "node-fetch": "2.6.7",
63
- "regenerator-runtime": "^0.13.9",
64
- "rollup": "^2.68.0",
65
- "rollup-plugin-command": "^1.1.3",
66
- "rollup-plugin-dts": "^4.2.2",
67
- "rollup-plugin-prettier": "^2.2.2",
68
- "rollup-plugin-terser": "^7.0.2",
69
- "typescript": "^4.7.2",
70
- "whatwg-fetch": "^3.6.2",
71
- "ws": "^8.5.0"
72
- },
73
30
  "repository": {
74
31
  "type": "git",
75
32
  "url": "https://github.com/liveblocks/liveblocks.git",
package/shared.d.ts CHANGED
@@ -312,6 +312,7 @@ declare type EventCallback<TRoomEvent extends Json> = ({
312
312
  }) => void;
313
313
  declare type ErrorCallback = (error: Error) => void;
314
314
  declare type ConnectionCallback = (state: ConnectionState) => void;
315
+ declare type HistoryCallback = (event: HistoryEvent) => void;
315
316
  declare type UpdateDelta =
316
317
  | {
317
318
  type: "update";
@@ -604,6 +605,27 @@ interface History {
604
605
  * // room.getPresence() equals { selectedId: "yy" }
605
606
  */
606
607
  redo: () => void;
608
+ /**
609
+ * Returns whether there are any operations to undo.
610
+ *
611
+ * @example
612
+ * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
613
+ * // room.history.canUndo() is true
614
+ * room.history.undo();
615
+ * // room.history.canUndo() is false
616
+ */
617
+ canUndo: () => boolean;
618
+ /**
619
+ * Returns whether there are any operations to redo.
620
+ *
621
+ * @example
622
+ * room.updatePresence({ selectedId: "xx" }, { addToHistory: true });
623
+ * room.history.undo();
624
+ * // room.history.canRedo() is true
625
+ * room.history.redo();
626
+ * // room.history.canRedo() is false
627
+ */
628
+ canRedo: () => boolean;
607
629
  /**
608
630
  * All future modifications made on the Room will be merged together to create a single history item until resume is called.
609
631
  *
@@ -631,6 +653,10 @@ interface History {
631
653
  */
632
654
  resume: () => void;
633
655
  }
656
+ interface HistoryEvent {
657
+ canUndo: boolean;
658
+ canRedo: boolean;
659
+ }
634
660
  declare type Room<
635
661
  TPresence extends JsonObject,
636
662
  TStorage extends LsonObject,
@@ -648,6 +674,8 @@ declare type Room<
648
674
  *
649
675
  * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
650
676
  *
677
+ * @returns Unsubscribe function.
678
+ *
651
679
  * @example
652
680
  * room.subscribe("my-presence", (presence) => {
653
681
  * // Do something
@@ -659,6 +687,8 @@ declare type Room<
659
687
  *
660
688
  * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
661
689
  *
690
+ * @returns Unsubscribe function.
691
+ *
662
692
  * @example
663
693
  * room.subscribe("others", (others) => {
664
694
  * // Do something
@@ -673,6 +703,8 @@ declare type Room<
673
703
  *
674
704
  * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
675
705
  *
706
+ * @returns Unsubscribe function.
707
+ *
676
708
  * @example
677
709
  * room.subscribe("event", ({ event, connectionId }) => {
678
710
  * // Do something
@@ -681,10 +713,14 @@ declare type Room<
681
713
  (type: "event", listener: EventCallback<TRoomEvent>): () => void;
682
714
  /**
683
715
  * Subscribe to errors thrown in the room.
716
+ *
717
+ * @returns Unsubscribe function.
684
718
  */
685
719
  (type: "error", listener: ErrorCallback): () => void;
686
720
  /**
687
721
  * Subscribe to connection state updates.
722
+ *
723
+ * @returns Unsubscribe function.
688
724
  */
689
725
  (type: "connection", listener: ConnectionCallback): () => void;
690
726
  /**
@@ -726,6 +762,17 @@ declare type Room<
726
762
  isDeep: true;
727
763
  }
728
764
  ): () => void;
765
+ /**
766
+ * Subscribe to the current user's history changes.
767
+ *
768
+ * @returns Unsubscribe function.
769
+ *
770
+ * @example
771
+ * room.subscribe("history", ({ canUndo, canRedo }) => {
772
+ * // Do something
773
+ * });
774
+ */
775
+ (type: "history", listener: HistoryCallback): () => void;
729
776
  };
730
777
  /**
731
778
  * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
package/shared.js CHANGED
@@ -571,6 +571,12 @@ function comparePosition(posA, posB) {
571
571
  'Impossible to compare similar position "' + posA + '" and "' + posB + '"'
572
572
  );
573
573
  }
574
+ function compareNodePosition(itemA, itemB) {
575
+ return comparePosition(
576
+ itemA._getParentKeyOrThrow(),
577
+ itemB._getParentKeyOrThrow()
578
+ );
579
+ }
574
580
  _Symbol$iterator$1 = Symbol.iterator;
575
581
  var LiveList = (function (_AbstractCrdt) {
576
582
  function LiveList(items) {
@@ -607,9 +613,7 @@ var LiveList = (function (_AbstractCrdt) {
607
613
  _id = _step$value[0],
608
614
  crdt = _step$value[1],
609
615
  child = deserialize([_id, crdt], parentToChildren, doc);
610
- child._setParentLink(list, crdt.parentKey),
611
- list._items.push(child),
612
- sortListItem(list._items);
616
+ child._setParentLink(list, crdt.parentKey), list._insertAndSort(child);
613
617
  }
614
618
  return list;
615
619
  });
@@ -640,6 +644,12 @@ var LiveList = (function (_AbstractCrdt) {
640
644
  }
641
645
  return ops;
642
646
  }),
647
+ (_proto._insertAndSort = function (item) {
648
+ this._items.push(item), this._sortItems();
649
+ }),
650
+ (_proto._sortItems = function () {
651
+ this._items.sort(compareNodePosition);
652
+ }),
643
653
  (_proto._indexOfPosition = function (position) {
644
654
  return this._items.findIndex(function (item) {
645
655
  return item._getParentKeyOrThrow() === position;
@@ -700,8 +710,7 @@ var LiveList = (function (_AbstractCrdt) {
700
710
  _deleteDelta2 = this._detachItemAssociatedToSetOperation(op.deletedId);
701
711
  return (
702
712
  _deleteDelta2 && updates.push(_deleteDelta2),
703
- this._items.push(child),
704
- sortListItem(this._items),
713
+ this._insertAndSort(child),
705
714
  updates.push(insertDelta(this._indexOfPosition(key), child)),
706
715
  { reverse: [], modified: makeUpdate(this, updates) }
707
716
  );
@@ -737,8 +746,7 @@ var LiveList = (function (_AbstractCrdt) {
737
746
  this._items.splice(indexOfItemWithSamePosition, 1),
738
747
  delta.push(deleteDelta(indexOfItemWithSamePosition)));
739
748
  var previousIndex = this._items.indexOf(existingItem);
740
- existingItem._setParentLink(this, op.parentKey),
741
- sortListItem(this._items);
749
+ existingItem._setParentLink(this, op.parentKey), this._sortItems();
742
750
  var newIndex = this._items.indexOf(existingItem);
743
751
  return (
744
752
  newIndex !== previousIndex &&
@@ -750,8 +758,7 @@ var LiveList = (function (_AbstractCrdt) {
750
758
  if (orphan && this._implicitlyDeletedItems.has(orphan)) {
751
759
  orphan._setParentLink(this, op.parentKey),
752
760
  this._implicitlyDeletedItems.delete(orphan),
753
- this._items.push(orphan),
754
- sortListItem(this._items);
761
+ this._insertAndSort(orphan);
755
762
  var recreatedItemIndex = this._items.indexOf(orphan);
756
763
  return {
757
764
  modified: makeUpdate(
@@ -820,7 +827,7 @@ var LiveList = (function (_AbstractCrdt) {
820
827
  -1 !== itemIndexAtPosition &&
821
828
  this._shiftItemPosition(itemIndexAtPosition, key),
822
829
  existingItem._setParentLink(this, key),
823
- sortListItem(this._items);
830
+ this._sortItems();
824
831
  var newIndex = this._indexOfPosition(key);
825
832
  return newIndex === oldPositionIndex
826
833
  ? { modified: !1 }
@@ -836,8 +843,7 @@ var LiveList = (function (_AbstractCrdt) {
836
843
  return (
837
844
  orphan._setParentLink(this, key),
838
845
  this._implicitlyDeletedItems.delete(orphan),
839
- this._items.push(orphan),
840
- sortListItem(this._items),
846
+ this._insertAndSort(orphan),
841
847
  {
842
848
  modified: makeUpdate(this, [
843
849
  insertDelta(this._indexOfPosition(key), orphan),
@@ -880,8 +886,7 @@ var LiveList = (function (_AbstractCrdt) {
880
886
  )),
881
887
  child._setParentLink(this, newKey));
882
888
  return (
883
- this._items.push(child),
884
- sortListItem(this._items),
889
+ this._insertAndSort(child),
885
890
  {
886
891
  modified: makeUpdate(this, [
887
892
  insertDelta(this._indexOfPosition(newKey), child),
@@ -916,8 +921,7 @@ var LiveList = (function (_AbstractCrdt) {
916
921
  { modified: makeUpdate(this, delta), reverse: reverse }
917
922
  );
918
923
  }
919
- this._items.push(child),
920
- sortListItem(this._items),
924
+ this._insertAndSort(child),
921
925
  this._detachItemAssociatedToSetOperation(op.deletedId);
922
926
  var newIndex = this._indexOfPosition(newKey);
923
927
  return {
@@ -961,8 +965,7 @@ var LiveList = (function (_AbstractCrdt) {
961
965
  return (
962
966
  this._implicitlyDeletedItems.delete(child),
963
967
  child._setParentLink(this, newKey),
964
- this._items.push(child),
965
- sortListItem(this._items),
968
+ this._insertAndSort(child),
966
969
  {
967
970
  modified: makeUpdate(this, [
968
971
  insertDelta(this._items.indexOf(child), child),
@@ -975,7 +978,7 @@ var LiveList = (function (_AbstractCrdt) {
975
978
  existingItemIndex = this._indexOfPosition(newKey);
976
979
  if (-1 === existingItemIndex) {
977
980
  var previousIndex = this._items.indexOf(child);
978
- child._setParentLink(this, newKey), sortListItem(this._items);
981
+ child._setParentLink(this, newKey), this._sortItems();
979
982
  var _newIndex4 = this._items.indexOf(child);
980
983
  return _newIndex4 === previousIndex
981
984
  ? { modified: !1 }
@@ -996,7 +999,7 @@ var LiveList = (function (_AbstractCrdt) {
996
999
  )
997
1000
  );
998
1001
  var _previousIndex = this._items.indexOf(child);
999
- child._setParentLink(this, newKey), sortListItem(this._items);
1002
+ child._setParentLink(this, newKey), this._sortItems();
1000
1003
  var _newIndex5 = this._items.indexOf(child);
1001
1004
  return _newIndex5 === _previousIndex
1002
1005
  ? { modified: !1 }
@@ -1026,8 +1029,7 @@ var LiveList = (function (_AbstractCrdt) {
1026
1029
  );
1027
1030
  return (
1028
1031
  child._setParentLink(this, newKey),
1029
- this._items.push(child),
1030
- sortListItem(this._items),
1032
+ this._insertAndSort(child),
1031
1033
  { modified: !1 }
1032
1034
  );
1033
1035
  }
@@ -1046,7 +1048,7 @@ var LiveList = (function (_AbstractCrdt) {
1046
1048
  )
1047
1049
  ),
1048
1050
  child._setParentLink(this, newKey),
1049
- sortListItem(this._items);
1051
+ this._sortItems();
1050
1052
  var newIndex = this._items.indexOf(child);
1051
1053
  return previousIndex === newIndex
1052
1054
  ? { modified: !1 }
@@ -1072,7 +1074,7 @@ var LiveList = (function (_AbstractCrdt) {
1072
1074
  : _this$_items4._getParentKeyOrThrow()
1073
1075
  )
1074
1076
  );
1075
- child._setParentLink(this, newKey), sortListItem(this._items);
1077
+ child._setParentLink(this, newKey), this._sortItems();
1076
1078
  var newIndex = this._items.indexOf(child);
1077
1079
  return previousIndex === newIndex
1078
1080
  ? { modified: !1 }
@@ -1130,8 +1132,7 @@ var LiveList = (function (_AbstractCrdt) {
1130
1132
  value = lsonToLiveNode(element);
1131
1133
  if (
1132
1134
  (value._setParentLink(this, position),
1133
- this._items.push(value),
1134
- sortListItem(this._items),
1135
+ this._insertAndSort(value),
1135
1136
  this._doc && this._id)
1136
1137
  ) {
1137
1138
  var id = this._doc.generateId();
@@ -1172,7 +1173,7 @@ var LiveList = (function (_AbstractCrdt) {
1172
1173
  previousPosition = item._getParentKeyOrThrow();
1173
1174
  if (
1174
1175
  (item._setParentLink(this, position),
1175
- sortListItem(this._items),
1176
+ this._sortItems(),
1176
1177
  this._doc && this._id)
1177
1178
  ) {
1178
1179
  var storageUpdates = new Map([
@@ -1349,8 +1350,7 @@ var LiveList = (function (_AbstractCrdt) {
1349
1350
  return (
1350
1351
  newItem._attach(op.id, nn(this._doc)),
1351
1352
  newItem._setParentLink(this, key),
1352
- this._items.push(newItem),
1353
- sortListItem(this._items),
1353
+ this._insertAndSort(newItem),
1354
1354
  { newItem: newItem, newIndex: this._indexOfPosition(key) }
1355
1355
  );
1356
1356
  }),
@@ -1426,14 +1426,6 @@ function moveDelta(previousIndex, index, item) {
1426
1426
  item: item instanceof LiveRegister ? item.data : item,
1427
1427
  };
1428
1428
  }
1429
- function sortListItem(items) {
1430
- items.sort(function (itemA, itemB) {
1431
- return comparePosition(
1432
- itemA._getParentKeyOrThrow(),
1433
- itemB._getParentKeyOrThrow()
1434
- );
1435
- });
1436
- }
1437
1429
  function addIntentAndDeletedIdToOperation(ops, deletedId) {
1438
1430
  if (0 === ops.length)
1439
1431
  throw new Error(
@@ -2573,7 +2565,8 @@ function isJsonArray(data) {
2573
2565
  "others" === value ||
2574
2566
  "event" === value ||
2575
2567
  "error" === value ||
2576
- "connection" === value
2568
+ "connection" === value ||
2569
+ "history" === value
2577
2570
  );
2578
2571
  }),
2579
2572
  (exports.isRootCrdt = function (crdt) {
package/shared.mjs CHANGED
@@ -66,7 +66,8 @@ function isRoomEventName(value) {
66
66
  "others" === value ||
67
67
  "event" === value ||
68
68
  "error" === value ||
69
- "connection" === value
69
+ "connection" === value ||
70
+ "history" === value
70
71
  );
71
72
  }
72
73
  function HasParent(node, key) {
@@ -363,6 +364,12 @@ function comparePosition(posA, posB) {
363
364
  `Impossible to compare similar position "${posA}" and "${posB}"`
364
365
  );
365
366
  }
367
+ function compareNodePosition(itemA, itemB) {
368
+ return comparePosition(
369
+ itemA._getParentKeyOrThrow(),
370
+ itemB._getParentKeyOrThrow()
371
+ );
372
+ }
366
373
  class LiveList extends AbstractCrdt {
367
374
  constructor(items = []) {
368
375
  let position;
@@ -385,9 +392,7 @@ class LiveList extends AbstractCrdt {
385
392
  if (null == children) return list;
386
393
  for (const [id, crdt] of children) {
387
394
  const child = deserialize([id, crdt], parentToChildren, doc);
388
- child._setParentLink(list, crdt.parentKey),
389
- list._items.push(child),
390
- sortListItem(list._items);
395
+ child._setParentLink(list, crdt.parentKey), list._insertAndSort(child);
391
396
  }
392
397
  return list;
393
398
  }
@@ -407,6 +412,12 @@ class LiveList extends AbstractCrdt {
407
412
  ops.push(...item._serialize(this._id, item._getParentKeyOrThrow(), doc));
408
413
  return ops;
409
414
  }
415
+ _insertAndSort(item) {
416
+ this._items.push(item), this._sortItems();
417
+ }
418
+ _sortItems() {
419
+ this._items.sort(compareNodePosition);
420
+ }
410
421
  _indexOfPosition(position) {
411
422
  return this._items.findIndex(
412
423
  (item) => item._getParentKeyOrThrow() === position
@@ -457,8 +468,7 @@ class LiveList extends AbstractCrdt {
457
468
  deleteDelta = this._detachItemAssociatedToSetOperation(op.deletedId);
458
469
  return (
459
470
  deleteDelta && updates.push(deleteDelta),
460
- this._items.push(child),
461
- sortListItem(this._items),
471
+ this._insertAndSort(child),
462
472
  updates.push(insertDelta(this._indexOfPosition(key), child)),
463
473
  { reverse: [], modified: makeUpdate(this, updates) }
464
474
  );
@@ -493,8 +503,7 @@ class LiveList extends AbstractCrdt {
493
503
  this._items.splice(indexOfItemWithSamePosition, 1),
494
504
  delta.push(deleteDelta(indexOfItemWithSamePosition)));
495
505
  const previousIndex = this._items.indexOf(existingItem);
496
- existingItem._setParentLink(this, op.parentKey),
497
- sortListItem(this._items);
506
+ existingItem._setParentLink(this, op.parentKey), this._sortItems();
498
507
  const newIndex = this._items.indexOf(existingItem);
499
508
  return (
500
509
  newIndex !== previousIndex &&
@@ -507,8 +516,7 @@ class LiveList extends AbstractCrdt {
507
516
  if (orphan && this._implicitlyDeletedItems.has(orphan)) {
508
517
  orphan._setParentLink(this, op.parentKey),
509
518
  this._implicitlyDeletedItems.delete(orphan),
510
- this._items.push(orphan),
511
- sortListItem(this._items);
519
+ this._insertAndSort(orphan);
512
520
  const recreatedItemIndex = this._items.indexOf(orphan);
513
521
  return {
514
522
  modified: makeUpdate(this, [
@@ -568,7 +576,7 @@ class LiveList extends AbstractCrdt {
568
576
  -1 !== itemIndexAtPosition &&
569
577
  this._shiftItemPosition(itemIndexAtPosition, key),
570
578
  existingItem._setParentLink(this, key),
571
- sortListItem(this._items);
579
+ this._sortItems();
572
580
  const newIndex = this._indexOfPosition(key);
573
581
  return newIndex === oldPositionIndex
574
582
  ? { modified: !1 }
@@ -585,8 +593,7 @@ class LiveList extends AbstractCrdt {
585
593
  if (orphan && this._implicitlyDeletedItems.has(orphan)) {
586
594
  orphan._setParentLink(this, key),
587
595
  this._implicitlyDeletedItems.delete(orphan),
588
- this._items.push(orphan),
589
- sortListItem(this._items);
596
+ this._insertAndSort(orphan);
590
597
  return {
591
598
  modified: makeUpdate(this, [
592
599
  insertDelta(this._indexOfPosition(key), orphan),
@@ -629,7 +636,7 @@ class LiveList extends AbstractCrdt {
629
636
  )),
630
637
  child._setParentLink(this, newKey);
631
638
  }
632
- this._items.push(child), sortListItem(this._items);
639
+ this._insertAndSort(child);
633
640
  return {
634
641
  modified: makeUpdate(this, [
635
642
  insertDelta(this._indexOfPosition(newKey), child),
@@ -663,8 +670,7 @@ class LiveList extends AbstractCrdt {
663
670
  );
664
671
  }
665
672
  {
666
- this._items.push(child),
667
- sortListItem(this._items),
673
+ this._insertAndSort(child),
668
674
  this._detachItemAssociatedToSetOperation(op.deletedId);
669
675
  const newIndex = this._indexOfPosition(newKey);
670
676
  return {
@@ -709,8 +715,7 @@ class LiveList extends AbstractCrdt {
709
715
  if (this._implicitlyDeletedItems.has(child)) {
710
716
  this._implicitlyDeletedItems.delete(child),
711
717
  child._setParentLink(this, newKey),
712
- this._items.push(child),
713
- sortListItem(this._items);
718
+ this._insertAndSort(child);
714
719
  return {
715
720
  modified: makeUpdate(this, [
716
721
  insertDelta(this._items.indexOf(child), child),
@@ -722,7 +727,7 @@ class LiveList extends AbstractCrdt {
722
727
  const existingItemIndex = this._indexOfPosition(newKey);
723
728
  if (-1 === existingItemIndex) {
724
729
  const previousIndex = this._items.indexOf(child);
725
- child._setParentLink(this, newKey), sortListItem(this._items);
730
+ child._setParentLink(this, newKey), this._sortItems();
726
731
  const newIndex = this._items.indexOf(child);
727
732
  return newIndex === previousIndex
728
733
  ? { modified: !1 }
@@ -744,7 +749,7 @@ class LiveList extends AbstractCrdt {
744
749
  )
745
750
  );
746
751
  const previousIndex = this._items.indexOf(child);
747
- child._setParentLink(this, newKey), sortListItem(this._items);
752
+ child._setParentLink(this, newKey), this._sortItems();
748
753
  const newIndex = this._items.indexOf(child);
749
754
  return newIndex === previousIndex
750
755
  ? { modified: !1 }
@@ -775,8 +780,7 @@ class LiveList extends AbstractCrdt {
775
780
  )
776
781
  ),
777
782
  child._setParentLink(this, newKey),
778
- this._items.push(child),
779
- sortListItem(this._items),
783
+ this._insertAndSort(child),
780
784
  { modified: !1 }
781
785
  );
782
786
  }
@@ -795,7 +799,7 @@ class LiveList extends AbstractCrdt {
795
799
  )
796
800
  ),
797
801
  child._setParentLink(this, newKey),
798
- sortListItem(this._items);
802
+ this._sortItems();
799
803
  const newIndex = this._items.indexOf(child);
800
804
  return previousIndex === newIndex
801
805
  ? { modified: !1 }
@@ -823,7 +827,7 @@ class LiveList extends AbstractCrdt {
823
827
  )
824
828
  ),
825
829
  child._setParentLink(this, newKey),
826
- sortListItem(this._items);
830
+ this._sortItems();
827
831
  const newIndex = this._items.indexOf(child);
828
832
  return previousIndex === newIndex
829
833
  ? { modified: !1 }
@@ -879,8 +883,7 @@ class LiveList extends AbstractCrdt {
879
883
  value = lsonToLiveNode(element);
880
884
  if (
881
885
  (value._setParentLink(this, position),
882
- this._items.push(value),
883
- sortListItem(this._items),
886
+ this._insertAndSort(value),
884
887
  this._doc && this._id)
885
888
  ) {
886
889
  const id = this._doc.generateId();
@@ -919,7 +922,7 @@ class LiveList extends AbstractCrdt {
919
922
  previousPosition = item._getParentKeyOrThrow();
920
923
  if (
921
924
  (item._setParentLink(this, position),
922
- sortListItem(this._items),
925
+ this._sortItems(),
923
926
  this._doc && this._id)
924
927
  ) {
925
928
  const storageUpdates = new Map([
@@ -1073,8 +1076,7 @@ class LiveList extends AbstractCrdt {
1073
1076
  const newItem = creationOpToLiveNode(op);
1074
1077
  newItem._attach(op.id, nn(this._doc)),
1075
1078
  newItem._setParentLink(this, key),
1076
- this._items.push(newItem),
1077
- sortListItem(this._items);
1079
+ this._insertAndSort(newItem);
1078
1080
  return { newItem: newItem, newIndex: this._indexOfPosition(key) };
1079
1081
  }
1080
1082
  _shiftItemPosition(index, key) {
@@ -1131,11 +1133,6 @@ function moveDelta(previousIndex, index, item) {
1131
1133
  item: item instanceof LiveRegister ? item.data : item,
1132
1134
  };
1133
1135
  }
1134
- function sortListItem(items) {
1135
- items.sort((itemA, itemB) =>
1136
- comparePosition(itemA._getParentKeyOrThrow(), itemB._getParentKeyOrThrow())
1137
- );
1138
- }
1139
1136
  function addIntentAndDeletedIdToOperation(ops, deletedId) {
1140
1137
  if (0 === ops.length)
1141
1138
  throw new Error(