edmaxlabs-core 1.2.0 → 1.2.3

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,
@@ -268,7 +300,7 @@ var Authentication = class {
268
300
  });
269
301
  return;
270
302
  };
271
- this.rules = async (path, oparation, context) => {
303
+ this.rules = async (path, context) => {
272
304
  const res = await new HttpsRequest({
273
305
  method: "POST" /* POST */,
274
306
  endpoint: this.client.getBaseUrl + "/auth/rules/verify",
@@ -277,9 +309,7 @@ var Authentication = class {
277
309
  },
278
310
  body: {
279
311
  path,
280
- op: oparation,
281
- context,
282
- hello: "world"
312
+ context
283
313
  }
284
314
  }).sendRequest();
285
315
  return res;