@posthog/browser 0.4.2 → 0.5.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.
- package/ARCHITECTURE.md +34 -46
- package/README.md +56 -63
- package/dist/composition/npm/core.d.ts +2 -3
- package/dist/composition/npm/core.js +2 -1
- package/dist/composition/npm/core.mjs +2 -1
- package/dist/composition/npm/index.d.ts +3 -4
- package/dist/composition/npm/index.js +2 -1
- package/dist/composition/npm/index.mjs +2 -1
- package/dist/composition/npm/root-composition.d.ts +4 -4
- package/dist/composition/npm/root-composition.js +0 -16
- package/dist/composition/npm/root-composition.mjs +0 -16
- package/dist/core/context/browser-client.d.ts +4 -35
- package/dist/core/extensions/registry.d.ts +15 -36
- package/dist/core/extensions/registry.js +67 -198
- package/dist/core/extensions/registry.mjs +67 -198
- package/dist/core/posthog.d.ts +3 -2
- package/dist/core/posthog.js +104 -161
- package/dist/core/posthog.mjs +105 -162
- package/dist/core/state/state.d.ts +1 -0
- package/dist/core/state/state.js +5 -2
- package/dist/core/state/state.mjs +3 -3
- package/dist/core/types.d.ts +50 -12
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +76 -0
- package/dist/products/analytics/analytics-core.js +472 -0
- package/dist/products/analytics/analytics-core.mjs +425 -0
- package/dist/products/analytics/analytics-internal.d.ts +14 -36
- package/dist/products/analytics/analytics-internal.js +2 -10
- package/dist/products/analytics/analytics-internal.mjs +1 -3
- package/dist/products/analytics/analytics-options.d.ts +0 -9
- package/dist/products/analytics/analytics-options.js +1 -38
- package/dist/products/analytics/analytics-options.mjs +1 -17
- package/dist/products/analytics/groups.d.ts +8 -0
- package/dist/products/analytics/groups.js +84 -0
- package/dist/products/analytics/groups.mjs +40 -0
- package/dist/products/analytics/person.d.ts +11 -0
- package/dist/products/analytics/person.js +91 -0
- package/dist/products/analytics/person.mjs +41 -0
- package/dist/products/autocapture/autocapture.js +2 -6
- package/dist/products/autocapture/autocapture.mjs +2 -6
- package/dist/products/flags/flags-internal.d.ts +1 -2
- package/dist/products/flags/flags-token.d.ts +3 -2
- package/dist/products/flags/flags.js +2 -7
- package/dist/products/flags/flags.mjs +2 -7
- package/dist/products/logs/logs.js +2 -5
- package/dist/products/logs/logs.mjs +2 -5
- package/dist/products/surveys/surveys-extension.js +4 -21
- package/dist/products/surveys/surveys-extension.mjs +4 -21
- package/package.json +4 -9
- package/dist/products/analytics/analytics-api.d.ts +0 -32
- package/dist/products/analytics/analytics-api.js +0 -18
- package/dist/products/analytics/analytics-api.mjs +0 -1
- package/dist/products/analytics/analytics-buffer.d.ts +0 -4
- package/dist/products/analytics/analytics-buffer.js +0 -41
- package/dist/products/analytics/analytics-buffer.mjs +0 -3
- package/dist/products/analytics/analytics-extension.d.ts +0 -87
- package/dist/products/analytics/analytics-extension.js +0 -649
- package/dist/products/analytics/analytics-extension.mjs +0 -611
- package/dist/products/analytics/analytics.d.ts +0 -6
- package/dist/products/analytics/analytics.js +0 -42
- package/dist/products/analytics/analytics.mjs +0 -4
- package/dist/products/analytics/automatic-analytics.d.ts +0 -3
- package/dist/products/analytics/automatic-analytics.js +0 -44
- package/dist/products/analytics/automatic-analytics.mjs +0 -6
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createPostHogCore } from "../../core/posthog.mjs";
|
|
2
|
+
import { createAnalyticsDelivery } from "../../products/analytics/delivery/analytics-delivery.mjs";
|
|
2
3
|
import { createProductComposition } from "./root-composition.mjs";
|
|
3
4
|
const createPostHog = async (options)=>{
|
|
4
5
|
const { extensions, loaders, automatic, loadingErrors } = createProductComposition(options);
|
|
5
|
-
const client = await createPostHogCore(options, extensions, loaders, automatic);
|
|
6
|
+
const client = await createPostHogCore(options, createAnalyticsDelivery, extensions, loaders, automatic);
|
|
6
7
|
for (const [label, error] of loadingErrors.reverse())client.logger.error(`Automatic ${label} loading failed`, error);
|
|
7
8
|
return client;
|
|
8
9
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { PostHogOptions } from '../../core/types';
|
|
1
|
+
import type { BrowserExtension } from '../../core/context/browser-client';
|
|
2
|
+
import type { IPostHog, PostHogOptions } from '../../core/types';
|
|
3
3
|
import type { AutomaticExtension, ExtensionLoader } from '../../core/extensions/registry';
|
|
4
4
|
interface ProductComposition {
|
|
5
5
|
extensions: BrowserExtension[];
|
|
6
|
-
loaders: Record<string, ExtensionLoader<
|
|
7
|
-
automatic: AutomaticExtension<
|
|
6
|
+
loaders: Record<string, ExtensionLoader<IPostHog>>;
|
|
7
|
+
automatic: AutomaticExtension<IPostHog>[];
|
|
8
8
|
loadingErrors: Array<[string, unknown]>;
|
|
9
9
|
}
|
|
10
10
|
export declare const createProductComposition: (options: PostHogOptions) => ProductComposition;
|
|
@@ -35,7 +35,6 @@ const surveys_config_namespaceObject = require("@posthog/browser-common/surveys-
|
|
|
35
35
|
const autocapture_options_js_namespaceObject = require("../../products/autocapture/autocapture-options.js");
|
|
36
36
|
const surveys_options_js_namespaceObject = require("../../products/surveys/surveys-options.js");
|
|
37
37
|
const logs_options_js_namespaceObject = require("../../products/logs/logs-options.js");
|
|
38
|
-
const analytics_options_js_namespaceObject = require("../../products/analytics/analytics-options.js");
|
|
39
38
|
const activateFromSignals = (hasSaved, enabled)=>(host, acquire)=>{
|
|
40
39
|
let loading;
|
|
41
40
|
host.onRemoteConfig((result)=>{
|
|
@@ -61,16 +60,6 @@ const registerProduct = (composition, name, label, configure)=>{
|
|
|
61
60
|
]);
|
|
62
61
|
}
|
|
63
62
|
};
|
|
64
|
-
const configureAnalytics = (options, composition)=>{
|
|
65
|
-
let configuration;
|
|
66
|
-
if (composition.extensions.some((extension)=>'analytics' === extension.name)) configuration = false;
|
|
67
|
-
else try {
|
|
68
|
-
configuration = options?.analytics;
|
|
69
|
-
} catch {}
|
|
70
|
-
const snapshot = (0, analytics_options_js_namespaceObject.snapshotAutomaticAnalyticsOptions)(configuration || {});
|
|
71
|
-
if (false !== configuration) composition.loaders.analytics = ()=>import("../../products/analytics/automatic-analytics.js").then(({ analytics })=>analytics(snapshot));
|
|
72
|
-
return snapshot;
|
|
73
|
-
};
|
|
74
63
|
const configureFlags = (options)=>{
|
|
75
64
|
const configuration = options?.flags;
|
|
76
65
|
if (false === configuration) return;
|
|
@@ -118,11 +107,6 @@ const createProductComposition = (options)=>{
|
|
|
118
107
|
automatic: [],
|
|
119
108
|
loadingErrors: []
|
|
120
109
|
};
|
|
121
|
-
const analyticsOptions = configureAnalytics(options, composition);
|
|
122
|
-
if ('eager' === analyticsOptions.load) composition.automatic.push({
|
|
123
|
-
name: 'analytics',
|
|
124
|
-
activate: (_host, acquire)=>acquire()
|
|
125
|
-
});
|
|
126
110
|
registerProduct(composition, 'featureFlags', 'flags', ()=>configureFlags(options));
|
|
127
111
|
registerProduct(composition, 'logs', 'logs', ()=>configureLogs(options));
|
|
128
112
|
registerProduct(composition, 'surveys', 'surveys', ()=>configureSurveys(options));
|
|
@@ -3,7 +3,6 @@ import { SURVEYS } from "@posthog/browser-common/surveys-config";
|
|
|
3
3
|
import { snapshotAutocaptureOptions } from "../../products/autocapture/autocapture-options.mjs";
|
|
4
4
|
import { snapshotSurveysOptions } from "../../products/surveys/surveys-options.mjs";
|
|
5
5
|
import { snapshotLogsOptions } from "../../products/logs/logs-options.mjs";
|
|
6
|
-
import { snapshotAutomaticAnalyticsOptions } from "../../products/analytics/analytics-options.mjs";
|
|
7
6
|
const activateFromSignals = (hasSaved, enabled)=>(host, acquire)=>{
|
|
8
7
|
let loading;
|
|
9
8
|
host.onRemoteConfig((result)=>{
|
|
@@ -29,16 +28,6 @@ const registerProduct = (composition, name, label, configure)=>{
|
|
|
29
28
|
]);
|
|
30
29
|
}
|
|
31
30
|
};
|
|
32
|
-
const configureAnalytics = (options, composition)=>{
|
|
33
|
-
let configuration;
|
|
34
|
-
if (composition.extensions.some((extension)=>'analytics' === extension.name)) configuration = false;
|
|
35
|
-
else try {
|
|
36
|
-
configuration = options?.analytics;
|
|
37
|
-
} catch {}
|
|
38
|
-
const snapshot = snapshotAutomaticAnalyticsOptions(configuration || {});
|
|
39
|
-
if (false !== configuration) composition.loaders.analytics = ()=>import("../../products/analytics/automatic-analytics.mjs").then(({ analytics })=>analytics(snapshot));
|
|
40
|
-
return snapshot;
|
|
41
|
-
};
|
|
42
31
|
const configureFlags = (options)=>{
|
|
43
32
|
const configuration = options?.flags;
|
|
44
33
|
if (false === configuration) return;
|
|
@@ -86,11 +75,6 @@ const createProductComposition = (options)=>{
|
|
|
86
75
|
automatic: [],
|
|
87
76
|
loadingErrors: []
|
|
88
77
|
};
|
|
89
|
-
const analyticsOptions = configureAnalytics(options, composition);
|
|
90
|
-
if ('eager' === analyticsOptions.load) composition.automatic.push({
|
|
91
|
-
name: 'analytics',
|
|
92
|
-
activate: (_host, acquire)=>acquire()
|
|
93
|
-
});
|
|
94
78
|
registerProduct(composition, 'featureFlags', 'flags', ()=>configureFlags(options));
|
|
95
79
|
registerProduct(composition, 'logs', 'logs', ()=>configureLogs(options));
|
|
96
80
|
registerProduct(composition, 'surveys', 'surveys', ()=>configureSurveys(options));
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { IAnalyticsExtension } from '../../products/analytics/analytics-internal';
|
|
1
|
+
import type { DeepReadonly, Extension } from '@posthog/browser-common';
|
|
2
|
+
import type { IPostHog } from '../types';
|
|
4
3
|
import type { FeatureFlags } from '../../products/flags/flags-token';
|
|
5
|
-
import type { FlagsExtension } from '../../products/flags/flags-internal';
|
|
6
4
|
import type { LogsExtension } from '../../products/logs/logs-internal';
|
|
7
5
|
import type { SurveysExtension } from '../../products/surveys/surveys-internal';
|
|
8
6
|
import type { ConsentState } from '../state/state';
|
|
@@ -29,9 +27,9 @@ export interface ConsentChangeInfo {
|
|
|
29
27
|
readonly current: ConsentState;
|
|
30
28
|
readonly previous: ConsentState;
|
|
31
29
|
}
|
|
32
|
-
|
|
30
|
+
/** The shared extension contract, hosted by the browser client itself. */
|
|
31
|
+
export type BrowserExtension = Extension<IPostHog>;
|
|
33
32
|
export interface BrowserProducts {
|
|
34
|
-
analytics: IAnalytics;
|
|
35
33
|
featureFlags: FeatureFlags;
|
|
36
34
|
logs: LogsExtension;
|
|
37
35
|
surveys: SurveysExtension;
|
|
@@ -39,32 +37,3 @@ export interface BrowserProducts {
|
|
|
39
37
|
setElementSelectors(selectors: Set<string>): void;
|
|
40
38
|
};
|
|
41
39
|
}
|
|
42
|
-
export interface InternalBrowserProducts extends BrowserProducts {
|
|
43
|
-
analytics: IAnalyticsExtension;
|
|
44
|
-
featureFlags: FlagsExtension;
|
|
45
|
-
}
|
|
46
|
-
export type BrowserExtensionAccess<Products extends BrowserProducts = BrowserProducts> = Readonly<Partial<Products>> & {
|
|
47
|
-
resolveExtension<T extends BrowserExtension>(token: ExtensionToken<T>): Promise<T | undefined>;
|
|
48
|
-
resolveExtension<Name extends keyof Products>(name: Name): Promise<Products[Name] | undefined>;
|
|
49
|
-
resolveExtension<T extends BrowserExtension = BrowserExtension>(name: string): Promise<T | undefined>;
|
|
50
|
-
};
|
|
51
|
-
/** Shared Core capabilities, without application administration or product controls. */
|
|
52
|
-
export interface ICoreCapabilities extends Omit<Client, 'getExtension' | 'capture' | 'registerDynamicEventProperties' | 'isRateLimited' | 'onEvent'> {
|
|
53
|
-
/** Fires when consent changes, including changes observed from another tab. Does not replay. */
|
|
54
|
-
readonly onConsentChange: Listener<ConsentChangeInfo>;
|
|
55
|
-
/** Fires after identification, independently of capture consent. Does not replay. */
|
|
56
|
-
readonly onIdentify: Listener<IdentifyInfo>;
|
|
57
|
-
/** Fires after group membership or property updates, independently of capture consent. Does not replay. */
|
|
58
|
-
readonly onGroup: Listener<GroupInfo>;
|
|
59
|
-
/** Fires after group-only reset clears local membership. Does not replay. */
|
|
60
|
-
readonly onGroupsReset: Listener<void>;
|
|
61
|
-
/** Fires after local identity and persisted extension state are reset. Does not replay. */
|
|
62
|
-
readonly onReset: Listener<void>;
|
|
63
|
-
}
|
|
64
|
-
/** SDK-internal scoped view; shared implementations receive a separate Client adapter. */
|
|
65
|
-
export interface IPostHogInternal extends ICoreCapabilities, BrowserExtensionAccess<InternalBrowserProducts> {
|
|
66
|
-
readonly sharedClient: Client;
|
|
67
|
-
/** Local person-property changes, independently of capture consent. Does not replay. */
|
|
68
|
-
readonly onChangeProperties: Listener<ChangePropertiesInfo>;
|
|
69
|
-
readonly onEvent: Client['onEvent'];
|
|
70
|
-
}
|
|
@@ -1,51 +1,30 @@
|
|
|
1
1
|
import type { Client, Extension } from '@posthog/browser-common';
|
|
2
|
-
export type ExtensionLoader<Host
|
|
3
|
-
|
|
4
|
-
export interface AutomaticExtension<Host = Client> {
|
|
2
|
+
export type ExtensionLoader<Host> = () => Extension<Host> | Promise<Extension<Host>>;
|
|
3
|
+
export interface AutomaticExtension<Host> {
|
|
5
4
|
name: string;
|
|
6
5
|
activate(host: Host, acquire: () => Promise<Extension<Host> | undefined>): void | Promise<unknown>;
|
|
7
6
|
}
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Installs extensions on the one client, publishes each as a client property, and owns their lifecycle.
|
|
9
|
+
* Extension names and their bindings share one lookup namespace.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ExtensionRegistry<Host extends object> {
|
|
12
|
+
private readonly _host;
|
|
11
13
|
private readonly _logger;
|
|
12
14
|
private readonly _loaders;
|
|
13
|
-
private readonly _setup;
|
|
14
15
|
private readonly _canInstall;
|
|
15
|
-
private readonly
|
|
16
|
-
private readonly
|
|
17
|
-
private readonly
|
|
18
|
-
private readonly
|
|
19
|
-
private readonly _sharedReservations;
|
|
16
|
+
private readonly _installed;
|
|
17
|
+
private readonly _settingUp;
|
|
18
|
+
private readonly _lookup;
|
|
19
|
+
private readonly _reserved;
|
|
20
20
|
private readonly _pending;
|
|
21
|
-
private readonly
|
|
22
|
-
private readonly _views;
|
|
23
|
-
private readonly _clients;
|
|
24
|
-
private readonly _publications;
|
|
21
|
+
private readonly _retired;
|
|
25
22
|
private _disposed;
|
|
26
|
-
constructor(
|
|
27
|
-
|
|
28
|
-
get<T extends Extension<Host> = Extension<Host>>(name: string): T | undefined;
|
|
29
|
-
getShared<T extends Extension = Extension>(name: string): T | undefined;
|
|
23
|
+
constructor(_host: Host, _logger: Client['logger'], _loaders?: Readonly<Record<string, ExtensionLoader<Host>>>, _canInstall?: () => boolean);
|
|
24
|
+
get<T extends Extension = Extension>(name: string): T | undefined;
|
|
30
25
|
resolve(name: string): Promise<Extension<Host> | undefined>;
|
|
31
26
|
activate({ name, activate }: AutomaticExtension<Host>): Promise<void>;
|
|
32
27
|
install(extension: Extension<Host>): Promise<void>;
|
|
33
|
-
private _existingResolution;
|
|
34
|
-
private _reserveInstallation;
|
|
35
|
-
private _bindingEntries;
|
|
36
|
-
private _publishInstallation;
|
|
37
|
-
rollback(extension: Extension<Host>): Promise<void>;
|
|
38
28
|
flush(reason?: 'flush' | 'shutdown'): Promise<void>;
|
|
39
29
|
dispose(): Promise<void>;
|
|
40
|
-
private _client;
|
|
41
|
-
private _load;
|
|
42
|
-
private _resolveDependency;
|
|
43
|
-
private _reaches;
|
|
44
|
-
private _removeRecord;
|
|
45
|
-
private _assertNameAvailable;
|
|
46
|
-
private _publish;
|
|
47
|
-
private _removePublication;
|
|
48
|
-
private _unpublish;
|
|
49
|
-
private _disposeRecord;
|
|
50
30
|
}
|
|
51
|
-
export {};
|
|
@@ -28,139 +28,91 @@ var __webpack_require__ = {};
|
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
class ExtensionRegistry {
|
|
31
|
-
constructor(
|
|
32
|
-
this.
|
|
31
|
+
constructor(_host, _logger, _loaders = {}, _canInstall = ()=>true){
|
|
32
|
+
this._host = _host;
|
|
33
33
|
this._logger = _logger;
|
|
34
34
|
this._loaders = _loaders;
|
|
35
|
-
this._setup = _setup;
|
|
36
35
|
this._canInstall = _canInstall;
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
41
|
-
this._sharedReservations = new Set();
|
|
36
|
+
this._installed = new Map();
|
|
37
|
+
this._settingUp = new Set();
|
|
38
|
+
this._lookup = new Map();
|
|
39
|
+
this._reserved = new Set();
|
|
42
40
|
this._pending = new Map();
|
|
43
|
-
this.
|
|
44
|
-
this._views = new Set();
|
|
45
|
-
this._clients = new Map();
|
|
46
|
-
this._publications = new WeakMap();
|
|
41
|
+
this._retired = new WeakSet();
|
|
47
42
|
this._disposed = false;
|
|
48
43
|
}
|
|
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);
|
|
61
|
-
}
|
|
62
44
|
get(name) {
|
|
63
|
-
return this.
|
|
64
|
-
}
|
|
65
|
-
getShared(name) {
|
|
66
|
-
return this._shared.get(name);
|
|
45
|
+
return this._lookup.get(name);
|
|
67
46
|
}
|
|
68
47
|
resolve(name) {
|
|
69
48
|
if (this._disposed || !this._canInstall()) return Promise.resolve(void 0);
|
|
70
|
-
const
|
|
71
|
-
if (
|
|
49
|
+
const installed = this._installed.get(name);
|
|
50
|
+
if (installed) return Promise.resolve(installed);
|
|
51
|
+
const pending = this._pending.get(name);
|
|
52
|
+
if (pending) return pending;
|
|
72
53
|
const load = Object.hasOwn(this._loaders, name) ? this._loaders[name] : void 0;
|
|
73
54
|
if (!load) return Promise.resolve(void 0);
|
|
74
|
-
const loading = Promise.resolve().then(
|
|
55
|
+
const loading = Promise.resolve().then(load).then(async (extension)=>{
|
|
56
|
+
if (this._disposed || !this._canInstall()) return void await extension.dispose?.();
|
|
57
|
+
if (extension.name !== name) {
|
|
58
|
+
await extension.dispose?.();
|
|
59
|
+
throw new Error(`Loader for "${name}" returned extension "${extension.name}"`);
|
|
60
|
+
}
|
|
61
|
+
await this.install(extension);
|
|
62
|
+
return this._installed.get(name);
|
|
63
|
+
}).catch((error)=>{
|
|
64
|
+
this._logger.error(`Extension "${name}" loading failed`, error);
|
|
65
|
+
}).finally(()=>this._pending.delete(name));
|
|
75
66
|
this._pending.set(name, loading);
|
|
76
|
-
loading.then(()=>{
|
|
77
|
-
if (this._pending.get(name) === loading) this._pending.delete(name);
|
|
78
|
-
});
|
|
79
67
|
return loading;
|
|
80
68
|
}
|
|
81
69
|
async activate({ name, activate }) {
|
|
82
|
-
if (this._disposed || !this._canInstall() || this.
|
|
70
|
+
if (this._disposed || !this._canInstall() || this._installed.has(name)) return;
|
|
83
71
|
try {
|
|
84
|
-
|
|
85
|
-
this._assertNameAvailable(name);
|
|
86
|
-
await activate(host, ()=>this.resolve(name));
|
|
72
|
+
await activate(this._host, ()=>this.resolve(name));
|
|
87
73
|
} catch (error) {
|
|
88
74
|
this._logger.error(`Extension "${name}" activation failed`, error);
|
|
89
75
|
}
|
|
90
76
|
}
|
|
91
77
|
async install(extension) {
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
if (this._disposed) throw new Error('The extension registry is disposed');
|
|
79
|
+
if (this._retired.has(extension)) throw new Error('The extension instance has already been disposed');
|
|
80
|
+
const names = [
|
|
81
|
+
extension.name,
|
|
82
|
+
...Object.keys(extension.bindings ?? {})
|
|
83
|
+
];
|
|
84
|
+
for (const key of names)if (!key || this._lookup.has(key) || this._reserved.has(key) || key in this._host) throw new Error(`Extension name "${key}" is invalid or already in use`);
|
|
85
|
+
for (const key of names)this._reserved.add(key);
|
|
86
|
+
this._settingUp.add(extension);
|
|
94
87
|
try {
|
|
95
|
-
|
|
96
|
-
this.
|
|
97
|
-
await this._setup(extension, host);
|
|
98
|
-
this._publishInstallation(installation);
|
|
88
|
+
await extension.setup(this._host);
|
|
89
|
+
if (this._disposed || !this._canInstall()) throw new Error('Extension installation stopped during setup');
|
|
99
90
|
} catch (error) {
|
|
100
|
-
this.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
91
|
+
if (!this._retired.has(extension)) {
|
|
92
|
+
this._retired.add(extension);
|
|
93
|
+
try {
|
|
94
|
+
await extension.dispose?.();
|
|
95
|
+
} catch (disposeError) {
|
|
96
|
+
this._logger.error('Extension cleanup failed after setup failed', disposeError);
|
|
97
|
+
}
|
|
106
98
|
}
|
|
107
99
|
throw error;
|
|
108
100
|
} finally{
|
|
109
|
-
this.
|
|
101
|
+
this._settingUp.delete(extension);
|
|
102
|
+
for (const key of names)this._reserved.delete(key);
|
|
110
103
|
}
|
|
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
104
|
const { name } = extension;
|
|
121
|
-
|
|
122
|
-
this.
|
|
123
|
-
const bindings
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
}
|
|
154
|
-
async rollback(extension) {
|
|
155
|
-
const record = this._records.get(extension.name);
|
|
156
|
-
if (record?.extension === extension) {
|
|
157
|
-
this._removeRecord(record);
|
|
158
|
-
record.finish(void 0);
|
|
159
|
-
await this._disposeRecord(record);
|
|
160
|
-
}
|
|
105
|
+
this._installed.set(name, extension);
|
|
106
|
+
this._lookup.set(name, extension);
|
|
107
|
+
for (const [key, target] of Object.entries(extension.bindings ?? {}))this._lookup.set(key, target);
|
|
108
|
+
Object.defineProperty(this._host, name, {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
configurable: true,
|
|
111
|
+
get: ()=>this._installed.get(name)
|
|
112
|
+
});
|
|
161
113
|
}
|
|
162
114
|
async flush(reason = 'flush') {
|
|
163
|
-
await Promise.all(Array.from(this.
|
|
115
|
+
await Promise.all(Array.from(this._installed.values()).map(async (extension)=>{
|
|
164
116
|
try {
|
|
165
117
|
await extension.flush?.(reason);
|
|
166
118
|
} catch (error) {
|
|
@@ -171,107 +123,24 @@ class ExtensionRegistry {
|
|
|
171
123
|
async dispose() {
|
|
172
124
|
if (this._disposed) return;
|
|
173
125
|
this._disposed = true;
|
|
174
|
-
const
|
|
175
|
-
this.
|
|
176
|
-
|
|
177
|
-
this.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
126
|
+
const installed = Array.from(this._installed.values()).reverse();
|
|
127
|
+
for (const { name } of installed)Reflect.deleteProperty(this._host, name);
|
|
128
|
+
this._installed.clear();
|
|
129
|
+
this._lookup.clear();
|
|
130
|
+
const extensions = [
|
|
131
|
+
...installed,
|
|
132
|
+
...this._settingUp
|
|
133
|
+
];
|
|
134
|
+
await Promise.all(extensions.map(async (extension)=>{
|
|
135
|
+
if (this._retired.has(extension)) return;
|
|
136
|
+
this._retired.add(extension);
|
|
185
137
|
try {
|
|
186
|
-
await
|
|
138
|
+
await extension.dispose?.();
|
|
187
139
|
} catch (error) {
|
|
188
|
-
this._logger.error(`Extension "${
|
|
140
|
+
this._logger.error(`Extension "${extension.name}" cleanup failed`, error);
|
|
189
141
|
}
|
|
190
142
|
}));
|
|
191
143
|
}
|
|
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
|
-
}
|
|
236
|
-
_removeRecord(record) {
|
|
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);
|
|
267
|
-
}
|
|
268
|
-
async _disposeRecord(record) {
|
|
269
|
-
if (!record.disposed) {
|
|
270
|
-
record.disposed = true;
|
|
271
|
-
this._retired.add(record.extension);
|
|
272
|
-
await record.extension.dispose?.();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
144
|
}
|
|
276
145
|
__webpack_require__.d(__webpack_exports__, {
|
|
277
146
|
ExtensionRegistry: ()=>ExtensionRegistry
|