Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jjl committed Jan 29, 2021
0 parents commit 21ceed7
Show file tree
Hide file tree
Showing 18 changed files with 4,390 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
/.spago
/.cache
/.yarnrc
12 changes: 12 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM debian:stable-slim

ENV CWD=/opt/app HOME=/opt/app TERM=xterm

RUN apt-get update && apt-get install -y bash curl inotify-tools git build-essential libtinfo5

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
WORKDIR /opt/app
EXPOSE 5000/tcp
CMD ["bash"]
373 changes: 373 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# purescript-toestand

Simple state management for Reactix

## Copyright and License

Copyright (c) 2021 James Laver

This software is free and open source software licensed under the
terms of the Mozilla Public License (MPL) 2.0

16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.5"
services:
frontend:
build:
context: .
dockerfile: "Dockerfile.dev"
stdin_open: true
tty: true
user: $UID:$GID
ports:
- 5000:5000
volumes:
- .:/opt/app
- /etc/group:/etc/group:ro
- /etc/passwd:/etc/passwd:ro
- /etc/shadow:/etc/shadow:ro
3 changes: 3 additions & 0 deletions env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export UID=$(id -u)
export GID=$(id -g)
alias darn="docker-compose run frontend yarn"
22 changes: 22 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Karma configuration
// Generated on Thu Feb 21 2019 19:31:16 GMT+0100 (Central European Standard Time)

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha'],
files: [ "output/bundle.js" ],
plugins: [ 'karma-chrome-launcher' , 'karma-mocha'],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: true,
concurrency: 1
})
}
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "purescript-toestand",
"version": "0.1.0",
"description": "A simple state management solution for reactix.",
"scripts": {
"rebase-set": "spago upgrade-set && spago psc-package-insdhall",
"rebuild-set": "spago psc-package-insdhall",
"install-ps": "psc-package install",
"repl": "pulp --psc-package repl",
"build": "pulp browserify -I test --main Test.Main > output/bundle.js",
"test": "karma start"
},
"author": "James Laver",
"license": "MPL-2.0",
"devDependencies": {
"karma": "^4.4.1",
"karma-chrome-launcher": "^3.1.0",
"karma-cli": "^2.0.0",
"karma-mocha": "^1.3.0",
"mocha": "^6.2.2",
"psc-package": "^3.0.1",
"pulp": "^13.0.0",
"purescript": "^0.13.8",
"react": "^17.0.1",
"spago": "^0.10.0"
},
"dependencies": {}
}
167 changes: 167 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
{-
Welcome to Spacchetti local packages!
Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.
## Warning: Don't Move This Top-Level Comment!
Due to how `dhall format` currently works, this comment's
instructions cannot appear near corresponding sections below
because `dhall format` will delete the comment. However,
it will not delete a top-level comment like this one.
## Use Cases
Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set
This file will continue to work whether you use one or both options.
Instructions for each option are explained below.
### Overriding/Patching a package
Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo
Syntax:
Replace the overrides' "{=}" (an empty record) with the following idea
The "//" or "⫽" means "merge these two records and
when they have the same value, use the one on the right:"
-------------------------------
let override =
{ packageName =
upstream.packageName ⫽ { updateEntity1 = "new value", updateEntity2 = "new value" }
, packageName =
upstream.packageName ⫽ { version = "v4.0.0" }
, packageName =
upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" }
}
-------------------------------
Example:
-------------------------------
let overrides =
{ halogen =
upstream.halogen ⫽ { version = "master" }
, halogen-vdom =
upstream.halogen-vdom ⫽ { version = "v4.0.0" }
}
-------------------------------
### Additions
Purpose:
- Add packages that aren't alread included in the default package set
Syntax:
Replace the additions' "{=}" (an empty record) with the following idea:
-------------------------------
let additions =
{ "package-name" =
mkPackage
[ "dependency1"
, "dependency2"
]
"https://example.com/path/to/git/repo.git"
"tag ('v4.0.0') or branch ('master')"
, "package-name" =
mkPackage
[ "dependency1"
, "dependency2"
]
"https://example.com/path/to/git/repo.git"
"tag ('v4.0.0') or branch ('master')"
, etc.
}
-------------------------------
Example:
-------------------------------
let additions =
{ benchotron =
mkPackage
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
"https://github.com/hdgarrood/purescript-benchotron.git"
"v7.0.0"
}
-------------------------------
-}

let mkPackage =
https://raw.githubusercontent.com/purescript/package-sets/psc-0.13.5-20191127/src/mkPackage.dhall sha256:0b197efa1d397ace6eb46b243ff2d73a3da5638d8d0ac8473e8e4a8fc528cf57

