-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow coroutine lifecycles + support thisArg + remove engine re…
…quirement (#2965) This PR adds an optional `ex.coroutine` timing parameter as an option bag to schedule when they are updated ```typescript const result = ex.coroutine(engine, function * () {...}, { timing: 'postupdate' }) ``` This PR also adds a way to set the `this` parameter for a generator ```typescript const result = ex.coroutine({myThis: 'value'}, engine, function * () {...}) ``` Additionally this PR removes the requirement to pass engine if done so under an Excalibur lifecycle ```typescript const result = ex.coroutine(function * () {...}); ``` --------- Co-authored-by: Matt Jennings <[email protected]>
- Loading branch information
1 parent
0233d98
commit faa4a37
Showing
8 changed files
with
565 additions
and
145 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export interface Context<TValue> { | ||
/** | ||
* Run the callback before popping the context value | ||
* @param value | ||
* @param cb | ||
*/ | ||
scope: <TReturn>(value: TValue, cb: () => TReturn) => TReturn; | ||
value: TValue; | ||
} | ||
|
||
|
||
/** | ||
* Creates a injectable context that can be retrieved later with `useContext(context)` | ||
* | ||
* Example | ||
* ```typescript | ||
* | ||
* const AppContext = createContext({some: 'value'}); | ||
* context.scope(val, () => { | ||
* const value = useContext(AppContext); | ||
* }) | ||
* | ||
* ``` | ||
*/ | ||
export function createContext<TValue>() { | ||
const ctx: Context<TValue> = { | ||
scope: (value, cb) => { | ||
ctx.value = value; | ||
return cb(); | ||
}, | ||
value: undefined | ||
}; | ||
return ctx; | ||
} | ||
|
||
/** | ||
* Retrieves the value from the current context | ||
*/ | ||
export function useContext<TValue>(context: Context<TValue>): TValue { | ||
return context.value; | ||
} |
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
Oops, something went wrong.