@milkdown/ctx
Advanced tools
Comparing version 5.4.0 to 5.4.1
@@ -46,3 +46,10 @@ var __defProp = Object.defineProperty; | ||
}; | ||
return { getSlice, sliceMap }; | ||
const getSliceByName = (sliceName) => { | ||
const result = [...sliceMap.values()].find((x) => x.name === sliceName); | ||
if (!result) { | ||
return null; | ||
} | ||
return result; | ||
}; | ||
return { getSlice, sliceMap, getSliceByName }; | ||
}; | ||
@@ -63,2 +70,3 @@ const shallowClone = (x) => { | ||
const context = { | ||
name, | ||
id, | ||
@@ -88,2 +96,3 @@ set: (next) => { | ||
this.use = (slice) => __privateGet(this, _container).getSlice(slice); | ||
this.useByName = (name) => __privateGet(this, _container).getSliceByName(name); | ||
this.get = (slice) => this.use(slice).get(); | ||
@@ -90,0 +99,0 @@ this.set = (slice, value) => this.use(slice).set(value); |
import { $Slice, Slice } from './slice'; | ||
export declare type Container = { | ||
readonly getSlice: <T>(slice: Slice<T>) => $Slice<T>; | ||
readonly getSliceByName: <T>(name: string) => $Slice<T> | null; | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
@@ -5,0 +6,0 @@ }; |
export declare type $Slice<T = unknown> = { | ||
readonly id: symbol; | ||
readonly name: string; | ||
readonly set: (value: T) => void; | ||
@@ -4,0 +5,0 @@ readonly get: () => T; |
@@ -17,2 +17,9 @@ import type { $Slice, Container, Slice } from '../context'; | ||
/** | ||
* Get the instance value by string name. | ||
* | ||
* @param slice - The slice needs to be used. | ||
* @returns The slice value. | ||
*/ | ||
readonly useByName: (name: string) => $Slice<unknown> | null; | ||
/** | ||
* Get the slice value. | ||
@@ -19,0 +26,0 @@ * |
{ | ||
"name": "@milkdown/ctx", | ||
"version": "5.4.0", | ||
"version": "5.4.1", | ||
"type": "module", | ||
@@ -23,3 +23,3 @@ "main": "./lib/index.es.js", | ||
"devDependencies": { | ||
"@milkdown/exception": "5.4.0" | ||
"@milkdown/exception": "5.4.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "peerDependencies": { |
@@ -8,2 +8,3 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
readonly getSlice: <T>(slice: Slice<T>) => $Slice<T>; | ||
readonly getSliceByName: <T>(name: string) => $Slice<T> | null; | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
@@ -23,3 +24,11 @@ }; | ||
return { getSlice, sliceMap }; | ||
const getSliceByName = <T>(sliceName: string): $Slice<T> | null => { | ||
const result = [...sliceMap.values()].find((x) => x.name === sliceName); | ||
if (!result) { | ||
return null; | ||
} | ||
return result as $Slice<T>; | ||
}; | ||
return { getSlice, sliceMap, getSliceByName }; | ||
}; |
@@ -8,2 +8,3 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
readonly id: symbol; | ||
readonly name: string; | ||
readonly set: (value: T) => void; | ||
@@ -30,2 +31,3 @@ readonly get: () => T; | ||
const context: $Slice<T> = { | ||
name, | ||
id, | ||
@@ -32,0 +34,0 @@ set: (next) => { |
@@ -26,2 +26,10 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
/** | ||
* Get the instance value by string name. | ||
* | ||
* @param slice - The slice needs to be used. | ||
* @returns The slice value. | ||
*/ | ||
readonly useByName = (name: string) => this.#container.getSliceByName(name); | ||
/** | ||
* Get the slice value. | ||
@@ -28,0 +36,0 @@ * |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38758
682