Skip to content

Commit 1a3b37e

Browse files
committed
call and reply + notify fix
1 parent 84600ce commit 1a3b37e

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

examples/call.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(function () {
2+
// call and reply macro set, v1.0.0
3+
'use strict';
4+
5+
var eventNameSpace = '.userland';
6+
var eventPrefix = ':tw-call-macro-';
7+
8+
function isDef (is) {
9+
return is != undefined;
10+
}
11+
12+
function evType (type) {
13+
return eventPrefix + String(type) + eventNameSpace;
14+
}
15+
16+
function setCall (type, args, test) {
17+
if (isDef(args) && isDef(test)) {
18+
args = Harlowe.helpers.arrayify(arguments, 2);
19+
} else if (isDef(args) && !(args instanceof Array)) {
20+
args = [args];
21+
}
22+
23+
$(document).trigger({
24+
type : evType(type),
25+
args : isDef(args) ? args : undefined
26+
});
27+
}
28+
29+
function removeCall (type) {
30+
$(document).off(evType(type));
31+
}
32+
33+
function reply (type, cb, once, context) {
34+
$(document)[ once ? 'one' : 'on' ](evType(type), function (ev) {
35+
var shallow = Harlowe.variable('args'), deep;
36+
if (shallow instanceof Map) {
37+
deep = new Map(shallow.entries());
38+
} else if (shallow instanceof Set) {
39+
deep = new Set(shallow.entries());
40+
} else if (typeof shallow === 'object') {
41+
deep = JSON.parse(JSON.stringify(shallow));
42+
} else {
43+
deep = shallow;
44+
}
45+
46+
Harlowe.variable('args', ev.args);
47+
// callback
48+
cb.call(context);
49+
Harlowe.variable('args', deep);
50+
});
51+
}
52+
53+
Harlowe.macro('call', function (type, args) {
54+
var err = this.typeCheck([ 'string' ]);
55+
if (err) throw err;
56+
57+
setCall.apply(null, arguments);
58+
});
59+
60+
Harlowe.macro('reply', function (type, once) {
61+
62+
var err = this.typeCheck([ 'string', 'boolean|undefined' ]);
63+
if (err) throw err;
64+
65+
}, function (type, once) {
66+
67+
this.descriptor.enabled = false;
68+
69+
reply(type, function () {
70+
this.enabled = true;
71+
}, !!once, this.descriptor);
72+
73+
});
74+
75+
Harlowe.macro('silence', function (type) {
76+
var err = this.typeCheck([ 'string' ]);
77+
if (err) throw err;
78+
79+
removeCall(type);
80+
});
81+
82+
}());

examples/minified/call.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/minified/notify.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/notify.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106

107107
});
108108

109-
setup.notify = notify;
109+
window.setup = window.setup || {};
110+
111+
window.setup.notify = notify;
110112

111113
}());

0 commit comments

Comments
 (0)