Skip to content

Commit 17440cb

Browse files
committed
Pass live api through context not prop
1 parent d9eba0a commit 17440cb

File tree

18 files changed

+64
-19
lines changed

18 files changed

+64
-19
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,14 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to
217217

218218
```svelte
219219
<script>
220+
import {getLive} from "live_svelte"
221+
222+
// live contains all exported LiveView methods available to the frontend
223+
const live = getLive()
224+
220225
// The number prop is reactive,
221226
// this means if the server assigns the number, it will update in the frontend
222227
export let number = 1
223-
// live contains all exported LiveView methods available to the frontend
224-
export let live
225228
226229
function increase() {
227230
// This pushes the event over the websocket
@@ -236,7 +239,7 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to
236239
}
237240
238241
function decrease() {
239-
pushEvent("set_number", {number: number - 1}, () => {})
242+
live.pushEvent("set_number", {number: number - 1}, () => {})
240243
}
241244
</script>
242245

assets/copy/js/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import "phoenix_html"
2020
// Establish Phoenix Socket and LiveView configuration.
2121
import {Socket} from "phoenix"
2222
import {LiveSocket} from "phoenix_live_view"
23+
import {getContext} from "svelte"
2324
import topbar from "../vendor/topbar"
24-
import {getHooks} from "live_svelte"
25+
import {getHooks, setupLive} from "live_svelte"
2526
import * as Components from "../svelte/**/*.svelte"
2627

28+
setupLive(getContext)
29+
2730
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
2831
let liveSocket = new LiveSocket("/live", Socket, {hooks: getHooks(Components), params: {_csrf_token: csrfToken}})
2932

assets/js/live_svelte/hooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {normalizeComponents} from "./utils"
1+
import {normalizeComponents, getLive} from "./utils"
22

33
function getAttributeJson(ref, attributeName) {
44
const data = ref.el.getAttribute(attributeName)
@@ -71,7 +71,6 @@ function getProps(ref) {
7171
return {
7272
...getAttributeJson(ref, "data-props"),
7373
...getLiveJsonProps(ref),
74-
live: ref,
7574
$$slots: getSlots(ref),
7675
$$scope: {},
7776
}
@@ -83,7 +82,7 @@ function findSlotCtx(component) {
8382
return component.$$.ctx.find(ctxElement => ctxElement?.default)
8483
}
8584

86-
export function getHooks(components) {
85+
export function getHooks(components, options) {
8786
components = normalizeComponents(components)
8887

8988
const SvelteHook = {
@@ -106,6 +105,7 @@ export function getHooks(components) {
106105
this._instance = new Component({
107106
target: this.el,
108107
props: getProps(this),
108+
context: new Map([[getLive, this]]),
109109
hydrate: this.el.hasAttribute("data-ssr"),
110110
})
111111
},

assets/js/live_svelte/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export {getRender} from "./render"
22
export {getHooks} from "./hooks"
3+
export {setupLive, getLive} from "./utils"

assets/js/live_svelte/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export type Live = {
1111

1212
export declare const getHooks: (components: object) => object
1313
export declare const getRender: (components: object) => (name: string, props: object, slots: object) => any
14+
export declare const setupLive: (getContext: (key: string) => any) => Live
15+
export declare const getLive: () => Live

assets/js/live_svelte/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ export function normalizeComponents(components) {
99
}
1010
return normalized
1111
}
12+
13+
let getSvelteContext = undefined
14+
15+
export function setupLive(getContext) {
16+
getSvelteContext = getContext
17+
}
18+
19+
export function getLive() {
20+
return getSvelteContext?.(getLive)
21+
}

example_project/assets/js/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import "phoenix_html"
2020
// Establish Phoenix Socket and LiveView configuration.
2121
import {Socket} from "phoenix"
2222
import {LiveSocket} from "phoenix_live_view"
23+
import {getContext} from "svelte"
2324
import topbar from "../vendor/topbar"
2425
import {createLiveJsonHooks} from "live_json"
25-
import {getHooks} from "live_svelte"
26+
import {getHooks, setupLive} from "live_svelte"
2627
import * as Components from "../svelte/**/*.svelte"
2728

29+
setupLive(getContext)
30+
2831
const Hooks = {
2932
...createLiveJsonHooks(),
3033
...getHooks(Components),

example_project/assets/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_project/assets/svelte/Chat.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import {fly} from "svelte/transition"
33
import {elasticOut} from "svelte/easing"
44
import {afterUpdate} from "svelte"
5+
import {getLive} from "live_svelte"
6+
7+
const live = getLive()
58
69
export let messages
710
export let name
8-
export let live
911
1012
let body = ""
1113
let messagesElement

example_project/assets/svelte/LightControllers.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<script>
2-
export let live
2+
import {getLive} from "live_svelte"
3+
4+
const live = getLive()
5+
36
export let isOn = false
7+
48
const toggleLight = () => {
59
isOn = !isOn
610
live.pushEvent(isOn ? "on" : "off")

0 commit comments

Comments
 (0)