@liveblocks/yjs
Advanced tools
Comparing version 1.10.0 to 1.10.1
@@ -1,2 +0,2 @@ | ||
import { Room, JsonObject, LsonObject, BaseUserMeta, Json } from '@liveblocks/client'; | ||
import { Room, JsonObject, LsonObject, BaseUserMeta, Json, User } from '@liveblocks/client'; | ||
import { Observable } from 'lib0/observable'; | ||
@@ -18,4 +18,4 @@ import * as Y from 'yjs'; | ||
doc: Y.Doc; | ||
clientID: number; | ||
states: Map<number, unknown>; | ||
actorToClientMap: Map<number, number>; | ||
meta: Map<number, MetaClientState>; | ||
@@ -25,2 +25,3 @@ _checkInterval: number; | ||
constructor(doc: Y.Doc, room: Room<JsonObject, LsonObject, BaseUserMeta, Json>); | ||
rebuildActorToClientMap(others: readonly User<JsonObject, BaseUserMeta>[]): void; | ||
destroy(): void; | ||
@@ -27,0 +28,0 @@ getLocalState(): JsonObject | null; |
@@ -76,2 +76,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts | ||
var Y_PRESENCE_KEY = "__yjs"; | ||
var Y_PRESENCE_ID_KEY = "__yjs_clientid"; | ||
var Awareness = class extends Observable { | ||
@@ -81,2 +82,4 @@ constructor(doc, room) { | ||
this.states = /* @__PURE__ */ new Map(); | ||
// used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client | ||
this.actorToClientMap = /* @__PURE__ */ new Map(); | ||
// Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to | ||
@@ -90,36 +93,47 @@ // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map. | ||
this.room = room; | ||
this.clientID = doc.clientID; | ||
this.room.updatePresence({ | ||
[Y_PRESENCE_ID_KEY]: this.doc.clientID | ||
}); | ||
this.othersUnsub = this.room.events.others.subscribe((event) => { | ||
let updates; | ||
this.rebuildActorToClientMap(event.others); | ||
if (event.type === "leave") { | ||
this.emit("change", [ | ||
{ added: [], updated: [], removed: [event.user.connectionId] }, | ||
"presence" | ||
]); | ||
this.emit("update", [ | ||
{ added: [], updated: [], removed: [event.user.connectionId] }, | ||
"presence" | ||
]); | ||
const targetClientId = this.actorToClientMap.get( | ||
event.user.connectionId | ||
); | ||
if (targetClientId !== void 0) { | ||
updates = { added: [], updated: [], removed: [targetClientId] }; | ||
} | ||
this.rebuildActorToClientMap(event.others); | ||
} | ||
if (event.type === "enter") { | ||
this.emit("change", [ | ||
{ added: [event.user.connectionId], updated: [], removed: [] }, | ||
"presence" | ||
]); | ||
this.emit("update", [ | ||
{ added: [event.user.connectionId], updated: [], removed: [] }, | ||
"presence" | ||
]); | ||
if (event.type === "enter" || event.type === "update") { | ||
this.rebuildActorToClientMap(event.others); | ||
const targetClientId = this.actorToClientMap.get( | ||
event.user.connectionId | ||
); | ||
if (targetClientId !== void 0) { | ||
updates = { | ||
added: event.type === "enter" ? [targetClientId] : [], | ||
updated: event.type === "update" ? [targetClientId] : [], | ||
removed: [] | ||
}; | ||
} | ||
} | ||
if (event.type === "update") { | ||
this.emit("change", [ | ||
{ added: [], updated: [event.user.connectionId], removed: [] }, | ||
"presence" | ||
]); | ||
this.emit("update", [ | ||
{ added: [], updated: [event.user.connectionId], removed: [] }, | ||
"presence" | ||
]); | ||
if (updates !== void 0) { | ||
this.emit("change", [updates, "presence"]); | ||
this.emit("update", [updates, "presence"]); | ||
} | ||
}); | ||
} | ||
rebuildActorToClientMap(others) { | ||
this.actorToClientMap.clear(); | ||
others.forEach((user) => { | ||
if (user.presence[Y_PRESENCE_ID_KEY] !== void 0) { | ||
this.actorToClientMap.set( | ||
user.connectionId, | ||
user.presence[Y_PRESENCE_ID_KEY] | ||
); | ||
} | ||
}); | ||
} | ||
destroy() { | ||
@@ -146,3 +160,3 @@ this.emit("destroy", [this]); | ||
this.emit("update", [ | ||
{ added: [], updated: [], removed: [this.clientID] }, | ||
{ added: [], updated: [], removed: [this.doc.clientID] }, | ||
"local" | ||
@@ -153,4 +167,4 @@ ]); | ||
const yPresence = _optionalChain([presence, 'optionalAccess', _6 => _6[Y_PRESENCE_KEY]]); | ||
const added = yPresence === void 0 ? [this.clientID] : []; | ||
const updated = yPresence === void 0 ? [] : [this.clientID]; | ||
const added = yPresence === void 0 ? [this.doc.clientID] : []; | ||
const updated = yPresence === void 0 ? [] : [this.doc.clientID]; | ||
this.room.updatePresence({ | ||
@@ -174,14 +188,13 @@ [Y_PRESENCE_KEY]: { | ||
const others = this.room.getOthers(); | ||
const presence = _optionalChain([this, 'access', _12 => _12.room, 'access', _13 => _13.getSelf, 'call', _14 => _14(), 'optionalAccess', _15 => _15.presence, 'access', _16 => _16[Y_PRESENCE_KEY]]); | ||
const states = others.reduce((acc, currentValue) => { | ||
if (currentValue.connectionId && currentValue.presence[Y_PRESENCE_KEY] !== void 0) { | ||
acc.set( | ||
currentValue.connectionId, | ||
currentValue.presence[Y_PRESENCE_KEY] || {} | ||
); | ||
const states = others.reduce((acc, otherUser) => { | ||
const otherPresence = otherUser.presence[Y_PRESENCE_KEY]; | ||
const otherClientId = otherUser.presence[Y_PRESENCE_ID_KEY]; | ||
if (otherPresence !== void 0 && otherClientId !== void 0) { | ||
acc.set(otherClientId, otherPresence || {}); | ||
} | ||
return acc; | ||
}, /* @__PURE__ */ new Map()); | ||
if (presence !== void 0) { | ||
states.set(this.clientID, presence); | ||
const localPresence = _optionalChain([this, 'access', _12 => _12.room, 'access', _13 => _13.getSelf, 'call', _14 => _14(), 'optionalAccess', _15 => _15.presence, 'access', _16 => _16[Y_PRESENCE_KEY]]); | ||
if (localPresence !== void 0) { | ||
states.set(this.doc.clientID, localPresence); | ||
} | ||
@@ -265,3 +278,3 @@ return states; | ||
var PKG_NAME = "@liveblocks/yjs"; | ||
var PKG_VERSION = "1.10.0"; | ||
var PKG_VERSION = "1.10.1"; | ||
var PKG_FORMAT = "cjs"; | ||
@@ -329,4 +342,2 @@ | ||
this.syncDoc = () => { | ||
this.rootDoc.clientID = _optionalChain([this, 'access', _27 => _27.room, 'access', _28 => _28.getSelf, 'call', _29 => _29(), 'optionalAccess', _30 => _30.connectionId]) || this.rootDoc.clientID; | ||
this.awareness.clientID = this.rootDoc.clientID; | ||
this.rootDocHandler.syncDoc(); | ||
@@ -346,6 +357,2 @@ for (const [_, handler] of this.subdocHandlers) { | ||
}); | ||
const connectionId = _optionalChain([this, 'access', _31 => _31.room, 'access', _32 => _32.getSelf, 'call', _33 => _33(), 'optionalAccess', _34 => _34.connectionId]); | ||
if (connectionId) { | ||
this.rootDoc.clientID = connectionId; | ||
} | ||
this.awareness = new Awareness(this.rootDoc, this.room); | ||
@@ -369,3 +376,3 @@ this.unsubscribers.push( | ||
if (guid !== void 0) { | ||
_optionalChain([this, 'access', _35 => _35.subdocHandlers, 'access', _36 => _36.get, 'call', _37 => _37(guid), 'optionalAccess', _38 => _38.handleServerUpdate, 'call', _39 => _39({ update, stateVector })]); | ||
_optionalChain([this, 'access', _27 => _27.subdocHandlers, 'access', _28 => _28.get, 'call', _29 => _29(guid), 'optionalAccess', _30 => _30.handleServerUpdate, 'call', _31 => _31({ update, stateVector })]); | ||
} else { | ||
@@ -372,0 +379,0 @@ this.rootDocHandler.handleServerUpdate({ update, stateVector }); |
{ | ||
"name": "@liveblocks/yjs", | ||
"version": "1.10.0", | ||
"version": "1.10.1", | ||
"description": "An integration with . Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.", | ||
@@ -35,4 +35,4 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@liveblocks/client": "1.10.0", | ||
"@liveblocks/core": "1.10.0", | ||
"@liveblocks/client": "1.10.1", | ||
"@liveblocks/core": "1.10.1", | ||
"js-base64": "^3.7.5" | ||
@@ -39,0 +39,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
97873
873
+ Added@liveblocks/client@1.10.1(transitive)
+ Added@liveblocks/core@1.10.1(transitive)
- Removed@liveblocks/client@1.10.0(transitive)
- Removed@liveblocks/core@1.10.0(transitive)
Updated@liveblocks/client@1.10.1
Updated@liveblocks/core@1.10.1