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

webnative-elm

v8.1.0

Published

Thin wrapper around webnative for Elm

Downloads

11

Readme

Built by FISSION Discord Discourse

A thin wrapper around Webnative for Elm.

The Webnative SDK empowers developers to build fully distributed web applications without needing a complex back-end. The SDK provides:

Webnative applications work offline and store data encrypted for the user by leveraging the power of the web platform. You can read more about Webnative in Fission's Webnative Guide. There's also an API reference which can be found at webnative.fission.app

QuickStart

elm install fission-codes/webnative-elm

# requires webnative version 0.35 or later
npm install webnative
npm install webnative-elm

Then import the javascript portion of this library and elm-taskport. We'll need to initialise both of these.

import * as TaskPort from "elm-taskport"
import * as WebnativeElm from "webnative-elm"

TaskPort.install()
WebnativeElm.init({ TaskPort })

// elmApp = Elm.Main.init()

Once we have that setup, we can write our Webnative Elm code. The following is an entire Webnative app which creates or links a user account, manages user sessions and their file system, and writes to and reads from that file system.

import Task
import Webnative
import Webnative.Auth
import Webnative.Configuration
import Webnative.Error exposing (Error)
import Webnative.FileSystem exposing (Base(..), FileSystem)
import Webnative.Namespace
import Webnative.Path as Path
import Webnative.Program exposing (Program)
import Webnative.Session exposing (Session)


-- INIT


appInfo : Webnative.AppInfo
appInfo =
  { creator = "Webnative", name = "Example" }


config : Webnative.Configuration
config =
  appInfo
    |> Webnative.Namespace.fromAppInfo
    |> Webnative.Configuration.fromNamespace


type Model
  = Unprepared
  | NotAuthenticated Program
  | Authenticated Program Session FileSystem


init : (Model, Cmd Msg)
init =
  ( Unprepared
  , -- 🚀
    config
      |> Webnative.program
      |> Webnative.attemptTask
          { ok = Liftoff
          , err = HandleWebnativeError
          }
  )



-- UPDATE


type Msg
  = HandleWebnativeError Error
  | GotFileContents String
  | GotSession Session
  | Liftoff Foundation
  | RegisterUser { success : Bool }


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    -----------------------------------------
    -- 🚀
    -----------------------------------------
    Liftoff foundation ->
      let
        newModel =
          -- Previous authenticated session?
          -- Presence of a FileSystem depends on your configuration.
          case (foundation.fileSystem, foundation.session) of
            (Just fs, Just session) -> Authenticated program session fs
            _                       -> NotAuthenticated program
      in
      ( newModel

      -- Next action
      --------------
      , case newModel of
          NotAuthenticated program ->
            -- Option (A), register a new account.
            -- We're skipping the username validation and
            -- username availability checking here to keep it short.
            { email = Nothing
            , username = Just "user"
            }
            |> Webnative.Auth.register program
            |> Webnative.attemptTask
                { ok = RegisterUser
                , error = HandleWebnativeError
                }

            -- Option (B), link an existing account.
            -- See 'Linking' section below.
          
          _ ->
            Cmd.none
      )

    -----------------------------------------
    -- 🙋
    -----------------------------------------
    RegisterUser { success } ->
      if success then
        ( model
        , program
            |> Webnative.Auth.sessionWithFileSystem
            |> Webnative.attemptTask
                { ok = RegisterUser
                , error = HandleWebnativeError
                }
        )
      else
        -- Could show message in create-account form.
        (model, Cmd.none)

    GotSessionAndFileSystem (Just { fileSystem, session }) ->
      ( -- Authenticated
        case model of
          NotAuthenticated program  -> Authenticated program session fileSystem
          _                         -> model

      -- Next action
      --------------
      , let
          path =
            Path.file [ "Sub Directory", "hello.txt" ]
        in
        "👋"
            |> Webnative.FileSystem.writeUtf8 fileSystem Private path
            |> Task.andThen (\_ -> Webnative.FileSystem.publish fileSystem)
            |> Task.andThen (\_ -> Webnative.FileSystem.readUtf8 fileSystem Private path)
            |> Webnative.attemptTask
                { ok = GotFileContents
                , error = HandleWebnativeError
                }
      )

    -----------------------------------------
    -- 💾
    -----------------------------------------
    GotFileContents string -> ...

    -----------------------------------------
    -- 🥵
    -----------------------------------------
    HandleWebnativeError UnsupportedBrowser ->        -- No indexedDB? Depends on how Webnative is configured.
    HandleWebnativeError InsecureContext ->           -- Webnative requires a secure context
    HandleWebnativeError (JavascriptError string) ->  -- Notification.push ("Got JS error: " ++ string)

Linking

When a user has already registered an account, they can link a device instead.

-- TODO: Yet to be implemented

Filesystem

Alternatively you can load the filesystem separately.
You may want to do this when working with a web worker.

import Webnative

config =
  { namespace = ...
  
  --
  , debug = Nothing
  , fileSystem = Just { loadImmediately = Just True, version = Nothing }
  , permissions = Nothing
  }

Webnative.program config

And then load it either in Elm or in javascript.

Webnative.FileSystem.load program { username = "username" }
const fs = await program.loadFileSystem("username")
webnativeElm.init({ fileSystems: [ fs ] })