UNPKG

2.13 kBPlain TextView Raw
1/**
2* Behaviors that do not require the composition lifecycle callbacks when replacing
3* their binding context.
4*/
5export const lifecycleOptionalBehaviors = ['focus', 'if', 'else', 'repeat', 'show', 'hide', 'with'];
6
7function behaviorRequiresLifecycle(instruction) {
8 let t = instruction.type;
9 let name = t.elementName !== null ? t.elementName : t.attributeName;
10 return lifecycleOptionalBehaviors.indexOf(name) === -1 && (t.handlesAttached || t.handlesBind || t.handlesCreated || t.handlesDetached || t.handlesUnbind)
11 || t.viewFactory && viewsRequireLifecycle(t.viewFactory)
12 || instruction.viewFactory && viewsRequireLifecycle(instruction.viewFactory);
13}
14
15function targetRequiresLifecycle(instruction) {
16 // check each behavior instruction.
17 let behaviors = instruction.behaviorInstructions;
18 if (behaviors) {
19 let i = behaviors.length;
20 while (i--) {
21 if (behaviorRequiresLifecycle(behaviors[i])) {
22 return true;
23 }
24 }
25 }
26
27 // check the instruction's view factory (if it has one).
28 return instruction.viewFactory && viewsRequireLifecycle(instruction.viewFactory);
29}
30
31export function viewsRequireLifecycle(viewFactory) {
32 // already analyzed?
33 if ('_viewsRequireLifecycle' in viewFactory) {
34 return viewFactory._viewsRequireLifecycle;
35 }
36
37 // set prop to avoid infinite recursion.
38 viewFactory._viewsRequireLifecycle = false;
39
40 // access inner view factory.
41 if (viewFactory.viewFactory) {
42 viewFactory._viewsRequireLifecycle = viewsRequireLifecycle(viewFactory.viewFactory);
43 return viewFactory._viewsRequireLifecycle;
44 }
45
46 // template uses animation?
47 if (viewFactory.template.querySelector('.au-animate')) {
48 viewFactory._viewsRequireLifecycle = true;
49 return true;
50 }
51
52 // target instructions require lifecycle?
53 for (let id in viewFactory.instructions) {
54 if (targetRequiresLifecycle(viewFactory.instructions[id])) {
55 viewFactory._viewsRequireLifecycle = true;
56 return true;
57 }
58 }
59
60 // safe to skip lifecycle.
61 viewFactory._viewsRequireLifecycle = false;
62 return false;
63}