Skip to content

Commit

Permalink
Pass live api through context not prop
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Sep 4, 2023
1 parent d9eba0a commit d031c27
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to

```svelte
<script>
import {getLive} from "live_svelte"
// live contains all exported LiveView methods available to the frontend
const live = getLive()
// The number prop is reactive,
// this means if the server assigns the number, it will update in the frontend
export let number = 1
// live contains all exported LiveView methods available to the frontend
export let live
function increase() {
// This pushes the event over the websocket
Expand All @@ -236,7 +239,7 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to
}
function decrease() {
pushEvent("set_number", {number: number - 1}, () => {})
live.pushEvent("set_number", {number: number - 1}, () => {})
}
</script>
Expand Down
5 changes: 4 additions & 1 deletion assets/copy/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import {getContext} from "svelte"
import topbar from "../vendor/topbar"
import {getHooks} from "live_svelte"
import {getHooks, setupLive} from "live_svelte"
import * as Components from "../svelte/**/*.svelte"

setupLive(getContext)

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

Expand Down
4 changes: 2 additions & 2 deletions assets/js/live_svelte/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function getProps(ref) {
return {
...getAttributeJson(ref, "data-props"),
...getLiveJsonProps(ref),
live: ref,
$$slots: getSlots(ref),
$$scope: {},
}
Expand All @@ -83,7 +82,7 @@ function findSlotCtx(component) {
return component.$$.ctx.find(ctxElement => ctxElement?.default)
}

export function getHooks(components) {
export function getHooks(components, options) {
components = normalizeComponents(components)

const SvelteHook = {
Expand All @@ -106,6 +105,7 @@ export function getHooks(components) {
this._instance = new Component({
target: this.el,
props: getProps(this),
context: new Map([["live-svelte", this]]),
hydrate: this.el.hasAttribute("data-ssr"),
})
},
Expand Down
1 change: 1 addition & 0 deletions assets/js/live_svelte/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export {getRender} from "./render"
export {getHooks} from "./hooks"
export {setupLive, getLive} from "./utils"
2 changes: 2 additions & 0 deletions assets/js/live_svelte/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export type Live = {

export declare const getHooks: (components: object) => object
export declare const getRender: (components: object) => (name: string, props: object, slots: object) => any
export declare const setupLive: (getContext: (key: string) => any) => Live
export declare const getLive: () => Live
10 changes: 10 additions & 0 deletions assets/js/live_svelte/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ export function normalizeComponents(components) {
}
return normalized
}

let getSvelteContext = undefined

export function setupLive(getContext) {
getSvelteContext = getContext
}

export function getLive() {
return getSvelteContext?.("live-svelte")
}
5 changes: 4 additions & 1 deletion example_project/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import {getContext} from "svelte"
import topbar from "../vendor/topbar"
import {createLiveJsonHooks} from "live_json"
import {getHooks} from "live_svelte"
import {getHooks, setupLive} from "live_svelte"
import * as Components from "../svelte/**/*.svelte"

setupLive(getContext)

const Hooks = {
...createLiveJsonHooks(),
...getHooks(Components),
Expand Down
2 changes: 1 addition & 1 deletion example_project/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion example_project/assets/svelte/Chat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import {fly} from "svelte/transition"
import {elasticOut} from "svelte/easing"
import {afterUpdate} from "svelte"
import {getLive} from "live_svelte"
const live = getLive()
export let messages
export let name
export let live
let body = ""
let messagesElement
Expand Down
6 changes: 5 additions & 1 deletion example_project/assets/svelte/LightControllers.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<script>
export let live
import {getLive} from "live_svelte"
const live = getLive()
export let isOn = false
const toggleLight = () => {
isOn = !isOn
live.pushEvent(isOn ? "on" : "off")
Expand Down
4 changes: 3 additions & 1 deletion example_project/assets/svelte/LogList.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
import {slide, fly} from "svelte/transition"
import {getLive} from "live_svelte"
const live = getLive()
export let live
export let items = []
let body
let i = 1
Expand Down
4 changes: 3 additions & 1 deletion example_project/lib/example_web/live/live_breaking_news.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ defmodule ExampleWeb.LiveExample5 do
import {slide, fly} from "svelte/transition"
import {Marquee} from "dynamic-marquee"
import {onMount} from "svelte"
import {getLive} from "live_svelte"
const live = getLive()
export let news = []
export let live
let newItem = ""
let marquee
Expand Down
4 changes: 3 additions & 1 deletion examples/advanced_chat/AdvancedChat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import {slide, fly, fade} from "svelte/transition"
import {elasticOut} from "svelte/easing"
import {afterUpdate} from "svelte"
import {getLive} from "live_svelte"
const live = getLive()
export let messages
export let name
export let live
let body = ""
let messagesElement
Expand Down
4 changes: 3 additions & 1 deletion examples/animated_number/Numbers.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script>
import {fly} from "svelte/transition"
import {getLive} from "live_svelte"
const live = getLive()
export let number = 1
export let live
function increase() {
live.pushEvent("set_number", {number: number + 1})
Expand Down
4 changes: 3 additions & 1 deletion examples/breaking_news/BreakingNews.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import {slide, fly} from "svelte/transition"
import {Marquee} from "dynamic-marquee"
import {onMount} from "svelte"
import {getLive} from "live_svelte"
const live = getLive()
export let news = []
export let live
let newItem = ""
let marquee
Expand Down
4 changes: 3 additions & 1 deletion examples/log_list/LogList.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
import {slide, fly} from "svelte/transition"
import {getLive} from "live_svelte"
const live = getLive()
export let live
export let items = []
let newItemName
Expand Down
5 changes: 4 additions & 1 deletion examples/simple/Simple.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script>
import {getLive} from "live_svelte"
const live = getLive()
export let number
export let live
function increase() {
live.pushEvent("set_number", {number: number + 1})
Expand Down
4 changes: 3 additions & 1 deletion examples/simple_chat/Chat.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script>
import {slide} from "svelte/transition"
import {getLive} from "live_svelte"
const live = getLive()
export let messages
export let live
let message = ""
let name = ""
Expand Down

0 comments on commit d031c27

Please sign in to comment.