Skip to content

Commit

Permalink
style: 补充代码注释
Browse files Browse the repository at this point in the history
  • Loading branch information
zenglingji committed Oct 11, 2021
1 parent 0348b08 commit 4ba3154
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/core/src/router/prefix-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ class RouterTreeLeafNode {
}
}

/**
* We use text-prefix tree to store the routes map. Cause when using plain text or template text
* to define a route, it might be many routes with the same namespace(prefix), using text-prefix tree
* can make a convenient access to match the route and get the handler.
*/

class NTree implements IRouterTree {
private root = new NTreeNode('');

addToTree(urlPattern: string, handler: any) {
let p = this.root;
// Padding an element to the rear of the array to make the leaf node.
const urlPatternComponents = [...urlPattern.split('/').filter(Boolean), LEAF_SIGN];

urlPatternComponents.forEach(component => {
const { children } = p;

// If quickMap has this component, it means the route has the same namespace
// with existed route, so get to the next level directly. If the node is a leaf
// node, just return cause it means redundant route is adding to the tree, we dont need it.
if (p.quickMap.has(component)) {
const node = p.quickMap.get(component)!;
if (isLeafNode(node)) {
Expand All @@ -53,6 +63,8 @@ class NTree implements IRouterTree {
const newNode = new NTreeNode(component);
children.push(newNode);
p.quickMap.set(component, newNode);
// When the expression like ':id' shows in the route, it should
// treat it as a parameter node.One tree node can only have one parameter node.
if (component.indexOf(':') > -1) {
p.paramNode = newNode;
}
Expand All @@ -70,6 +82,8 @@ class NTree implements IRouterTree {
while (p) {
const component = urlComponents[i ++];

// If the quickMap has the component, return it if it's also a leaf node.
// Or just move to the next level and store the path.
if (p.quickMap.has(component)) {
const node = p.quickMap.get(component)!;
if (isLeafNode(node)) {
Expand All @@ -81,6 +95,7 @@ class NTree implements IRouterTree {
continue;
}
if (component) {
// If no parameter node found, treat it as an undefined route.
if (!p.paramNode) {
throw new Error('Route not defined')
}
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/router/router-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ class RouterMap {
}
}


/**
* Add params/body/query data to the context object.
* @param ctx Context object
* @param url Request URL
* @param urlPattern Matched URL pattern
* @returns Wrapped context object with params
*/
async function wrapCtx (ctx: IContext, url: string, urlPattern: string) {
ctx.extendInfo = ctx.extendInfo || {};
const [urlWithParams, queryString = ''] = url.split('?');
Expand Down

0 comments on commit 4ba3154

Please sign in to comment.