Sync Module¶
-
class
js2p.sync.metatuple(owner, timestamp)¶ This class is used to store metadata for a particular key
Arguments: - owner (string) – The owner of this change
- timestamp (Number) – The time of this change
-
class
js2p.sync.SyncSocket(addr, port[, leasing[, protocol[, out_addr[, debug_level]]]])¶ This is the class for mesh network socket abstraction. It inherits from
js2p.mesh.MeshSocket(). Because of this inheritence, this can also be used as an alert network.This also implements and optional leasing system by default. This leasing system means that if node A sets a key, node B cannot overwrite the value at that key for an hour.
This may be turned off by setting
leasingtofalseto the constructor.Arguments: - addr (string) – The address you’d like to bind to
- port (number) – The port you’d like to bind to
- leasing (boolean) – Whether this class’s leasing system should be enabled (default:
true) - protocol (js2p.base.Protocol) – The subnet you’re looking to connect to
- out_addr (array) – Your outward-facing address
- debug_level (number) – The verbosity of debug prints
-
js2p.sync.SyncSocket Event 'update'(conn, key, new_data, metatuple)¶ This event is triggered when a key is updated in your synchronized dictionary.
new_metawill be an object containing metadata of this change, including the time of change, and who initiated the change.Arguments: - conn (js2p.sync.SyncSocket) – A reference to this abstract socket
- key (Buffer) – The key which has a new value
- new_data – The new value at that key
- new_meta (js2p.sync.metatuple) – Metadata on the key changer
-
js2p.sync.SyncSocket Event 'delete'(conn, key)¶ This event is triggered when a key is deleted from your synchronized dictionary.
Arguments: - conn (js2p.sync.SyncSocket) – A reference to this abstract socket
- key (Buffer) – The key which has a new value
-
js2p.sync.SyncSocket.__store(key, new_data, new_meta, error)¶ Private API method for storing data
Arguments: - key – The key you wish to store data at
- new_data – The data you wish to store in said key
- new_meta – The metadata associated with this storage
- error – A boolean which says whether to raise a
KeyErrorif you can’t store there
Raises Error: If someone else has a lease at this value, and
erroris notfalse
-
js2p.sync.SyncSocket._send_peers(handler)¶ Shortcut method to send a handshake response. This method is extracted from
__handle_handshake()in order to allow cleaner inheritence fromjs2p.sync.SyncSocket()
-
js2p.sync.SyncSocket.__handle_store()¶ This callback is used to deal with data storage signals. Its two primary jobs are:
- store data in a given key
- delete data in a given key
param msg: A Message()param handler: A MeshConnection()returns: Either trueorundefined
-
js2p.sync.SyncSocket.get(key[, fallback])¶ Retrieves the value at a given key
Arguments: - key – The key you wish to look up (must be transformable into a
Buffer()) - fallback – The value it should return when the key has no data
Returns: The value at the given key, or
fallback.Raises TypeError: If the key could not be transformed into a
Buffer()- key – The key you wish to look up (must be transformable into a
-
js2p.sync.SyncSocket.set(key, value)¶ Sets the value at a given key
Arguments: - key – The key you wish to look up (must be transformable into a
Buffer()) - value – The value you wish to store
Raises TypeError: If a key could not be transformed into a
Buffer()Raises: See
__store()- key – The key you wish to look up (must be transformable into a
-
js2p.sync.SyncSocket.update(update_dict)¶ For each key/value pair in the given object, calls
set()Arguments: - update_dict (Object) – An object with keys and values which can be transformed into a
Buffer()
Raises: See
set()- update_dict (Object) – An object with keys and values which can be transformed into a
-
js2p.sync.SyncSocket.del(key)¶ Clears the value at a given key
Arguments: - key – The key you wish to look up (must be transformable into a
Buffer())
Raises TypeError: If a key or value could not be transformed into a
Buffer()Raises: See
set()- key – The key you wish to look up (must be transformable into a
-
js2p.sync.SyncSocket.keys()¶ Returns a generator for all keys presently in the dictionary
Because this data is changed asynchronously, the key is only garunteed to be present at the time of generation.
Returns: A generator which yields Buffer()s
-
js2p.sync.SyncSocket.values()¶ Returns a generator for all values presently in the dictionary
Because this data is changed asynchronously, the value is only garunteed to be accurate at the time of generation.
Returns: A generator which yields Buffer()s
-
js2p.sync.SyncSocket.items()¶ Returns a generator for all associations presently in the dictionary
Because this data is changed asynchronously, the association is only garunteed to be present at the time of generation.
Returns: A generator which yields pairs of Buffer()s
-
js2p.sync.SyncSocket.pop(key[, fallback])¶ Returns the value at a given key. As a side effect, it it deletes that key.
Returns: A Buffer()
-
js2p.sync.SyncSocket.popitem()¶ Returns the association at a key. As a side effect, it it deletes that key.
Returns: A pair of Buffer()s