Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@instantdb/core

Package Overview
Dependencies
Maintainers
0
Versions
205
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@instantdb/core - npm Package Compare versions

Comparing version 0.12.32 to 0.12.33

13

dist/module/Reactor.d.ts

@@ -34,3 +34,11 @@ /**

_broadcastChannel: BroadcastChannel | undefined;
/** @type {Record<string, {isConnected: boolean; error: any}>} */
_rooms: Record<string, {
isConnected: boolean;
error: any;
}>;
/** @type {Record<string, boolean>} */
_roomsPendingLeave: Record<string, boolean>;
_presence: {};
_broadcastQueue: any[];
_broadcastSubs: {};

@@ -73,2 +81,3 @@ _currentUserCached: {

_cleanPendingMutations(txId: any): void;
_flushEnqueuedRoomData(roomId: any): void;
_handleReceive(msg: any): void;

@@ -244,2 +253,5 @@ _sessionId: any;

publishPresence<RoomType extends keyof RoomSchema>(roomType: RoomType, roomId: string | number, partialData: Partial<RoomSchema[RoomType]["presence"]>): void;
_trySetPresence(roomId: any, data: any): void;
_tryJoinRoom(roomId: any): void;
_tryLeaveRoom(roomId: any): void;
/**

@@ -264,2 +276,3 @@ * @template {keyof RoomSchema} RoomType

}): void;
_tryBroadcast(roomId: any, roomType: any, topic: any, data: any): void;
subscribeTopic(roomId: any, topic: any, cb: any): () => void;

@@ -266,0 +279,0 @@ _notifyBroadcastSubs(room: any, topic: any, msg: any): void;

123

dist/module/Reactor.js

@@ -71,3 +71,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

this._linkIndex = null;
/** @type {Record<string, {isConnected: boolean; error: any}>} */
this._rooms = {};
/** @type {Record<string, boolean>} */
this._roomsPendingLeave = {};
this._presence = {};
this._broadcastQueue = [];
this._broadcastSubs = {};

@@ -223,2 +228,5 @@ this._currentUserCached = { isLoading: true, error: undefined, user: undefined };

this._setStatus(STATUS.CLOSED);
for (const room of Object.values(this._rooms)) {
room.isConnected = false;
}
if (this._isShutdown) {

@@ -343,2 +351,17 @@ log.info("[socket-close] socket has been shut down and will not reconnect");

}
_flushEnqueuedRoomData(roomId) {
var _a, _b;
const enqueuedUserPresence = (_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.user;
const enqueuedBroadcasts = this._broadcastQueue[roomId];
this._broadcastQueue[roomId] = [];
if (enqueuedUserPresence) {
this._trySetPresence(roomId, enqueuedUserPresence);
}
if (enqueuedBroadcasts) {
for (const item of enqueuedBroadcasts) {
const { topic, roomType, data } = item;
this._tryBroadcast(roomId, roomType, topic, data);
}
}
}
_handleReceive(msg) {

@@ -360,2 +383,5 @@ var _a, _b, _c, _d;

this._sessionId = msg["session-id"];
for (const roomId of Object.keys(this._rooms)) {
this._tryJoinRoom(roomId);
}
break;

@@ -432,11 +458,17 @@ case "add-query-ok":

const loadingRoomId = msg["room-id"];
const loadingRoom = this._presence[loadingRoomId];
if (loadingRoom) {
loadingRoom.isLoading = false;
const joinedRoom = this._rooms[loadingRoomId];
if (!joinedRoom) {
if (this._roomsPendingLeave[roomId]) {
this._tryLeaveRoom(loadingRoomId);
delete this._roomsPendingLeave[roomId];
}
break;
}
joinedRoom.isConnected = true;
this._notifyPresenceSubs(loadingRoomId);
this._flushEnqueuedRoomData(loadingRoomId);
break;
case "join-room-error":
const errorRoomId = msg["room-id"];
const errorRoom = this._presence[errorRoomId];
const errorRoom = this._rooms[errorRoomId];
if (errorRoom) {

@@ -731,17 +763,2 @@ errorRoom.error = msg["error"];

});
const roomIds = Object.keys(this._presence);
roomIds.forEach((roomId) => {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
});
const presence = Object.entries(this._presence);
presence.forEach(([roomId, { result }]) => {
const user = result === null || result === void 0 ? void 0 : result.user;
if (!user)
return;
this._trySendAuthed(uuid(), {
op: "set-presence",
"room-id": roomId,
data: user,
});
});
}

@@ -1104,3 +1121,9 @@ _trySendAuthed(eventId, msg) {

joinRoom(roomId) {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
if (!this._rooms[roomId]) {
this._rooms[roomId] = {
isConnected: false,
error: undefined,
};
}
this._tryJoinRoom(roomId);
return () => {

@@ -1111,8 +1134,15 @@ this._cleanupRoom(roomId);

_cleanupRoom(roomId) {
var _a, _b, _c;
var _a, _b, _c, _d;
if (!((_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.handlers) === null || _b === void 0 ? void 0 : _b.length) &&
!Object.keys((_c = this._broadcastSubs[roomId]) !== null && _c !== void 0 ? _c : {}).length) {
const isConnected = (_d = this._rooms[roomId]) === null || _d === void 0 ? void 0 : _d.isConnected;
delete this._rooms[roomId];
delete this._presence[roomId];
delete this._broadcastSubs[roomId];
this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId });
if (isConnected) {
this._tryLeaveRoom(roomId);
}
else {
this._roomsPendingLeave[roomId] = true;
}
}

@@ -1131,6 +1161,7 @@ }

getPresence(roomType, roomId, opts = {}) {
const room = this._presence[roomId];
if (!room || !room.result)
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence || !presence.result)
return null;
return Object.assign(Object.assign({}, buildPresenceSlice(room.result, opts)), { isLoading: room.isLoading, error: room.error });
return Object.assign(Object.assign({}, buildPresenceSlice(presence.result, opts)), { isLoading: !room.isConnected, error: room.error });
}

@@ -1144,4 +1175,17 @@ /**

publishPresence(roomType, roomId, partialData) {
var _a, _b;
const data = Object.assign(Object.assign({}, (_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.user), partialData);
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence) {
return;
}
presence.result = presence.result || {};
const data = Object.assign(Object.assign({}, presence.result.user), partialData);
presence.result.user = data;
if (!room.isConnected) {
return;
}
this._trySetPresence(roomId, data);
this._notifyPresenceSubs(roomId);
}
_trySetPresence(roomId, data) {
this._trySendAuthed(uuid(), {

@@ -1152,7 +1196,10 @@ op: "set-presence",

});
this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].result = this._presence[roomId].result || {};
this._presence[roomId].result.user = data;
this._notifyPresenceSubs(roomId);
}
_tryJoinRoom(roomId) {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
delete this._roomsPendingLeave[roomId];
}
_tryLeaveRoom(roomId) {
this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId });
}
/**

@@ -1171,3 +1218,2 @@ * @template {keyof RoomSchema} RoomType

this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].isLoading = true;
this._presence[roomId].handlers = this._presence[roomId].handlers || [];

@@ -1212,2 +1258,15 @@ this._presence[roomId].handlers.push(handler);

publishTopic({ roomType, roomId, topic, data }) {
var _a;
const room = this._rooms[roomId];
if (!room) {
return;
}
if (!room.isConnected) {
this._broadcastQueue[roomId] = (_a = this._broadcastQueue[roomId]) !== null && _a !== void 0 ? _a : [];
this._broadcastQueue[roomId].push({ topic, roomType, data });
return;
}
this._tryBroadcast(roomId, roomType, topic, data);
}
_tryBroadcast(roomId, roomType, topic, data) {
this._trySendAuthed(uuid(), {

@@ -1214,0 +1273,0 @@ op: "client-broadcast",

@@ -34,3 +34,11 @@ /**

_broadcastChannel: BroadcastChannel | undefined;
/** @type {Record<string, {isConnected: boolean; error: any}>} */
_rooms: Record<string, {
isConnected: boolean;
error: any;
}>;
/** @type {Record<string, boolean>} */
_roomsPendingLeave: Record<string, boolean>;
_presence: {};
_broadcastQueue: any[];
_broadcastSubs: {};

@@ -73,2 +81,3 @@ _currentUserCached: {

_cleanPendingMutations(txId: any): void;
_flushEnqueuedRoomData(roomId: any): void;
_handleReceive(msg: any): void;

@@ -244,2 +253,5 @@ _sessionId: any;

publishPresence<RoomType extends keyof RoomSchema>(roomType: RoomType, roomId: string | number, partialData: Partial<RoomSchema[RoomType]["presence"]>): void;
_trySetPresence(roomId: any, data: any): void;
_tryJoinRoom(roomId: any): void;
_tryLeaveRoom(roomId: any): void;
/**

@@ -264,2 +276,3 @@ * @template {keyof RoomSchema} RoomType

}): void;
_tryBroadcast(roomId: any, roomType: any, topic: any, data: any): void;
subscribeTopic(roomId: any, topic: any, cb: any): () => void;

@@ -266,0 +279,0 @@ _notifyBroadcastSubs(room: any, topic: any, msg: any): void;

@@ -99,3 +99,8 @@ "use strict";

this._linkIndex = null;
/** @type {Record<string, {isConnected: boolean; error: any}>} */
this._rooms = {};
/** @type {Record<string, boolean>} */
this._roomsPendingLeave = {};
this._presence = {};
this._broadcastQueue = [];
this._broadcastSubs = {};

@@ -251,2 +256,5 @@ this._currentUserCached = { isLoading: true, error: undefined, user: undefined };

this._setStatus(STATUS.CLOSED);
for (const room of Object.values(this._rooms)) {
room.isConnected = false;
}
if (this._isShutdown) {

@@ -371,2 +379,17 @@ log_1.default.info("[socket-close] socket has been shut down and will not reconnect");

}
_flushEnqueuedRoomData(roomId) {
var _a, _b;
const enqueuedUserPresence = (_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.user;
const enqueuedBroadcasts = this._broadcastQueue[roomId];
this._broadcastQueue[roomId] = [];
if (enqueuedUserPresence) {
this._trySetPresence(roomId, enqueuedUserPresence);
}
if (enqueuedBroadcasts) {
for (const item of enqueuedBroadcasts) {
const { topic, roomType, data } = item;
this._tryBroadcast(roomId, roomType, topic, data);
}
}
}
_handleReceive(msg) {

@@ -388,2 +411,5 @@ var _a, _b, _c, _d;

this._sessionId = msg["session-id"];
for (const roomId of Object.keys(this._rooms)) {
this._tryJoinRoom(roomId);
}
break;

@@ -460,11 +486,17 @@ case "add-query-ok":

const loadingRoomId = msg["room-id"];
const loadingRoom = this._presence[loadingRoomId];
if (loadingRoom) {
loadingRoom.isLoading = false;
const joinedRoom = this._rooms[loadingRoomId];
if (!joinedRoom) {
if (this._roomsPendingLeave[roomId]) {
this._tryLeaveRoom(loadingRoomId);
delete this._roomsPendingLeave[roomId];
}
break;
}
joinedRoom.isConnected = true;
this._notifyPresenceSubs(loadingRoomId);
this._flushEnqueuedRoomData(loadingRoomId);
break;
case "join-room-error":
const errorRoomId = msg["room-id"];
const errorRoom = this._presence[errorRoomId];
const errorRoom = this._rooms[errorRoomId];
if (errorRoom) {

@@ -759,17 +791,2 @@ errorRoom.error = msg["error"];

});
const roomIds = Object.keys(this._presence);
roomIds.forEach((roomId) => {
this._trySendAuthed((0, uuid_1.default)(), { op: "join-room", "room-id": roomId });
});
const presence = Object.entries(this._presence);
presence.forEach(([roomId, { result }]) => {
const user = result === null || result === void 0 ? void 0 : result.user;
if (!user)
return;
this._trySendAuthed((0, uuid_1.default)(), {
op: "set-presence",
"room-id": roomId,
data: user,
});
});
}

@@ -1132,3 +1149,9 @@ _trySendAuthed(eventId, msg) {

joinRoom(roomId) {
this._trySendAuthed((0, uuid_1.default)(), { op: "join-room", "room-id": roomId });
if (!this._rooms[roomId]) {
this._rooms[roomId] = {
isConnected: false,
error: undefined,
};
}
this._tryJoinRoom(roomId);
return () => {

@@ -1139,8 +1162,15 @@ this._cleanupRoom(roomId);

_cleanupRoom(roomId) {
var _a, _b, _c;
var _a, _b, _c, _d;
if (!((_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.handlers) === null || _b === void 0 ? void 0 : _b.length) &&
!Object.keys((_c = this._broadcastSubs[roomId]) !== null && _c !== void 0 ? _c : {}).length) {
const isConnected = (_d = this._rooms[roomId]) === null || _d === void 0 ? void 0 : _d.isConnected;
delete this._rooms[roomId];
delete this._presence[roomId];
delete this._broadcastSubs[roomId];
this._trySendAuthed((0, uuid_1.default)(), { op: "leave-room", "room-id": roomId });
if (isConnected) {
this._tryLeaveRoom(roomId);
}
else {
this._roomsPendingLeave[roomId] = true;
}
}

@@ -1159,6 +1189,7 @@ }

getPresence(roomType, roomId, opts = {}) {
const room = this._presence[roomId];
if (!room || !room.result)
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence || !presence.result)
return null;
return Object.assign(Object.assign({}, (0, presence_1.buildPresenceSlice)(room.result, opts)), { isLoading: room.isLoading, error: room.error });
return Object.assign(Object.assign({}, (0, presence_1.buildPresenceSlice)(presence.result, opts)), { isLoading: !room.isConnected, error: room.error });
}

@@ -1172,4 +1203,17 @@ /**

publishPresence(roomType, roomId, partialData) {
var _a, _b;
const data = Object.assign(Object.assign({}, (_b = (_a = this._presence[roomId]) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.user), partialData);
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence) {
return;
}
presence.result = presence.result || {};
const data = Object.assign(Object.assign({}, presence.result.user), partialData);
presence.result.user = data;
if (!room.isConnected) {
return;
}
this._trySetPresence(roomId, data);
this._notifyPresenceSubs(roomId);
}
_trySetPresence(roomId, data) {
this._trySendAuthed((0, uuid_1.default)(), {

@@ -1180,7 +1224,10 @@ op: "set-presence",

});
this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].result = this._presence[roomId].result || {};
this._presence[roomId].result.user = data;
this._notifyPresenceSubs(roomId);
}
_tryJoinRoom(roomId) {
this._trySendAuthed((0, uuid_1.default)(), { op: "join-room", "room-id": roomId });
delete this._roomsPendingLeave[roomId];
}
_tryLeaveRoom(roomId) {
this._trySendAuthed((0, uuid_1.default)(), { op: "leave-room", "room-id": roomId });
}
/**

@@ -1199,3 +1246,2 @@ * @template {keyof RoomSchema} RoomType

this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].isLoading = true;
this._presence[roomId].handlers = this._presence[roomId].handlers || [];

@@ -1240,2 +1286,15 @@ this._presence[roomId].handlers.push(handler);

publishTopic({ roomType, roomId, topic, data }) {
var _a;
const room = this._rooms[roomId];
if (!room) {
return;
}
if (!room.isConnected) {
this._broadcastQueue[roomId] = (_a = this._broadcastQueue[roomId]) !== null && _a !== void 0 ? _a : [];
this._broadcastQueue[roomId].push({ topic, roomType, data });
return;
}
this._tryBroadcast(roomId, roomType, topic, data);
}
_tryBroadcast(roomId, roomType, topic, data) {
this._trySendAuthed((0, uuid_1.default)(), {

@@ -1242,0 +1301,0 @@ op: "client-broadcast",

{
"name": "@instantdb/core",
"version": "v0.12.32",
"version": "v0.12.33",
"description": "Instant's core local abstraction",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -80,3 +80,8 @@ // @ts-check

/** @type {Record<string, {isConnected: boolean; error: any}>} */
_rooms = {};
/** @type {Record<string, boolean>} */
_roomsPendingLeave = {};
_presence = {};
_broadcastQueue = [];
_broadcastSubs = {};

@@ -283,2 +288,20 @@ _currentUserCached = { isLoading: true, error: undefined, user: undefined };

_flushEnqueuedRoomData(roomId) {
const enqueuedUserPresence = this._presence[roomId]?.result?.user;
const enqueuedBroadcasts = this._broadcastQueue[roomId];
this._broadcastQueue[roomId] = [];
if (enqueuedUserPresence) {
this._trySetPresence(roomId, enqueuedUserPresence);
}
if (enqueuedBroadcasts) {
for (const item of enqueuedBroadcasts) {
const { topic, roomType, data } = item;
this._tryBroadcast(roomId, roomType, topic, data);
}
}
}
_handleReceive(msg) {

@@ -301,2 +324,6 @@ // opt-out, enabled by default if schema

this._sessionId = msg["session-id"];
for (const roomId of Object.keys(this._rooms)) {
this._tryJoinRoom(roomId);
}
break;

@@ -389,11 +416,20 @@ case "add-query-ok":

const loadingRoomId = msg["room-id"];
const loadingRoom = this._presence[loadingRoomId];
if (loadingRoom) {
loadingRoom.isLoading = false;
const joinedRoom = this._rooms[loadingRoomId];
if (!joinedRoom) {
if (this._roomsPendingLeave[roomId]) {
this._tryLeaveRoom(loadingRoomId);
delete this._roomsPendingLeave[roomId];
}
break;
}
joinedRoom.isConnected = true;
this._notifyPresenceSubs(loadingRoomId);
this._flushEnqueuedRoomData(loadingRoomId);
break;
case "join-room-error":
const errorRoomId = msg["room-id"];
const errorRoom = this._presence[errorRoomId];
const errorRoom = this._rooms[errorRoomId];
if (errorRoom) {

@@ -811,16 +847,2 @@ errorRoom.error = msg["error"];

});
const roomIds = Object.keys(this._presence);
roomIds.forEach((roomId) => {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
});
const presence = Object.entries(this._presence);
presence.forEach(([roomId, { result }]) => {
const user = result?.user;
if (!user) return;
this._trySendAuthed(uuid(), {
op: "set-presence",
"room-id": roomId,
data: user,
});
});
}

@@ -871,2 +893,6 @@

for (const room of Object.values(this._rooms)) {
room.isConnected = false;
}
if (this._isShutdown) {

@@ -1249,4 +1275,11 @@ log.info(

joinRoom(roomId) {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
if (!this._rooms[roomId]) {
this._rooms[roomId] = {
isConnected: false,
error: undefined,
};
}
this._tryJoinRoom(roomId);
return () => {

@@ -1262,5 +1295,13 @@ this._cleanupRoom(roomId);

) {
const isConnected = this._rooms[roomId]?.isConnected;
delete this._rooms[roomId];
delete this._presence[roomId];
delete this._broadcastSubs[roomId];
this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId });
if (isConnected) {
this._tryLeaveRoom(roomId);
} else {
this._roomsPendingLeave[roomId] = true;
}
}

@@ -1281,8 +1322,9 @@ }

getPresence(roomType, roomId, opts = {}) {
const room = this._presence[roomId];
if (!room || !room.result) return null;
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence || !presence.result) return null;
return {
...buildPresenceSlice(room.result, opts),
isLoading: room.isLoading,
...buildPresenceSlice(presence.result, opts),
isLoading: !room.isConnected,
error: room.error,

@@ -1299,7 +1341,26 @@ };

publishPresence(roomType, roomId, partialData) {
const room = this._rooms[roomId];
const presence = this._presence[roomId];
if (!room || !presence) {
return;
}
presence.result = presence.result || {};
const data = {
...this._presence[roomId]?.result?.user,
...presence.result.user,
...partialData,
};
presence.result.user = data;
if (!room.isConnected) {
return;
}
this._trySetPresence(roomId, data);
this._notifyPresenceSubs(roomId);
}
_trySetPresence(roomId, data) {
this._trySendAuthed(uuid(), {

@@ -1310,9 +1371,13 @@ op: "set-presence",

});
}
this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].result = this._presence[roomId].result || {};
this._presence[roomId].result.user = data;
this._notifyPresenceSubs(roomId);
_tryJoinRoom(roomId) {
this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId });
delete this._roomsPendingLeave[roomId];
}
_tryLeaveRoom(roomId) {
this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId });
}
/**

@@ -1333,3 +1398,2 @@ * @template {keyof RoomSchema} RoomType

this._presence[roomId] = this._presence[roomId] || {};
this._presence[roomId].isLoading = true;
this._presence[roomId].handlers = this._presence[roomId].handlers || [];

@@ -1386,2 +1450,19 @@ this._presence[roomId].handlers.push(handler);

publishTopic({ roomType, roomId, topic, data }) {
const room = this._rooms[roomId];
if (!room) {
return;
}
if (!room.isConnected) {
this._broadcastQueue[roomId] = this._broadcastQueue[roomId] ?? [];
this._broadcastQueue[roomId].push({ topic, roomType, data });
return;
}
this._tryBroadcast(roomId, roomType, topic, data);
}
_tryBroadcast(roomId, roomType, topic, data) {
this._trySendAuthed(uuid(), {

@@ -1388,0 +1469,0 @@ op: "client-broadcast",

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

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 too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc