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.mjs CHANGED
@@ -79,6 +79,26 @@ var HttpsRequest = class {
79
79
  }
80
80
  };
81
81
 
82
+ // src/utils/waiter.ts
83
+ var listeners = /* @__PURE__ */ new Map();
84
+ var waiters = /* @__PURE__ */ new Map();
85
+ function emitValue(key, value) {
86
+ const keyListeners = listeners.get(key) || [];
87
+ keyListeners.forEach((listener) => listener(value));
88
+ const keyWaiters = waiters.get(key) || [];
89
+ keyWaiters.forEach((resolve) => resolve(value));
90
+ waiters.delete(key);
91
+ }
92
+ function onValue(key, callback) {
93
+ const current = listeners.get(key) || [];
94
+ current.push(callback);
95
+ listeners.set(key, current);
96
+ return () => {
97
+ const next = (listeners.get(key) || []).filter((cb) => cb !== callback);
98
+ listeners.set(key, next);
99
+ };
100
+ }
101
+
82
102
  // src/authentication/Authentication.ts
83
103
  var Authentication = class {
84
104
  constructor() {
@@ -96,6 +116,7 @@ var Authentication = class {
96
116
  return;
97
117
  }
98
118
  ldb.setItem("eauth", JSON.stringify(credentials));
119
+ emitValue("credentials", credentials);
99
120
  };
100
121
  this.currentUser = () => {
101
122
  const ldb = localStorage;
@@ -111,48 +132,59 @@ var Authentication = class {
111
132
  onSignOut,
112
133
  onDeleted
113
134
  }) => {
114
- const cuser = this.currentUser();
115
- if (!cuser)
116
- return;
117
- const userRef = this.app?.database.collection("users").doc(cuser?.uid);
118
- userRef?.onSnapshot((snapshot, change) => {
119
- if (this.eUser === null || this.eUser === void 0)
135
+ let userDocUnsubscribe;
136
+ const attachUserListener = (creds) => {
137
+ if (userDocUnsubscribe) {
138
+ userDocUnsubscribe();
139
+ userDocUnsubscribe = void 0;
140
+ }
141
+ if (!creds) {
142
+ this.eUser = void 0;
143
+ onSignOut?.();
120
144
  return;
121
- if (change === "insert") {
122
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
123
- this.eUser = void 0;
124
- this.saveCredentials(null);
125
- if (onSignOut)
126
- onSignOut();
127
- return;
128
- }
129
- const result = Credentials.fromMap(snapshot.toMap());
130
- this.eUser = result;
131
- this.saveCredentials(result);
132
- onChange(result);
133
145
  }
134
- if (change === "update") {
135
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
136
- this.eUser = void 0;
137
- this.saveCredentials(null);
138
- if (onSignOut)
139
- onSignOut();
140
- return;
146
+ const userRef = this.app?.database.collection("users").doc(creds.uid);
147
+ if (!userRef)
148
+ return;
149
+ userDocUnsubscribe = userRef.onSnapshot(
150
+ (snapshot, change) => {
151
+ if (change === "insert" || change === "update") {
152
+ if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
153
+ this.eUser = void 0;
154
+ this.saveCredentials(null);
155
+ onSignOut?.();
156
+ return;
157
+ }
158
+ const result = Credentials.fromMap(snapshot.toMap());
159
+ this.eUser = result;
160
+ this.saveCredentials(result);
161
+ onChange(result);
162
+ }
163
+ if (change === "delete") {
164
+ this.eUser = void 0;
165
+ this.saveCredentials(null);
166
+ onSignOut?.();
167
+ onDeleted?.();
168
+ }
141
169
  }
142
- const result = Credentials.fromMap(snapshot.toMap());
143
- this.eUser = result;
144
- this.saveCredentials(result);
145
- onChange(result);
146
- }
147
- if (change === "delete") {
148
- this.eUser = void 0;
149
- this.saveCredentials(null);
150
- if (onSignOut)
151
- onSignOut();
152
- if (onDeleted)
153
- onDeleted();
170
+ );
171
+ };
172
+ const current = this.currentUser();
173
+ if (current) {
174
+ this.eUser = current;
175
+ onChange(current);
176
+ attachUserListener(current);
177
+ }
178
+ const unsubscribeCredentials = onValue(
179
+ "credentials",
180
+ (creds) => {
181
+ attachUserListener(creds);
154
182
  }
155
- });
183
+ );
184
+ return () => {
185
+ unsubscribeCredentials?.();
186
+ userDocUnsubscribe?.();
187
+ };
156
188
  };
157
189
  this.createUserWithEmailAndPassword = async ({
158
190
  email,
@@ -211,6 +243,7 @@ var Authentication = class {
211
243
  logged: true,
212
244
  lastLogged: Date.now()
213
245
  });
246
+ console.log("Success !");
214
247
  return Credentials.fromMap(data[0].toMap());
215
248
  };
216
249
  this.deleteUser = async () => {
@@ -253,8 +286,7 @@ var Authentication = class {
253
286
  };
254
287
  this.client = EdmaxLabs.instance;
255
288
  this.app = new EdmaxLabs({
256
- token: this.client.getAuth.authToken,
257
- authToken: this.client.getAuth.authToken,
289
+ token: "auth",
258
290
  project: this.client.getAuth.project
259
291
  });
260
292
  this.init();
@@ -1540,11 +1572,11 @@ var socketURI = "https://api.edmaxlabs.com";
1540
1572
 
1541
1573
  // src/core/EdmaxLabs.ts
1542
1574
  var _EdmaxLabs = class _EdmaxLabs {
1543
- constructor({ token, authToken, project }) {
1575
+ constructor({ token, project }) {
1544
1576
  this.persistence = false;
1545
1577
  this.baseUrl = serverURI;
1546
1578
  this.socketUrl = socketURI;
1547
- this.auth = { token, authToken, project };
1579
+ this.auth = { token, project };
1548
1580
  this._db = new Database(this);
1549
1581
  this._hosting = new Hosting(this);
1550
1582
  this.realtime = new Realtime(this);