@posthog/browser 0.1.1 → 0.2.0

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.
Files changed (64) hide show
  1. package/ARCHITECTURE.md +28 -22
  2. package/README.md +65 -29
  3. package/dist/analytics-api.d.ts +26 -0
  4. package/dist/analytics-api.js +18 -0
  5. package/dist/analytics-api.mjs +1 -0
  6. package/dist/analytics-buffer.d.ts +3 -3
  7. package/dist/analytics-buffer.js +2 -161
  8. package/dist/analytics-buffer.mjs +2 -161
  9. package/dist/analytics-extension.d.ts +76 -0
  10. package/dist/analytics-extension.js +589 -0
  11. package/dist/analytics-extension.mjs +551 -0
  12. package/dist/analytics-internal.d.ts +32 -18
  13. package/dist/analytics-options.d.ts +7 -2
  14. package/dist/analytics-options.js +38 -1
  15. package/dist/analytics-options.mjs +17 -1
  16. package/dist/analytics.d.ts +4 -3
  17. package/dist/autocapture.d.ts +2 -2
  18. package/dist/autocapture.js +21 -10
  19. package/dist/autocapture.mjs +21 -10
  20. package/dist/automatic-analytics.d.ts +2 -2
  21. package/dist/browser-client.d.ts +44 -4
  22. package/dist/capture-properties.d.ts +9 -0
  23. package/dist/capture-properties.js +68 -0
  24. package/dist/capture-properties.mjs +30 -0
  25. package/dist/capture-v1.js +7 -19
  26. package/dist/capture-v1.mjs +1 -13
  27. package/dist/core.d.ts +7 -5
  28. package/dist/extensions/registry.d.ts +42 -7
  29. package/dist/extensions/registry.js +194 -37
  30. package/dist/extensions/registry.mjs +194 -37
  31. package/dist/flags-token.d.ts +4 -4
  32. package/dist/flags.js +15 -2
  33. package/dist/flags.mjs +15 -2
  34. package/dist/immutable.d.ts +1 -0
  35. package/dist/immutable.js +46 -0
  36. package/dist/immutable.mjs +8 -0
  37. package/dist/index.d.ts +7 -5
  38. package/dist/index.js +3 -67
  39. package/dist/index.mjs +3 -67
  40. package/dist/logs-internal.d.ts +2 -2
  41. package/dist/logs.js +5 -2
  42. package/dist/logs.mjs +5 -2
  43. package/dist/person-processing.d.ts +11 -0
  44. package/dist/person-processing.js +84 -0
  45. package/dist/person-processing.mjs +37 -0
  46. package/dist/posthog.d.ts +4 -3
  47. package/dist/posthog.js +153 -353
  48. package/dist/posthog.mjs +153 -353
  49. package/dist/root-composition.d.ts +11 -0
  50. package/dist/root-composition.js +138 -0
  51. package/dist/root-composition.mjs +100 -0
  52. package/dist/state.d.ts +1 -0
  53. package/dist/state.js +4 -0
  54. package/dist/state.mjs +4 -0
  55. package/dist/surveys-extension.js +11 -6
  56. package/dist/surveys-extension.mjs +11 -6
  57. package/dist/surveys-internal.d.ts +3 -2
  58. package/dist/types.d.ts +13 -19
  59. package/dist/utf8.d.ts +1 -0
  60. package/dist/utf8.js +52 -0
  61. package/dist/utf8.mjs +14 -0
  62. package/dist/version.js +1 -1
  63. package/dist/version.mjs +1 -1
  64. package/package.json +5 -5
