@wix/services-manager 0.2.6 → 0.2.9
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/Readme.md
CHANGED
|
@@ -80,6 +80,7 @@ In most cases, you can use the default `SignalsRegistry` implementation (create
|
|
|
80
80
|
- `getService<T>(serviceDefinition: ServiceDefinition<T>): T` - Returns the service instance for the given service definition, uses the `ServiceFactory` in order to create the service if it is not already created.
|
|
81
81
|
- `hasService(serviceDefinition: ServiceDefinition): boolean` - Returns true if the service is registered.
|
|
82
82
|
- `addService<T, TConfig>(serviceDefinition: ServiceDefinition<T>, factory: ServiceFactory<T, TConfig>, config: TConfig): void` - Registers a new service.
|
|
83
|
+
- `addServices(servicesRegistrar: ServicesRegistrar): void` - Registers and initializes multiple services at once from a `ServicesRegistrar` (as created by `createServicesMap`).
|
|
83
84
|
- `getSignalsRegistry(): SignalsRegistry` - Returns the signals registry instance.
|
|
84
85
|
|
|
85
86
|
#### Core Services
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { ServiceBinding,
|
|
1
|
+
import { ServiceBinding, ServicesManager, ThreadMode, ServicesRegistrar } from './types.js';
|
|
2
2
|
import { createSignalsRegistry, SignalsRegistry } from './core-services/signals/registry.js';
|
|
3
3
|
export { createSignalsRegistry, SignalsRegistry };
|
|
4
4
|
export declare function createServicesManager<M extends ThreadMode = ThreadMode.MAIN>(servicesBindings?: ServicesRegistrar, { signalsRegistry, }?: {
|
|
5
5
|
signalsRegistry?: SignalsRegistry;
|
|
6
6
|
auth?: any;
|
|
7
7
|
}): ServicesManager<M>;
|
|
8
|
-
export type ServicesRegistrar = {
|
|
9
|
-
registeredServices: ServiceBinding<any, any>[];
|
|
10
|
-
addService<T extends ServiceDefinition<any, any> = any, Impl extends ServiceFactory<T, any, M> = any, M extends ThreadMode = any>(definition: T, impl: Impl, config?: ServiceFactoryConfig<Impl, M>): ServicesRegistrar;
|
|
11
|
-
};
|
|
12
8
|
export declare function createServicesMap(registeredServices?: ServiceBinding<any, any>[]): ServicesRegistrar;
|
|
@@ -46,12 +46,44 @@ signalsRegistry = createSignalsRegistry(), } = {}) {
|
|
|
46
46
|
impl,
|
|
47
47
|
config: config || {},
|
|
48
48
|
});
|
|
49
|
+
// Immediately initialize the service if not already initialized
|
|
50
|
+
if (!initializedServices.has(definition.toString())) {
|
|
51
|
+
initializedServices.set(definition.toString(), impl({
|
|
52
|
+
config: config || {},
|
|
53
|
+
getService: manager.getService,
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
49
56
|
}
|
|
50
57
|
else {
|
|
51
58
|
// this needs meaningful error message
|
|
52
59
|
throw new Error(`Service ${definition.toString()} is already provided`);
|
|
53
60
|
}
|
|
54
61
|
},
|
|
62
|
+
addServices(servicesToAdd) {
|
|
63
|
+
// First, check for duplicates
|
|
64
|
+
servicesToAdd.registeredServices.forEach(({ definition }) => {
|
|
65
|
+
if (manager.hasService(definition)) {
|
|
66
|
+
throw new Error(`Service ${definition.toString()} is already provided`);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// Register all definitions first
|
|
70
|
+
servicesToAdd.registeredServices.forEach(({ definition, impl, config }) => {
|
|
71
|
+
bindingsByDefinition.set(definition.toString(), {
|
|
72
|
+
definition,
|
|
73
|
+
impl,
|
|
74
|
+
config: config || {},
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
// Then initialize all, but only if not already initialized
|
|
78
|
+
servicesToAdd.registeredServices.forEach(({ definition, impl, config }) => {
|
|
79
|
+
if (!initializedServices.has(definition.toString())) {
|
|
80
|
+
initializedServices.set(definition.toString(), impl({
|
|
81
|
+
config: config || {},
|
|
82
|
+
getService: manager.getService,
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
},
|
|
55
87
|
enableContext(context) {
|
|
56
88
|
let resolvedContext = wixContext;
|
|
57
89
|
if (context === 'global') {
|
|
@@ -69,11 +101,14 @@ signalsRegistry = createSignalsRegistry(), } = {}) {
|
|
|
69
101
|
return this;
|
|
70
102
|
},
|
|
71
103
|
};
|
|
104
|
+
// Then initialize all, but only if not already initialized
|
|
72
105
|
bindingsByDefinition.forEach(({ definition, impl, config }) => {
|
|
73
|
-
initializedServices.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
106
|
+
if (!initializedServices.has(definition.toString())) {
|
|
107
|
+
initializedServices.set(definition, impl({
|
|
108
|
+
config: config || {},
|
|
109
|
+
getService: manager.getService,
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
77
112
|
});
|
|
78
113
|
return manager;
|
|
79
114
|
}
|
package/build/types.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { SignalsRegistry } from './core-services/signals/registry.js';
|
|
2
2
|
export type * from '@wix/services-definitions';
|
|
3
|
-
import { GetService, ServiceFactory, ServiceFactoryConfig, ThreadMode, ServiceDefinition, NamedSignals } from '@wix/services-definitions';
|
|
3
|
+
import { GetService, ServiceFactory, ServiceFactoryConfig, ThreadMode, ServiceDefinition, NamedSignals, ServiceBinding } from '@wix/services-definitions';
|
|
4
|
+
export type ServicesRegistrar = {
|
|
5
|
+
registeredServices: ServiceBinding<any, any>[];
|
|
6
|
+
addService<T extends ServiceDefinition<any, any> = any, Impl extends ServiceFactory<T, any, M> = any, M extends ThreadMode = any>(definition: T, impl: Impl, config?: ServiceFactoryConfig<Impl, M>): ServicesRegistrar;
|
|
7
|
+
};
|
|
4
8
|
export type ServicesManager<M extends ThreadMode = ThreadMode.MAIN> = {
|
|
5
9
|
getService: GetService<M>;
|
|
6
10
|
getServiceSignals<T extends ServiceDefinition<any, any>>(definition: T): NamedSignals;
|
|
7
11
|
hasService<T extends ServiceDefinition<any, any>>(definition: T): boolean;
|
|
8
12
|
addService<T extends ServiceDefinition<any, any>, Impl extends ServiceFactory<T, any, ThreadMode.MAIN>>(definition: T, impl: Impl, config: ServiceFactoryConfig<Impl, ThreadMode.MAIN>): void;
|
|
9
13
|
addService<T extends ServiceDefinition<any, any>, Impl extends ServiceFactory<T, any, ThreadMode.REMOTE>>(definition: T, impl: Impl): void;
|
|
14
|
+
addServices(servicesBindings: ServicesRegistrar): void;
|
|
10
15
|
getSignalsRegistry(): SignalsRegistry;
|
|
11
16
|
enableContext(context: 'global' | 'module'): ServicesManager<M>;
|
|
12
17
|
};
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { ServiceBinding,
|
|
1
|
+
import { ServiceBinding, ServicesManager, ThreadMode, ServicesRegistrar } from './types.js';
|
|
2
2
|
import { createSignalsRegistry, SignalsRegistry } from './core-services/signals/registry.js';
|
|
3
3
|
export { createSignalsRegistry, SignalsRegistry };
|
|
4
4
|
export declare function createServicesManager<M extends ThreadMode = ThreadMode.MAIN>(servicesBindings?: ServicesRegistrar, { signalsRegistry, }?: {
|
|
5
5
|
signalsRegistry?: SignalsRegistry;
|
|
6
6
|
auth?: any;
|
|
7
7
|
}): ServicesManager<M>;
|
|
8
|
-
export type ServicesRegistrar = {
|
|
9
|
-
registeredServices: ServiceBinding<any, any>[];
|
|
10
|
-
addService<T extends ServiceDefinition<any, any> = any, Impl extends ServiceFactory<T, any, M> = any, M extends ThreadMode = any>(definition: T, impl: Impl, config?: ServiceFactoryConfig<Impl, M>): ServicesRegistrar;
|
|
11
|
-
};
|
|
12
8
|
export declare function createServicesMap(registeredServices?: ServiceBinding<any, any>[]): ServicesRegistrar;
|
|
@@ -52,12 +52,44 @@ signalsRegistry = (0, registry_js_1.createSignalsRegistry)(), } = {}) {
|
|
|
52
52
|
impl,
|
|
53
53
|
config: config || {},
|
|
54
54
|
});
|
|
55
|
+
// Immediately initialize the service if not already initialized
|
|
56
|
+
if (!initializedServices.has(definition.toString())) {
|
|
57
|
+
initializedServices.set(definition.toString(), impl({
|
|
58
|
+
config: config || {},
|
|
59
|
+
getService: manager.getService,
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
55
62
|
}
|
|
56
63
|
else {
|
|
57
64
|
// this needs meaningful error message
|
|
58
65
|
throw new Error(`Service ${definition.toString()} is already provided`);
|
|
59
66
|
}
|
|
60
67
|
},
|
|
68
|
+
addServices(servicesToAdd) {
|
|
69
|
+
// First, check for duplicates
|
|
70
|
+
servicesToAdd.registeredServices.forEach(({ definition }) => {
|
|
71
|
+
if (manager.hasService(definition)) {
|
|
72
|
+
throw new Error(`Service ${definition.toString()} is already provided`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
// Register all definitions first
|
|
76
|
+
servicesToAdd.registeredServices.forEach(({ definition, impl, config }) => {
|
|
77
|
+
bindingsByDefinition.set(definition.toString(), {
|
|
78
|
+
definition,
|
|
79
|
+
impl,
|
|
80
|
+
config: config || {},
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
// Then initialize all, but only if not already initialized
|
|
84
|
+
servicesToAdd.registeredServices.forEach(({ definition, impl, config }) => {
|
|
85
|
+
if (!initializedServices.has(definition.toString())) {
|
|
86
|
+
initializedServices.set(definition.toString(), impl({
|
|
87
|
+
config: config || {},
|
|
88
|
+
getService: manager.getService,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
},
|
|
61
93
|
enableContext(context) {
|
|
62
94
|
let resolvedContext = sdk_context_1.wixContext;
|
|
63
95
|
if (context === 'global') {
|
|
@@ -75,11 +107,14 @@ signalsRegistry = (0, registry_js_1.createSignalsRegistry)(), } = {}) {
|
|
|
75
107
|
return this;
|
|
76
108
|
},
|
|
77
109
|
};
|
|
110
|
+
// Then initialize all, but only if not already initialized
|
|
78
111
|
bindingsByDefinition.forEach(({ definition, impl, config }) => {
|
|
79
|
-
initializedServices.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
112
|
+
if (!initializedServices.has(definition.toString())) {
|
|
113
|
+
initializedServices.set(definition, impl({
|
|
114
|
+
config: config || {},
|
|
115
|
+
getService: manager.getService,
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
83
118
|
});
|
|
84
119
|
return manager;
|
|
85
120
|
}
|
package/cjs/build/types.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { SignalsRegistry } from './core-services/signals/registry.js';
|
|
2
2
|
export type * from '@wix/services-definitions';
|
|
3
|
-
import { GetService, ServiceFactory, ServiceFactoryConfig, ThreadMode, ServiceDefinition, NamedSignals } from '@wix/services-definitions';
|
|
3
|
+
import { GetService, ServiceFactory, ServiceFactoryConfig, ThreadMode, ServiceDefinition, NamedSignals, ServiceBinding } from '@wix/services-definitions';
|
|
4
|
+
export type ServicesRegistrar = {
|
|
5
|
+
registeredServices: ServiceBinding<any, any>[];
|
|
6
|
+
addService<T extends ServiceDefinition<any, any> = any, Impl extends ServiceFactory<T, any, M> = any, M extends ThreadMode = any>(definition: T, impl: Impl, config?: ServiceFactoryConfig<Impl, M>): ServicesRegistrar;
|
|
7
|
+
};
|
|
4
8
|
export type ServicesManager<M extends ThreadMode = ThreadMode.MAIN> = {
|
|
5
9
|
getService: GetService<M>;
|
|
6
10
|
getServiceSignals<T extends ServiceDefinition<any, any>>(definition: T): NamedSignals;
|
|
7
11
|
hasService<T extends ServiceDefinition<any, any>>(definition: T): boolean;
|
|
8
12
|
addService<T extends ServiceDefinition<any, any>, Impl extends ServiceFactory<T, any, ThreadMode.MAIN>>(definition: T, impl: Impl, config: ServiceFactoryConfig<Impl, ThreadMode.MAIN>): void;
|
|
9
13
|
addService<T extends ServiceDefinition<any, any>, Impl extends ServiceFactory<T, any, ThreadMode.REMOTE>>(definition: T, impl: Impl): void;
|
|
14
|
+
addServices(servicesBindings: ServicesRegistrar): void;
|
|
10
15
|
getSignalsRegistry(): SignalsRegistry;
|
|
11
16
|
enableContext(context: 'global' | 'module'): ServicesManager<M>;
|
|
12
17
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/services-manager",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./cjs/build/services-manager.js",
|
|
6
6
|
"exports": {
|
|
@@ -53,22 +53,22 @@
|
|
|
53
53
|
"*.{js,ts}": "yarn lint"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@preact/signals-core": "^1.
|
|
56
|
+
"@preact/signals-core": "^1.10.0",
|
|
57
57
|
"@wix/sdk-context": "0.0.1",
|
|
58
|
-
"@wix/services-definitions": "^0.1.
|
|
58
|
+
"@wix/services-definitions": "^0.1.3",
|
|
59
59
|
"comlink": "^4.4.2"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@playwright/test": "^1.
|
|
62
|
+
"@playwright/test": "^1.53.0",
|
|
63
63
|
"@types/is-ci": "^3.0.4",
|
|
64
|
-
"@types/node": "^20.
|
|
64
|
+
"@types/node": "^20.19.0",
|
|
65
65
|
"@vitest/ui": "^1.6.1",
|
|
66
66
|
"@vitest/web-worker": "^2.1.9",
|
|
67
67
|
"eslint": "^8.57.1",
|
|
68
68
|
"eslint-config-sdk": "0.0.0",
|
|
69
69
|
"is-ci": "^3.0.1",
|
|
70
70
|
"jsdom": "^22.1.0",
|
|
71
|
-
"msw": "^2.
|
|
71
|
+
"msw": "^2.10.2",
|
|
72
72
|
"typescript": "^5.8.3",
|
|
73
73
|
"vitest": "^1.6.1",
|
|
74
74
|
"vitest-teamcity-reporter": "^0.3.1"
|
|
@@ -90,5 +90,5 @@
|
|
|
90
90
|
]
|
|
91
91
|
}
|
|
92
92
|
},
|
|
93
|
-
"falconPackageHash": "
|
|
93
|
+
"falconPackageHash": "c4952a72d4f880ed8da42ddf28a40a5bfc7a92e00f0492c71a6da2e5"
|
|
94
94
|
}
|