Skip to content

Commit 220325c

Browse files
[docs] add inline documentation to svelte runtime functions (#7846)
* add documentation * add links do docs
1 parent 2b73938 commit 220325c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/runtime/internal/lifecycle.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,47 @@ export function get_current_component() {
1111
return current_component;
1212
}
1313

14+
/**
15+
* Schedules a callback to run immediately before the component is updated after any state change.
16+
*
17+
* The first time the callback runs will be before the initial `onMount`
18+
*
19+
* https://svelte.dev/docs#run-time-svelte-beforeupdate
20+
*/
1421
export function beforeUpdate(fn: () => any) {
1522
get_current_component().$$.before_update.push(fn);
1623
}
1724

25+
/**
26+
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.
27+
* It must be called during the component's initialisation (but doesn't need to live *inside* the component;
28+
* it can be called from an external module).
29+
*
30+
* `onMount` does not run inside a [server-side component](/docs#run-time-server-side-component-api).
31+
*
32+
* https://svelte.dev/docs#run-time-svelte-onmount
33+
*/
1834
export function onMount(fn: () => any) {
1935
get_current_component().$$.on_mount.push(fn);
2036
}
2137

38+
/**
39+
* Schedules a callback to run immediately after the component has been updated.
40+
*
41+
* The first time the callback runs will be after the initial `onMount`
42+
*/
2243
export function afterUpdate(fn: () => any) {
2344
get_current_component().$$.after_update.push(fn);
2445
}
2546

47+
/**
48+
* Schedules a callback to run immediately before the component is unmounted.
49+
*
50+
* Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the
51+
* only one that runs inside a server-side component.
52+
*
53+
* https://svelte.dev/docs#run-time-svelte-ondestroy
54+
*/
2655
export function onDestroy(fn: () => any) {
2756
get_current_component().$$.on_destroy.push(fn);
2857
}
@@ -31,6 +60,18 @@ export interface DispatchOptions {
3160
cancelable?: boolean;
3261
}
3362

63+
/**
64+
* Creates an event dispatcher that can be used to dispatch [component events](/docs#template-syntax-component-directives-on-eventname).
65+
* Event dispatchers are functions that can take two arguments: `name` and `detail`.
66+
*
67+
* Component events created with `createEventDispatcher` create a
68+
* [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent).
69+
* These events do not [bubble](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture).
70+
* The `detail` argument corresponds to the [CustomEvent.detail](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail)
71+
* property and can contain any type of data.
72+
*
73+
* https://svelte.dev/docs#run-time-svelte-createeventdispatcher
74+
*/
3475
export function createEventDispatcher<EventMap extends {} = any>(): <
3576
EventKey extends Extract<keyof EventMap, string>
3677
>(
@@ -57,19 +98,47 @@ export function createEventDispatcher<EventMap extends {} = any>(): <
5798
};
5899
}
59100

101+
/**
102+
* Associates an arbitrary `context` object with the current component and the specified `key`
103+
* and returns that object. The context is then available to children of the component
104+
* (including slotted content) with `getContext`.
105+
*
106+
* Like lifecycle functions, this must be called during component initialisation.
107+
*
108+
* https://svelte.dev/docs#run-time-svelte-setcontext
109+
*/
60110
export function setContext<T>(key, context: T): T {
61111
get_current_component().$$.context.set(key, context);
62112
return context;
63113
}
64114

115+
/**
116+
* Retrieves the context that belongs to the closest parent component with the specified `key`.
117+
* Must be called during component initialisation.
118+
*
119+
* https://svelte.dev/docs#run-time-svelte-getcontext
120+
*/
65121
export function getContext<T>(key): T {
66122
return get_current_component().$$.context.get(key);
67123
}
68124

125+
/**
126+
* Retrieves the whole context map that belongs to the closest parent component.
127+
* Must be called during component initialisation. Useful, for example, if you
128+
* programmatically create a component and want to pass the existing context to it.
129+
*
130+
* https://svelte.dev/docs#run-time-svelte-getallcontexts
131+
*/
69132
export function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T {
70133
return get_current_component().$$.context;
71134
}
72135

136+
/**
137+
* Checks whether a given `key` has been set in the context of a parent component.
138+
* Must be called during component initialisation.
139+
*
140+
* https://svelte.dev/docs#run-time-svelte-hascontext
141+
*/
73142
export function hasContext(key): boolean {
74143
return get_current_component().$$.context.has(key);
75144
}

0 commit comments

Comments
 (0)