Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add a location special property #139

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ It is designed to have a low footprint on services code.
In fact, the Knifecycle API is aimed to allow to statically
build its services load/unload code once in production.

[See in context](./src/index.ts#L213-L232)
[See in context](./src/index.ts#L207-L226)



Expand All @@ -52,7 +52,7 @@ A service provider is full of state since its concern is
[encapsulate](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
your application global states.

[See in context](./src/index.ts#L234-L243)
[See in context](./src/index.ts#L228-L237)



Expand Down Expand Up @@ -92,7 +92,7 @@ The `?` flag indicates an optional dependency.
It allows to write generic services with fixed
dependencies and remap their name at injection time.

[See in context](./src/util.ts#L1372-L1381)
[See in context](./src/util.ts#L1462-L1471)



Expand Down Expand Up @@ -121,7 +121,7 @@ Initializers can be of three types:
instanciated once for all for each executions silos using
them (we will cover this topic later on).

[See in context](./src/index.ts#L332-L356)
[See in context](./src/index.ts#L326-L350)



Expand All @@ -137,7 +137,7 @@ Depending on your application design, you could run it
in only one execution silo or into several ones
according to the isolation level your wish to reach.

[See in context](./src/index.ts#L688-L698)
[See in context](./src/index.ts#L682-L692)



Expand All @@ -157,7 +157,7 @@ For the build to work, we need:
- the dependencies list you want to
initialize

[See in context](./src/build.ts#L37-L52)
[See in context](./src/build.ts#L39-L54)



Expand All @@ -173,5 +173,5 @@ Sadly TypeScript does not allow to add generic types
For more details, see:
https://stackoverflow.com/questions/64948037/generics-type-loss-while-infering/64950184#64950184

[See in context](./src/util.ts#L1442-L1453)
[See in context](./src/util.ts#L1532-L1543)

53 changes: 37 additions & 16 deletions src/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('buildInitializer', () => {
inject: [],
type: 'service',
name: 'dep1',
location: {
url: `file://services/dep1`,
exportName: 'default',
},
},
aProvider,
),
Expand All @@ -29,6 +33,10 @@ describe('buildInitializer', () => {
inject: ['dep1', 'NODE_ENV'],
type: 'provider',
name: 'dep2',
location: {
url: `file://services/dep2`,
exportName: 'default',
},
},
aProvider,
),
Expand All @@ -37,6 +45,10 @@ describe('buildInitializer', () => {
inject: ['dep5', 'dep2', 'dep1', '?depOpt'],
type: 'service',
name: 'dep3',
location: {
url: `file://services/dep3`,
exportName: 'default',
},
},
aProvider,
),
Expand All @@ -45,6 +57,10 @@ describe('buildInitializer', () => {
inject: ['dep5', 'dep2', 'dep1', '?depOpt'],
type: 'service',
name: 'dep4',
location: {
url: `file://services/dep4`,
exportName: 'default',
},
},
aProvider,
),
Expand All @@ -53,6 +69,10 @@ describe('buildInitializer', () => {
inject: ['$ready'],
type: 'service',
name: 'dep5',
location: {
url: `file://services/dep5`,
exportName: 'initDep5',
},
},
aProvider,
),
Expand All @@ -61,6 +81,10 @@ describe('buildInitializer', () => {
inject: [],
type: 'service',
name: 'dep6',
location: {
url: `file://services/dep6`,
exportName: 'aDep6',
},
},
aProvider,
),
Expand All @@ -75,10 +99,7 @@ describe('buildInitializer', () => {
async () => {
return async function $autoload(name) {
return mockedDepsHash[name]
? Promise.resolve({
path: `./services/${name}`,
initializer: mockedDepsHash[name],
})
? Promise.resolve(mockedDepsHash[name])
: Promise.reject(new YError('E_UNMATCHED_DEPENDENCY', name));
};
},
Expand Down Expand Up @@ -121,15 +142,15 @@ const $instance = {


// Definition batch #0
import initDep1 from './services/dep1';
import initDep1 from 'file://services/dep1';
const NODE_ENV = "development";

// Definition batch #1
import initDep5 from './services/dep5';
import initDep2 from './services/dep2';
import { initDep5 } from 'file://services/dep5';
import initDep2 from 'file://services/dep2';

// Definition batch #2
import initDep3 from './services/dep3';
import initDep3 from 'file://services/dep3';

export async function initialize(services = {}) {
const $fatalError = await initFatalError();
Expand Down Expand Up @@ -255,15 +276,15 @@ const $instance = {


// Definition batch #0
import initDep1 from './services/dep1';
import initDep6 from './services/dep6';
import initDep1 from 'file://services/dep1';
import { aDep6 as initDep6 } from 'file://services/dep6';
const NODE_ENV = "development";

// Definition batch #1
import initDep2 from './services/dep2';
import initDep2 from 'file://services/dep2';

// Definition batch #2
import initDep4 from './services/dep4';
import initDep4 from 'file://services/dep4';

export async function initialize(services = {}) {
const $fatalError = await initFatalError();
Expand Down Expand Up @@ -384,16 +405,16 @@ const $instance = {


// Definition batch #0
import initDep1 from './services/dep1';
import initDep1 from 'file://services/dep1';
const NODE_ENV = "development";
const $siloContext = undefined;

// Definition batch #1
import initDep5 from './services/dep5';
import initDep2 from './services/dep2';
import { initDep5 } from 'file://services/dep5';
import initDep2 from 'file://services/dep2';

// Definition batch #2
import initDep3 from './services/dep3';
import initDep3 from 'file://services/dep3';

export async function initialize(services = {}) {
const $fatalError = await initFatalError();
Expand Down
75 changes: 46 additions & 29 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {
parseDependencyDeclaration,
initializer,
READY,
location,
} from './util.js';
import { buildInitializationSequence } from './sequence.js';
import { FATAL_ERROR } from './fatalError.js';
import { DISPOSE } from './dispose.js';
import { type Overrides, type Autoloader } from './index.js';
import type {
DependencyDeclaration,
Initializer,
Dependencies,
import {
type DependencyDeclaration,
type Initializer,
type Dependencies,
type LocationInformation,
} from './util.js';
import { OVERRIDES, pickOverridenName } from './overrides.js';

Expand All @@ -26,7 +28,7 @@ type DependencyTreeNode = {
__inject: DependencyDeclaration[];
__type: 'provider' | 'constant' | 'service';
__initializerName: string;
__path: string;
__location: LocationInformation | 'managed' | 'no_location';
__parentsNames: string[];
};

Expand All @@ -51,13 +53,16 @@ For the build to work, we need:
initialize
*/

export default initializer(
{
name: 'buildInitializer',
type: 'service',
inject: [AUTOLOAD, OVERRIDES],
},
initInitializerBuilder,
export default location(
initializer(
{
name: 'buildInitializer',
type: 'service',
inject: [AUTOLOAD, OVERRIDES],
},
initInitializerBuilder,
),
import.meta.url,
);

/**
Expand Down Expand Up @@ -146,23 +151,36 @@ ${batches
(batch, index) => `
// Definition batch #${index}${batch
.map((name) => {
if (MANAGED_SERVICES.includes(name)) {
if (dependenciesHash[name].__location === 'managed') {
return '';
}
if (
'constant' ===
dependenciesHash[name].__initializer[SPECIAL_PROPS.TYPE]
) {
return `
if (dependenciesHash[name].__location === 'no_location') {
if (
'constant' ===
dependenciesHash[name].__initializer[SPECIAL_PROPS.TYPE] &&
dependenciesHash[name].__location === 'no_location'
) {
return `
const ${name} = ${JSON.stringify(
dependenciesHash[name].__initializer[SPECIAL_PROPS.VALUE],
null,
2,
)};`;
dependenciesHash[name].__initializer[SPECIAL_PROPS.VALUE],
null,
2,
)};`;
}
return `
// No location for "${name}" service
const ${name} = undefined;`;
}

return `
import ${dependenciesHash[name].__initializerName} from '${dependenciesHash[name].__path}';`;
import ${
dependenciesHash[name].__location.exportName === 'default'
? dependenciesHash[name].__initializerName
: dependenciesHash[name].__location.exportName ===
dependenciesHash[name].__initializerName
? `{ ${dependenciesHash[name].__initializerName} }`
: `{ ${dependenciesHash[name].__location.exportName} as ${dependenciesHash[name].__initializerName} }`
} from '${dependenciesHash[name].__location.url}';`;
})
.join('')}`,
)
Expand Down Expand Up @@ -268,22 +286,21 @@ async function buildDependencyTree(
mappedName,
]);

if(MANAGED_SERVICES.includes(finalName)) {
if (MANAGED_SERVICES.includes(finalName)) {
return {

__name: finalName,
__initializer: async() => {},
__initializer: async () => {},
__inject: [],
__type: 'constant',
__initializerName: 'init' + upperCaseFirst(finalName.slice(1)),
__path: `internal://managed/${finalName}`,
__location: 'managed',
__childNodes: [],
__parentsNames: [...parentsNames, finalName],
};
}

try {
const { path, initializer } = await $autoload(finalName);
const initializer = await $autoload(finalName);
const node: DependencyTreeNode = {
__name: finalName,
__initializer: initializer,
Expand All @@ -296,7 +313,7 @@ async function buildDependencyTree(
? initializer[SPECIAL_PROPS.TYPE]
: 'provider',
__initializerName: 'init' + upperCaseFirst(finalName),
__path: path,
__location: initializer[SPECIAL_PROPS.LOCATION] || 'no_location',
__childNodes: [],
__parentsNames: [...parentsNames, finalName],
};
Expand Down
6 changes: 5 additions & 1 deletion src/dispose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NO_PROVIDER,
SILO_CONTEXT,
SPECIAL_PROPS,
location,
parseDependencyDeclaration,
service,
} from './util.js';
Expand Down Expand Up @@ -151,4 +152,7 @@ async function initDispose({
};
}

export default service(initDispose, DISPOSE, [INSTANCE, SILO_CONTEXT]);
export default location(
service(initDispose, DISPOSE, [INSTANCE, SILO_CONTEXT]),
import.meta.url,
);
7 changes: 5 additions & 2 deletions src/fatalError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { printStackTrace } from 'yerror';
import { service } from './util.js';
import { location, service } from './util.js';
import initDebug from 'debug';

const debug = initDebug('knifecycle');
Expand Down Expand Up @@ -57,4 +57,7 @@ async function initFatalError(): Promise<FatalErrorService> {
};
}

export default service(initFatalError, FATAL_ERROR, [], true);
export default location(
service(initFatalError, FATAL_ERROR, [], true),
import.meta.url,
);
Loading
Loading