@instantdb/core
Advanced tools
Comparing version 0.12.27 to 0.12.29
@@ -36,2 +36,7 @@ /** | ||
_broadcastSubs: {}; | ||
_currentUserCached: { | ||
isLoading: boolean; | ||
error: any; | ||
user: any; | ||
}; | ||
_initStorage(Storage: any): void; | ||
@@ -61,2 +66,7 @@ _finishTransaction(ok: any, status: any, clientId: any, errDetails: any): void; | ||
_setAttrs(attrs: any): void; | ||
getPreviousResult: (q: any) => { | ||
data: {}; | ||
} | { | ||
error: any; | ||
}; | ||
/** | ||
@@ -147,10 +157,10 @@ * When a user subscribes to a query the following side effects occur: | ||
setCurrentUser(user: any): Promise<void>; | ||
getCurrentUserCached(): { | ||
isLoading: boolean; | ||
error: any; | ||
user: any; | ||
}; | ||
getCurrentUser(): Promise<{ | ||
error: { | ||
message: string; | ||
}; | ||
user?: undefined; | ||
} | { | ||
user: any; | ||
error?: undefined; | ||
error: any; | ||
}>; | ||
@@ -157,0 +167,0 @@ _hasCurrentUser(): Promise<boolean>; |
@@ -73,2 +73,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this._broadcastSubs = {}; | ||
this._currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
/** | ||
@@ -126,2 +127,16 @@ * merge querySubs from storage and in memory. Has the following side | ||
}; | ||
// --------------------------- | ||
// Queries | ||
this.getPreviousResult = (q) => { | ||
var _a, _b, _c; | ||
const hash = weakHash(q); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = (_c = (_b = (_a = this.querySubs) === null || _a === void 0 ? void 0 : _a.currentValue) === null || _b === void 0 ? void 0 : _b[hash]) === null || _c === void 0 ? void 0 : _c.result; | ||
if (errorMessage) { | ||
return { error: errorMessage }; | ||
} | ||
else if (prevResult) { | ||
return this.dataForResult(q, prevResult); | ||
} | ||
}; | ||
/** Re-run instaql and call all callbacks with new data */ | ||
@@ -243,2 +258,4 @@ this.notifyOne = (hash) => { | ||
this._initStorage(Storage); | ||
// kick off a request to cache it | ||
this.getCurrentUser(); | ||
NetworkListener.getIsOnline().then((isOnline) => { | ||
@@ -481,4 +498,2 @@ this._isOnline = isOnline; | ||
} | ||
// --------------------------- | ||
// Queries | ||
/** | ||
@@ -495,3 +510,2 @@ * When a user subscribes to a query the following side effects occur: | ||
subscribeQuery(q, cb) { | ||
var _a, _b; | ||
const eventId = uuid(); | ||
@@ -506,10 +520,6 @@ const hash = weakHash(q); | ||
this._trySendAuthed(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 (errorMessage) { | ||
cb({ error: errorMessage }); | ||
const prevResult = this.getPreviousResult(q); | ||
if (prevResult) { | ||
cb(prevResult); | ||
} | ||
else if (prevResult) { | ||
cb(this.dataForResult(q, prevResult)); | ||
} | ||
return () => { | ||
@@ -872,2 +882,6 @@ this.queryCbs[hash] = this.queryCbs[hash].filter((x) => x !== cb); | ||
this.authCbs.push(cb); | ||
const currUserCached = this._currentUserCached; | ||
if (!currUserCached.isLoading) { | ||
cb(this._currentUserCached); | ||
} | ||
let unsubbed = false; | ||
@@ -877,2 +891,4 @@ this.getCurrentUser().then((resp) => { | ||
return; | ||
if (areObjectsDeepEqual(resp, currUserCached)) | ||
return; | ||
cb(resp); | ||
@@ -911,2 +927,5 @@ }); | ||
} | ||
getCurrentUserCached() { | ||
return this._currentUserCached; | ||
} | ||
getCurrentUser() { | ||
@@ -916,6 +935,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
if (oauthResp === null || oauthResp === void 0 ? void 0 : oauthResp.error) { | ||
return { error: oauthResp.error }; | ||
const errorV = { error: oauthResp.error, user: undefined }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, errorV); | ||
return errorV; | ||
} | ||
const user = yield this._persister.getItem(currentUserKey); | ||
return { user: JSON.parse(user) }; | ||
const userV = { user: JSON.parse(user), error: undefined }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, userV); | ||
return userV; | ||
}); | ||
@@ -951,2 +974,4 @@ } | ||
updateUser(newUser) { | ||
const newV = { error: undefined, user: newUser }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, newV); | ||
this.querySubs.set((prev) => { | ||
@@ -961,3 +986,3 @@ Object.keys(prev).forEach((k) => { | ||
this._oauthCallbackResponse = null; | ||
this.notifyAuthSubs({ user: newUser }); | ||
this.notifyAuthSubs(newV); | ||
} | ||
@@ -964,0 +989,0 @@ sendMagicCode({ email }) { |
@@ -175,2 +175,3 @@ import { produce, enableMapSet } from "immer"; | ||
function addTriple(store, rawTriple) { | ||
var _a; | ||
const triple = resolveLookupRefs(store, rawTriple); | ||
@@ -189,3 +190,8 @@ if (!triple) { | ||
} | ||
const enhancedTriple = [...triple, getCreatedAt(store, attr, triple)]; | ||
const existingTriple = getInMap(store.eav, [eid, aid, v]); | ||
// Reuse the created_at for a triple if it's already in the store. | ||
// Prevents updates from temporarily pushing an entity to the top | ||
// while waiting for the server response. | ||
const t = (_a = existingTriple === null || existingTriple === void 0 ? void 0 : existingTriple[3]) !== null && _a !== void 0 ? _a : getCreatedAt(store, attr, triple); | ||
const enhancedTriple = [eid, aid, v, t]; | ||
if (hasEA(attr)) { | ||
@@ -192,0 +198,0 @@ setInMap(store.eav, [eid, aid], new Map([[v, enhancedTriple]])); |
@@ -36,2 +36,7 @@ /** | ||
_broadcastSubs: {}; | ||
_currentUserCached: { | ||
isLoading: boolean; | ||
error: any; | ||
user: any; | ||
}; | ||
_initStorage(Storage: any): void; | ||
@@ -61,2 +66,7 @@ _finishTransaction(ok: any, status: any, clientId: any, errDetails: any): void; | ||
_setAttrs(attrs: any): void; | ||
getPreviousResult: (q: any) => { | ||
data: {}; | ||
} | { | ||
error: any; | ||
}; | ||
/** | ||
@@ -147,10 +157,10 @@ * When a user subscribes to a query the following side effects occur: | ||
setCurrentUser(user: any): Promise<void>; | ||
getCurrentUserCached(): { | ||
isLoading: boolean; | ||
error: any; | ||
user: any; | ||
}; | ||
getCurrentUser(): Promise<{ | ||
error: { | ||
message: string; | ||
}; | ||
user?: undefined; | ||
} | { | ||
user: any; | ||
error?: undefined; | ||
error: any; | ||
}>; | ||
@@ -157,0 +167,0 @@ _hasCurrentUser(): Promise<boolean>; |
@@ -101,2 +101,3 @@ "use strict"; | ||
this._broadcastSubs = {}; | ||
this._currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
/** | ||
@@ -154,2 +155,16 @@ * merge querySubs from storage and in memory. Has the following side | ||
}; | ||
// --------------------------- | ||
// Queries | ||
this.getPreviousResult = (q) => { | ||
var _a, _b, _c; | ||
const hash = (0, weakHash_1.default)(q); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = (_c = (_b = (_a = this.querySubs) === null || _a === void 0 ? void 0 : _a.currentValue) === null || _b === void 0 ? void 0 : _b[hash]) === null || _c === void 0 ? void 0 : _c.result; | ||
if (errorMessage) { | ||
return { error: errorMessage }; | ||
} | ||
else if (prevResult) { | ||
return this.dataForResult(q, prevResult); | ||
} | ||
}; | ||
/** Re-run instaql and call all callbacks with new data */ | ||
@@ -271,2 +286,4 @@ this.notifyOne = (hash) => { | ||
this._initStorage(Storage); | ||
// kick off a request to cache it | ||
this.getCurrentUser(); | ||
NetworkListener.getIsOnline().then((isOnline) => { | ||
@@ -509,4 +526,2 @@ this._isOnline = isOnline; | ||
} | ||
// --------------------------- | ||
// Queries | ||
/** | ||
@@ -523,3 +538,2 @@ * When a user subscribes to a query the following side effects occur: | ||
subscribeQuery(q, cb) { | ||
var _a, _b; | ||
const eventId = (0, uuid_1.default)(); | ||
@@ -534,10 +548,6 @@ const hash = (0, weakHash_1.default)(q); | ||
this._trySendAuthed(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 (errorMessage) { | ||
cb({ error: errorMessage }); | ||
const prevResult = this.getPreviousResult(q); | ||
if (prevResult) { | ||
cb(prevResult); | ||
} | ||
else if (prevResult) { | ||
cb(this.dataForResult(q, prevResult)); | ||
} | ||
return () => { | ||
@@ -900,2 +910,6 @@ this.queryCbs[hash] = this.queryCbs[hash].filter((x) => x !== cb); | ||
this.authCbs.push(cb); | ||
const currUserCached = this._currentUserCached; | ||
if (!currUserCached.isLoading) { | ||
cb(this._currentUserCached); | ||
} | ||
let unsubbed = false; | ||
@@ -905,2 +919,4 @@ this.getCurrentUser().then((resp) => { | ||
return; | ||
if ((0, object_1.areObjectsDeepEqual)(resp, currUserCached)) | ||
return; | ||
cb(resp); | ||
@@ -939,2 +955,5 @@ }); | ||
} | ||
getCurrentUserCached() { | ||
return this._currentUserCached; | ||
} | ||
getCurrentUser() { | ||
@@ -944,6 +963,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
if (oauthResp === null || oauthResp === void 0 ? void 0 : oauthResp.error) { | ||
return { error: oauthResp.error }; | ||
const errorV = { error: oauthResp.error, user: undefined }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, errorV); | ||
return errorV; | ||
} | ||
const user = yield this._persister.getItem(currentUserKey); | ||
return { user: JSON.parse(user) }; | ||
const userV = { user: JSON.parse(user), error: undefined }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, userV); | ||
return userV; | ||
}); | ||
@@ -979,2 +1002,4 @@ } | ||
updateUser(newUser) { | ||
const newV = { error: undefined, user: newUser }; | ||
this._currentUserCached = Object.assign({ isLoading: false }, newV); | ||
this.querySubs.set((prev) => { | ||
@@ -989,3 +1014,3 @@ Object.keys(prev).forEach((k) => { | ||
this._oauthCallbackResponse = null; | ||
this.notifyAuthSubs({ user: newUser }); | ||
this.notifyAuthSubs(newV); | ||
} | ||
@@ -992,0 +1017,0 @@ sendMagicCode({ email }) { |
@@ -181,2 +181,3 @@ "use strict"; | ||
function addTriple(store, rawTriple) { | ||
var _a; | ||
const triple = resolveLookupRefs(store, rawTriple); | ||
@@ -195,3 +196,8 @@ if (!triple) { | ||
} | ||
const enhancedTriple = [...triple, getCreatedAt(store, attr, triple)]; | ||
const existingTriple = getInMap(store.eav, [eid, aid, v]); | ||
// Reuse the created_at for a triple if it's already in the store. | ||
// Prevents updates from temporarily pushing an entity to the top | ||
// while waiting for the server response. | ||
const t = (_a = existingTriple === null || existingTriple === void 0 ? void 0 : existingTriple[3]) !== null && _a !== void 0 ? _a : getCreatedAt(store, attr, triple); | ||
const enhancedTriple = [eid, aid, v, t]; | ||
if (hasEA(attr)) { | ||
@@ -198,0 +204,0 @@ setInMap(store.eav, [eid, aid], new Map([[v, enhancedTriple]])); |
{ | ||
"name": "@instantdb/core", | ||
"version": "v0.12.27", | ||
"version": "v0.12.29", | ||
"description": "Instant's core local abstraction", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -82,2 +82,3 @@ // @ts-check | ||
_broadcastSubs = {}; | ||
_currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
@@ -114,2 +115,5 @@ constructor( | ||
// kick off a request to cache it | ||
this.getCurrentUser(); | ||
NetworkListener.getIsOnline().then((isOnline) => { | ||
@@ -464,2 +468,13 @@ this._isOnline = isOnline; | ||
getPreviousResult = (q) => { | ||
const hash = weakHash(q); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = this.querySubs?.currentValue?.[hash]?.result; | ||
if (errorMessage) { | ||
return { error: errorMessage }; | ||
} else if (prevResult) { | ||
return this.dataForResult(q, prevResult); | ||
} | ||
}; | ||
/** | ||
@@ -486,8 +501,5 @@ * When a user subscribes to a query the following side effects occur: | ||
this._trySendAuthed(eventId, { op: "add-query", q }); | ||
const errorMessage = this._errorMessage; | ||
const prevResult = this.querySubs.currentValue?.[hash]?.result; | ||
if (errorMessage) { | ||
cb({ error: errorMessage }); | ||
} else if (prevResult) { | ||
cb(this.dataForResult(q, prevResult)); | ||
const prevResult = this.getPreviousResult(q); | ||
if (prevResult) { | ||
cb(prevResult); | ||
} | ||
@@ -1009,5 +1021,10 @@ return () => { | ||
this.authCbs.push(cb); | ||
const currUserCached = this._currentUserCached; | ||
if (!currUserCached.isLoading) { | ||
cb(this._currentUserCached); | ||
} | ||
let unsubbed = false; | ||
this.getCurrentUser().then((resp) => { | ||
if (unsubbed) return; | ||
if (areObjectsDeepEqual(resp, currUserCached)) return; | ||
cb(resp); | ||
@@ -1051,9 +1068,20 @@ }); | ||
getCurrentUserCached() { | ||
return this._currentUserCached; | ||
} | ||
async getCurrentUser() { | ||
const oauthResp = await this._waitForOAuthCallbackResponse(); | ||
if (oauthResp?.error) { | ||
return { error: oauthResp.error }; | ||
const errorV = { error: oauthResp.error, user: undefined }; | ||
this._currentUserCached = { isLoading: false, ...errorV }; | ||
return errorV; | ||
} | ||
const user = await this._persister.getItem(currentUserKey); | ||
return { user: JSON.parse(user) }; | ||
const userV = { user: JSON.parse(user), error: undefined }; | ||
this._currentUserCached = { | ||
isLoading: false, | ||
...userV, | ||
}; | ||
return userV; | ||
} | ||
@@ -1086,2 +1114,4 @@ | ||
updateUser(newUser) { | ||
const newV = { error: undefined, user: newUser }; | ||
this._currentUserCached = { isLoading: false, ...newV }; | ||
this.querySubs.set((prev) => { | ||
@@ -1096,3 +1126,3 @@ Object.keys(prev).forEach((k) => { | ||
this._oauthCallbackResponse = null; | ||
this.notifyAuthSubs({ user: newUser }); | ||
this.notifyAuthSubs(newV); | ||
} | ||
@@ -1099,0 +1129,0 @@ |
@@ -211,4 +211,10 @@ import { produce, enableMapSet } from "immer"; | ||
} | ||
const enhancedTriple = [...triple, getCreatedAt(store, attr, triple)]; | ||
const existingTriple = getInMap(store.eav, [eid, aid, v]); | ||
// Reuse the created_at for a triple if it's already in the store. | ||
// Prevents updates from temporarily pushing an entity to the top | ||
// while waiting for the server response. | ||
const t = existingTriple?.[3] ?? getCreatedAt(store, attr, triple); | ||
const enhancedTriple = [eid, aid, v, t]; | ||
if (hasEA(attr)) { | ||
@@ -215,0 +221,0 @@ setInMap(store.eav, [eid, aid], new Map([[v, enhancedTriple]])); |
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 not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
1970165
40201
318