This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tink.reactive.signals; | ||
import tink.collections.maps.FunctionMap; | ||
|
||
/** | ||
* ... | ||
* @author back2dos | ||
*/ | ||
|
||
interface Signal<T> { | ||
function watch(handler:T->Dynamic):Void; | ||
function unwatch(handler:T->Dynamic):Void; | ||
} | ||
|
||
class SimpleSignal<T> implements Signal<T> { | ||
var handlers:FunctionMap<T->Dynamic, T->Dynamic>; | ||
public function new() { | ||
this.handlers = new FunctionMap(); | ||
} | ||
public function watch(handler) { | ||
handlers.set(handler, handler); | ||
} | ||
public function unwatch(handler) { | ||
handlers.remove(handler); | ||
} | ||
public function fire(data:T) { | ||
for (h in handlers.keys()) h(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tink.reactive.signals.macros; | ||
|
||
/** | ||
* ... | ||
* @author back2dos | ||
*/ | ||
import tink.macro.build.Member; | ||
import tink.macro.build.MemberTransformer; | ||
import haxe.macro.Expr; | ||
|
||
using tink.macro.tools.MacroTools; | ||
|
||
class SignalBuilder { | ||
static inline var SIGNAL = ':signal'; | ||
static public function make(ctx:ClassBuildContext) { | ||
for (member in ctx.members) | ||
switch (member.extractMeta(SIGNAL)) { | ||
case Success(tag): | ||
switch (member.kind) { | ||
case FVar(t, e): | ||
if (e != null) e.reject(); | ||
if (t == null) member.pos.error('type required'); | ||
|
||
var own = '_' + member.name; | ||
ctx.add(Member.plain(own, 'tink.reactive.signals.Signal.SimpleSignal'.asComplexType([TPType(t)]), tag.pos)); | ||
|
||
member.kind = FProp('default', 'null', 'tink.reactive.signals.Signal'.asComplexType([TPType(t)])); | ||
ctx.getCtor().init(member.name, tag.pos, own.resolve(tag.pos), true); | ||
ctx.getCtor().init(own, tag.pos, 'tink.reactive.signals.Signal.SimpleSignal'.instantiate(tag.pos), true); | ||
member.publish(); | ||
default: | ||
member.pos.error('can only declare signals on variables'); | ||
} | ||
default: | ||
} | ||
} | ||
} |