@talyaron1/mithril 0.0.0-stage → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/api/mount-redraw.js +49 -0
- package/api/router.js +252 -0
- package/browser.js +5 -0
- package/hyperscript.js +8 -0
- package/index.js +26 -0
- package/mithril.js +1775 -0
- package/mithril.min.js +1 -0
- package/mount-redraw.js +5 -0
- package/mount.js +3 -0
- package/package.json +36 -4
- package/pathname/build.js +45 -0
- package/pathname/compileTemplate.js +44 -0
- package/pathname/parse.js +25 -0
- package/querystring/build.js +26 -0
- package/querystring/parse.js +48 -0
- package/redraw.js +3 -0
- package/render/cachedAttrsIsStaticMap.js +12 -0
- package/render/delayedRemoval.js +3 -0
- package/render/domFor.js +24 -0
- package/render/emptyAttrs.js +4 -0
- package/render/fragment.js +13 -0
- package/render/hyperscript.js +101 -0
- package/render/hyperscriptVnode.js +23 -0
- package/render/render.js +964 -0
- package/render/trust.js +8 -0
- package/render/vnode.js +34 -0
- package/render.js +3 -0
- package/request/request.js +205 -0
- package/request.js +5 -0
- package/route.js +5 -0
- package/stream/stream.js +267 -0
- package/stream/stream.min.js +1 -0
- package/stream.js +3 -0
- package/util/censor.js +47 -0
- package/util/decodeURIComponentSafe.js +35 -0
- package/util/hasOwn.js +4 -0
package/render/trust.js
ADDED
package/render/vnode.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
function Vnode(tag, key, attrs, children, text, dom) {
|
|
4
|
+
return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, is: undefined, domSize: undefined, state: undefined, events: undefined, instance: undefined}
|
|
5
|
+
}
|
|
6
|
+
Vnode.normalize = function(node) {
|
|
7
|
+
if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined)
|
|
8
|
+
if (node == null || typeof node === "boolean") return null
|
|
9
|
+
if (typeof node === "object") return node
|
|
10
|
+
return Vnode("#", undefined, undefined, String(node), undefined, undefined)
|
|
11
|
+
}
|
|
12
|
+
Vnode.normalizeChildren = function(input) {
|
|
13
|
+
// Preallocate the array length (initially holey) and fill every index immediately in order.
|
|
14
|
+
// Benchmarking shows better performance on V8.
|
|
15
|
+
var children = new Array(input.length)
|
|
16
|
+
// Count the number of keyed normalized vnodes for consistency check.
|
|
17
|
+
// Note: this is a perf-sensitive check.
|
|
18
|
+
// Fun fact: merging the loop like this is somehow faster than splitting
|
|
19
|
+
// the check within updateNodes(), noticeably so.
|
|
20
|
+
var numKeyed = 0
|
|
21
|
+
for (var i = 0; i < input.length; i++) {
|
|
22
|
+
children[i] = Vnode.normalize(input[i])
|
|
23
|
+
if (children[i] !== null && children[i].key != null) numKeyed++
|
|
24
|
+
}
|
|
25
|
+
if (numKeyed !== 0 && numKeyed !== input.length) {
|
|
26
|
+
throw new TypeError(children.includes(null)
|
|
27
|
+
? "In fragments, vnodes must either all have keys or none have keys. You may wish to consider using an explicit keyed empty fragment, m.fragment({key: ...}), instead of a hole."
|
|
28
|
+
: "In fragments, vnodes must either all have keys or none have keys."
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
return children
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = Vnode
|
package/render.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
var buildPathname = require("../pathname/build")
|
|
4
|
+
var hasOwn = require("../util/hasOwn")
|
|
5
|
+
|
|
6
|
+
module.exports = function($window, oncompletion) {
|
|
7
|
+
function PromiseProxy(executor) {
|
|
8
|
+
return new Promise(executor)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function makeRequest(url, args) {
|
|
12
|
+
return new Promise(function(resolve, reject) {
|
|
13
|
+
url = buildPathname(url, args.params)
|
|
14
|
+
var method = args.method != null ? args.method.toUpperCase() : "GET"
|
|
15
|
+
var body = args.body
|
|
16
|
+
var assumeJSON = (args.serialize == null || args.serialize === JSON.stringify) && !(body instanceof $window.FormData || body instanceof $window.URLSearchParams)
|
|
17
|
+
var responseType = args.responseType || (typeof args.extract === "function" ? "" : "json")
|
|
18
|
+
|
|
19
|
+
var xhr = new $window.XMLHttpRequest(), aborted = false, isTimeout = false
|
|
20
|
+
var original = xhr, replacedAbort
|
|
21
|
+
var abort = xhr.abort
|
|
22
|
+
|
|
23
|
+
xhr.abort = function() {
|
|
24
|
+
aborted = true
|
|
25
|
+
abort.call(this)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
xhr.open(method, url, args.async !== false, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
|
|
29
|
+
|
|
30
|
+
if (assumeJSON && body != null && !hasHeader(args, "content-type")) {
|
|
31
|
+
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
|
32
|
+
}
|
|
33
|
+
if (typeof args.deserialize !== "function" && !hasHeader(args, "accept")) {
|
|
34
|
+
xhr.setRequestHeader("Accept", "application/json, text/*")
|
|
35
|
+
}
|
|
36
|
+
if (args.withCredentials) xhr.withCredentials = args.withCredentials
|
|
37
|
+
if (args.timeout) xhr.timeout = args.timeout
|
|
38
|
+
xhr.responseType = responseType
|
|
39
|
+
|
|
40
|
+
for (var key in args.headers) {
|
|
41
|
+
if (hasOwn.call(args.headers, key)) {
|
|
42
|
+
xhr.setRequestHeader(key, args.headers[key])
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
xhr.onreadystatechange = function(ev) {
|
|
47
|
+
// Don't throw errors on xhr.abort().
|
|
48
|
+
if (aborted) return
|
|
49
|
+
|
|
50
|
+
if (ev.target.readyState === 4) {
|
|
51
|
+
try {
|
|
52
|
+
var success = (ev.target.status >= 200 && ev.target.status < 300) || ev.target.status === 304 || (/^file:\/\//i).test(url)
|
|
53
|
+
// When the response type isn't "" or "text",
|
|
54
|
+
// `xhr.responseText` is the wrong thing to use.
|
|
55
|
+
// Browsers do the right thing and throw here, and we
|
|
56
|
+
// should honor that and do the right thing by
|
|
57
|
+
// preferring `xhr.response` where possible/practical.
|
|
58
|
+
var response = ev.target.response, message
|
|
59
|
+
|
|
60
|
+
if (responseType === "json") {
|
|
61
|
+
// For IE and Edge, which don't implement
|
|
62
|
+
// `responseType: "json"`.
|
|
63
|
+
if (!ev.target.responseType && typeof args.extract !== "function") {
|
|
64
|
+
// Handle no-content which will not parse.
|
|
65
|
+
try { response = JSON.parse(ev.target.responseText) }
|
|
66
|
+
catch (e) { response = null }
|
|
67
|
+
}
|
|
68
|
+
} else if (!responseType || responseType === "text") {
|
|
69
|
+
// Only use this default if it's text. If a parsed
|
|
70
|
+
// document is needed on old IE and friends (all
|
|
71
|
+
// unsupported), the user should use a custom
|
|
72
|
+
// `config` instead. They're already using this at
|
|
73
|
+
// their own risk.
|
|
74
|
+
if (response == null) response = ev.target.responseText
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof args.extract === "function") {
|
|
78
|
+
response = args.extract(ev.target, args)
|
|
79
|
+
success = true
|
|
80
|
+
} else if (typeof args.deserialize === "function") {
|
|
81
|
+
response = args.deserialize(response)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (success) {
|
|
85
|
+
if (typeof args.type === "function") {
|
|
86
|
+
if (Array.isArray(response)) {
|
|
87
|
+
for (var i = 0; i < response.length; i++) {
|
|
88
|
+
response[i] = new args.type(response[i])
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else response = new args.type(response)
|
|
92
|
+
}
|
|
93
|
+
resolve(response)
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
var completeErrorResponse = function() {
|
|
97
|
+
try { message = ev.target.responseText }
|
|
98
|
+
catch (e) { message = response }
|
|
99
|
+
// Error() stringifies non-strings poorly (e.g. [object Object]);
|
|
100
|
+
// responseText is also unavailable for responseType json/arraybuffer/etc.
|
|
101
|
+
if (typeof message !== "string") {
|
|
102
|
+
try { message = JSON.stringify(message) }
|
|
103
|
+
catch (stringifyError) { message = String(message) }
|
|
104
|
+
}
|
|
105
|
+
var error = new Error(message)
|
|
106
|
+
error.code = ev.target.status
|
|
107
|
+
error.response = response
|
|
108
|
+
reject(error)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (xhr.status === 0) {
|
|
112
|
+
// Use setTimeout to push this code block onto the event queue
|
|
113
|
+
// This allows `xhr.ontimeout` to run in the case that there is a timeout
|
|
114
|
+
// Without this setTimeout, `xhr.ontimeout` doesn't have a chance to reject
|
|
115
|
+
// as `xhr.onreadystatechange` will run before it
|
|
116
|
+
setTimeout(function() {
|
|
117
|
+
if (isTimeout) return
|
|
118
|
+
completeErrorResponse()
|
|
119
|
+
})
|
|
120
|
+
} else completeErrorResponse()
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
reject(e)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
xhr.ontimeout = function (ev) {
|
|
130
|
+
isTimeout = true
|
|
131
|
+
var error = new Error("Request timed out")
|
|
132
|
+
error.code = ev.target.status
|
|
133
|
+
reject(error)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (typeof args.config === "function") {
|
|
137
|
+
xhr = args.config(xhr, args, url) || xhr
|
|
138
|
+
|
|
139
|
+
// Propagate the `abort` to any replacement XHR as well.
|
|
140
|
+
if (xhr !== original) {
|
|
141
|
+
replacedAbort = xhr.abort
|
|
142
|
+
xhr.abort = function() {
|
|
143
|
+
aborted = true
|
|
144
|
+
replacedAbort.call(this)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (body == null) xhr.send()
|
|
150
|
+
else if (typeof args.serialize === "function") xhr.send(args.serialize(body))
|
|
151
|
+
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
|
|
152
|
+
else xhr.send(JSON.stringify(body))
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// In case the global Promise is some userland library's where they rely on
|
|
157
|
+
// `foo instanceof this.constructor`, `this.constructor.resolve(value)`, or
|
|
158
|
+
// similar. Let's *not* break them.
|
|
159
|
+
PromiseProxy.prototype = Promise.prototype
|
|
160
|
+
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto
|
|
161
|
+
|
|
162
|
+
function hasHeader(args, name) {
|
|
163
|
+
for (var key in args.headers) {
|
|
164
|
+
if (hasOwn.call(args.headers, key) && key.toLowerCase() === name) return true
|
|
165
|
+
}
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
request: function(url, args) {
|
|
171
|
+
if (typeof url !== "string") { args = url; url = url.url }
|
|
172
|
+
else if (args == null) args = {}
|
|
173
|
+
var promise = makeRequest(url, args)
|
|
174
|
+
if (args.background === true) return promise
|
|
175
|
+
var count = 0
|
|
176
|
+
function complete() {
|
|
177
|
+
if (--count === 0 && typeof oncompletion === "function") oncompletion()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return wrap(promise)
|
|
181
|
+
|
|
182
|
+
function wrap(promise) {
|
|
183
|
+
var then = promise.then
|
|
184
|
+
// Set the constructor, so engines know to not await or resolve
|
|
185
|
+
// this as a native promise. At the time of writing, this is
|
|
186
|
+
// only necessary for V8, but their behavior is the correct
|
|
187
|
+
// behavior per spec. See this spec issue for more details:
|
|
188
|
+
// https://github.com/tc39/ecma262/issues/1577. Also, see the
|
|
189
|
+
// corresponding comment in `request/tests/test-request.js` for
|
|
190
|
+
// a bit more background on the issue at hand.
|
|
191
|
+
promise.constructor = PromiseProxy
|
|
192
|
+
promise.then = function() {
|
|
193
|
+
count++
|
|
194
|
+
var next = then.apply(promise, arguments)
|
|
195
|
+
next.then(complete, function(e) {
|
|
196
|
+
complete()
|
|
197
|
+
if (count === 0) throw e
|
|
198
|
+
})
|
|
199
|
+
return wrap(next)
|
|
200
|
+
}
|
|
201
|
+
return promise
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
package/request.js
ADDED
package/route.js
ADDED
package/stream/stream.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
;(function() {
|
|
3
|
+
"use strict"
|
|
4
|
+
/* eslint-enable */
|
|
5
|
+
Stream.SKIP = {}
|
|
6
|
+
Stream.lift = lift
|
|
7
|
+
Stream.scan = scan
|
|
8
|
+
Stream.merge = merge
|
|
9
|
+
Stream.combine = combine
|
|
10
|
+
Stream.scanMerge = scanMerge
|
|
11
|
+
Stream["fantasy-land/of"] = Stream
|
|
12
|
+
|
|
13
|
+
var warnedHalt = false
|
|
14
|
+
Object.defineProperty(Stream, "HALT", {
|
|
15
|
+
get: function() {
|
|
16
|
+
warnedHalt || console.log("HALT is deprecated and has been renamed to SKIP");
|
|
17
|
+
warnedHalt = true
|
|
18
|
+
return Stream.SKIP
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
function Stream(value) {
|
|
23
|
+
var dependentStreams = []
|
|
24
|
+
var dependentFns = []
|
|
25
|
+
// Incremented on every update, to detect an update made from inside a callback.
|
|
26
|
+
var version = 0
|
|
27
|
+
// The state to go back to when an announced change doesn't arrive.
|
|
28
|
+
var settledState
|
|
29
|
+
|
|
30
|
+
function stream(v) {
|
|
31
|
+
if (arguments.length) {
|
|
32
|
+
if (v !== Stream.SKIP) {
|
|
33
|
+
value = v
|
|
34
|
+
var current = ++version
|
|
35
|
+
if (open(stream)) {
|
|
36
|
+
// Not `stream._changing()`: `combine` overrides that one to forward
|
|
37
|
+
// announcements coming from upstream, which this is not.
|
|
38
|
+
changing()
|
|
39
|
+
stream._state = "active"
|
|
40
|
+
// Cloning the lists to ensure they're still iterated in intended
|
|
41
|
+
// order
|
|
42
|
+
var streams = dependentStreams.slice()
|
|
43
|
+
var fns = dependentFns.slice()
|
|
44
|
+
var i = 0
|
|
45
|
+
try {
|
|
46
|
+
for (; i < streams.length; i++) {
|
|
47
|
+
if (open(streams[i])) {
|
|
48
|
+
var result = fns[i](value)
|
|
49
|
+
// The callback updated this stream. That update has already
|
|
50
|
+
// reached every dependent, so the rest of this one is stale.
|
|
51
|
+
if (version !== current) break
|
|
52
|
+
streams[i](result)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} finally {
|
|
56
|
+
// Only reached with `i < streams.length` if a callback threw or
|
|
57
|
+
// this update was superseded. Either way, the remaining
|
|
58
|
+
// dependents won't be updated by it.
|
|
59
|
+
for (; i < streams.length; i++) streams[i]._unchanging(true)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// The change announced by `_changing()` was skipped.
|
|
64
|
+
else if (stream._state === "changing") stream._unchanging(false)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return value
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
stream.constructor = Stream
|
|
71
|
+
stream._state = arguments.length && value !== Stream.SKIP ? "active" : "pending"
|
|
72
|
+
stream._parents = []
|
|
73
|
+
|
|
74
|
+
// Announces that this stream and everything downstream is about to change.
|
|
75
|
+
// `combine` uses it to wait for all its sources before updating.
|
|
76
|
+
function changing() {
|
|
77
|
+
if (open(stream)) {
|
|
78
|
+
if (stream._state !== "changing") settledState = stream._state
|
|
79
|
+
stream._state = "changing"
|
|
80
|
+
}
|
|
81
|
+
dependentStreams.forEach(function(s) {
|
|
82
|
+
s._changing()
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
stream._changing = changing
|
|
86
|
+
|
|
87
|
+
// Takes back the announcement, for this stream and everything downstream.
|
|
88
|
+
// `silent` is set while an error is propagating: no callback may run then.
|
|
89
|
+
stream._unchanging = function(silent) {
|
|
90
|
+
if (stream._state === "changing") stream._state = settledState
|
|
91
|
+
dependentStreams.slice().forEach(function(s) {
|
|
92
|
+
s._unchanging(silent)
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
stream._map = function(fn, ignoreInitial) {
|
|
97
|
+
var target = Stream()
|
|
98
|
+
// Register the target first, so it isn't missed if `fn` updates this
|
|
99
|
+
// stream while computing the initial value.
|
|
100
|
+
target._parents.push(stream)
|
|
101
|
+
dependentStreams.push(target)
|
|
102
|
+
dependentFns.push(fn)
|
|
103
|
+
if (!ignoreInitial) {
|
|
104
|
+
var initialVersion = version
|
|
105
|
+
var initial, done = false
|
|
106
|
+
try {
|
|
107
|
+
initial = fn(value)
|
|
108
|
+
done = true
|
|
109
|
+
} finally {
|
|
110
|
+
if (!done) stream._unregisterChild(target)
|
|
111
|
+
}
|
|
112
|
+
// If `fn` updated this stream, the target was already offered the newer
|
|
113
|
+
// value, and this one is stale.
|
|
114
|
+
if (version === initialVersion) target(initial)
|
|
115
|
+
}
|
|
116
|
+
return target
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
stream.map = function(fn) {
|
|
120
|
+
return stream._map(fn, stream._state !== "active")
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
var end
|
|
124
|
+
function createEnd() {
|
|
125
|
+
end = Stream()
|
|
126
|
+
end.map(function(value) {
|
|
127
|
+
if (value === true) {
|
|
128
|
+
stream._parents.forEach(function (p) {p._unregisterChild(stream)})
|
|
129
|
+
stream._state = "ended"
|
|
130
|
+
stream._parents.length = dependentStreams.length = dependentFns.length = 0
|
|
131
|
+
}
|
|
132
|
+
return value
|
|
133
|
+
})
|
|
134
|
+
return end
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
stream.toJSON = function() { return value != null && typeof value.toJSON === "function" ? value.toJSON() : value }
|
|
138
|
+
|
|
139
|
+
stream["fantasy-land/map"] = stream.map
|
|
140
|
+
stream["fantasy-land/ap"] = function(x) { return combine(function(s1, s2) { return s1()(s2()) }, [x, stream]) }
|
|
141
|
+
|
|
142
|
+
stream._unregisterChild = function(child) {
|
|
143
|
+
var childIndex = dependentStreams.indexOf(child)
|
|
144
|
+
if (childIndex !== -1) {
|
|
145
|
+
dependentStreams.splice(childIndex, 1)
|
|
146
|
+
dependentFns.splice(childIndex, 1)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
Object.defineProperty(stream, "end", {
|
|
151
|
+
get: function() { return end || createEnd() }
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
return stream
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function combine(fn, streams) {
|
|
158
|
+
var ready = streams.every(function(s) {
|
|
159
|
+
if (s.constructor !== Stream)
|
|
160
|
+
throw new Error("Ensure that each item passed to stream.combine/stream.merge/lift is a stream.")
|
|
161
|
+
return s._state === "active"
|
|
162
|
+
})
|
|
163
|
+
var stream = ready
|
|
164
|
+
? Stream(fn.apply(null, streams.concat([streams])))
|
|
165
|
+
: Stream()
|
|
166
|
+
|
|
167
|
+
var changed = []
|
|
168
|
+
|
|
169
|
+
// Updates the combined stream once every change announced upstream has
|
|
170
|
+
// arrived, so that it runs once per upstream update and never sees a mix of
|
|
171
|
+
// old and new values. `current` is the mapper being updated, if any.
|
|
172
|
+
function settle(current) {
|
|
173
|
+
for (var i = 0; i < mappers.length; i++) {
|
|
174
|
+
if (mappers[i] !== current && mappers[i]._state === "changing") return
|
|
175
|
+
}
|
|
176
|
+
if (changed.length !== 0 && (ready || streams.every(function(s) { return s._state !== "pending" }))) {
|
|
177
|
+
ready = true
|
|
178
|
+
var list = changed
|
|
179
|
+
changed = []
|
|
180
|
+
stream(fn.apply(null, streams.concat([list])))
|
|
181
|
+
}
|
|
182
|
+
// Nothing to emit, so take back what was announced downstream.
|
|
183
|
+
else stream._unchanging(false)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
var mappers = streams.map(function(s) {
|
|
187
|
+
var mapper = s._map(function(value) {
|
|
188
|
+
changed.push(s)
|
|
189
|
+
settle(mapper)
|
|
190
|
+
return value
|
|
191
|
+
}, true)
|
|
192
|
+
var changing = mapper._changing
|
|
193
|
+
var unchanging = mapper._unchanging
|
|
194
|
+
// The combined stream isn't a regular dependent of the mappers, so the
|
|
195
|
+
// announcements have to be forwarded to it by hand.
|
|
196
|
+
mapper._changing = function() {
|
|
197
|
+
changing()
|
|
198
|
+
stream._changing()
|
|
199
|
+
}
|
|
200
|
+
mapper._unchanging = function(silent) {
|
|
201
|
+
unchanging(silent)
|
|
202
|
+
if (silent) stream._unchanging(true)
|
|
203
|
+
else settle()
|
|
204
|
+
}
|
|
205
|
+
return mapper
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
var endStream = stream.end.map(function(value) {
|
|
209
|
+
if (value === true) {
|
|
210
|
+
mappers.forEach(function(mapper) { mapper.end(true) })
|
|
211
|
+
endStream.end(true)
|
|
212
|
+
}
|
|
213
|
+
return undefined
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
return stream
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function merge(streams) {
|
|
220
|
+
return combine(function() { return streams.map(function(s) { return s() }) }, streams)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function scan(fn, acc, origin) {
|
|
224
|
+
var stream = origin.map(function(v) {
|
|
225
|
+
var next = fn(acc, v)
|
|
226
|
+
if (next !== Stream.SKIP) acc = next
|
|
227
|
+
return next
|
|
228
|
+
})
|
|
229
|
+
stream(acc)
|
|
230
|
+
return stream
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function scanMerge(tuples, seed) {
|
|
234
|
+
var streams = tuples.map(function(tuple) { return tuple[0] })
|
|
235
|
+
|
|
236
|
+
var stream = combine(function() {
|
|
237
|
+
var changed = arguments[arguments.length - 1]
|
|
238
|
+
streams.forEach(function(stream, i) {
|
|
239
|
+
if (changed.indexOf(stream) > -1)
|
|
240
|
+
seed = tuples[i][1](seed, stream())
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
return seed
|
|
244
|
+
}, streams)
|
|
245
|
+
|
|
246
|
+
stream(seed)
|
|
247
|
+
|
|
248
|
+
return stream
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function lift() {
|
|
252
|
+
var fn = arguments[0]
|
|
253
|
+
var streams = Array.prototype.slice.call(arguments, 1)
|
|
254
|
+
return merge(streams).map(function(streams) {
|
|
255
|
+
return fn.apply(undefined, streams)
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function open(s) {
|
|
260
|
+
return s._state === "pending" || s._state === "active" || s._state === "changing"
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (typeof module !== "undefined") module["exports"] = Stream
|
|
264
|
+
else if (typeof window.m === "function" && !("stream" in window.m)) window.m.stream = Stream
|
|
265
|
+
else window.m = {stream : Stream}
|
|
266
|
+
|
|
267
|
+
}());
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(){"use strict";t.SKIP={},t.lift=function(){var n=arguments[0],t=Array.prototype.slice.call(arguments,1);return r(t).map((function(t){return n.apply(void 0,t)}))},t.scan=function(n,e,r){var a=r.map((function(r){var a=n(e,r);return a!==t.SKIP&&(e=a),a}));return a(e),a},t.merge=r,t.combine=e,t.scanMerge=function(n,t){var r=n.map((function(n){return n[0]})),a=e((function(){var e=arguments[arguments.length-1];return r.forEach((function(r,a){e.indexOf(r)>-1&&(t=n[a][1](t,r()))})),t}),r);return a(t),a},t["fantasy-land/of"]=t;var n=!1;function t(n){var r,i,c=[],u=[],o=0;function f(e){if(arguments.length)if(e!==t.SKIP){n=e;var r=++o;if(a(f)){g(),f._state="active";var i=c.slice(),s=u.slice(),h=0;try{for(;h<i.length;h++)if(a(i[h])){var l=s[h](n);if(o!==r)break;i[h](l)}}finally{for(;h<i.length;h++)i[h]._unchanging(!0)}}}else"changing"===f._state&&f._unchanging(!1);return n}function g(){a(f)&&("changing"!==f._state&&(r=f._state),f._state="changing"),c.forEach((function(n){n._changing()}))}return f.constructor=t,f._state=arguments.length&&n!==t.SKIP?"active":"pending",f._parents=[],f._changing=g,f._unchanging=function(n){"changing"===f._state&&(f._state=r),c.slice().forEach((function(t){t._unchanging(n)}))},f._map=function(e,r){var a=t();if(a._parents.push(f),c.push(a),u.push(e),!r){var i,g=o,s=!1;try{i=e(n),s=!0}finally{s||f._unregisterChild(a)}o===g&&a(i)}return a},f.map=function(n){return f._map(n,"active"!==f._state)},f.toJSON=function(){return null!=n&&"function"==typeof n.toJSON?n.toJSON():n},f["fantasy-land/map"]=f.map,f["fantasy-land/ap"]=function(n){return e((function(n,t){return n()(t())}),[n,f])},f._unregisterChild=function(n){var t=c.indexOf(n);-1!==t&&(c.splice(t,1),u.splice(t,1))},Object.defineProperty(f,"end",{get:function(){return i||((i=t()).map((function(n){return!0===n&&(f._parents.forEach((function(n){n._unregisterChild(f)})),f._state="ended",f._parents.length=c.length=u.length=0),n})),i)}}),f}function e(n,e){var r=e.every((function(n){if(n.constructor!==t)throw new Error("Ensure that each item passed to stream.combine/stream.merge/lift is a stream.");return"active"===n._state})),a=r?t(n.apply(null,e.concat([e]))):t(),i=[];function c(t){for(var c=0;c<u.length;c++)if(u[c]!==t&&"changing"===u[c]._state)return;if(0!==i.length&&(r||e.every((function(n){return"pending"!==n._state})))){r=!0;var o=i;i=[],a(n.apply(null,e.concat([o])))}else a._unchanging(!1)}var u=e.map((function(n){var t=n._map((function(e){return i.push(n),c(t),e}),!0),e=t._changing,r=t._unchanging;return t._changing=function(){e(),a._changing()},t._unchanging=function(n){r(n),n?a._unchanging(!0):c()},t})),o=a.end.map((function(n){!0===n&&(u.forEach((function(n){n.end(!0)})),o.end(!0))}));return a}function r(n){return e((function(){return n.map((function(n){return n()}))}),n)}function a(n){return"pending"===n._state||"active"===n._state||"changing"===n._state}Object.defineProperty(t,"HALT",{get:function(){return n||console.log("HALT is deprecated and has been renamed to SKIP"),n=!0,t.SKIP}}),"undefined"!=typeof module?module.exports=t:"function"!=typeof window.m||"stream"in window.m?window.m={stream:t}:window.m.stream=t}();
|
package/stream.js
ADDED
package/util/censor.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
// Note: this is mildly perf-sensitive.
|
|
4
|
+
//
|
|
5
|
+
// It does *not* use `delete` - dynamic `delete`s usually cause objects to bail
|
|
6
|
+
// out into dictionary mode and just generally cause a bunch of optimization
|
|
7
|
+
// issues within engines.
|
|
8
|
+
//
|
|
9
|
+
// Ideally, I would've preferred to do this, if it weren't for the optimization
|
|
10
|
+
// issues:
|
|
11
|
+
//
|
|
12
|
+
// ```js
|
|
13
|
+
// const hasOwn = require("./hasOwn")
|
|
14
|
+
// const magic = [
|
|
15
|
+
// "key", "oninit", "oncreate", "onbeforeupdate", "onupdate",
|
|
16
|
+
// "onbeforeremove", "onremove",
|
|
17
|
+
// ]
|
|
18
|
+
// module.exports = (attrs, extras) => {
|
|
19
|
+
// const result = Object.assign(Object.create(null), attrs)
|
|
20
|
+
// for (const key of magic) delete result[key]
|
|
21
|
+
// if (extras != null) for (const key of extras) delete result[key]
|
|
22
|
+
// return result
|
|
23
|
+
// }
|
|
24
|
+
// ```
|
|
25
|
+
|
|
26
|
+
var hasOwn = require("./hasOwn")
|
|
27
|
+
var magic = /^(?:key|oninit|oncreate|onbeforeupdate|onupdate|onbeforeremove|onremove)$/
|
|
28
|
+
|
|
29
|
+
module.exports = function(attrs, extras) {
|
|
30
|
+
var result = {}
|
|
31
|
+
|
|
32
|
+
if (extras != null) {
|
|
33
|
+
for (var key in attrs) {
|
|
34
|
+
if (hasOwn.call(attrs, key) && !magic.test(key) && extras.indexOf(key) < 0) {
|
|
35
|
+
result[key] = attrs[key]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
for (var key in attrs) {
|
|
40
|
+
if (hasOwn.call(attrs, key) && !magic.test(key)) {
|
|
41
|
+
result[key] = attrs[key]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return result
|
|
47
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Percent encodings encode UTF-8 bytes, so this regexp needs to match that.
|
|
5
|
+
Here's how UTF-8 encodes stuff:
|
|
6
|
+
- `00-7F`: 1-byte, for U+0000-U+007F
|
|
7
|
+
- `C2-DF 80-BF`: 2-byte, for U+0080-U+07FF
|
|
8
|
+
- `E0-EF 80-BF 80-BF`: 3-byte, encodes U+0800-U+FFFF
|
|
9
|
+
- `F0-F4 80-BF 80-BF 80-BF`: 4-byte, encodes U+10000-U+10FFFF
|
|
10
|
+
In this, there's a number of invalid byte sequences:
|
|
11
|
+
- `80-BF`: Continuation byte, invalid as start
|
|
12
|
+
- `C0-C1 80-BF`: Overlong encoding for U+0000-U+007F
|
|
13
|
+
- `E0 80-9F 80-BF`: Overlong encoding for U+0080-U+07FF
|
|
14
|
+
- `ED A0-BF 80-BF`: Encoding for UTF-16 surrogate U+D800-U+DFFF
|
|
15
|
+
- `F0 80-8F 80-BF 80-BF`: Overlong encoding for U+0800-U+FFFF
|
|
16
|
+
- `F4 90-BF`: RFC 3629 restricted UTF-8 to only code points UTF-16 could encode.
|
|
17
|
+
- `F5-FF`: RFC 3629 restricted UTF-8 to only code points UTF-16 could encode.
|
|
18
|
+
So in reality, only the following sequences can encode are valid characters:
|
|
19
|
+
- 00-7F
|
|
20
|
+
- C2-DF 80-BF
|
|
21
|
+
- E0 A0-BF 80-BF
|
|
22
|
+
- E1-EC 80-BF 80-BF
|
|
23
|
+
- ED 80-9F 80-BF
|
|
24
|
+
- EE-EF 80-BF 80-BF
|
|
25
|
+
- F0 90-BF 80-BF 80-BF
|
|
26
|
+
- F1-F3 80-BF 80-BF 80-BF
|
|
27
|
+
- F4 80-8F 80-BF 80-BF
|
|
28
|
+
|
|
29
|
+
The regexp just tries to match this as compactly as possible.
|
|
30
|
+
*/
|
|
31
|
+
var validUtf8Encodings = /%(?:[0-7]|(?!c[01]|e0%[89]|ed%[ab]|f0%8|f4%[9ab])(?:c|d|(?:e|f[0-4]%[89ab])[\da-f]%[89ab])[\da-f]%[89ab])[\da-f]/gi
|
|
32
|
+
|
|
33
|
+
module.exports = function(str) {
|
|
34
|
+
return String(str).replace(validUtf8Encodings, decodeURIComponent)
|
|
35
|
+
}
|
package/util/hasOwn.js
ADDED