-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new execution context design (#5800)
* refactor: new design for execution context * feat: add two new event helpers to the execution context and tests * fix: wip update types to match new context apis * fix: update foundation and components template types * Change files * fix: update template type in fast-website * fix: update site components for new template types * fix: add missing api updates Co-authored-by: EisenbergEffect <[email protected]>
- Loading branch information
1 parent
d57a864
commit bfa540d
Showing
43 changed files
with
989 additions
and
416 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-components-9bddf676-bcae-4054-bb49-e554ec2091eb.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "fix: update foundation and components template types", | ||
"packageName": "@microsoft/fast-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-element-cb2ca562-779a-4f22-9b87-3fdc5483c7d5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "refactor: new design for execution context", | ||
"packageName": "@microsoft/fast-element", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-foundation-087192c5-d2a2-4873-b209-f8965bc11d14.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "fix: update foundation and components template types", | ||
"packageName": "@microsoft/fast-foundation", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-router-989db3af-0549-4d20-b4f6-c462d43aa913.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "major", | ||
"comment": "refactor: new design for execution context", | ||
"packageName": "@microsoft/fast-router", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
197 changes: 138 additions & 59 deletions
197
packages/web-components/fast-element/docs/api-report.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
packages/web-components/fast-element/src/observation/behavior.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import type { ExecutionContext } from "./observable.js"; | ||
import type { ExecutionContext, RootContext } from "./observable.js"; | ||
|
||
/** | ||
* Represents an object that can contribute behavior to a view or | ||
* element's bind/unbind operations. | ||
* @public | ||
*/ | ||
export interface Behavior<TSource = any, TParent = any, TGrandparent = any> { | ||
export interface Behavior< | ||
TSource = any, | ||
TParent = any, | ||
TContext extends ExecutionContext<TParent> = RootContext | ||
> { | ||
/** | ||
* Bind this behavior to the source. | ||
* @param source - The source to bind to. | ||
* @param context - The execution context that the binding is operating within. | ||
*/ | ||
bind(source: TSource, context: ExecutionContext<TParent, TGrandparent>): void; | ||
bind(source: TSource, context: TContext): void; | ||
|
||
/** | ||
* Unbinds this behavior from the source. | ||
* @param source - The source to unbind from. | ||
*/ | ||
unbind(source: TSource, context: ExecutionContext<TParent, TGrandparent>): void; | ||
unbind(source: TSource, context: TContext): void; | ||
} |
154 changes: 154 additions & 0 deletions
154
packages/web-components/fast-element/src/observation/execution-context.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { expect } from "chai"; | ||
import { ExecutionContext, ItemContext } from "./observable"; | ||
|
||
describe("The ExecutionContext", () => { | ||
it("has a default", () => { | ||
const defaultContext = ExecutionContext.default; | ||
const newContext = ExecutionContext.create(); | ||
|
||
expect(defaultContext.constructor).equals(newContext.constructor); | ||
}); | ||
|
||
function createEvent() { | ||
const detail = { hello: "world" }; | ||
const event = new CustomEvent('my-event', { detail }); | ||
|
||
return { event, detail }; | ||
} | ||
|
||
it("can get the current event", () => { | ||
const { event } = createEvent(); | ||
|
||
ExecutionContext.setEvent(event); | ||
const context = ExecutionContext.create(); | ||
|
||
expect(context.event).equals(event); | ||
|
||
ExecutionContext.setEvent(null); | ||
}); | ||
|
||
it("can get the current event detail", () => { | ||
const { event, detail } = createEvent(); | ||
|
||
ExecutionContext.setEvent(event); | ||
const context = ExecutionContext.create(); | ||
|
||
expect(context.eventDetail()).equals(detail); | ||
expect(context.eventDetail<typeof detail>().hello).equals(detail.hello); | ||
|
||
ExecutionContext.setEvent(null); | ||
}); | ||
|
||
it("can create a child context for a parent source", () => { | ||
const parentSource = {}; | ||
const parentContext = ExecutionContext.create(); | ||
const childContext = parentContext.createChildContext(parentSource); | ||
|
||
expect(childContext.parent).equals(parentSource); | ||
expect(childContext.parentContext).equals(parentContext); | ||
}); | ||
|
||
it("can create an item context from a child context", () => { | ||
const parentSource = {}; | ||
const parentContext = ExecutionContext.create(); | ||
const childContext = parentContext.createChildContext(parentSource); | ||
const itemContext = childContext.createItemContext(7, 42); | ||
|
||
expect(itemContext.parent).equals(parentSource); | ||
expect(itemContext.parentContext).equals(parentContext); | ||
expect(itemContext.index).equals(7); | ||
expect(itemContext.length).equals(42); | ||
}); | ||
|
||
context("item context", () => { | ||
const scenarios = [ | ||
{ | ||
name: "even is first", | ||
index: 0, | ||
length: 42, | ||
isEven: true, | ||
isOdd: false, | ||
isFirst: true, | ||
isMiddle: false, | ||
isLast: false | ||
}, | ||
{ | ||
name: "odd in middle", | ||
index: 7, | ||
length: 42, | ||
isEven: false, | ||
isOdd: true, | ||
isFirst: false, | ||
isMiddle: true, | ||
isLast: false | ||
}, | ||
{ | ||
name: "even in middle", | ||
index: 8, | ||
length: 42, | ||
isEven: true, | ||
isOdd: false, | ||
isFirst: false, | ||
isMiddle: true, | ||
isLast: false | ||
}, | ||
{ | ||
name: "odd at end", | ||
index: 41, | ||
length: 42, | ||
isEven: false, | ||
isOdd: true, | ||
isFirst: false, | ||
isMiddle: false, | ||
isLast: true | ||
}, | ||
{ | ||
name: "even at end", | ||
index: 40, | ||
length: 41, | ||
isEven: true, | ||
isOdd: false, | ||
isFirst: false, | ||
isMiddle: false, | ||
isLast: true | ||
} | ||
]; | ||
|
||
function assert(itemContext: ItemContext, scenario: typeof scenarios[0]) { | ||
expect(itemContext.index).equals(scenario.index); | ||
expect(itemContext.length).equals(scenario.length); | ||
expect(itemContext.isEven).equals(scenario.isEven); | ||
expect(itemContext.isOdd).equals(scenario.isOdd); | ||
expect(itemContext.isFirst).equals(scenario.isFirst); | ||
expect(itemContext.isInMiddle).equals(scenario.isMiddle); | ||
expect(itemContext.isLast).equals(scenario.isLast); | ||
} | ||
|
||
for (const scenario of scenarios) { | ||
it(`has correct position when ${scenario.name}`, () => { | ||
const parentSource = {}; | ||
const parentContext = ExecutionContext.create(); | ||
const childContext = parentContext.createChildContext(parentSource); | ||
const itemContext = childContext.createItemContext(scenario.index, scenario.length); | ||
|
||
assert(itemContext, scenario); | ||
}); | ||
} | ||
|
||
it ("can update its index and length", () => { | ||
const scenario1 = scenarios[0]; | ||
const scenario2 = scenarios[1]; | ||
|
||
const parentSource = {}; | ||
const parentContext = ExecutionContext.create(); | ||
const childContext = parentContext.createChildContext(parentSource); | ||
const itemContext = childContext.createItemContext(scenario1.index, scenario1.length); | ||
|
||
assert(itemContext, scenario1); | ||
|
||
itemContext.updatePosition(scenario2.index, scenario2.length); | ||
|
||
assert(itemContext, scenario2); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.