Skip to content

Commit

Permalink
Add new and run to Record.ST (#71)
Browse files Browse the repository at this point in the history
* Add Record.ST.new

* Add Record.ST.run

* Copy changes to Foreign.Object.run documentation
  • Loading branch information
kl0tl authored Dec 23, 2020
1 parent 4e864ba commit d1bc42e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Record/ST.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
"use strict";

exports.run = function (f) {
return f();
};

exports["new"] = function () {
return {};
};

function copyRecord(rec) {
var copy = {};
for (var key in rec) {
Expand Down
11 changes: 11 additions & 0 deletions src/Record/ST.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Record.ST
( STRecord
, run
, new
, freeze
, thaw
, peek
Expand All @@ -21,6 +23,15 @@ foreign import data STRecord :: Region -> Row Type -> Type

type role STRecord nominal representational

-- | Freeze a mutable record, creating an immutable record. Use this function as you would use
-- | `Control.Monad.ST.run` (from the `purescript-st` package) to freeze a mutable reference.
-- |
-- | The rank-2 type prevents the record from escaping the scope of `run`.
foreign import run :: forall r. (forall h. ST h (STRecord h r)) -> Record r

-- | Create a new, empty mutable record
foreign import new :: forall h. ST h (STRecord h ())

-- | Freeze a mutable record, creating a copy.
foreign import freeze :: forall h r. STRecord h r -> ST h (Record r)

Expand Down
7 changes: 3 additions & 4 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import Prelude
import Effect (Effect)
import Record (delete, equal, get, insert, merge, modify, rename, set)
import Record.Builder as Builder
import Control.Monad.ST (run) as ST
import Record.ST (poke, thaw, freeze, modify) as ST
import Record.ST (run, poke, thaw, modify) as ST
import Record.Unsafe (unsafeHas)
import Test.Assert (assert')
import Type.Proxy (Proxy(..))
Expand Down Expand Up @@ -45,11 +44,11 @@ main = do
rec <- ST.thaw { x: 41, y: "" }
ST.poke x 42 rec
ST.poke y "testing" rec
ST.freeze rec
pure rec
stTest2 = ST.run do
rec <- ST.thaw { x: 41 }
ST.modify x (_ + 1) rec
ST.freeze rec
pure rec

assert' "pokeSTRecord" $
stTest1.x == 42 && stTest1.y == "testing"
Expand Down

0 comments on commit d1bc42e

Please sign in to comment.