botbuilder-dialogs-adaptive-runtime-core
Advanced tools
Comparing version
"use strict"; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
@@ -15,10 +38,6 @@ var t = {}; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ServiceCollection = void 0; | ||
const assert_1 = __importDefault(require("assert")); | ||
const assert_1 = __importStar(require("assert")); | ||
const dependency_graph_1 = require("dependency-graph"); | ||
const assert_2 = require("assert"); | ||
const util_1 = require("./util"); | ||
@@ -77,3 +96,3 @@ /** | ||
// Asserts factory is not undefined | ||
(0, assert_2.ok)(factory, 'illegal invocation with undefined factory'); | ||
(0, assert_1.ok)(factory, 'illegal invocation with undefined factory'); | ||
if (dependencies) { | ||
@@ -88,4 +107,2 @@ this.dependencies.set(key, dependencies); | ||
} | ||
// Note: we have done the type checking above, so disabling no-explicit-any is okay. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this.graph.addNode(key, factories.concat(factory)); | ||
@@ -100,3 +117,3 @@ return this; | ||
return this.addFactory(key, Array.isArray(depsOrFactory) ? depsOrFactory : [], (dependencies, value) => { | ||
(0, assert_2.ok)(value, `unable to create ${key}, initial value undefined`); | ||
(0, assert_1.ok)(value, `unable to create ${key}, initial value undefined`); | ||
return maybeFactory(dependencies, value); | ||
@@ -106,5 +123,5 @@ }); | ||
else { | ||
(0, assert_2.ok)(typeof depsOrFactory === 'function', 'illegal invocation with undefined factory'); | ||
(0, assert_1.ok)(typeof depsOrFactory === 'function', 'illegal invocation with undefined factory'); | ||
return this.addFactory(key, (value) => { | ||
(0, assert_2.ok)(value, `unable to create ${key}, initial value undefined`); | ||
(0, assert_1.ok)(value, `unable to create ${key}, initial value undefined`); | ||
return depsOrFactory(value); | ||
@@ -111,0 +128,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"author": "Microsoft Corp.", | ||
"version": "4.23.1-preview", | ||
"version": "4.23.2-dev1.preview", | ||
"license": "MIT", | ||
@@ -42,3 +42,3 @@ "description": "Bot Framework Adaptive Dialogs runtime core components", | ||
"depcheck": "depcheck --config ../../.depcheckrc --ignores dependency-graph", | ||
"lint": "eslint . --ext .js,.ts", | ||
"lint": "eslint . --config ../../eslint.config.cjs", | ||
"postbuild": "downlevel-dts lib _ts3.4/lib --checksum", | ||
@@ -45,0 +45,0 @@ "test": "nyc mocha", |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
import assert from 'assert'; | ||
import assert, { ok } from 'assert'; | ||
import { DepGraph } from 'dependency-graph'; | ||
import { ok } from 'assert'; | ||
import { stringify } from './util'; | ||
@@ -16,3 +15,3 @@ | ||
export type Factory<Type, Initial extends boolean> = ( | ||
initialValue: Initial extends true ? Type : Type | undefined | ||
initialValue: Initial extends true ? Type : Type | undefined, | ||
) => Type; | ||
@@ -30,3 +29,3 @@ | ||
dependencies: Dependencies, | ||
initialValue: Initial extends true ? Type : Type | undefined | ||
initialValue: Initial extends true ? Type : Type | undefined, | ||
) => Type; | ||
@@ -102,3 +101,3 @@ | ||
dependencies: string[], | ||
factory: DependencyFactory<InstanceType, Dependencies, false> | ||
factory: DependencyFactory<InstanceType, Dependencies, false>, | ||
): this; | ||
@@ -112,3 +111,3 @@ | ||
depsOrFactory: string[] | Factory<InstanceType, false>, | ||
maybeFactory?: DependencyFactory<InstanceType, Dependencies, false> | ||
maybeFactory?: DependencyFactory<InstanceType, Dependencies, false>, | ||
): this { | ||
@@ -136,4 +135,2 @@ const dependencies = Array.isArray(depsOrFactory) ? depsOrFactory : undefined; | ||
// Note: we have done the type checking above, so disabling no-explicit-any is okay. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this.graph.addNode(key, factories.concat(factory) as any); | ||
@@ -165,3 +162,3 @@ | ||
dependencies: string[], | ||
factory: DependencyFactory<InstanceType, Dependencies, true> | ||
factory: DependencyFactory<InstanceType, Dependencies, true>, | ||
): this; | ||
@@ -175,3 +172,3 @@ | ||
depsOrFactory: string[] | Factory<InstanceType, true>, | ||
maybeFactory?: DependencyFactory<InstanceType, Dependencies, true> | ||
maybeFactory?: DependencyFactory<InstanceType, Dependencies, true>, | ||
): this { | ||
@@ -186,3 +183,3 @@ if (maybeFactory) { | ||
return maybeFactory(dependencies, value); | ||
} | ||
}, | ||
); | ||
@@ -204,3 +201,3 @@ } else { | ||
generateNodes: () => string[], | ||
reuseServices: Record<string, unknown> = {} | ||
reuseServices: Record<string, unknown> = {}, | ||
): ReturnType { | ||
@@ -210,3 +207,3 @@ // Consume all dependencies and then reset so updating registrations without re-registering | ||
this.dependencies.forEach((dependencies, node) => | ||
dependencies.forEach((dependency) => this.graph.addDependency(node, stringify(dependency))) | ||
dependencies.forEach((dependency) => this.graph.addDependency(node, stringify(dependency))), | ||
); | ||
@@ -217,28 +214,34 @@ | ||
const services = nodes.reduce((services, service) => { | ||
// Extra precaution | ||
if (!this.graph.hasNode(service)) { | ||
return services; | ||
} | ||
const services = nodes.reduce( | ||
(services, service) => { | ||
// Extra precaution | ||
if (!this.graph.hasNode(service)) { | ||
return services; | ||
} | ||
// Helper to generate return value | ||
const assignValue = (value: unknown) => ({ | ||
...services, | ||
[service]: value, | ||
}); | ||
// Helper to generate return value | ||
const assignValue = (value: unknown) => ({ | ||
...services, | ||
[service]: value, | ||
}); | ||
// Optionally reuse existing service | ||
const reusedService = reuseServices[service]; | ||
if (reusedService !== undefined) { | ||
return assignValue(reusedService); | ||
} | ||
// Optionally reuse existing service | ||
const reusedService = reuseServices[service]; | ||
if (reusedService !== undefined) { | ||
return assignValue(reusedService); | ||
} | ||
// Each node stores a list of factory methods. | ||
const factories = this.graph.getNodeData(service); | ||
// Each node stores a list of factory methods. | ||
const factories = this.graph.getNodeData(service); | ||
// Produce the instance by reducing those factories, passing the instance along for composition. | ||
const instance = factories.reduce((value, factory) => factory(services, value), <unknown>services[service]); | ||
// Produce the instance by reducing those factories, passing the instance along for composition. | ||
const instance = factories.reduce( | ||
(value, factory) => factory(services, value), | ||
<unknown>services[service], | ||
); | ||
return assignValue(instance); | ||
}, <Record<string, unknown>>{}); | ||
return assignValue(instance); | ||
}, | ||
<Record<string, unknown>>{}, | ||
); | ||
@@ -268,3 +271,3 @@ // Cache results for subsequent invocations that may desire pre-constructed instances | ||
() => this.graph.dependenciesOf(key).concat(key), | ||
initialServices | ||
initialServices, | ||
); | ||
@@ -271,0 +274,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
45450
1.55%835
2.45%