npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cable-streams

v0.1.0-beta3

Published

Extend Turbo Stream with Custom Turbo Stream Actions or CableReady operations

Downloads

2

Readme

Cable Streams

Extend Turbo Streams with Custom Turbo Stream Actions and CableReady operations.

Getting Started

yarn add cable-streams
import * as Turbo from '@hotwired/turbo'
import CableReady from 'cable_ready'
import CableStreams from 'cable-streams'

Adding Custom Turbo Stream Actions (Deprecated)

Note: Custom Turbo Stream Actions via CableStreams are deprecated. Instead register your Custom Turbo Stream Actions directly on Turbo.StreamActions.

You can define your own Turbo Stream actions on the CableStreams.customActions object.

Within the scope of your custom action function this always points to the <turbo-stream> element.

If your action is targeting specific elements in the DOM you can access them via this.targetElements. The <turbo-stream> element lookups the right elements using the provided content of the [target] attribute on the <turbo-stream> element.

You can also access the content of the <template> element within the <turbo-stream> via this.templateContent.

Example using the <template> element

// IMPORTANT: make sure you are explicitly using the `function` keyword
// for defining your custom action in order to keep the right scope!

CableStreams.customActions.log = function() {
  console.log(this.templateContent)
}

Now if you insert a <turbo-stream> element into the DOM it will be picked up and processed by your custom action.

<turbo-stream action="log" target="body">
  <template>
    This will be logged
  </template>
</turbo-stream>

Example using the regular Web API for HTMLElement

If you don't want to rely on the <template> element you can also define regular attributes on the <turbo-stream> element. The payload from the example above can be represented as:

<turbo-stream action="log" message="This will be logged"></turbo-stream>

Since the <turbo-stream> element is a regular HTMLElement you can also use every available function and property on it. With that, the custom action can be rewritten as:

CableStreams.customActions.log = function() {
  console.log(this.getAttribute("message"))
}

This leaves a lot of room for creativity.

Register CableReady operations as Turbo Stream Actions

You can register all available CableReady operations as Turbo Stream Actions.

CableStreams.registerCableReadyOperations()

Now you can use any CableReady operations serialized as JSON in the <template> tag.

For example:

<turbo-stream action="consoleLog">
  <template>
    { "message": "Hello from CableReady", "operation": "consoleLog" }
  </template>
</turbo-stream>

You can also leave out the operation option in the CableReady operation object, since it's already present on the <turbo-stream> element.

<turbo-stream action="consoleLog">
  <template>
    { "message": "Hello from CableReady without the operation key" }
  </template>
</turbo-stream>

It also works with multiple operations, passed in as an array.

<turbo-stream action="consoleLog">
  <template>
    [
      { "message": "Message 1" },
      { "message": "Message 2" }
    ]
  </template>
</turbo-stream>

Usage with Rails

There is a Rails companion gem which ships view helpers for all CableReady operations. All options are identical to the regular CableReady operations.

Installation

bundle add cable_streams

Example

Here's the same example from above using the console_log operation:

<%= turbo_stream.console_log(message: "hello world") %>

Which renders to:

<turbo-stream action="consoleLog" target="body">
  <template>
    [
      { "message": "hello world", "operation": "consoleLog" }
    ]
  </template>
</turbo-stream>