edmaxlabs-core 1.2.1 → 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.d.cts CHANGED
@@ -70,7 +70,7 @@ declare class Authentication {
70
70
  onChange: (data: Credentials) => void;
71
71
  onDeleted?: () => void;
72
72
  onSignOut?: () => void;
73
- }) => void;
73
+ }) => () => void;
74
74
  createUserWithEmailAndPassword: ({ email, password, }: {
75
75
  email: string;
76
76
  password: string;
package/dist/index.d.ts CHANGED
@@ -70,7 +70,7 @@ declare class Authentication {
70
70
  onChange: (data: Credentials) => void;
71
71
  onDeleted?: () => void;
72
72
  onSignOut?: () => void;
73
- }) => void;
73
+ }) => () => void;
74
74
  createUserWithEmailAndPassword: ({ email, password, }: {
75
75
  email: string;
76
76
  password: string;
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,