@liveblocks/client 0.15.0-alpha.2 → 0.15.0
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/lib/esm/index.js +2991 -5
- package/lib/esm/index.mjs +2991 -0
- package/lib/esm/internal.js +149 -0
- package/lib/esm/internal.mjs +149 -0
- package/lib/{esm/types.d.ts → index.d.ts} +255 -50
- package/lib/index.js +4237 -0
- package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
- package/lib/internal.js +193 -0
- package/package.json +36 -9
- package/lib/cjs/AbstractCrdt.d.ts +0 -67
- package/lib/cjs/AbstractCrdt.js +0 -95
- package/lib/cjs/LiveList.d.ts +0 -144
- package/lib/cjs/LiveList.js +0 -527
- package/lib/cjs/LiveMap.d.ts +0 -91
- package/lib/cjs/LiveMap.js +0 -322
- package/lib/cjs/LiveObject.d.ts +0 -80
- package/lib/cjs/LiveObject.js +0 -453
- package/lib/cjs/LiveRegister.d.ts +0 -29
- package/lib/cjs/LiveRegister.js +0 -88
- package/lib/cjs/authentication.d.ts +0 -3
- package/lib/cjs/authentication.js +0 -71
- package/lib/cjs/client.d.ts +0 -27
- package/lib/cjs/client.js +0 -80
- package/lib/cjs/immutable.d.ts +0 -9
- package/lib/cjs/immutable.js +0 -291
- package/lib/cjs/index.d.ts +0 -6
- package/lib/cjs/index.js +0 -18
- package/lib/cjs/live.d.ts +0 -181
- package/lib/cjs/live.js +0 -49
- package/lib/cjs/position.d.ts +0 -6
- package/lib/cjs/position.js +0 -113
- package/lib/cjs/room.d.ts +0 -159
- package/lib/cjs/room.js +0 -1094
- package/lib/cjs/types.d.ts +0 -489
- package/lib/cjs/types.js +0 -2
- package/lib/cjs/utils.d.ts +0 -11
- package/lib/cjs/utils.js +0 -175
- package/lib/esm/AbstractCrdt.d.ts +0 -67
- package/lib/esm/AbstractCrdt.js +0 -91
- package/lib/esm/LiveList.d.ts +0 -144
- package/lib/esm/LiveList.js +0 -523
- package/lib/esm/LiveMap.d.ts +0 -91
- package/lib/esm/LiveMap.js +0 -318
- package/lib/esm/LiveObject.d.ts +0 -80
- package/lib/esm/LiveObject.js +0 -449
- package/lib/esm/LiveRegister.d.ts +0 -29
- package/lib/esm/LiveRegister.js +0 -84
- package/lib/esm/authentication.d.ts +0 -3
- package/lib/esm/authentication.js +0 -66
- package/lib/esm/client.d.ts +0 -27
- package/lib/esm/client.js +0 -76
- package/lib/esm/immutable.d.ts +0 -9
- package/lib/esm/immutable.js +0 -282
- package/lib/esm/index.d.ts +0 -6
- package/lib/esm/live.js +0 -46
- package/lib/esm/position.d.ts +0 -6
- package/lib/esm/position.js +0 -106
- package/lib/esm/room.d.ts +0 -159
- package/lib/esm/room.js +0 -1069
- package/lib/esm/types.js +0 -1
- package/lib/esm/utils.d.ts +0 -11
- package/lib/esm/utils.js +0 -164
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var ServerMessageType = /* @__PURE__ */ ((ServerMessageType2) => {
|
|
2
|
+
ServerMessageType2[ServerMessageType2["UpdatePresence"] = 100] = "UpdatePresence";
|
|
3
|
+
ServerMessageType2[ServerMessageType2["UserJoined"] = 101] = "UserJoined";
|
|
4
|
+
ServerMessageType2[ServerMessageType2["UserLeft"] = 102] = "UserLeft";
|
|
5
|
+
ServerMessageType2[ServerMessageType2["Event"] = 103] = "Event";
|
|
6
|
+
ServerMessageType2[ServerMessageType2["RoomState"] = 104] = "RoomState";
|
|
7
|
+
ServerMessageType2[ServerMessageType2["InitialStorageState"] = 200] = "InitialStorageState";
|
|
8
|
+
ServerMessageType2[ServerMessageType2["UpdateStorage"] = 201] = "UpdateStorage";
|
|
9
|
+
return ServerMessageType2;
|
|
10
|
+
})(ServerMessageType || {});
|
|
11
|
+
var ClientMessageType = /* @__PURE__ */ ((ClientMessageType2) => {
|
|
12
|
+
ClientMessageType2[ClientMessageType2["UpdatePresence"] = 100] = "UpdatePresence";
|
|
13
|
+
ClientMessageType2[ClientMessageType2["ClientEvent"] = 103] = "ClientEvent";
|
|
14
|
+
ClientMessageType2[ClientMessageType2["FetchStorage"] = 200] = "FetchStorage";
|
|
15
|
+
ClientMessageType2[ClientMessageType2["UpdateStorage"] = 201] = "UpdateStorage";
|
|
16
|
+
return ClientMessageType2;
|
|
17
|
+
})(ClientMessageType || {});
|
|
18
|
+
var CrdtType = /* @__PURE__ */ ((CrdtType2) => {
|
|
19
|
+
CrdtType2[CrdtType2["Object"] = 0] = "Object";
|
|
20
|
+
CrdtType2[CrdtType2["List"] = 1] = "List";
|
|
21
|
+
CrdtType2[CrdtType2["Map"] = 2] = "Map";
|
|
22
|
+
CrdtType2[CrdtType2["Register"] = 3] = "Register";
|
|
23
|
+
return CrdtType2;
|
|
24
|
+
})(CrdtType || {});
|
|
25
|
+
var OpType = /* @__PURE__ */ ((OpType2) => {
|
|
26
|
+
OpType2[OpType2["Init"] = 0] = "Init";
|
|
27
|
+
OpType2[OpType2["SetParentKey"] = 1] = "SetParentKey";
|
|
28
|
+
OpType2[OpType2["CreateList"] = 2] = "CreateList";
|
|
29
|
+
OpType2[OpType2["UpdateObject"] = 3] = "UpdateObject";
|
|
30
|
+
OpType2[OpType2["CreateObject"] = 4] = "CreateObject";
|
|
31
|
+
OpType2[OpType2["DeleteCrdt"] = 5] = "DeleteCrdt";
|
|
32
|
+
OpType2[OpType2["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
33
|
+
OpType2[OpType2["CreateMap"] = 7] = "CreateMap";
|
|
34
|
+
OpType2[OpType2["CreateRegister"] = 8] = "CreateRegister";
|
|
35
|
+
return OpType2;
|
|
36
|
+
})(OpType || {});
|
|
37
|
+
var WebsocketCloseCodes = /* @__PURE__ */ ((WebsocketCloseCodes2) => {
|
|
38
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
39
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["INVALID_MESSAGE_FORMAT"] = 4e3] = "INVALID_MESSAGE_FORMAT";
|
|
40
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
|
|
41
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
|
|
42
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
|
|
43
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
44
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
45
|
+
return WebsocketCloseCodes2;
|
|
46
|
+
})(WebsocketCloseCodes || {});
|
|
47
|
+
|
|
48
|
+
const min = 32;
|
|
49
|
+
const max = 126;
|
|
50
|
+
function makePosition(before, after) {
|
|
51
|
+
if (before == null && after == null) {
|
|
52
|
+
return pos([min + 1]);
|
|
53
|
+
}
|
|
54
|
+
if (before != null && after == null) {
|
|
55
|
+
return getNextPosition(before);
|
|
56
|
+
}
|
|
57
|
+
if (before == null && after != null) {
|
|
58
|
+
return getPreviousPosition(after);
|
|
59
|
+
}
|
|
60
|
+
return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
61
|
+
}
|
|
62
|
+
function getPreviousPosition(after) {
|
|
63
|
+
const result = [];
|
|
64
|
+
const afterCodes = posCodes(after);
|
|
65
|
+
for (let i = 0; i < afterCodes.length; i++) {
|
|
66
|
+
const code = afterCodes[i];
|
|
67
|
+
if (code <= min + 1) {
|
|
68
|
+
result.push(min);
|
|
69
|
+
if (afterCodes.length - 1 === i) {
|
|
70
|
+
result.push(max);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
result.push(code - 1);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return pos(result);
|
|
79
|
+
}
|
|
80
|
+
function getNextPosition(before) {
|
|
81
|
+
const result = [];
|
|
82
|
+
const beforeCodes = posCodes(before);
|
|
83
|
+
for (let i = 0; i < beforeCodes.length; i++) {
|
|
84
|
+
const code = beforeCodes[i];
|
|
85
|
+
if (code === max) {
|
|
86
|
+
result.push(code);
|
|
87
|
+
if (beforeCodes.length - 1 === i) {
|
|
88
|
+
result.push(min + 1);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
result.push(code + 1);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return pos(result);
|
|
97
|
+
}
|
|
98
|
+
function makePositionFromCodes(before, after) {
|
|
99
|
+
let index = 0;
|
|
100
|
+
const result = [];
|
|
101
|
+
while (true) {
|
|
102
|
+
const beforeDigit = before[index] || min;
|
|
103
|
+
const afterDigit = after[index] || max;
|
|
104
|
+
if (beforeDigit > afterDigit) {
|
|
105
|
+
throw new Error(`Impossible to generate position between ${before} and ${after}`);
|
|
106
|
+
}
|
|
107
|
+
if (beforeDigit === afterDigit) {
|
|
108
|
+
result.push(beforeDigit);
|
|
109
|
+
index++;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (afterDigit - beforeDigit === 1) {
|
|
113
|
+
result.push(beforeDigit);
|
|
114
|
+
result.push(...makePositionFromCodes(before.slice(index + 1), []));
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
const mid = afterDigit + beforeDigit >> 1;
|
|
118
|
+
result.push(mid);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
function posCodes(str) {
|
|
124
|
+
const codes = [];
|
|
125
|
+
for (let i = 0; i < str.length; i++) {
|
|
126
|
+
codes.push(str.charCodeAt(i));
|
|
127
|
+
}
|
|
128
|
+
return codes;
|
|
129
|
+
}
|
|
130
|
+
function pos(codes) {
|
|
131
|
+
return String.fromCharCode(...codes);
|
|
132
|
+
}
|
|
133
|
+
function compare(posA, posB) {
|
|
134
|
+
const aCodes = posCodes(posA);
|
|
135
|
+
const bCodes = posCodes(posB);
|
|
136
|
+
const maxLength = Math.max(aCodes.length, bCodes.length);
|
|
137
|
+
for (let i = 0; i < maxLength; i++) {
|
|
138
|
+
const a = aCodes[i] == null ? min : aCodes[i];
|
|
139
|
+
const b = bCodes[i] == null ? min : bCodes[i];
|
|
140
|
+
if (a === b) {
|
|
141
|
+
continue;
|
|
142
|
+
} else {
|
|
143
|
+
return a - b;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { ClientMessageType, CrdtType, OpType, ServerMessageType, WebsocketCloseCodes, compare, makePosition, max, min, pos, posCodes };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var ServerMessageType = /* @__PURE__ */ ((ServerMessageType2) => {
|
|
2
|
+
ServerMessageType2[ServerMessageType2["UpdatePresence"] = 100] = "UpdatePresence";
|
|
3
|
+
ServerMessageType2[ServerMessageType2["UserJoined"] = 101] = "UserJoined";
|
|
4
|
+
ServerMessageType2[ServerMessageType2["UserLeft"] = 102] = "UserLeft";
|
|
5
|
+
ServerMessageType2[ServerMessageType2["Event"] = 103] = "Event";
|
|
6
|
+
ServerMessageType2[ServerMessageType2["RoomState"] = 104] = "RoomState";
|
|
7
|
+
ServerMessageType2[ServerMessageType2["InitialStorageState"] = 200] = "InitialStorageState";
|
|
8
|
+
ServerMessageType2[ServerMessageType2["UpdateStorage"] = 201] = "UpdateStorage";
|
|
9
|
+
return ServerMessageType2;
|
|
10
|
+
})(ServerMessageType || {});
|
|
11
|
+
var ClientMessageType = /* @__PURE__ */ ((ClientMessageType2) => {
|
|
12
|
+
ClientMessageType2[ClientMessageType2["UpdatePresence"] = 100] = "UpdatePresence";
|
|
13
|
+
ClientMessageType2[ClientMessageType2["ClientEvent"] = 103] = "ClientEvent";
|
|
14
|
+
ClientMessageType2[ClientMessageType2["FetchStorage"] = 200] = "FetchStorage";
|
|
15
|
+
ClientMessageType2[ClientMessageType2["UpdateStorage"] = 201] = "UpdateStorage";
|
|
16
|
+
return ClientMessageType2;
|
|
17
|
+
})(ClientMessageType || {});
|
|
18
|
+
var CrdtType = /* @__PURE__ */ ((CrdtType2) => {
|
|
19
|
+
CrdtType2[CrdtType2["Object"] = 0] = "Object";
|
|
20
|
+
CrdtType2[CrdtType2["List"] = 1] = "List";
|
|
21
|
+
CrdtType2[CrdtType2["Map"] = 2] = "Map";
|
|
22
|
+
CrdtType2[CrdtType2["Register"] = 3] = "Register";
|
|
23
|
+
return CrdtType2;
|
|
24
|
+
})(CrdtType || {});
|
|
25
|
+
var OpType = /* @__PURE__ */ ((OpType2) => {
|
|
26
|
+
OpType2[OpType2["Init"] = 0] = "Init";
|
|
27
|
+
OpType2[OpType2["SetParentKey"] = 1] = "SetParentKey";
|
|
28
|
+
OpType2[OpType2["CreateList"] = 2] = "CreateList";
|
|
29
|
+
OpType2[OpType2["UpdateObject"] = 3] = "UpdateObject";
|
|
30
|
+
OpType2[OpType2["CreateObject"] = 4] = "CreateObject";
|
|
31
|
+
OpType2[OpType2["DeleteCrdt"] = 5] = "DeleteCrdt";
|
|
32
|
+
OpType2[OpType2["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
33
|
+
OpType2[OpType2["CreateMap"] = 7] = "CreateMap";
|
|
34
|
+
OpType2[OpType2["CreateRegister"] = 8] = "CreateRegister";
|
|
35
|
+
return OpType2;
|
|
36
|
+
})(OpType || {});
|
|
37
|
+
var WebsocketCloseCodes = /* @__PURE__ */ ((WebsocketCloseCodes2) => {
|
|
38
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
39
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["INVALID_MESSAGE_FORMAT"] = 4e3] = "INVALID_MESSAGE_FORMAT";
|
|
40
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
|
|
41
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
|
|
42
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
|
|
43
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
44
|
+
WebsocketCloseCodes2[WebsocketCloseCodes2["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
45
|
+
return WebsocketCloseCodes2;
|
|
46
|
+
})(WebsocketCloseCodes || {});
|
|
47
|
+
|
|
48
|
+
const min = 32;
|
|
49
|
+
const max = 126;
|
|
50
|
+
function makePosition(before, after) {
|
|
51
|
+
if (before == null && after == null) {
|
|
52
|
+
return pos([min + 1]);
|
|
53
|
+
}
|
|
54
|
+
if (before != null && after == null) {
|
|
55
|
+
return getNextPosition(before);
|
|
56
|
+
}
|
|
57
|
+
if (before == null && after != null) {
|
|
58
|
+
return getPreviousPosition(after);
|
|
59
|
+
}
|
|
60
|
+
return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
61
|
+
}
|
|
62
|
+
function getPreviousPosition(after) {
|
|
63
|
+
const result = [];
|
|
64
|
+
const afterCodes = posCodes(after);
|
|
65
|
+
for (let i = 0; i < afterCodes.length; i++) {
|
|
66
|
+
const code = afterCodes[i];
|
|
67
|
+
if (code <= min + 1) {
|
|
68
|
+
result.push(min);
|
|
69
|
+
if (afterCodes.length - 1 === i) {
|
|
70
|
+
result.push(max);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
result.push(code - 1);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return pos(result);
|
|
79
|
+
}
|
|
80
|
+
function getNextPosition(before) {
|
|
81
|
+
const result = [];
|
|
82
|
+
const beforeCodes = posCodes(before);
|
|
83
|
+
for (let i = 0; i < beforeCodes.length; i++) {
|
|
84
|
+
const code = beforeCodes[i];
|
|
85
|
+
if (code === max) {
|
|
86
|
+
result.push(code);
|
|
87
|
+
if (beforeCodes.length - 1 === i) {
|
|
88
|
+
result.push(min + 1);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
result.push(code + 1);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return pos(result);
|
|
97
|
+
}
|
|
98
|
+
function makePositionFromCodes(before, after) {
|
|
99
|
+
let index = 0;
|
|
100
|
+
const result = [];
|
|
101
|
+
while (true) {
|
|
102
|
+
const beforeDigit = before[index] || min;
|
|
103
|
+
const afterDigit = after[index] || max;
|
|
104
|
+
if (beforeDigit > afterDigit) {
|
|
105
|
+
throw new Error(`Impossible to generate position between ${before} and ${after}`);
|
|
106
|
+
}
|
|
107
|
+
if (beforeDigit === afterDigit) {
|
|
108
|
+
result.push(beforeDigit);
|
|
109
|
+
index++;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (afterDigit - beforeDigit === 1) {
|
|
113
|
+
result.push(beforeDigit);
|
|
114
|
+
result.push(...makePositionFromCodes(before.slice(index + 1), []));
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
const mid = afterDigit + beforeDigit >> 1;
|
|
118
|
+
result.push(mid);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
function posCodes(str) {
|
|
124
|
+
const codes = [];
|
|
125
|
+
for (let i = 0; i < str.length; i++) {
|
|
126
|
+
codes.push(str.charCodeAt(i));
|
|
127
|
+
}
|
|
128
|
+
return codes;
|
|
129
|
+
}
|
|
130
|
+
function pos(codes) {
|
|
131
|
+
return String.fromCharCode(...codes);
|
|
132
|
+
}
|
|
133
|
+
function compare(posA, posB) {
|
|
134
|
+
const aCodes = posCodes(posA);
|
|
135
|
+
const bCodes = posCodes(posB);
|
|
136
|
+
const maxLength = Math.max(aCodes.length, bCodes.length);
|
|
137
|
+
for (let i = 0; i < maxLength; i++) {
|
|
138
|
+
const a = aCodes[i] == null ? min : aCodes[i];
|
|
139
|
+
const b = bCodes[i] == null ? min : bCodes[i];
|
|
140
|
+
if (a === b) {
|
|
141
|
+
continue;
|
|
142
|
+
} else {
|
|
143
|
+
return a - b;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { ClientMessageType, CrdtType, OpType, ServerMessageType, WebsocketCloseCodes, compare, makePosition, max, min, pos, posCodes };
|
|
@@ -1,41 +1,189 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The LiveList class represents an ordered collection of items that is synchorinized across clients.
|
|
3
|
+
*/
|
|
4
|
+
declare class LiveList<T> extends AbstractCrdt {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(items?: T[]);
|
|
7
|
+
/**
|
|
8
|
+
* Returns the number of elements.
|
|
9
|
+
*/
|
|
10
|
+
get length(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Adds one element to the end of the LiveList.
|
|
13
|
+
* @param element The element to add to the end of the LiveList.
|
|
14
|
+
*/
|
|
15
|
+
push(element: T): void;
|
|
16
|
+
/**
|
|
17
|
+
* Inserts one element at a specified index.
|
|
18
|
+
* @param element The element to insert.
|
|
19
|
+
* @param index The index at which you want to insert the element.
|
|
20
|
+
*/
|
|
21
|
+
insert(element: T, index: number): void;
|
|
22
|
+
/**
|
|
23
|
+
* Move one element from one index to another.
|
|
24
|
+
* @param index The index of the element to move
|
|
25
|
+
* @param targetIndex The index where the element should be after moving.
|
|
26
|
+
*/
|
|
27
|
+
move(index: number, targetIndex: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* Deletes an element at the specified index
|
|
30
|
+
* @param index The index of the element to delete
|
|
31
|
+
*/
|
|
32
|
+
delete(index: number): void;
|
|
33
|
+
clear(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Returns an Array of all the elements in the LiveList.
|
|
36
|
+
*/
|
|
37
|
+
toArray(): T[];
|
|
38
|
+
/**
|
|
39
|
+
* Tests whether all elements pass the test implemented by the provided function.
|
|
40
|
+
* @param predicate Function to test for each element, taking two arguments (the element and its index).
|
|
41
|
+
* @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
|
|
42
|
+
*/
|
|
43
|
+
every(predicate: (value: T, index: number) => unknown): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an array with all elements that pass the test implemented by the provided function.
|
|
46
|
+
* @param predicate Function to test each element of the LiveList. Return a value that coerces to true to keep the element, or to false otherwise.
|
|
47
|
+
* @returns An array with the elements that pass the test.
|
|
48
|
+
*/
|
|
49
|
+
filter(predicate: (value: T, index: number) => unknown): T[];
|
|
50
|
+
/**
|
|
51
|
+
* Returns the first element that satisfies the provided testing function.
|
|
52
|
+
* @param predicate Function to execute on each value.
|
|
53
|
+
* @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
|
|
54
|
+
*/
|
|
55
|
+
find(predicate: (value: T, index: number) => unknown): T | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the index of the first element in the LiveList that satisfies the provided testing function.
|
|
58
|
+
* @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
|
|
59
|
+
* @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
|
|
60
|
+
*/
|
|
61
|
+
findIndex(predicate: (value: T, index: number) => unknown): number;
|
|
62
|
+
/**
|
|
63
|
+
* Executes a provided function once for each element.
|
|
64
|
+
* @param callbackfn Function to execute on each element.
|
|
65
|
+
*/
|
|
66
|
+
forEach(callbackfn: (value: T, index: number) => void): void;
|
|
67
|
+
/**
|
|
68
|
+
* Get the element at the specified index.
|
|
69
|
+
* @param index The index on the element to get.
|
|
70
|
+
* @returns The element at the specified index or undefined.
|
|
71
|
+
*/
|
|
72
|
+
get(index: number): T | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
|
|
75
|
+
* @param searchElement Element to locate.
|
|
76
|
+
* @param fromIndex The index to start the search at.
|
|
77
|
+
* @returns The first index of the element in the LiveList; -1 if not found.
|
|
78
|
+
*/
|
|
79
|
+
indexOf(searchElement: T, fromIndex?: number): number;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the last index at which a given element can be found in the LiveList, or -1 if it is not present. The LiveLsit is searched backwards, starting at fromIndex.
|
|
82
|
+
* @param searchElement Element to locate.
|
|
83
|
+
* @param fromIndex The index at which to start searching backwards.
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
lastIndexOf(searchElement: T, fromIndex?: number): number;
|
|
87
|
+
/**
|
|
88
|
+
* Creates an array populated with the results of calling a provided function on every element.
|
|
89
|
+
* @param callback Function that is called for every element.
|
|
90
|
+
* @returns An array with each element being the result of the callback function.
|
|
91
|
+
*/
|
|
92
|
+
map<U>(callback: (value: T, index: number) => U): U[];
|
|
93
|
+
/**
|
|
94
|
+
* Tests whether at least one element in the LiveList passes the test implemented by the provided function.
|
|
95
|
+
* @param predicate Function to test for each element.
|
|
96
|
+
* @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
|
|
97
|
+
*/
|
|
98
|
+
some(predicate: (value: T, index: number) => unknown): boolean;
|
|
99
|
+
[Symbol.iterator](): IterableIterator<T>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
|
|
104
|
+
* Keys should be a string, and values should be serializable to JSON.
|
|
105
|
+
* If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
|
|
106
|
+
*/
|
|
107
|
+
declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
|
|
108
|
+
#private;
|
|
109
|
+
constructor(entries?: readonly (readonly [TKey, TValue])[] | null | undefined);
|
|
110
|
+
/**
|
|
111
|
+
* Returns a specified element from the LiveMap.
|
|
112
|
+
* @param key The key of the element to return.
|
|
113
|
+
* @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
|
|
114
|
+
*/
|
|
115
|
+
get(key: TKey): TValue | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Adds or updates an element with a specified key and a value.
|
|
118
|
+
* @param key The key of the element to add. Should be a string.
|
|
119
|
+
* @param value The value of the element to add. Should be serializable to JSON.
|
|
120
|
+
*/
|
|
121
|
+
set(key: TKey, value: TValue): void;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the number of elements in the LiveMap.
|
|
124
|
+
*/
|
|
125
|
+
get size(): number;
|
|
126
|
+
/**
|
|
127
|
+
* Returns a boolean indicating whether an element with the specified key exists or not.
|
|
128
|
+
* @param key The key of the element to test for presence.
|
|
129
|
+
*/
|
|
130
|
+
has(key: TKey): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Removes the specified element by key.
|
|
133
|
+
* @param key The key of the element to remove.
|
|
134
|
+
* @returns true if an element existed and has been removed, or false if the element does not exist.
|
|
135
|
+
*/
|
|
136
|
+
delete(key: TKey): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Returns a new Iterator object that contains the [key, value] pairs for each element.
|
|
139
|
+
*/
|
|
140
|
+
entries(): IterableIterator<[string, TValue]>;
|
|
141
|
+
/**
|
|
142
|
+
* Same function object as the initial value of the entries method.
|
|
143
|
+
*/
|
|
144
|
+
[Symbol.iterator](): IterableIterator<[string, TValue]>;
|
|
145
|
+
/**
|
|
146
|
+
* Returns a new Iterator object that contains the keys for each element.
|
|
147
|
+
*/
|
|
148
|
+
keys(): IterableIterator<TKey>;
|
|
149
|
+
/**
|
|
150
|
+
* Returns a new Iterator object that contains the values for each element.
|
|
151
|
+
*/
|
|
152
|
+
values(): IterableIterator<TValue>;
|
|
153
|
+
/**
|
|
154
|
+
* Executes a provided function once per each key/value pair in the Map object, in insertion order.
|
|
155
|
+
* @param callback Function to execute for each entry in the map.
|
|
156
|
+
*/
|
|
157
|
+
forEach(callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void): void;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
|
|
161
|
+
declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
|
|
162
|
+
declare type EventCallback = ({ connectionId, event, }: {
|
|
8
163
|
connectionId: number;
|
|
9
164
|
event: any;
|
|
10
165
|
}) => void;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"my-presence": MyPresenceCallback;
|
|
15
|
-
others: OthersEventCallback;
|
|
16
|
-
event: EventCallback;
|
|
17
|
-
error: ErrorCallback;
|
|
18
|
-
connection: ConnectionCallback;
|
|
19
|
-
};
|
|
20
|
-
export declare type UpdateDelta = {
|
|
166
|
+
declare type ErrorCallback = (error: Error) => void;
|
|
167
|
+
declare type ConnectionCallback = (state: ConnectionState) => void;
|
|
168
|
+
declare type UpdateDelta = {
|
|
21
169
|
type: "update";
|
|
22
170
|
} | {
|
|
23
171
|
type: "delete";
|
|
24
172
|
};
|
|
25
|
-
|
|
173
|
+
declare type LiveMapUpdates<TKey extends string = string, TValue = any> = {
|
|
26
174
|
type: "LiveMap";
|
|
27
175
|
node: LiveMap<TKey, TValue>;
|
|
28
176
|
updates: Record<TKey, UpdateDelta>;
|
|
29
177
|
};
|
|
30
|
-
|
|
178
|
+
declare type LiveObjectUpdateDelta<T> = Partial<{
|
|
31
179
|
[Property in keyof T]: UpdateDelta;
|
|
32
180
|
}>;
|
|
33
|
-
|
|
181
|
+
declare type LiveObjectUpdates<TData = any> = {
|
|
34
182
|
type: "LiveObject";
|
|
35
183
|
node: LiveObject<TData>;
|
|
36
184
|
updates: LiveObjectUpdateDelta<TData>;
|
|
37
185
|
};
|
|
38
|
-
|
|
186
|
+
declare type LiveListUpdateDelta = {
|
|
39
187
|
index: number;
|
|
40
188
|
item: AbstractCrdt;
|
|
41
189
|
type: "insert";
|
|
@@ -48,12 +196,12 @@ export declare type LiveListUpdateDelta = {
|
|
|
48
196
|
item: AbstractCrdt;
|
|
49
197
|
type: "move";
|
|
50
198
|
};
|
|
51
|
-
|
|
199
|
+
declare type LiveListUpdates<TItem = any> = {
|
|
52
200
|
type: "LiveList";
|
|
53
201
|
node: LiveList<TItem>;
|
|
54
202
|
updates: LiveListUpdateDelta[];
|
|
55
203
|
};
|
|
56
|
-
|
|
204
|
+
declare type BroadcastOptions = {
|
|
57
205
|
/**
|
|
58
206
|
* Whether or not event is queued if the connection is currently closed.
|
|
59
207
|
*
|
|
@@ -61,9 +209,8 @@ export declare type BroadcastOptions = {
|
|
|
61
209
|
*/
|
|
62
210
|
shouldQueueEventIfNotReady: boolean;
|
|
63
211
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
export declare type Client = {
|
|
212
|
+
declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
|
|
213
|
+
declare type Client = {
|
|
67
214
|
/**
|
|
68
215
|
* Gets a room. Returns null if {@link Client.enter} has not been called previously.
|
|
69
216
|
*
|
|
@@ -85,15 +232,10 @@ export declare type Client = {
|
|
|
85
232
|
*/
|
|
86
233
|
leave(roomId: string): void;
|
|
87
234
|
};
|
|
88
|
-
export declare type AuthenticationToken = {
|
|
89
|
-
actor: number;
|
|
90
|
-
id?: string;
|
|
91
|
-
info?: any;
|
|
92
|
-
};
|
|
93
235
|
/**
|
|
94
236
|
* Represents all the other users connected in the room. Treated as immutable.
|
|
95
237
|
*/
|
|
96
|
-
|
|
238
|
+
interface Others<TPresence extends Presence = Presence> {
|
|
97
239
|
/**
|
|
98
240
|
* Number of other users in the room.
|
|
99
241
|
*/
|
|
@@ -114,7 +256,7 @@ export interface Others<TPresence extends Presence = Presence> {
|
|
|
114
256
|
/**
|
|
115
257
|
* Represents a user connected in a room. Treated as immutable.
|
|
116
258
|
*/
|
|
117
|
-
|
|
259
|
+
declare type User<TPresence extends Presence = Presence> = {
|
|
118
260
|
/**
|
|
119
261
|
* The connection id of the user. It is unique and increment at every new connection.
|
|
120
262
|
*/
|
|
@@ -133,19 +275,21 @@ export declare type User<TPresence extends Presence = Presence> = {
|
|
|
133
275
|
*/
|
|
134
276
|
readonly presence?: TPresence;
|
|
135
277
|
};
|
|
136
|
-
|
|
278
|
+
declare type Presence = {
|
|
137
279
|
[key: string]: any;
|
|
138
280
|
};
|
|
139
281
|
declare type AuthEndpointCallback = (room: string) => Promise<{
|
|
140
282
|
token: string;
|
|
141
283
|
}>;
|
|
142
|
-
|
|
284
|
+
declare type AuthEndpoint = string | AuthEndpointCallback;
|
|
143
285
|
/**
|
|
144
286
|
* The authentication endpoint that is called to ensure that the current user has access to a room.
|
|
145
287
|
* Can be an url or a callback if you need to add additional headers.
|
|
146
288
|
*/
|
|
147
|
-
|
|
289
|
+
declare type ClientOptions = {
|
|
148
290
|
throttle?: number;
|
|
291
|
+
fetchPolyfill?: any;
|
|
292
|
+
WebSocketPolyfill?: any;
|
|
149
293
|
} & ({
|
|
150
294
|
publicApiKey: string;
|
|
151
295
|
authEndpoint?: never;
|
|
@@ -153,19 +297,8 @@ export declare type ClientOptions = {
|
|
|
153
297
|
publicApiKey?: never;
|
|
154
298
|
authEndpoint: AuthEndpoint;
|
|
155
299
|
});
|
|
156
|
-
export declare type AuthorizeResponse = {
|
|
157
|
-
token: string;
|
|
158
|
-
};
|
|
159
300
|
declare type ConnectionState = "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
|
|
160
|
-
|
|
161
|
-
state: "closed" | "authenticating" | "unavailable" | "failed";
|
|
162
|
-
} | {
|
|
163
|
-
state: "open" | "connecting";
|
|
164
|
-
id: number;
|
|
165
|
-
userId?: string;
|
|
166
|
-
userInfo?: any;
|
|
167
|
-
};
|
|
168
|
-
export declare type OthersEvent<T extends Presence = Presence> = {
|
|
301
|
+
declare type OthersEvent<T extends Presence = Presence> = {
|
|
169
302
|
type: "leave";
|
|
170
303
|
user: User<T>;
|
|
171
304
|
} | {
|
|
@@ -178,7 +311,11 @@ export declare type OthersEvent<T extends Presence = Presence> = {
|
|
|
178
311
|
} | {
|
|
179
312
|
type: "reset";
|
|
180
313
|
};
|
|
181
|
-
|
|
314
|
+
declare type Room = {
|
|
315
|
+
/**
|
|
316
|
+
* The id of the room.
|
|
317
|
+
*/
|
|
318
|
+
readonly id: string;
|
|
182
319
|
getConnectionState(): ConnectionState;
|
|
183
320
|
subscribe: {
|
|
184
321
|
/**
|
|
@@ -486,4 +623,72 @@ export declare type Room = {
|
|
|
486
623
|
*/
|
|
487
624
|
batch: (fn: () => void) => void;
|
|
488
625
|
};
|
|
489
|
-
|
|
626
|
+
|
|
627
|
+
declare abstract class AbstractCrdt {
|
|
628
|
+
#private;
|
|
629
|
+
get roomId(): string | null;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
|
|
634
|
+
* Keys should be a string, and values should be serializable to JSON.
|
|
635
|
+
* If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
|
|
636
|
+
*/
|
|
637
|
+
declare class LiveObject<T extends Record<string, any> = Record<string, any>> extends AbstractCrdt {
|
|
638
|
+
#private;
|
|
639
|
+
constructor(object?: T);
|
|
640
|
+
/**
|
|
641
|
+
* Transform the LiveObject into a javascript object
|
|
642
|
+
*/
|
|
643
|
+
toObject(): T;
|
|
644
|
+
/**
|
|
645
|
+
* Adds or updates a property with a specified key and a value.
|
|
646
|
+
* @param key The key of the property to add
|
|
647
|
+
* @param value The value of the property to add
|
|
648
|
+
*/
|
|
649
|
+
set<TKey extends keyof T>(key: TKey, value: T[TKey]): void;
|
|
650
|
+
/**
|
|
651
|
+
* Returns a specified property from the LiveObject.
|
|
652
|
+
* @param key The key of the property to get
|
|
653
|
+
*/
|
|
654
|
+
get<TKey extends keyof T>(key: TKey): T[TKey];
|
|
655
|
+
/**
|
|
656
|
+
* Deletes a key from the LiveObject
|
|
657
|
+
* @param key The key of the property to delete
|
|
658
|
+
*/
|
|
659
|
+
delete(key: keyof T): void;
|
|
660
|
+
/**
|
|
661
|
+
* Adds or updates multiple properties at once with an object.
|
|
662
|
+
* @param overrides The object used to overrides properties
|
|
663
|
+
*/
|
|
664
|
+
update(overrides: Partial<T>): void;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Create a client that will be responsible to communicate with liveblocks servers.
|
|
669
|
+
*
|
|
670
|
+
* @example
|
|
671
|
+
* const client = createClient({
|
|
672
|
+
* authEndpoint: "/api/auth"
|
|
673
|
+
* });
|
|
674
|
+
*
|
|
675
|
+
* // It's also possible to use a function to call your authentication endpoint.
|
|
676
|
+
* // Useful to add additional headers or use an API wrapper (like Firebase functions)
|
|
677
|
+
* const client = createClient({
|
|
678
|
+
* authEndpoint: async (room) => {
|
|
679
|
+
* const response = await fetch("/api/auth", {
|
|
680
|
+
* method: "POST",
|
|
681
|
+
* headers: {
|
|
682
|
+
* Authentication: "token",
|
|
683
|
+
* "Content-Type": "application/json"
|
|
684
|
+
* },
|
|
685
|
+
* body: JSON.stringify({ room })
|
|
686
|
+
* });
|
|
687
|
+
*
|
|
688
|
+
* return await response.json();
|
|
689
|
+
* }
|
|
690
|
+
* });
|
|
691
|
+
*/
|
|
692
|
+
declare function createClient(options: ClientOptions): Client;
|
|
693
|
+
|
|
694
|
+
export { BroadcastOptions, Client, LiveList, LiveMap, LiveObject, Others, Presence, Room, StorageUpdate, User, createClient };
|