|
| 1 | +# Context Patterns |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +This guide shows the main ways to structure feature lifetimes with `Context`. |
| 6 | + |
| 7 | +Use it when you already understand what `Context` is, but you want a clearer sense of how many scopes to create, where services belong, and when `use(...)` is the right tool. |
| 8 | + |
| 9 | +## Use It When |
| 10 | + |
| 11 | +- a feature owns several timers, subscriptions, or tracked entities together |
| 12 | +- a feature needs shorter-lived nested work inside a larger lifetime |
| 13 | +- you are deciding between direct ownership, a child scope, a service, or explicit teardown |
| 14 | +- you want authored code to follow the same ownership shape across the repo |
| 15 | + |
| 16 | +## Core Model |
| 17 | + |
| 18 | +- one feature lifetime -> one `Context` |
| 19 | +- one shorter nested lifetime -> one child scope |
| 20 | +- one shared runtime helper -> one service on the scope that should own it |
| 21 | +- one custom teardown with no dedicated helper -> `use(...)` |
| 22 | + |
| 23 | +The goal is not to create more scopes for their own sake. The goal is to make ownership obvious. |
| 24 | + |
| 25 | +## Important Patterns |
| 26 | + |
| 27 | +### Feature Scope |
| 28 | + |
| 29 | +Use one `Context` for one feature lifetime. |
| 30 | + |
| 31 | +This is the default pattern for: |
| 32 | + |
| 33 | +- a temporary gameplay system |
| 34 | +- a feature session |
| 35 | +- one controller object that owns several runtime behaviours together |
| 36 | + |
| 37 | +Keep related work on that one context: |
| 38 | + |
| 39 | +- `subscribe(...)` |
| 40 | +- `timeout(...)` |
| 41 | +- `interval(...)` |
| 42 | +- `run(...)` |
| 43 | +- `trackEntity(...)` |
| 44 | + |
| 45 | +Dispose the context when the feature ends. |
| 46 | + |
| 47 | +### Child Scope |
| 48 | + |
| 49 | +Use `createScope(...)` when one part of a feature has a shorter lifetime than the parent feature. |
| 50 | + |
| 51 | +Good fits: |
| 52 | + |
| 53 | +- a temporary effect inside a longer-running system |
| 54 | +- one spawned entity inside a broader feature |
| 55 | +- one branch of logic that should stop without tearing down the whole parent |
| 56 | + |
| 57 | +The child scope should represent a real lifetime boundary. If it does not, keep the work on the parent context. |
| 58 | + |
| 59 | +### Subscription Ownership |
| 60 | + |
| 61 | +Use `subscribe(...)` when the work is event-driven and the subscription should end with the context. |
| 62 | + |
| 63 | +This is the normal pattern for Bedrock events: |
| 64 | + |
| 65 | +- register through `ctx.subscribe(...)` |
| 66 | +- keep the returned unsubscribe only when the feature may stop earlier than the context lifetime |
| 67 | + |
| 68 | +If the handler should only run a fixed number of times, use `subscribe({ source, n }, handler)`. |
| 69 | + |
| 70 | +### Service On A Parent Scope |
| 71 | + |
| 72 | +Use a service when several parts of the same feature tree need one shared runtime-owned helper. |
| 73 | + |
| 74 | +Good fits: |
| 75 | + |
| 76 | +- monitors |
| 77 | +- registries |
| 78 | +- caches tied to one feature lifetime |
| 79 | +- helper objects that own both state and behaviour |
| 80 | + |
| 81 | +Put the service on the scope that should own its lifetime. |
| 82 | + |
| 83 | +That usually means: |
| 84 | + |
| 85 | +- feature-wide helper -> parent scope service |
| 86 | +- local one-off helper -> local scope value, not a service |
| 87 | + |
| 88 | +### Entity Ownership |
| 89 | + |
| 90 | +Use `trackEntity(...)` when a context owns one or more entities. |
| 91 | + |
| 92 | +This is a good pattern when: |
| 93 | + |
| 94 | +- one feature scope owns several spawned entities that should be removed together |
| 95 | +- one scope exists because one specific entity exists |
| 96 | +- entities should be removed when the owning context is disposed |
| 97 | +- one tracked entity should also be able to end the context lifetime when it disappears |
| 98 | + |
| 99 | +Important defaults: |
| 100 | + |
| 101 | +- `removeOnDispose` defaults to `true` |
| 102 | +- `linkRemove` defaults to `false` |
| 103 | + |
| 104 | +The important distinction is: |
| 105 | + |
| 106 | +- `removeOnDispose` makes the context own entity cleanup |
| 107 | +- `linkRemove` lets a tracked entity act as a lifetime signal for the context |
| 108 | + |
| 109 | +One entity per scope is a useful pattern, but it is not the only intended use. A single context can own many related entities. |
| 110 | + |
| 111 | +### Explicit Teardown |
| 112 | + |
| 113 | +Use `use(...)` when the feature needs teardown that does not already map to a more specific helper. |
| 114 | + |
| 115 | +This is the lowest-level ownership primitive, so it should stay the exception, not the first thing every feature reaches for. |
| 116 | + |
| 117 | +Reach for it when: |
| 118 | + |
| 119 | +- you have custom teardown logic |
| 120 | +- the work is not a service, subscription, timer, or tracked entity |
| 121 | +- the more specific helpers would only wrap the same teardown less clearly |
| 122 | + |
| 123 | +## Choosing The Right Pattern |
| 124 | + |
| 125 | +- one feature lifetime -> `new Context()` |
| 126 | +- shorter nested lifetime -> `createScope(...)` |
| 127 | +- Bedrock event ownership -> `subscribe(...)` |
| 128 | +- shared runtime helper -> service |
| 129 | +- one or more owned entities -> `trackEntity(...)` |
| 130 | +- custom teardown -> `use(...)` |
| 131 | + |
| 132 | +If more than one pattern seems possible, prefer the one that makes lifetime ownership easiest to explain in one sentence. |
0 commit comments