edmaxlabs-core 1.2.1 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -110,6 +110,26 @@ var HttpsRequest = class {
110
110
  }
111
111
  };
112
112
 
113
+ // src/utils/waiter.ts
114
+ var listeners = /* @__PURE__ */ new Map();
115
+ var waiters = /* @__PURE__ */ new Map();
116
+ function emitValue(key, value) {
117
+ const keyListeners = listeners.get(key) || [];
118
+ keyListeners.forEach((listener) => listener(value));
119
+ const keyWaiters = waiters.get(key) || [];
120
+ keyWaiters.forEach((resolve) => resolve(value));
121
+ waiters.delete(key);
122
+ }
123
+ function onValue(key, callback) {
124
+ const current = listeners.get(key) || [];
125
+ current.push(callback);
126
+ listeners.set(key, current);
127
+ return () => {
128
+ const next = (listeners.get(key) || []).filter((cb) => cb !== callback);
129
+ listeners.set(key, next);
130
+ };
131
+ }
132
+
113
133
  // src/authentication/Authentication.ts
114
134
  var Authentication = class {
115
135
  constructor() {
@@ -127,6 +147,7 @@ var Authentication = class {
127
147
  return;
128
148
  }
129
149
  ldb.setItem("eauth", JSON.stringify(credentials));
150
+ emitValue("credentials", credentials);
130
151
  };
131
152
  this.currentUser = () => {
132
153
  const ldb = localStorage;
@@ -142,48 +163,59 @@ var Authentication = class {
142
163
  onSignOut,
143
164
  onDeleted
144
165
  }) => {
145
- const cuser = this.currentUser();
146
- if (!cuser)
147
- return;
148
- const userRef = this.app?.database.collection("users").doc(cuser?.uid);
149
- userRef?.onSnapshot((snapshot, change) => {
150
- if (this.eUser === null || this.eUser === void 0)
166
+ let userDocUnsubscribe;
167
+ const attachUserListener = (creds) => {
168
+ if (userDocUnsubscribe) {
169
+ userDocUnsubscribe();
170
+ userDocUnsubscribe = void 0;
171
+ }
172
+ if (!creds) {
173
+ this.eUser = void 0;
174
+ onSignOut?.();
151
175
  return;
152
- if (change === "insert") {
153
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
154
- this.eUser = void 0;
155
- this.saveCredentials(null);
156
- if (onSignOut)
157
- onSignOut();
158
- return;
159
- }
160
- const result = Credentials.fromMap(snapshot.toMap());
161
- this.eUser = result;
162
- this.saveCredentials(result);
163
- onChange(result);
164
176
  }
165
- if (change === "update") {
166
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
167
- this.eUser = void 0;
168
- this.saveCredentials(null);
169
- if (onSignOut)
170
- onSignOut();
171
- return;
177
+ const userRef = this.app?.database.collection("users").doc(creds.uid);
178
+ if (!userRef)
179
+ return;
180
+ userDocUnsubscribe = userRef.onSnapshot(
181
+ (snapshot, change) => {
182
+ if (change === "insert" || change === "update") {
183
+ if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
184
+ this.eUser = void 0;
185
+ this.saveCredentials(null);
186
+ onSignOut?.();
187
+ return;
188
+ }
189
+ const result = Credentials.fromMap(snapshot.toMap());
190
+ this.eUser = result;
191
+ this.saveCredentials(result);
192
+ onChange(result);
193
+ }
194
+ if (change === "delete") {
195
+ this.eUser = void 0;
196
+ this.saveCredentials(null);
197
+ onSignOut?.();
198
+ onDeleted?.();
199
+ }
172
200
  }
173
- const result = Credentials.fromMap(snapshot.toMap());
174
- this.eUser = result;
175
- this.saveCredentials(result);
176
- onChange(result);
177
- }
178
- if (change === "delete") {
179
- this.eUser = void 0;
180
- this.saveCredentials(null);
181
- if (onSignOut)
182
- onSignOut();
183
- if (onDeleted)
184
- onDeleted();
201
+ );
202
+ };
203
+ const current = this.currentUser();
204
+ if (current) {
205
+ this.eUser = current;
206
+ onChange(current);
207
+ attachUserListener(current);
208
+ }
209
+ const unsubscribeCredentials = onValue(
210
+ "credentials",
211
+ (creds) => {
212
+ attachUserListener(creds);
185
213
  }
186
- });
214
+ );
215
+ return () => {
216
+ unsubscribeCredentials?.();
217
+ userDocUnsubscribe?.();
218
+ };
187
219
  };
188
220
  this.createUserWithEmailAndPassword = async ({
189
221
  email,
@@ -242,6 +274,7 @@ var Authentication = class {
242
274
  logged: true,
243
275
  lastLogged: Date.now()
244
276
  });
277
+ console.log("Success !");
245
278
  return Credentials.fromMap(data[0].toMap());
246
279
  };
247
280
  this.deleteUser = async () => {
@@ -284,8 +317,7 @@ var Authentication = class {
284
317
  };
285
318
  this.client = EdmaxLabs.instance;
286
319
  this.app = new EdmaxLabs({
287
- token: this.client.getAuth.authToken,
288
- authToken: this.client.getAuth.authToken,
320
+ token: "auth",
289
321
  project: this.client.getAuth.project
290
322
  });
291
323
  this.init();
@@ -1571,11 +1603,11 @@ var socketURI = "https://api.edmaxlabs.com";
1571
1603
 
1572
1604
  // src/core/EdmaxLabs.ts
1573
1605
  var _EdmaxLabs = class _EdmaxLabs {
1574
- constructor({ token, authToken, project }) {
1606
+ constructor({ token, project }) {
1575
1607
  this.persistence = false;
1576
1608
  this.baseUrl = serverURI;
1577
1609
  this.socketUrl = socketURI;
1578
- this.auth = { token, authToken, project };
1610
+ this.auth = { token, project };
1579
1611
  this._db = new Database(this);
1580
1612
  this._hosting = new Hosting(this);
1581
1613
  this.realtime = new Realtime(this);