Skip to content

Latest commit

 

History

History
193 lines (118 loc) · 2.93 KB

storage.md

File metadata and controls

193 lines (118 loc) · 2.93 KB

ReplPad Storage

The ReplPad Storage module provides an API to the localStorage and sessionStorage objects in JavaScript. It has two main interfaces:

  1. Through schemes specific to getting and setting to each object directly,

  2. Through a filesystem scheme that attempts to simulate a desktop filesystem.

On initiating the module, these schemes are immediately available:

do %storage.reb

Storage Scheme

The Storage scheme maps each storage object to its own port. A port! for each object is created upon initiating the Storage module:

storage/local  ; maps to localStorage
storage/session  ; maps to sessionStorage

The storage scheme supports the following methods:

MethodDescriptionExample

poke

Used to set a key's value

poke storage/session "Key" "Value"

select

Used to retrieve a key's value

select storage/session "Key"

query

Returns a listing of an object's keys

query storage/session

clear

Clears all stored values of a given obect

clear storage/session

File Scheme

The File/Dir schemes replaces the inbuilt File/Dir schemes (which are redundant within the browser context) simulating the functionality of those schemes using localStorage and sessionStorage objects in place of the local filesystem. Upon initiating the Storage module, File/Dir schemes are ready to use:

probe read %/
change-dir %/tmp/
write %my-file.txt "Some Text"
probe read %my-file.txt
make-dir %dir/
change-dir %dir/
probe what-dir
probe read %../
write %hello.reb {Rebol [] print "Hello!"}
do %hello.reb

The localStorage object is mounted as %/ and the sessionStorage object is mounted as %/tmp/. Any files stored to %/tmp will be lost once the current browser session is closed.

The storage scheme supports the following methods, amongst others:

MethodDescriptionExample

create %folder/

Creates a new folder

; MAKE-DIR wraps CREATE
make-dir %/folder/

write %file

Writes content to a given file

write %/folder/file.txt "Some Text"

read %file

Read a file's content (returns binary!)

read %/folder/file.txt

read %folder/

Read a folder's content (returns block!)

read %/folder/

delete %file

Deletes a file (returns the full path to a deleted file)

delete %/folder/file.txt

delete %folder/

Deletes a folder (so long as the folder is empty)

delete %/folder/