Skip to content

Commit 4cd321c

Browse files
committed
Scope optimizations
1 parent 306fce5 commit 4cd321c

3 files changed

Lines changed: 57 additions & 63 deletions

File tree

src/core/compile/compile.spec.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10169,25 +10169,29 @@ describe("$compile", () => {
1016910169

1017010170
expect(typeof componentScope._isolateBindings).toBe("object");
1017110171

10172-
expect(componentScope._isolateBindings.attr.mode).toBe("@");
10173-
expect(componentScope._isolateBindings.attr.attrName).toBe("attr");
10174-
expect(componentScope._isolateBindings.attrAlias.attrName).toBe("attr");
10175-
10176-
expect(componentScope._isolateBindings.ref.mode).toBe("=");
10177-
expect(componentScope._isolateBindings.ref.attrName).toBe("ref");
10178-
expect(componentScope._isolateBindings.refAlias.attrName).toBe("ref");
10179-
expect(componentScope._isolateBindings.reference.mode).toBe("=");
10180-
expect(componentScope._isolateBindings.reference.attrName).toBe(
10172+
expect(componentScope._isolateBindings.attr._mode).toBe("@");
10173+
expect(componentScope._isolateBindings.attr._attrName).toBe("attr");
10174+
expect(componentScope._isolateBindings.attrAlias._attrName).toBe(
10175+
"attr",
10176+
);
10177+
10178+
expect(componentScope._isolateBindings.ref._mode).toBe("=");
10179+
expect(componentScope._isolateBindings.ref._attrName).toBe("ref");
10180+
expect(componentScope._isolateBindings.refAlias._attrName).toBe("ref");
10181+
expect(componentScope._isolateBindings.reference._mode).toBe("=");
10182+
expect(componentScope._isolateBindings.reference._attrName).toBe(
1018110183
"reference",
1018210184
);
10183-
expect(componentScope._isolateBindings.owRef.mode).toBe("<");
10184-
expect(componentScope._isolateBindings.owRef.attrName).toBe("owRef");
10185-
expect(componentScope._isolateBindings.owRefAlias.attrName).toBe(
10185+
expect(componentScope._isolateBindings.owRef._mode).toBe("<");
10186+
expect(componentScope._isolateBindings.owRef._attrName).toBe("owRef");
10187+
expect(componentScope._isolateBindings.owRefAlias._attrName).toBe(
1018610188
"owRef",
1018710189
);
10188-
expect(componentScope._isolateBindings.expr.mode).toBe("&");
10189-
expect(componentScope._isolateBindings.expr.attrName).toBe("expr");
10190-
expect(componentScope._isolateBindings.exprAlias.attrName).toBe("expr");
10190+
expect(componentScope._isolateBindings.expr._mode).toBe("&");
10191+
expect(componentScope._isolateBindings.expr._attrName).toBe("expr");
10192+
expect(componentScope._isolateBindings.exprAlias._attrName).toBe(
10193+
"expr",
10194+
);
1019110195

1019210196
const firstComponentScope = componentScope;
1019310197
const first$$isolateBindings = componentScope._isolateBindings;

src/core/compile/compile.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,23 @@ export interface InternalDirective extends ng.Directive {
318318
}
319319

320320
export interface IsolateBinding {
321-
mode: string;
322-
collection: boolean;
323-
optional: boolean;
324-
attrName: string;
321+
/** @internal */
322+
_mode: string;
323+
/** @internal */
324+
_collection: boolean;
325+
/** @internal */
326+
_optional: boolean;
327+
/** @internal */
328+
_attrName: string;
325329
}
326330

327331
export type IsolateBindingMap = Record<string, IsolateBinding>;
328332

329333
export interface ParsedDirectiveBindings {
330-
isolateScope: IsolateBindingMap | null;
331-
bindToController: IsolateBindingMap | null;
334+
/** @internal */
335+
_isolateScope: IsolateBindingMap | null;
336+
/** @internal */
337+
_bindToController: IsolateBindingMap | null;
332338
}
333339

334340
export type DirectiveRegistry = Record<string, ng.DirectiveFactory[]>;
@@ -685,10 +691,10 @@ export class CompileProvider {
685691
}
686692

687693
bindings[scopeName] = {
688-
mode: match[1][0],
689-
collection: match[2] === "*",
690-
optional: match[3] === "?",
691-
attrName: match[4] || scopeName,
694+
_mode: match[1][0],
695+
_collection: match[2] === "*",
696+
_optional: match[3] === "?",
697+
_attrName: match[4] || scopeName,
692698
};
693699

694700
if (match[4]) {
@@ -705,20 +711,20 @@ export class CompileProvider {
705711
directiveName: string,
706712
): ParsedDirectiveBindings {
707713
const bindings: ParsedDirectiveBindings = {
708-
isolateScope: null,
709-
bindToController: null,
714+
_isolateScope: null,
715+
_bindToController: null,
710716
};
711717

712718
if (isObject(directive.scope)) {
713719
if (directive.bindToController === true) {
714-
bindings.bindToController = parseIsolateBindings(
720+
bindings._bindToController = parseIsolateBindings(
715721
directive.scope,
716722
directiveName,
717723
true,
718724
);
719-
bindings.isolateScope = {};
725+
bindings._isolateScope = {};
720726
} else {
721-
bindings.isolateScope = parseIsolateBindings(
727+
bindings._isolateScope = parseIsolateBindings(
722728
directive.scope,
723729
directiveName,
724730
false,
@@ -727,14 +733,14 @@ export class CompileProvider {
727733
}
728734

729735
if (isObject(directive.bindToController)) {
730-
bindings.bindToController = parseIsolateBindings(
736+
bindings._bindToController = parseIsolateBindings(
731737
directive.bindToController,
732738
directiveName,
733739
true,
734740
);
735741
}
736742

737-
if (bindings.bindToController && !directive.controller) {
743+
if (bindings._bindToController && !directive.controller) {
738744
// There is no controller
739745
throw $compileMinErr(
740746
"noctrl",
@@ -1354,9 +1360,6 @@ export class CompileProvider {
13541360

13551361
const node = stableNodeList[_index];
13561362

1357-
/** @internal */
1358-
(node as Node & { _stable?: boolean })._stable = true;
1359-
13601363
let childScope: Scope;
13611364

13621365
let childBoundTranscludeFn: BoundTranscludeFn | null;
@@ -2386,7 +2389,7 @@ export class CompileProvider {
23862389
const controller = elementControllers[name];
23872390

23882391
const bindings = controllerDirective._bindings
2389-
.bindToController as any;
2392+
._bindToController as any;
23902393

23912394
const controllerInstance = controller();
23922395

@@ -3384,8 +3387,8 @@ export class CompileProvider {
33843387
const bindings = (directive._bindings =
33853388
parseDirectiveBindings(directive, directive.name));
33863389

3387-
if (isObject(bindings.isolateScope)) {
3388-
directive._isolateBindings = bindings.isolateScope;
3390+
if (isObject(bindings._isolateScope)) {
3391+
directive._isolateBindings = bindings._isolateScope;
33893392
}
33903393
}
33913394
tDirectives.push(directive);
@@ -4037,9 +4040,9 @@ export class CompileProvider {
40374040
const definition = bindings[scopeName];
40384041

40394042
const {
4040-
attrName,
4041-
optional,
4042-
mode, // @, =, <, or &
4043+
_attrName: attrName,
4044+
_optional: optional,
4045+
_mode: mode, // @, =, <, or &
40434046
} = definition;
40444047

40454048
let lastValue: any;

src/core/scope/scope.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,6 @@ export class RootScopeProvider {
147147
];
148148
}
149149

150-
const nonscopeSetsCache = new WeakMap<object, Set<string>>();
151-
152-
/** Returns a cached set for `$nonscope` arrays so repeated checks stay cheap. */
153-
function getNonscopeSet(arr: unknown): Set<string> | null {
154-
if (!isArray(arr)) return null;
155-
156-
let cached = nonscopeSetsCache.get(arr);
157-
158-
if (!cached) {
159-
cached = new Set(arr);
160-
nonscopeSetsCache.set(arr, cached);
161-
}
162-
163-
return cached;
164-
}
165-
166150
function getNodeName(node: any): string | undefined {
167151
return node?._name;
168152
}
@@ -382,14 +366,19 @@ export function createScope(target: any = {}, context?: Scope): any {
382366

383367
const keyList = keys(target);
384368

385-
const ctorNonScope = getNonscopeSet(target.constructor?.$nonscope);
369+
const ctorNonScope = target.constructor?.$nonscope;
386370

387-
const instNonScope = getNonscopeSet(target.$nonscope);
371+
const instNonScope = target.$nonscope;
388372

389373
for (let i = 0, l = keyList.length; i < l; i++) {
390374
const key = keyList[i];
391375

392-
if (ctorNonScope?.has(key) || instNonScope?.has(key)) continue;
376+
if (
377+
(isArray(ctorNonScope) && ctorNonScope.includes(key)) ||
378+
(isArray(instNonScope) && instNonScope.includes(key))
379+
) {
380+
continue;
381+
}
393382
target[key] = createScope(target[key], proxy.$handler);
394383
}
395384

@@ -713,9 +702,7 @@ export class Scope {
713702

714703
const nonscopeProps = target.constructor?.$nonscope ?? target.$nonscope;
715704

716-
const nsSet = getNonscopeSet(nonscopeProps);
717-
718-
if (nsSet?.has(property)) {
705+
if (isArray(nonscopeProps) && nonscopeProps.includes(property)) {
719706
target[property] = value;
720707

721708
return true;

0 commit comments

Comments
 (0)