@@ -28,62 +28,139 @@ var __webpack_require__ = {};
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
30
  class ExtensionRegistry {
31
- constructor(createClient, logger){
31
+ constructor(_createClient, _logger, _loaders = {}, _setup = (extension, host)=>extension.setup(host), _canInstall = ()=>true){
32
+ this._createClient = _createClient;
33
+ this._logger = _logger;
34
+ this._loaders = _loaders;
35
+ this._setup = _setup;
36
+ this._canInstall = _canInstall;
32
37
  this._records = new Map();
38
+ this._retired = new WeakSet();
33
39
  this._extensions = new Map();
40
+ this._shared = new Map();
41
+ this._sharedReservations = new Set();
42
+ this._pending = new Map();
43
+ this._dependencies = new Map();
44
+ this._views = new Set();
45
+ this._clients = new Map();
46
+ this._publications = new WeakMap();
34
47
  this._disposed = false;
35
- this._createClient = createClient;
36
- this._logger = logger;
48
+ }
49
+ expose(target) {
50
+ if (this._disposed || this._views.has(target)) return;
51
+ const publications = new Map();
52
+ this._publications.set(target, publications);
53
+ try {
54
+ for (const extension of this._extensions.values())this._publish(target, extension.name);
55
+ } catch (error) {
56
+ for (const name of publications.keys())this._removePublication(target, name);
57
+ this._publications.delete(target);
58
+ throw error;
59
+ }
60
+ this._views.add(target);
37
61
  }
38
62
  get(name) {
39
63
  return this._extensions.get(name);
40
64
  }
41
- async install(extension) {
42
- if (this._disposed) throw new Error('The extension registry is disposed');
43
- const { name } = extension;
44
- const bindings = [
45
- [
46
- name,
47
- extension
48
- ],
49
- ...Object.entries(extension.bindings ?? {})
50
- ];
51
- const names = new Set();
52
- for (const [key] of bindings){
53
- if (!key || names.has(key) || this._extensions.has(key)) throw new Error(`An extension named "${key}" is already installed or the name is invalid`);
54
- names.add(key);
65
+ getShared(name) {
66
+ return this._shared.get(name);
67
+ }
68
+ resolve(name) {
69
+ if (this._disposed || !this._canInstall()) return Promise.resolve(void 0);
70
+ const existing = this._existingResolution(name);
71
+ if (existing) return existing;
72
+ const load = Object.hasOwn(this._loaders, name) ? this._loaders[name] : void 0;
73
+ if (!load) return Promise.resolve(void 0);
74
+ const loading = Promise.resolve().then(()=>this._load(name, load));
75
+ this._pending.set(name, loading);
76
+ loading.then(()=>{
77
+ if (this._pending.get(name) === loading) this._pending.delete(name);
78
+ });
79
+ return loading;
80
+ }
81
+ async activate({ name, activate }) {
82
+ if (this._disposed || !this._canInstall() || this.get(name)) return;
83
+ try {
84
+ const host = this._client(name);
85
+ this._assertNameAvailable(name);
86
+ await activate(host, ()=>this.resolve(name));
87
+ } catch (error) {
88
+ this._logger.error(`Extension "${name}" activation failed`, error);
55
89
  }
56
- const record = [
57
- extension,
58
- false,
59
- [
60
- ...names
61
- ]
62
- ];
63
- this._records.set(name, record);
64
- for (const [key, target] of bindings)this._extensions.set(key, target);
90
+ }
91
+ async install(extension) {
92
+ const installation = this._reserveInstallation(extension);
93
+ const { name, record } = installation;
65
94
  try {
66
- await extension.setup(this._createClient(name));
67
- if (this._disposed || record[1]) throw new Error('The extension registry was disposed during setup');
95
+ const host = this._client(name);
96
+ this._assertNameAvailable(name);
97
+ await this._setup(extension, host);
98
+ this._publishInstallation(installation);
68
99
  } catch (error) {
69
- if (this._records.get(name) === record) this._removeRecord(record);
100
+ this._removeRecord(record);
101
+ record.finish(void 0);
70
102
  try {
71
103
  await this._disposeRecord(record);
72
104
  } catch (disposeError) {
73
105
  this._logger.error('Extension cleanup failed after setup failed', disposeError);
74
106
  }
75
107
  throw error;
108
+ } finally{
109
+ this._dependencies.delete(name);
76
110
  }
77
111
  }
112
+ _existingResolution(name) {
113
+ const installed = this.get(name);
114
+ if (installed) return Promise.resolve(installed);
115
+ return this._pending.get(name) ?? this._records.get(name)?.ready;
116
+ }
117
+ _reserveInstallation(extension) {
118
+ if (this._disposed) throw new Error('The extension registry is disposed');
119
+ if (this._retired.has(extension)) throw new Error('The extension instance has already been disposed');
120
+ const { name } = extension;
121
+ if (!name || this._records.has(name)) throw new Error(`An extension named "${name}" is already installed or the name is invalid`);
122
+ this._assertNameAvailable(name);
123
+ const bindings = this._bindingEntries(extension);
124
+ let finish;
125
+ const record = {
126
+ extension,
127
+ disposed: false,
128
+ sharedNames: bindings.map(([key])=>key),
129
+ ready: new Promise((resolve)=>{
130
+ finish = resolve;
131
+ }),
132
+ finish: (value)=>finish(value)
133
+ };
134
+ this._records.set(name, record);
135
+ for (const [key] of bindings)this._sharedReservations.add(key);
136
+ return {
137
+ name,
138
+ bindings,
139
+ record
140
+ };
141
+ }
142
+ _bindingEntries(extension) {
143
+ const bindings = Object.entries(extension.bindings ?? {});
144
+ for (const [key] of bindings)if (!key || this._sharedReservations.has(key)) throw new Error(`A shared extension named "${key}" is already installed or the name is invalid`);
145
+ return bindings;
146
+ }
147
+ _publishInstallation({ name, bindings, record }) {
148
+ if (this._disposed || record.disposed || !this._canInstall()) throw new Error('Extension installation stopped during setup');
149
+ this._extensions.set(name, record.extension);
150
+ for (const view of this._views)this._publish(view, name);
151
+ for (const [key, target] of bindings)this._shared.set(key, target);
152
+ record.finish(record.extension);
153
+ }
78
154
  async rollback(extension) {
79
155
  const record = this._records.get(extension.name);
80
- if (record?.[0] === extension) {
156
+ if (record?.extension === extension) {
81
157
  this._removeRecord(record);
158
+ record.finish(void 0);
82
159
  await this._disposeRecord(record);
83
160
  }
84
161
  }
85
162
  async flush(reason = 'flush') {
86
- await Promise.all(Array.from(this._records.values()).map(async ([extension])=>{
163
+ await Promise.all(Array.from(this._extensions.values()).map(async (extension)=>{
87
164
  try {
88
165
  await extension.flush?.(reason);
89
166
  } catch (error) {
@@ -96,23 +173,103 @@ class ExtensionRegistry {
96
173
  this._disposed = true;
97
174
  const records = Array.from(this._records.values()).reverse();
98
175
  this._records.clear();
176
+ for (const name of this._extensions.keys())this._unpublish(name);
99
177
  this._extensions.clear();
178
+ this._shared.clear();
179
+ this._sharedReservations.clear();
180
+ this._dependencies.clear();
181
+ this._views.clear();
182
+ this._clients.clear();
100
183
  await Promise.all(records.map(async (record)=>{
184
+ record.finish(void 0);
101
185
  try {
102
186
  await this._disposeRecord(record);
103
187
  } catch (error) {
104
- this._logger.error(`Extension "${record[0].name}" cleanup failed`, error);
188
+ this._logger.error(`Extension "${record.extension.name}" cleanup failed`, error);
105
189
  }
106
190
  }));
107
191
  }
192
+ _client(name) {
193
+ const existing = this._clients.get(name);
194
+ if (void 0 !== existing) return existing;
195
+ const host = this._createClient(name, (target)=>this._resolveDependency(name, target));
196
+ if (!this._disposed) this._clients.set(name, host);
197
+ return host;
198
+ }
199
+ async _load(name, load) {
200
+ try {
201
+ const host = this._client(name);
202
+ this._assertNameAvailable(name);
203
+ const extension = await load(host);
204
+ if (this._disposed || extension.name !== name) {
205
+ await extension.dispose?.();
206
+ if (!this._disposed) throw new Error(`Loader for "${name}" returned extension "${extension.name}"`);
207
+ return;
208
+ }
209
+ await this.install(extension);
210
+ return this.get(name);
211
+ } catch (error) {
212
+ this._logger.error(`Extension "${name}" loading failed`, error);
213
+ return;
214
+ } finally{
215
+ this._dependencies.delete(name);
216
+ }
217
+ }
218
+ _resolveDependency(owner, target) {
219
+ if (this.get(owner) || this.get(target)) return this.resolve(target);
220
+ if (owner === target || this._reaches(target, owner, new Set())) {
221
+ this._logger.error(`Circular extension dependency: "${owner}" -> "${target}"`, new Error('Circular extension dependency'));
222
+ return Promise.resolve(void 0);
223
+ }
224
+ let dependencies = this._dependencies.get(owner);
225
+ if (!dependencies) this._dependencies.set(owner, dependencies = new Set());
226
+ dependencies.add(target);
227
+ return this.resolve(target);
228
+ }
229
+ _reaches(from, target, visited) {
230
+ if (from === target) return true;
231
+ if (visited.has(from)) return false;
232
+ visited.add(from);
233
+ for (const next of this._dependencies.get(from) ?? [])if (this._reaches(next, target, visited)) return true;
234
+ return false;
235
+ }
108
236
  _removeRecord(record) {
109
- this._records.delete(record[0].name);
110
- for (const name of record[2])this._extensions.delete(name);
237
+ const name = record.extension.name;
238
+ if (this._records.get(name) !== record) return;
239
+ this._records.delete(name);
240
+ this._extensions.delete(name);
241
+ this._unpublish(name);
242
+ for (const key of record.sharedNames){
243
+ this._shared.delete(key);
244
+ this._sharedReservations.delete(key);
245
+ }
246
+ }
247
+ _assertNameAvailable(name) {
248
+ for (const view of this._views)if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
249
+ }
250
+ _publish(view, name) {
251
+ if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
252
+ const read = ()=>this.get(name);
253
+ Object.defineProperty(view, name, {
254
+ enumerable: true,
255
+ configurable: true,
256
+ get: read
257
+ });
258
+ this._publications.get(view)?.set(name, read);
259
+ }
260
+ _removePublication(view, name) {
261
+ const read = this._publications.get(view)?.get(name);
262
+ if (read && Object.getOwnPropertyDescriptor(view, name)?.get === read) Reflect.deleteProperty(view, name);
263
+ this._publications.get(view)?.delete(name);
264
+ }
265
+ _unpublish(name) {
266
+ for (const view of this._views)this._removePublication(view, name);
111
267
  }
112
268
  async _disposeRecord(record) {
113
- if (!record[1]) {
114
- record[1] = true;
115
- await record[0].dispose?.();
269
+ if (!record.disposed) {
270
+ record.disposed = true;
271
+ this._retired.add(record.extension);
272
+ await record.extension.dispose?.();
116
273
  }
117
274
  }
118
275
  }
@@ -1,60 +1,137 @@
1
1
  class ExtensionRegistry {
2
- constructor(createClient, logger){
2
+ constructor(_createClient, _logger, _loaders = {}, _setup = (extension, host)=>extension.setup(host), _canInstall = ()=>true){
3
+ this._createClient = _createClient;
4
+ this._logger = _logger;
5
+ this._loaders = _loaders;
6
+ this._setup = _setup;
7
+ this._canInstall = _canInstall;
3
8
  this._records = new Map();
9
+ this._retired = new WeakSet();
4
10
  this._extensions = new Map();
11
+ this._shared = new Map();
12
+ this._sharedReservations = new Set();
13
+ this._pending = new Map();
14
+ this._dependencies = new Map();
15
+ this._views = new Set();
16
+ this._clients = new Map();
17
+ this._publications = new WeakMap();
5
18
  this._disposed = false;
6
- this._createClient = createClient;
7
- this._logger = logger;
19
+ }
20
+ expose(target) {
21
+ if (this._disposed || this._views.has(target)) return;
22
+ const publications = new Map();
23
+ this._publications.set(target, publications);
24
+ try {
25
+ for (const extension of this._extensions.values())this._publish(target, extension.name);
26
+ } catch (error) {
27
+ for (const name of publications.keys())this._removePublication(target, name);
28
+ this._publications.delete(target);
29
+ throw error;
30
+ }
31
+ this._views.add(target);
8
32
  }
9
33
  get(name) {
10
34
  return this._extensions.get(name);
11
35
  }
12
- async install(extension) {
13
- if (this._disposed) throw new Error('The extension registry is disposed');
14
- const { name } = extension;
15
- const bindings = [
16
- [
17
- name,
18
- extension
19
- ],
20
- ...Object.entries(extension.bindings ?? {})
21
- ];
22
- const names = new Set();
23
- for (const [key] of bindings){
24
- if (!key || names.has(key) || this._extensions.has(key)) throw new Error(`An extension named "${key}" is already installed or the name is invalid`);
25
- names.add(key);
36
+ getShared(name) {
37
+ return this._shared.get(name);
38
+ }
39
+ resolve(name) {
40
+ if (this._disposed || !this._canInstall()) return Promise.resolve(void 0);
41
+ const existing = this._existingResolution(name);
42
+ if (existing) return existing;
43
+ const load = Object.hasOwn(this._loaders, name) ? this._loaders[name] : void 0;
44
+ if (!load) return Promise.resolve(void 0);
45
+ const loading = Promise.resolve().then(()=>this._load(name, load));
46
+ this._pending.set(name, loading);
47
+ loading.then(()=>{
48
+ if (this._pending.get(name) === loading) this._pending.delete(name);
49
+ });
50
+ return loading;
51
+ }
52
+ async activate({ name, activate }) {
53
+ if (this._disposed || !this._canInstall() || this.get(name)) return;
54
+ try {
55
+ const host = this._client(name);
56
+ this._assertNameAvailable(name);
57
+ await activate(host, ()=>this.resolve(name));
58
+ } catch (error) {
59
+ this._logger.error(`Extension "${name}" activation failed`, error);
26
60
  }
27
- const record = [
28
- extension,
29
- false,
30
- [
31
- ...names
32
- ]
33
- ];
34
- this._records.set(name, record);
35
- for (const [key, target] of bindings)this._extensions.set(key, target);
61
+ }
62
+ async install(extension) {
63
+ const installation = this._reserveInstallation(extension);
64
+ const { name, record } = installation;
36
65
  try {
37
- await extension.setup(this._createClient(name));
38
- if (this._disposed || record[1]) throw new Error('The extension registry was disposed during setup');
66
+ const host = this._client(name);
67
+ this._assertNameAvailable(name);
68
+ await this._setup(extension, host);
69
+ this._publishInstallation(installation);
39
70
  } catch (error) {
40
- if (this._records.get(name) === record) this._removeRecord(record);
71
+ this._removeRecord(record);
72
+ record.finish(void 0);
41
73
  try {
42
74
  await this._disposeRecord(record);
43
75
  } catch (disposeError) {
44
76
  this._logger.error('Extension cleanup failed after setup failed', disposeError);
45
77
  }
46
78
  throw error;
79
+ } finally{
80
+ this._dependencies.delete(name);
47
81
  }
48
82
  }
83
+ _existingResolution(name) {
84
+ const installed = this.get(name);
85
+ if (installed) return Promise.resolve(installed);
86
+ return this._pending.get(name) ?? this._records.get(name)?.ready;
87
+ }
88
+ _reserveInstallation(extension) {
89
+ if (this._disposed) throw new Error('The extension registry is disposed');
90
+ if (this._retired.has(extension)) throw new Error('The extension instance has already been disposed');
91
+ const { name } = extension;
92
+ if (!name || this._records.has(name)) throw new Error(`An extension named "${name}" is already installed or the name is invalid`);
93
+ this._assertNameAvailable(name);
94
+ const bindings = this._bindingEntries(extension);
95
+ let finish;
96
+ const record = {
97
+ extension,
98
+ disposed: false,
99
+ sharedNames: bindings.map(([key])=>key),
100
+ ready: new Promise((resolve)=>{
101
+ finish = resolve;
102
+ }),
103
+ finish: (value)=>finish(value)
104
+ };
105
+ this._records.set(name, record);
106
+ for (const [key] of bindings)this._sharedReservations.add(key);
107
+ return {
108
+ name,
109
+ bindings,
110
+ record
111
+ };
112
+ }
113
+ _bindingEntries(extension) {
114
+ const bindings = Object.entries(extension.bindings ?? {});
115
+ for (const [key] of bindings)if (!key || this._sharedReservations.has(key)) throw new Error(`A shared extension named "${key}" is already installed or the name is invalid`);
116
+ return bindings;
117
+ }
118
+ _publishInstallation({ name, bindings, record }) {
119
+ if (this._disposed || record.disposed || !this._canInstall()) throw new Error('Extension installation stopped during setup');
120
+ this._extensions.set(name, record.extension);
121
+ for (const view of this._views)this._publish(view, name);
122
+ for (const [key, target] of bindings)this._shared.set(key, target);
123
+ record.finish(record.extension);
124
+ }
49
125
  async rollback(extension) {
50
126
  const record = this._records.get(extension.name);
51
- if (record?.[0] === extension) {
127
+ if (record?.extension === extension) {
52
128
  this._removeRecord(record);
129
+ record.finish(void 0);
53
130
  await this._disposeRecord(record);
54
131
  }
55
132
  }
56
133
  async flush(reason = 'flush') {
57
- await Promise.all(Array.from(this._records.values()).map(async ([extension])=>{
134
+ await Promise.all(Array.from(this._extensions.values()).map(async (extension)=>{
58
135
  try {
59
136
  await extension.flush?.(reason);
60
137
  } catch (error) {
@@ -67,23 +144,103 @@ class ExtensionRegistry {
67
144
  this._disposed = true;
68
145
  const records = Array.from(this._records.values()).reverse();
69
146
  this._records.clear();
147
+ for (const name of this._extensions.keys())this._unpublish(name);
70
148
  this._extensions.clear();
149
+ this._shared.clear();
150
+ this._sharedReservations.clear();
151
+ this._dependencies.clear();
152
+ this._views.clear();
153
+ this._clients.clear();
71
154
  await Promise.all(records.map(async (record)=>{
155
+ record.finish(void 0);
72
156
  try {
73
157
  await this._disposeRecord(record);
74
158
  } catch (error) {
75
- this._logger.error(`Extension "${record[0].name}" cleanup failed`, error);
159
+ this._logger.error(`Extension "${record.extension.name}" cleanup failed`, error);
76
160
  }
77
161
  }));
78
162
  }
163
+ _client(name) {
164
+ const existing = this._clients.get(name);
165
+ if (void 0 !== existing) return existing;
166
+ const host = this._createClient(name, (target)=>this._resolveDependency(name, target));
167
+ if (!this._disposed) this._clients.set(name, host);
168
+ return host;
169
+ }
170
+ async _load(name, load) {
171
+ try {
172
+ const host = this._client(name);
173
+ this._assertNameAvailable(name);
174
+ const extension = await load(host);
175
+ if (this._disposed || extension.name !== name) {
176
+ await extension.dispose?.();
177
+ if (!this._disposed) throw new Error(`Loader for "${name}" returned extension "${extension.name}"`);
178
+ return;
179
+ }
180
+ await this.install(extension);
181
+ return this.get(name);
182
+ } catch (error) {
183
+ this._logger.error(`Extension "${name}" loading failed`, error);
184
+ return;
185
+ } finally{
186
+ this._dependencies.delete(name);
187
+ }
188
+ }
189
+ _resolveDependency(owner, target) {
190
+ if (this.get(owner) || this.get(target)) return this.resolve(target);
191
+ if (owner === target || this._reaches(target, owner, new Set())) {
192
+ this._logger.error(`Circular extension dependency: "${owner}" -> "${target}"`, new Error('Circular extension dependency'));
193
+ return Promise.resolve(void 0);
194
+ }
195
+ let dependencies = this._dependencies.get(owner);
196
+ if (!dependencies) this._dependencies.set(owner, dependencies = new Set());
197
+ dependencies.add(target);
198
+ return this.resolve(target);
199
+ }
200
+ _reaches(from, target, visited) {
201
+ if (from === target) return true;
202
+ if (visited.has(from)) return false;
203
+ visited.add(from);
204
+ for (const next of this._dependencies.get(from) ?? [])if (this._reaches(next, target, visited)) return true;
205
+ return false;
206
+ }
79
207
  _removeRecord(record) {
80
- this._records.delete(record[0].name);
81
- for (const name of record[2])this._extensions.delete(name);
208
+ const name = record.extension.name;
209
+ if (this._records.get(name) !== record) return;
210
+ this._records.delete(name);
211
+ this._extensions.delete(name);
212
+ this._unpublish(name);
213
+ for (const key of record.sharedNames){
214
+ this._shared.delete(key);
215
+ this._sharedReservations.delete(key);
216
+ }
217
+ }
218
+ _assertNameAvailable(name) {
219
+ for (const view of this._views)if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
220
+ }
221
+ _publish(view, name) {
222
+ if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
223
+ const read = ()=>this.get(name);
224
+ Object.defineProperty(view, name, {
225
+ enumerable: true,
226
+ configurable: true,
227
+ get: read
228
+ });
229
+ this._publications.get(view)?.set(name, read);
230
+ }
231
+ _removePublication(view, name) {
232
+ const read = this._publications.get(view)?.get(name);
233
+ if (read && Object.getOwnPropertyDescriptor(view, name)?.get === read) Reflect.deleteProperty(view, name);
234
+ this._publications.get(view)?.delete(name);
235
+ }
236
+ _unpublish(name) {
237
+ for (const view of this._views)this._removePublication(view, name);
82
238
  }
83
239
  async _disposeRecord(record) {
84
- if (!record[1]) {
85
- record[1] = true;
86
- await record[0].dispose?.();
240
+ if (!record.disposed) {
241
+ record.disposed = true;
242
+ this._retired.add(record.extension);
243
+ await record.extension.dispose?.();
87
244
  }
88
245
  }
89
246
  }
@@ -1,8 +1,8 @@
1
- import type { Disposable, Extension, ExtensionToken } from '@posthog/browser-common';
1
+ import type { Disposable, ExtensionToken } from '@posthog/browser-common';
2
2
  import type { FeatureFlagResult, FeatureFlagsReloadResult, FlagsCallback, JsonType } from './flags-options';
3
- import type { BrowserClient } from './browser-client';
4
- export interface FeatureFlags extends Extension {
5
- setup(client: BrowserClient): void | Promise<void>;
3
+ import type { IPostHogInternal, BrowserExtension } from './browser-client';
4
+ export interface FeatureFlags extends BrowserExtension {
5
+ setup(client: IPostHogInternal): void | Promise<void>;
6
6
  getFeatureFlag(key: string): FeatureFlagResult | undefined;
7
7
  /** Refresh remotely and await its outcome. Calls during an active evaluation wait for a follow-up request. */
8
8
  reloadFeatureFlags(): Promise<FeatureFlagsReloadResult>;
package/dist/flags.js CHANGED
@@ -63,6 +63,7 @@ const flags = (options = {})=>{
63
63
  const extension = {
64
64
  name: external_flags_token_js_namespaceObject.FeatureFlagsExtension,
65
65
  bindings: {
66
+ featureFlags: shared,
66
67
  [extension_tokens_namespaceObject.FeatureFlagsCommonExtension]: shared
67
68
  },
68
69
  getSurveyContext: ()=>({
@@ -71,17 +72,25 @@ const flags = (options = {})=>{
71
72
  }),
72
73
  setup: async (value)=>{
73
74
  client = value;
74
- await shared.setup(value);
75
+ await value.resolveExtension('analytics');
75
76
  if (disposed) return;
76
- subscriptions.push(value.onIdentify(({ distinctId, previousDistinctId, wasIdentified, set, setOnce })=>{
77
+ await shared.setup(value.sharedClient);
78
+ if (disposed) return;
79
+ subscriptions.push(value.onIdentify(({ distinctId, previousDistinctId, wasIdentified })=>{
77
80
  if (distinctId !== previousDistinctId) {
78
81
  shared.resetFlagCallReported();
79
82
  if (!wasIdentified) shared.setAnonymousDistinctId(previousDistinctId);
80
83
  }
84
+ shared.reloadFeatureFlags();
85
+ }));
86
+ subscriptions.push(value.onChangeProperties(({ set, setOnce, unset })=>{
81
87
  if (set || setOnce) shared.setPersonPropertiesForFlags({
82
88
  $set: set ?? {},
83
89
  $set_once: setOnce ?? {}
84
90
  }, false);
91
+ if (unset) shared.unsetPersonPropertiesForFlags([
92
+ ...unset
93
+ ], false);
85
94
  shared.reloadFeatureFlags();
86
95
  }));
87
96
  subscriptions.push(value.onGroup(({ type, changed, properties })=>{
@@ -91,6 +100,10 @@ const flags = (options = {})=>{
91
100
  }, false);
92
101
  shared.reloadFeatureFlags();
93
102
  }));
103
+ subscriptions.push(value.onGroupsReset(()=>{
104
+ shared.resetGroupPropertiesForFlags();
105
+ shared.reloadFeatureFlags();
106
+ }));
94
107
  subscriptions.push(value.onEvent(()=>shared.sendFlagCallsPendingCapture()));
95
108
  subscriptions.push(value.onReset(()=>{
96
109
  shared.reset();
package/dist/flags.mjs CHANGED
@@ -30,6 +30,7 @@ const flags = (options = {})=>{
30
30
  const extension = {
31
31
  name: FeatureFlagsExtension,
32
32
  bindings: {
33
+ featureFlags: shared,
33
34
  [FeatureFlagsCommonExtension]: shared
34
35
  },
35
36
  getSurveyContext: ()=>({
@@ -38,17 +39,25 @@ const flags = (options = {})=>{
38
39
  }),
39
40
  setup: async (value)=>{
40
41
  client = value;
41
- await shared.setup(value);
42
+ await value.resolveExtension('analytics');
42
43
  if (disposed) return;
43
- subscriptions.push(value.onIdentify(({ distinctId, previousDistinctId, wasIdentified, set, setOnce })=>{
44
+ await shared.setup(value.sharedClient);
45
+ if (disposed) return;
46
+ subscriptions.push(value.onIdentify(({ distinctId, previousDistinctId, wasIdentified })=>{
44
47
  if (distinctId !== previousDistinctId) {
45
48
  shared.resetFlagCallReported();
46
49
  if (!wasIdentified) shared.setAnonymousDistinctId(previousDistinctId);
47
50
  }
51
+ shared.reloadFeatureFlags();
52
+ }));
53
+ subscriptions.push(value.onChangeProperties(({ set, setOnce, unset })=>{
48
54
  if (set || setOnce) shared.setPersonPropertiesForFlags({
49
55
  $set: set ?? {},
50
56
  $set_once: setOnce ?? {}
51
57
  }, false);
58
+ if (unset) shared.unsetPersonPropertiesForFlags([
59
+ ...unset
60
+ ], false);
52
61
  shared.reloadFeatureFlags();
53
62
  }));
54
63
  subscriptions.push(value.onGroup(({ type, changed, properties })=>{
@@ -58,6 +67,10 @@ const flags = (options = {})=>{
58
67
  }, false);
59
68
  shared.reloadFeatureFlags();
60
69
  }));
70
+ subscriptions.push(value.onGroupsReset(()=>{
71
+ shared.resetGroupPropertiesForFlags();
72
+ shared.reloadFeatureFlags();
73
+ }));
61
74
  subscriptions.push(value.onEvent(()=>shared.sendFlagCallsPendingCapture()));
62
75
  subscriptions.push(value.onReset(()=>{
63
76
  shared.reset();
@@ -0,0 +1 @@
1
+ export declare const deepFreeze: <T>(value: T) => T;