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.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,
@@ -237,7 +269,7 @@ var Authentication = class {
237
269
  });
238
270
  return;
239
271
  };
240
- this.rules = async (path, oparation, context) => {
272
+ this.rules = async (path, context) => {
241
273
  const res = await new HttpsRequest({
242
274
  method: "POST" /* POST */,
243
275
  endpoint: this.client.getBaseUrl + "/auth/rules/verify",
@@ -246,9 +278,7 @@ var Authentication = class {
246
278
  },
247
279
  body: {
248
280
  path,
249
- op: oparation,
250
- context,
251
- hello: "world"
281
+ context
252
282
  }
253
283
  }).sendRequest();
254
284
  return res;