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

hyperapp-lifecycle

v0.0.4

Published

Small wrapper for Hyperapp and Superfine to emulate connected and disconnected lifecycle events

Downloads

16

Readme

                                 ╭──╮ ╭───┬╮ ╭────╮
                            ╭──╮ ├──┤ │    │ │ ─  │ ╭────╮
  ╭────◉  connected         │  │ │  │ │   ─┤ │    │ │ ╭──╯ ╭──╮╭╮
  │                         │  │ │  │ │   ╭╯ │ `○─┤ │ ╰──╮ │  │││ ╭────╮
╭─┴─╮  ╭──╮╭╮               │  │ ╰──╯ ╰───╯  ╰────╯ │    │ │  ╰╯│ │ ╭──╯ ╭──╮ ╭────╮
│   ╰╮ │  │││ ╭────╮        ╰──╯                    ╰────╯ ├─── │ │ ╰──╮ │  │ │ ○  │
│    │ │  ╰╯│ │  ╭╮│ ╭────╮         \||/  ╭────╮ ╭────╮    ╰────╯ │    │ │  │ │    │
│  ╭╮│ ├──○ │ │  ╰╯│ │ ─  │ ╭──┬─╮ ╭(oo)╮ │  ╭╮│ │  ╭╮│           ╰────╯ │  │ │ ───┤
╰──╯╰╯ ╰────╯ │   ╭╯ │    │ │    │ ├─── │ │  ╰╯│ │  ╰╯│                  ╰──╯ ╰──┬─╯
              ╰───╯  │ ───┤ │   ╭╯ │╭╮  │ │   ╭╯ │   ╭╯    disconnected  ◉───────╯
                     ╰────╯ │   │  │╰╯  │ ╰───╯  ╰───╯
                            ╰───╯  ╰──┴─╯

Small wrapper for Hyperapp and Superfine to emulate connected and disconnected lifecycle events.

Hyperapp demo

Superfine demo

Overview

Hyperapp is super tiny ui framework that is centered around app state with awesome flexibility and rich ecosystem. With super lean code however, it is inevitable that some features that we might be used to aren't available in the core library.

One such feature is the ability to grab the reference of a newly created or being removed DOM node. I found myself wanting this feature since I started using Hyperapp few weeks ago and this package is one possible answer for this requirement.

What do you get

Ability to listen to two lifecycle events, connected and disconnected similar to connectedCallback and disconnectedCallback methods in custom elements.

Usage

Full app lifecycle events coverage including app root node.

import * as hyperapp from 'https://unpkg.com/hyperapp?module'
import { timeout } from 'https://unpkg.com/@hyperapp/time?module'
import { lifecycle } from 'https://unpkg.com/hyperapp-lifecycle?module'

const { app, h } = lifecycle(hyperapp)

const Log = type => (state, evt) => console.log(`${type}:`, evt.target.tagName) || state
const RemoveWorld = state => ({ ...state, world: false })

const init = { world: true }

const view = state =>
  h('section', { onconnected: Log('Connected') },                    // Connected: SECTION
    h('main', { onconnected: Log('Connected') },                     // Connected: MAIN
      h('div', { onconnected: Log('Connected') },                    // Connected: DIV
        h('span', { style: { color: 'green' } }, 'Hello'),
        state.world &&                                               // Disconnected: SPAN
        h('span', { ondisconnected: Log('Disconnected'), style: { color: 'blue' } }, ' World')
      )
    ),
    h('p', null, h('i', null, 'open browser console to see events print out'))
  )

const subscriptions = state => [timeout(RemoveWorld, { delay: 1000 })]

app({ init, view, node, subscriptions })

Notice that, the custom event target is the DOM node that defined the event handler.

Lite mode

Lite mode features a supplementary function l that can be used as needed and where h remains unchanged. This gives more control on which nodes are allowed to fire lifecycle events for their children. The root node lifecycle events will be triggered as in full coverage mode.

The main point here to be aware of -as shown by the example below- is that, the use of l will ensure lifecycle events for children nodes and not for the node itself.

import * as hyperapp from 'https://unpkg.com/hyperapp?module'
import { timeout } from 'https://unpkg.com/@hyperapp/time?module'
import { lifecycle } from 'https://unpkg.com/hyperapp-lifecycle?module'

const { app, h, l } = lifecycle(hyperapp, /* lite mode */ true)

const Log = type => (state, evt) => console.log(`${type}:`, evt.target.tagName) || state
const RemoveWorld = state => ({ ...state, world: false })

const init = { world: true }

const view = state =>
  h('section', { onconnected: Log('Connected') },                    // Connected: SECTION
    h('main', { onconnected: Log('Connected') },
      l('div', { onconnected: Log('Connected') },
        h('span', { style: { color: 'green' } }, 'Hello'),
        state.world &&                                               // Disconnected: SPAN
        h('span', { ondisconnected: Log('Disconnected'), style: { color: 'blue' } }, ' World')
      )
    ),
    h('p', null, h('i', null, 'open browser console to see events print out'))
  )

const subscriptions = state => [timeout(RemoveWorld, { delay: 1000 })]

app({ init, view, node, subscriptions })

Superfine

The same works for the Superfine library, yay!

import * as superfine from 'https://unpkg.com/superfine?module'
import { timeout } from 'https://unpkg.com/@hyperapp/time?module'
import { lifecycle } from 'https://unpkg.com/hyperapp-lifecycle?module'

const { patch, h } = lifecycle(superfine)

const Log = type => evt => console.log(`${type}:`, evt.target.tagName)
const RemoveWorld = state => ({ ...state, world: false })

const init = { world: true }

const view = state =>
  h('section', { onconnected: Log('Connected') },                    // Connected: SECTION
    h('main', { onconnected: Log('Connected') },                     // Connected: MAIN
      h('div', { onconnected: Log('Connected') },                    // Connected: DIV
        h('span', { style: 'color:green;' }, 'Hello'),
        state.world &&                                               // Disconnected: SPAN
        h('span', { ondisconnected: Log('Disconnected'), style: 'color:blue;' }, ' World')
      )
    ),
    h('p', null, h('i', null, 'open browser console to see events print out'))
  )

const app = state => patch(node, view(state))

app(init)

setTimeout(()=> app(RemoveWorld(init)), 1000)

Credits

This package is based on the work of Sergey Shpak as can be viewed here. The code performs black magic to hijack few of the node DOM methods in order to fire custom events. In addition to adding few extra features, the original code was slightly modified to ensure connected events are fired on appendChild and insertBefore calls.

Support

Need help or have a question? post a questions at StackOverflow

Please don't use the issue trackers for support/questions.

Contributions

Happy to accept external contributions to the project in the form of feedback, bug reports and even better - pull requests

Copyright and license

MIT license Copyright (c) Web Semantics, Inc.