let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20210118/packages.dhall sha256:a59c5c93a68d5d066f3815a89f398bcf00e130a51cb185b2da29b20e2d8ae115

let overrides = {=}

let additions =
{ dom-simple =
mkPackage
[ "console"
, "effect"
, "functions"
, "nullable"
, "prelude"
, "spec"
, "spec-mocha"
, "unsafe-coerce"
]
"https://github.com/poorscript/purescript-dom-simple"
"v0.2.7"
, ffi-simple =
mkPackage
[ "prelude"
, "effect"
, "maybe"
, "functions"
, "nullable"
, "unsafe-coerce"
]
"https://github.com/poorscript/purescript-ffi-simple"
"v0.2.10"
, reactix =
mkPackage
[ "prelude"
, "effect"
, "maybe"
, "functions"
, "nullable"
, "unsafe-coerce"
]
"https://github.com/poorscript/purescript-reactix"
"v0.4.8"
, inflection =
mkPackage
[ "functions" ]
"https://github.com/athanclark/purescript-inflection"
"v1.0.0"
, spec-mocha =
mkPackage
[ "console", "foldable-traversable", "exceptions", "spec" ]
"https://github.com/purescript-spec/purescript-spec-mocha"
"v4.0.0"
}

in upstream // overrides // additions
13 changes: 13 additions & 0 deletions psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "toestand",
"set": "local",
"source": ".psc-package",
"depends":
[ "effect"
, "reactix"
, "prelude"
, "psci-support"
, "spec"
, "spec-mocha"
]
}
15 changes: 15 additions & 0 deletions src/Toestand.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Toestand
( module Toestand.Classes
, module Toestand.Cell
, module Toestand.Cursor
, module Toestand.View
) where

import Toestand.Classes
( class Read, read, listen
, class Write, write
, modify
)
import Toestand.Cell (Cell, useCell)
import Toestand.Cursor (Cursor, cursor, subcursor, useCursor)
import Toestand.View (View, view)
64 changes: 64 additions & 0 deletions src/Toestand/Cell.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- | The Cell is essentially a mutable value held in a react ref with
-- | some added state management features:
-- |
-- | * Listen for changes to the value.
-- | * Selectively alert listeners when the value has changed.
-- | * Selectively refresh the component when the value changes.
module Toestand.Cell
( module Toestand.Watches
, Cell, ShouldRefresh, listen, read, useCell, write
) where

import Prelude (Unit, (<$>), ($), (+), bind, discard, pure)
import Control.Applicative (when)
import Control.Monad (class Monad)
import Effect (Effect)
import Data.Tuple (snd)
import Reactix as R
import Reactix.React (readRefM)
import Toestand.Watches as W
import Toestand.Watches (Listener, ShouldNotify)

type ShouldRefresh c = c -> c -> Boolean

-- | A flexible mutable wrapper for state based around a react
-- | ref. See module documentation for more.
newtype Cell c = Cell (R.Ref (Cell' c))

newtype Cell' c = Cell'
{ value :: c
, refresh :: R.Setter Int
, shouldRefresh :: ShouldRefresh c
, watches :: W.Watches c
}

-- | A hook which allocates a Cell, a flexible mutable wrapper for
-- | state utilising a react ref. Will refresh the component when the
-- | should refresh predicate returns true.
useCell :: forall c. ShouldRefresh c -> ShouldNotify c -> c -> R.Hooks (Cell c)
useCell shouldRefresh shouldNotify value = do
refresh <- snd <$> R.useState' 0
watches <- R.unsafeHooksEffect $ W.new shouldNotify
Cell <$> R.useRef (Cell' { value, refresh, shouldRefresh, watches })

-- | Read the current value in the cell. Can be called in either Hooks or Effect
read :: forall m c. Monad m => Cell c -> m c
read (Cell ref) = do
(Cell' c) <- readRefM ref
pure c.value

-- | Replace the value held in the cell without regard to its current value.
write :: forall c. c -> Cell c -> Effect c
write value (Cell ref) = do
(Cell' cell) <- readRefM ref
let new = Cell' $ cell { value = value }
R.setRef ref new
W.notify cell.watches value cell.value
when (cell.shouldRefresh value cell.value) (cell.refresh (_ + 1))
pure value

-- | Run an Effectful function when a change occurs. Returns a cancel effect.
listen :: forall c. Cell c -> (c -> c -> Effect Unit) -> Effect (Effect Unit)
listen (Cell ref) l = do
(Cell' cell) <- readRefM ref
W.listen cell.watches l
Loading

0 comments on commit 21ceed7

Please sign in to comment.