Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Added watching and compare-and-set! for atoms #532

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates to the Atom class
Added the IWatch#-with-watch and the IValidate#-with-validator methods.
  • Loading branch information
egregius313 authored Jun 16, 2017
commit 4fb90f615576fede18de668b3b96eea9a29d5d14
37 changes: 35 additions & 2 deletions pixie/vm/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,41 @@ def with_meta(self, meta):
def meta(self):
return self._meta

def __init__(self, boxed_value, meta=nil):
def with_validator(self, validator):
return Atom(self._boxed_value,
self._meta,
self._watch_key,
self._watch_fn,
validator=validator)

def with_watch(self, watch_key, watch_fn):
return Atom(self._boxed_value,
self._meta,
watch_key,
watch_fn,
self._validator)

def __init__(self, boxed_value,
meta=nil,
watch_key=nil, watch_fn=nil,
validator=nil):
self._boxed_value = boxed_value
self._meta = meta
self._watch_key = watch_key
self._watch_fn = watch_fn
self._validator = validator


@extend(proto._reset_BANG_, Atom)
def _reset(self, v):
assert isinstance(self, Atom)
if self._validator is not nil:
affirm(self._validator.invoke([v]), u"Invalid State Exception: Invalid reference state.")
if self._watch_fn is not nil:
self._watch_fn.invoke([self._watch_key, self, self._boxed_value, v])
self._boxed_value = v
return v


@extend(proto._deref, Atom)
def _deref(self):
assert isinstance(self, Atom)
Expand All @@ -40,6 +63,16 @@ def _with_meta(self, meta):
assert isinstance(self, Atom)
return self.with_meta(meta)

@extend(proto._with_watch, Atom)
def _with_watch(self, watch_key, watch_fn):
assert isinstance(self, Atom)
return self.with_watch(watch_key, watch_fn)

@extend(proto._with_validator, Atom)
def _with_validator(self, validate_fn):
assert isinstance(self, Atom)
return self.with_validator(validate_fn)

@as_var("atom")
def atom(val=nil):
return Atom(val)