@instantdb/core
Advanced tools
Comparing version 0.5.8 to 0.5.9
@@ -16,2 +16,4 @@ export default class ReactiveDB { | ||
_localIdPromises: {}; | ||
_errorMessage: any; | ||
_setStatus(status: any, err: any): void; | ||
/** | ||
@@ -18,0 +20,0 @@ * merge querySubs from storage and in memory. Has the following side |
@@ -24,2 +24,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
CLOSED: "closed", | ||
ERRORED: "errored", | ||
}; | ||
@@ -112,2 +113,3 @@ function _extractTriplesHelper(idNodes, acc = []) { | ||
this._localIdPromises = {}; | ||
this._errorMessage = null; | ||
/** | ||
@@ -168,7 +170,14 @@ * merge querySubs from storage and in memory. Has the following side | ||
const cbs = this.queryCbs[hash] || []; | ||
if (!cbs) | ||
return; | ||
const errorMessage = this._errorMessage; | ||
if (errorMessage) { | ||
cbs.forEach((cb) => cb({ error: errorMessage })); | ||
return; | ||
} | ||
const { q, result } = this.querySubs.currentValue[hash] || {}; | ||
if (!result || !cbs) | ||
return; | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
if (result) { | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
} | ||
}; | ||
@@ -181,3 +190,3 @@ this.notifyQueryError = (hash, msg) => { | ||
log.info("[socket] connected"); | ||
this.status = STATUS.OPENED; | ||
this._setStatus(STATUS.OPENED); | ||
this.getCurrentUser().then((currentUser) => { | ||
@@ -198,3 +207,3 @@ this._forceSend(uuid(), { | ||
this._wsOnClose = () => { | ||
this.status = STATUS.CLOSED; | ||
this._setStatus(STATUS.CLOSED); | ||
log.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
@@ -245,2 +254,6 @@ setTimeout(() => { | ||
} | ||
_setStatus(status, err) { | ||
this.status = status; | ||
this._errorMessage = err; | ||
} | ||
/** | ||
@@ -263,6 +276,6 @@ * On refresh we clear out pending mutations that we know have been applied | ||
_handleReceive(msg) { | ||
var _a; | ||
var _a, _b; | ||
switch (msg.op) { | ||
case "init-ok": | ||
this.status = STATUS.AUTHENTICATED; | ||
this._setStatus(STATUS.AUTHENTICATED); | ||
this._reconnectTimeoutMs = 0; | ||
@@ -355,2 +368,8 @@ this._setAttrs(msg.attrs); | ||
} | ||
const isInitError = ((_b = msg["original-event"]) === null || _b === void 0 ? void 0 : _b.op) === "init"; | ||
if (isInitError) { | ||
const errorMessage = { message: msg.message || "Uh-oh, something went wrong. Ping Joe & Stopa." }; | ||
this._setStatus(STATUS.ERRORED, errorMessage); | ||
this.notifyAll(); | ||
} | ||
break; | ||
@@ -390,4 +409,8 @@ default: | ||
this._send(eventId, { op: "add-query", q }); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = (_b = (_a = this.querySubs.currentValue) === null || _a === void 0 ? void 0 : _a[hash]) === null || _b === void 0 ? void 0 : _b.result; | ||
if (prevResult) { | ||
if (errorMessage) { | ||
cb({ error: errorMessage }); | ||
} | ||
else if (prevResult) { | ||
cb({ data: this.dataForResult(q, prevResult) }); | ||
@@ -394,0 +417,0 @@ } |
@@ -16,2 +16,4 @@ export default class ReactiveDB { | ||
_localIdPromises: {}; | ||
_errorMessage: any; | ||
_setStatus(status: any, err: any): void; | ||
/** | ||
@@ -18,0 +20,0 @@ * merge querySubs from storage and in memory. Has the following side |
@@ -52,2 +52,3 @@ "use strict"; | ||
CLOSED: "closed", | ||
ERRORED: "errored", | ||
}; | ||
@@ -140,2 +141,3 @@ function _extractTriplesHelper(idNodes, acc = []) { | ||
this._localIdPromises = {}; | ||
this._errorMessage = null; | ||
/** | ||
@@ -196,7 +198,14 @@ * merge querySubs from storage and in memory. Has the following side | ||
const cbs = this.queryCbs[hash] || []; | ||
if (!cbs) | ||
return; | ||
const errorMessage = this._errorMessage; | ||
if (errorMessage) { | ||
cbs.forEach((cb) => cb({ error: errorMessage })); | ||
return; | ||
} | ||
const { q, result } = this.querySubs.currentValue[hash] || {}; | ||
if (!result || !cbs) | ||
return; | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
if (result) { | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
} | ||
}; | ||
@@ -209,3 +218,3 @@ this.notifyQueryError = (hash, msg) => { | ||
log_1.default.info("[socket] connected"); | ||
this.status = STATUS.OPENED; | ||
this._setStatus(STATUS.OPENED); | ||
this.getCurrentUser().then((currentUser) => { | ||
@@ -226,3 +235,3 @@ this._forceSend((0, uuid_1.default)(), { | ||
this._wsOnClose = () => { | ||
this.status = STATUS.CLOSED; | ||
this._setStatus(STATUS.CLOSED); | ||
log_1.default.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
@@ -273,2 +282,6 @@ setTimeout(() => { | ||
} | ||
_setStatus(status, err) { | ||
this.status = status; | ||
this._errorMessage = err; | ||
} | ||
/** | ||
@@ -291,6 +304,6 @@ * On refresh we clear out pending mutations that we know have been applied | ||
_handleReceive(msg) { | ||
var _a; | ||
var _a, _b; | ||
switch (msg.op) { | ||
case "init-ok": | ||
this.status = STATUS.AUTHENTICATED; | ||
this._setStatus(STATUS.AUTHENTICATED); | ||
this._reconnectTimeoutMs = 0; | ||
@@ -383,2 +396,8 @@ this._setAttrs(msg.attrs); | ||
} | ||
const isInitError = ((_b = msg["original-event"]) === null || _b === void 0 ? void 0 : _b.op) === "init"; | ||
if (isInitError) { | ||
const errorMessage = { message: msg.message || "Uh-oh, something went wrong. Ping Joe & Stopa." }; | ||
this._setStatus(STATUS.ERRORED, errorMessage); | ||
this.notifyAll(); | ||
} | ||
break; | ||
@@ -418,4 +437,8 @@ default: | ||
this._send(eventId, { op: "add-query", q }); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = (_b = (_a = this.querySubs.currentValue) === null || _a === void 0 ? void 0 : _a[hash]) === null || _b === void 0 ? void 0 : _b.result; | ||
if (prevResult) { | ||
if (errorMessage) { | ||
cb({ error: errorMessage }); | ||
} | ||
else if (prevResult) { | ||
cb({ data: this.dataForResult(q, prevResult) }); | ||
@@ -422,0 +445,0 @@ } |
{ | ||
"name": "@instantdb/core", | ||
"version": "0.5.8", | ||
"version": "0.5.9", | ||
"description": "Instant's core local abstraction", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -16,2 +16,3 @@ import log from "./utils/log"; | ||
CLOSED: "closed", | ||
ERRORED: "errored", | ||
}; | ||
@@ -124,2 +125,3 @@ | ||
_localIdPromises = {}; | ||
_errorMessage = null; | ||
@@ -179,2 +181,7 @@ constructor( | ||
_setStatus(status, err) { | ||
this.status = status; | ||
this._errorMessage = err; | ||
} | ||
/** | ||
@@ -261,3 +268,3 @@ * merge querySubs from storage and in memory. Has the following side | ||
case "init-ok": | ||
this.status = STATUS.AUTHENTICATED; | ||
this._setStatus(STATUS.AUTHENTICATED); | ||
this._reconnectTimeoutMs = 0; | ||
@@ -357,2 +364,8 @@ this._setAttrs(msg.attrs); | ||
} | ||
const isInitError = msg["original-event"]?.op === "init"; | ||
if (isInitError) { | ||
const errorMessage = { message: msg.message || "Uh-oh, something went wrong. Ping Joe & Stopa." } | ||
this._setStatus(STATUS.ERRORED, errorMessage); | ||
this.notifyAll(); | ||
} | ||
break; | ||
@@ -394,10 +407,10 @@ default: | ||
}); | ||
this._send(eventId, { op: "add-query", q }); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = this.querySubs.currentValue?.[hash]?.result; | ||
if (prevResult) { | ||
if (errorMessage) { | ||
cb({ error: errorMessage }); | ||
} else if (prevResult) { | ||
cb({ data: this.dataForResult(q, prevResult) }); | ||
} | ||
return () => { | ||
@@ -503,6 +516,13 @@ this.queryCbs[hash] = this.queryCbs[hash].filter((x) => x !== cb); | ||
const cbs = this.queryCbs[hash] || []; | ||
if (!cbs) return | ||
const errorMessage = this._errorMessage; | ||
if (errorMessage) { | ||
cbs.forEach((cb) => cb({ error: errorMessage })); | ||
return; | ||
} | ||
const { q, result } = this.querySubs.currentValue[hash] || {}; | ||
if (!result || !cbs) return; | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
if (result) { | ||
const data = this.dataForResult(q, result); | ||
cbs.forEach((cb) => cb({ data })); | ||
} | ||
}; | ||
@@ -515,3 +535,2 @@ | ||
/** Re-compute all subscriptions */ | ||
@@ -606,3 +625,3 @@ notifyAll() { | ||
log.info("[socket] connected"); | ||
this.status = STATUS.OPENED; | ||
this._setStatus(STATUS.OPENED); | ||
this.getCurrentUser().then((currentUser) => { | ||
@@ -626,3 +645,3 @@ this._forceSend(uuid(), { | ||
_wsOnClose = () => { | ||
this.status = STATUS.CLOSED; | ||
this._setStatus(STATUS.CLOSED); | ||
log.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
@@ -629,0 +648,0 @@ setTimeout(() => { |
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
1338796
28176