@instantdb/core
Advanced tools
Comparing version 0.4.3 to 0.5.0
@@ -8,2 +8,3 @@ export default class ReactiveDB { | ||
queryCbs: {}; | ||
authCbs: any[]; | ||
config: any; | ||
@@ -87,2 +88,15 @@ _persister: IndexedDBStorage; | ||
getLocalId(name: any): Promise<any>; | ||
subscribeAuth(cb: any): () => void; | ||
notifyAuthSubs(user: any): void; | ||
setCurrentUser(user: any): Promise<void>; | ||
getCurrentUser(): Promise<any>; | ||
changeCurrentUser(newUser: any): Promise<void>; | ||
sendMagicCode({ email }: { | ||
email: any; | ||
}): Promise<any>; | ||
verifyMagicCode({ email, code }: { | ||
email: any; | ||
code: any; | ||
}): Promise<any>; | ||
signOut(): void; | ||
} | ||
@@ -89,0 +103,0 @@ declare class PersistedObject { |
@@ -18,2 +18,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import WindowNetworkListener from "./WindowNetworkListener"; | ||
import * as authAPI from "./authAPI"; | ||
const STATUS = { | ||
@@ -97,2 +98,3 @@ CONNECTING: "connecting", | ||
this.queryCbs = {}; | ||
this.authCbs = []; | ||
this._reconnectTimeoutId = null; | ||
@@ -172,5 +174,8 @@ this._reconnectTimeoutMs = 0; | ||
this.status = STATUS.OPENED; | ||
this._forceSend(uuid(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
this.getCurrentUser().then((currentUser) => { | ||
this._forceSend(uuid(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
"refresh-token": currentUser === null || currentUser === void 0 ? void 0 : currentUser["refresh_token"] | ||
}); | ||
}); | ||
@@ -186,5 +191,5 @@ }; | ||
this.status = STATUS.CLOSED; | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, 10000); | ||
log.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
setTimeout(() => { | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, 10000); | ||
if (!this._isOnline) { | ||
@@ -312,7 +317,7 @@ log.info("[socket-close] we are offline, no need to start socket"); | ||
delete errorObj.message; | ||
delete errorObj.hint; | ||
console.error(msg.message, errorObj); | ||
const errorPrevMutation = this.pendingMutations.currentValue.get(errorEventId); | ||
if (msg.hint) { | ||
console.error("This error comes with some debugging information. Here it is:"); | ||
console.error(msg.hint); | ||
console.error("This error comes with some debugging information. Here it is: \n", msg.hint); | ||
} | ||
@@ -552,3 +557,64 @@ if (errorPrevMutation) { | ||
} | ||
// ----- | ||
// Auth | ||
subscribeAuth(cb) { | ||
this.authCbs.push(cb); | ||
let unsubbed = false; | ||
this.getCurrentUser().then((currentUser) => { | ||
if (unsubbed) | ||
return; | ||
cb({ user: currentUser }); | ||
}); | ||
return () => { | ||
unsubbed = true; | ||
this.authCbs = this.authCbs.filter((x) => x !== cb); | ||
}; | ||
} | ||
notifyAuthSubs(user) { | ||
this.authCbs.forEach((cb) => cb(user)); | ||
} | ||
setCurrentUser(user) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const k = `currentUser`; | ||
yield this._persister.setItem(k, JSON.stringify(user)); | ||
}); | ||
} | ||
getCurrentUser() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const k = `currentUser`; | ||
const user = yield this._persister.getItem(k); | ||
return JSON.parse(user); | ||
}); | ||
} | ||
changeCurrentUser(newUser) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.setCurrentUser(newUser); | ||
this._reconnectTimeoutMs = 0; | ||
this._ws.close(); | ||
this.notifyAuthSubs({ user: newUser }); | ||
}); | ||
} | ||
sendMagicCode({ email }) { | ||
return authAPI.sendMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email: email, | ||
}); | ||
} | ||
verifyMagicCode({ email, code }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield authAPI.verifyMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email, | ||
code, | ||
}); | ||
this.changeCurrentUser(res.user); | ||
return res; | ||
}); | ||
} | ||
signOut() { | ||
this.changeCurrentUser(null); | ||
} | ||
} | ||
//# sourceMappingURL=ReactiveDB.js.map |
@@ -8,2 +8,3 @@ export default class ReactiveDB { | ||
queryCbs: {}; | ||
authCbs: any[]; | ||
config: any; | ||
@@ -87,2 +88,15 @@ _persister: IndexedDBStorage; | ||
getLocalId(name: any): Promise<any>; | ||
subscribeAuth(cb: any): () => void; | ||
notifyAuthSubs(user: any): void; | ||
setCurrentUser(user: any): Promise<void>; | ||
getCurrentUser(): Promise<any>; | ||
changeCurrentUser(newUser: any): Promise<void>; | ||
sendMagicCode({ email }: { | ||
email: any; | ||
}): Promise<any>; | ||
verifyMagicCode({ email, code }: { | ||
email: any; | ||
code: any; | ||
}): Promise<any>; | ||
signOut(): void; | ||
} | ||
@@ -89,0 +103,0 @@ declare class PersistedObject { |
@@ -46,2 +46,3 @@ "use strict"; | ||
const WindowNetworkListener_1 = __importDefault(require("./WindowNetworkListener")); | ||
const authAPI = __importStar(require("./authAPI")); | ||
const STATUS = { | ||
@@ -125,2 +126,3 @@ CONNECTING: "connecting", | ||
this.queryCbs = {}; | ||
this.authCbs = []; | ||
this._reconnectTimeoutId = null; | ||
@@ -200,5 +202,8 @@ this._reconnectTimeoutMs = 0; | ||
this.status = STATUS.OPENED; | ||
this._forceSend((0, uuid_1.default)(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
this.getCurrentUser().then((currentUser) => { | ||
this._forceSend((0, uuid_1.default)(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
"refresh-token": currentUser === null || currentUser === void 0 ? void 0 : currentUser["refresh_token"] | ||
}); | ||
}); | ||
@@ -214,5 +219,5 @@ }; | ||
this.status = STATUS.CLOSED; | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, 10000); | ||
log_1.default.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
setTimeout(() => { | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, 10000); | ||
if (!this._isOnline) { | ||
@@ -340,7 +345,7 @@ log_1.default.info("[socket-close] we are offline, no need to start socket"); | ||
delete errorObj.message; | ||
delete errorObj.hint; | ||
console.error(msg.message, errorObj); | ||
const errorPrevMutation = this.pendingMutations.currentValue.get(errorEventId); | ||
if (msg.hint) { | ||
console.error("This error comes with some debugging information. Here it is:"); | ||
console.error(msg.hint); | ||
console.error("This error comes with some debugging information. Here it is: \n", msg.hint); | ||
} | ||
@@ -580,4 +585,65 @@ if (errorPrevMutation) { | ||
} | ||
// ----- | ||
// Auth | ||
subscribeAuth(cb) { | ||
this.authCbs.push(cb); | ||
let unsubbed = false; | ||
this.getCurrentUser().then((currentUser) => { | ||
if (unsubbed) | ||
return; | ||
cb({ user: currentUser }); | ||
}); | ||
return () => { | ||
unsubbed = true; | ||
this.authCbs = this.authCbs.filter((x) => x !== cb); | ||
}; | ||
} | ||
notifyAuthSubs(user) { | ||
this.authCbs.forEach((cb) => cb(user)); | ||
} | ||
setCurrentUser(user) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const k = `currentUser`; | ||
yield this._persister.setItem(k, JSON.stringify(user)); | ||
}); | ||
} | ||
getCurrentUser() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const k = `currentUser`; | ||
const user = yield this._persister.getItem(k); | ||
return JSON.parse(user); | ||
}); | ||
} | ||
changeCurrentUser(newUser) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.setCurrentUser(newUser); | ||
this._reconnectTimeoutMs = 0; | ||
this._ws.close(); | ||
this.notifyAuthSubs({ user: newUser }); | ||
}); | ||
} | ||
sendMagicCode({ email }) { | ||
return authAPI.sendMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email: email, | ||
}); | ||
} | ||
verifyMagicCode({ email, code }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield authAPI.verifyMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email, | ||
code, | ||
}); | ||
this.changeCurrentUser(res.user); | ||
return res; | ||
}); | ||
} | ||
signOut() { | ||
this.changeCurrentUser(null); | ||
} | ||
} | ||
exports.default = ReactiveDB; | ||
//# sourceMappingURL=ReactiveDB.js.map |
{ | ||
"name": "@instantdb/core", | ||
"version": "0.4.3", | ||
"version": "0.5.0", | ||
"description": "Instant's core local abstraction", | ||
@@ -33,5 +33,2 @@ "main": "dist/index.js", | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16" | ||
}, | ||
"dependencies": { | ||
@@ -38,0 +35,0 @@ "immer": "9.0.14", |
@@ -9,2 +9,3 @@ import log from "./utils/log"; | ||
import WindowNetworkListener from "./WindowNetworkListener"; | ||
import * as authAPI from "./authAPI"; | ||
@@ -103,2 +104,3 @@ const STATUS = { | ||
queryCbs = {}; | ||
authCbs = []; | ||
config; | ||
@@ -318,2 +320,3 @@ _persister; | ||
delete errorObj.message; | ||
delete errorObj.hint; | ||
console.error(msg.message, errorObj); | ||
@@ -324,5 +327,5 @@ const errorPrevMutation = | ||
console.error( | ||
"This error comes with some debugging information. Here it is:", | ||
"This error comes with some debugging information. Here it is: \n", | ||
msg.hint | ||
); | ||
console.error(msg.hint); | ||
} | ||
@@ -574,5 +577,8 @@ if (errorPrevMutation) { | ||
this.status = STATUS.OPENED; | ||
this._forceSend(uuid(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
this.getCurrentUser().then((currentUser) => { | ||
this._forceSend(uuid(), { | ||
op: "init", | ||
"app-id": this.config.appId, | ||
"refresh-token": currentUser?.["refresh_token"] | ||
}); | ||
}); | ||
@@ -591,5 +597,6 @@ }; | ||
this.status = STATUS.CLOSED; | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, 10000); | ||
log.info("[socket-close] scheduling reconnect", this._reconnectTimeoutMs); | ||
setTimeout(() => { | ||
this._reconnectTimeoutMs = Math.min(this._reconnectTimeoutMs + 1000, | ||
10000); | ||
if (!this._isOnline) { | ||
@@ -633,2 +640,64 @@ log.info("[socket-close] we are offline, no need to start socket"); | ||
} | ||
// ----- | ||
// Auth | ||
subscribeAuth(cb) { | ||
this.authCbs.push(cb); | ||
let unsubbed = false; | ||
this.getCurrentUser().then((currentUser) => { | ||
if (unsubbed) return; | ||
cb({ user: currentUser }); | ||
}); | ||
return () => { | ||
unsubbed = true; | ||
this.authCbs = this.authCbs.filter((x) => x !== cb); | ||
} | ||
} | ||
notifyAuthSubs(user) { | ||
this.authCbs.forEach((cb) => cb(user)); | ||
} | ||
async setCurrentUser(user) { | ||
const k = `currentUser`; | ||
await this._persister.setItem(k, JSON.stringify(user)); | ||
} | ||
async getCurrentUser() { | ||
const k = `currentUser`; | ||
const user = await this._persister.getItem(k) | ||
return JSON.parse(user); | ||
} | ||
async changeCurrentUser(newUser) { | ||
await this.setCurrentUser(newUser); | ||
this._reconnectTimeoutMs = 0; | ||
this._ws.close(); | ||
this.notifyAuthSubs({ user: newUser }); | ||
} | ||
sendMagicCode({ email }) { | ||
return authAPI.sendMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email: email, | ||
}) | ||
} | ||
async verifyMagicCode({ email, code }) { | ||
const res = await authAPI.verifyMagicCode({ | ||
apiURI: this.config.apiURI, | ||
appId: this.config.appId, | ||
email, | ||
code, | ||
}) | ||
this.changeCurrentUser(res.user); | ||
return res; | ||
} | ||
signOut() { | ||
this.changeCurrentUser(null); | ||
} | ||
} | ||
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
1024681
3
154
19684