The ReplPad Storage module provides an API to the localStorage and sessionStorage objects in JavaScript. It has two main interfaces:
-
Through schemes specific to getting and setting to each object directly,
-
Through a filesystem scheme that attempts to simulate a desktop filesystem.
On initiating the module, these schemes are immediately available:
do %storage.reb
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:
Method | Description | Example |
---|---|---|
poke |
Used to set a key's value |
|
select |
Used to retrieve a key's value |
|
query |
Returns a listing of an object's keys |
|
clear |
Clears all stored values of a given obect |
|
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:
Method | Description | Example |
---|---|---|
create %folder/ |
Creates a new folder |
|
write %file |
Writes content to a given file |
|
read %file |
Read a file's content (returns binary!) |
|
read %folder/ |
Read a folder's content (returns block!) |
|
delete %file |
Deletes a file (returns the full path to a deleted file) |
|
delete %folder/ |
Deletes a folder (so long as the folder is empty) |
|