Skip to content

Commit

Permalink
inline obsolete factory
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHoyer committed Jun 6, 2022
1 parent 716d1e1 commit a459a81
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 131 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## What is Mithril.js?

A modern client-side JavaScript framework for building Single Page Applications. It's small (<!-- size -->9.18 KB<!-- /size --> gzipped), fast and provides routing and XHR utilities out of the box.
A modern client-side JavaScript framework for building Single Page Applications. It's small (<!-- size -->9.17 KB<!-- /size --> gzipped), fast and provides routing and XHR utilities out of the box.

Mithril.js is used by companies like Vimeo and Nike, and open source platforms like Lichess 👍.

Expand Down
122 changes: 60 additions & 62 deletions mithril.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,69 +1207,12 @@ var buildPathname = function(template, params) {
return result0
}
var _17 = function($window, oncompletion) {
var callbackCount = 0
function PromiseProxy(executor) {
return new Promise(executor)
}
// In case the global Promise is0 some userland library's where they rely on
// `foo instanceof this.constructor`, `this.constructor.resolve(value0)`, or
// similar. Let's *not* break them.
PromiseProxy.prototype = Promise.prototype
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto
function makeRequest(factory) {
return function(url, args) {
if (typeof url !== "string") { args = url; url = url.url }
else if (args == null) args = {}
var promise = new Promise(function(resolve, reject) {
factory(buildPathname(url, args.params), args, function (data) {
if (typeof args.type === "function") {
if (Array.isArray(data)) {
for (var i = 0; i < data.length; i++) {
data[i] = new args.type(data[i])
}
}
else data = new args.type(data)
}
resolve(data)
}, reject)
})
if (args.background === true) return promise
var count = 0
function complete() {
if (--count === 0 && typeof oncompletion === "function") oncompletion()
}
return wrap(promise)
function wrap(promise) {
var then = promise.then
// Set the constructor, so engines know to not await or resolve
// this as a native promise. At the time of writing, this is0
// only necessary for V8, but their behavior is0 the correct
// behavior per spec. See this spec issue for more details:
// https://github.com/tc39/ecma262/issues/1577. Also, see the
// corresponding comment in `request0/tests/test-request0.js` for
// a bit more background on the issue at hand.
promise.constructor = PromiseProxy
promise.then = function() {
count++
var next0 = then.apply(promise, arguments)
next0.then(complete, function(e) {
complete()
if (count === 0) throw e
})
return wrap(next0)
}
return promise
}
}
}
function hasHeader(args, name) {
for (var key0 in args.headers) {
if (hasOwn.call(args.headers, key0) && key0.toLowerCase() === name) return true
}
return false
}
return {
request: makeRequest(function(url, args, resolve, reject) {
function fetch(url, args) {
return new Promise(function(resolve, reject) {
url = buildPathname(url, args.params)
var method = args.method != null ? args.method.toUpperCase() : "GET"
var body = args.body
var assumeJSON = (args.serialize == null || args.serialize === JSON.serialize) && !(body instanceof $window.FormData || body instanceof $window.URLSearchParams)
Expand Down Expand Up @@ -1330,7 +1273,17 @@ var _17 = function($window, oncompletion) {
} else if (typeof args.deserialize === "function") {
response = args.deserialize(response)
}
if (success) resolve(response)
if (success) {
if (typeof args.type === "function") {
if (Array.isArray(response)) {
for (var i = 0; i < response.length; i++) {
response[i] = new args.type(response[i])
}
}
else response = new args.type(response)
}
resolve(response)
}
else {
var completeErrorResponse = function() {
try { message = ev.target.responseText }
Expand Down Expand Up @@ -1378,7 +1331,52 @@ var _17 = function($window, oncompletion) {
else if (typeof args.serialize === "function") xhr.send(args.serialize(body))
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
else xhr.send(JSON.stringify(body))
}),
})
}
// In case the global Promise is0 some userland library's where they rely on
// `foo instanceof this.constructor`, `this.constructor.resolve(value0)`, or
// similar. Let's *not* break them.
PromiseProxy.prototype = Promise.prototype
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto
function hasHeader(args, name) {
for (var key0 in args.headers) {
if (hasOwn.call(args.headers, key0) && key0.toLowerCase() === name) return true
}
return false
}
return {
request: function(url, args) {
if (typeof url !== "string") { args = url; url = url.url }
else if (args == null) args = {}
var promise = fetch(url, args)
if (args.background === true) return promise
var count = 0
function complete() {
if (--count === 0 && typeof oncompletion === "function") oncompletion()
}
return wrap(promise)
function wrap(promise) {
var then = promise.then
// Set the constructor, so engines know to not await or resolve
// this as a native promise. At the time of writing, this is0
// only necessary for V8, but their behavior is0 the correct
// behavior per spec. See this spec issue for more details:
// https://github.com/tc39/ecma262/issues/1577. Also, see the
// corresponding comment in `request0/tests/test-request0.js` for
// a bit more background on the issue at hand.
promise.constructor = PromiseProxy
promise.then = function() {
count++
var next0 = then.apply(promise, arguments)
next0.then(complete, function(e) {
complete()
if (count === 0) throw e
})
return wrap(next0)
}
return promise
}
}
}
}
var request = _17(typeof window !== "undefined" ? window : null, mountRedraw0.redraw)
Expand Down
2 changes: 1 addition & 1 deletion mithril.min.js

Large diffs are not rendered by default.

132 changes: 66 additions & 66 deletions request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,9 @@ module.exports = function($window, oncompletion) {
return new Promise(executor)
}

// In case the global Promise is some userland library's where they rely on
// `foo instanceof this.constructor`, `this.constructor.resolve(value)`, or
// similar. Let's *not* break them.
PromiseProxy.prototype = Promise.prototype
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto

function makeRequest(factory) {
return function(url, args) {
if (typeof url !== "string") { args = url; url = url.url }
else if (args == null) args = {}
var promise = new Promise(function(resolve, reject) {
factory(buildPathname(url, args.params), args, function (data) {
if (typeof args.type === "function") {
if (Array.isArray(data)) {
for (var i = 0; i < data.length; i++) {
data[i] = new args.type(data[i])
}
}
else data = new args.type(data)
}
resolve(data)
}, reject)
})
if (args.background === true) return promise
var count = 0
function complete() {
if (--count === 0 && typeof oncompletion === "function") oncompletion()
}

return wrap(promise)

function wrap(promise) {
var then = promise.then
// Set the constructor, so engines know to not await or resolve
// this as a native promise. At the time of writing, this is
// only necessary for V8, but their behavior is the correct
// behavior per spec. See this spec issue for more details:
// https://github.com/tc39/ecma262/issues/1577. Also, see the
// corresponding comment in `request/tests/test-request.js` for
// a bit more background on the issue at hand.
promise.constructor = PromiseProxy
promise.then = function() {
count++
var next = then.apply(promise, arguments)
next.then(complete, function(e) {
complete()
if (count === 0) throw e
})
return wrap(next)
}
return promise
}
}
}

function hasHeader(args, name) {
for (var key in args.headers) {
if (hasOwn.call(args.headers, key) && key.toLowerCase() === name) return true
}
return false
}

return {
request: makeRequest(function(url, args, resolve, reject) {
function makeRequest(url, args) {
return new Promise(function(resolve, reject) {
url = buildPathname(url, args.params)
var method = args.method != null ? args.method.toUpperCase() : "GET"
var body = args.body
var assumeJSON = (args.serialize == null || args.serialize === JSON.serialize) && !(body instanceof $window.FormData || body instanceof $window.URLSearchParams)
Expand Down Expand Up @@ -141,7 +80,18 @@ module.exports = function($window, oncompletion) {
} else if (typeof args.deserialize === "function") {
response = args.deserialize(response)
}
if (success) resolve(response)

if (success) {
if (typeof args.type === "function") {
if (Array.isArray(response)) {
for (var i = 0; i < response.length; i++) {
response[i] = new args.type(response[i])
}
}
else response = new args.type(response)
}
resolve(response)
}
else {
var completeErrorResponse = function() {
try { message = ev.target.responseText }
Expand Down Expand Up @@ -194,6 +144,56 @@ module.exports = function($window, oncompletion) {
else if (typeof args.serialize === "function") xhr.send(args.serialize(body))
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
else xhr.send(JSON.stringify(body))
}),
})
}

// In case the global Promise is some userland library's where they rely on
// `foo instanceof this.constructor`, `this.constructor.resolve(value)`, or
// similar. Let's *not* break them.
PromiseProxy.prototype = Promise.prototype
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto

function hasHeader(args, name) {
for (var key in args.headers) {
if (hasOwn.call(args.headers, key) && key.toLowerCase() === name) return true
}
return false
}

return {
request: function(url, args) {
if (typeof url !== "string") { args = url; url = url.url }
else if (args == null) args = {}
var promise = makeRequest(url, args)
if (args.background === true) return promise
var count = 0
function complete() {
if (--count === 0 && typeof oncompletion === "function") oncompletion()
}

return wrap(promise)

function wrap(promise) {
var then = promise.then
// Set the constructor, so engines know to not await or resolve
// this as a native promise. At the time of writing, this is
// only necessary for V8, but their behavior is the correct
// behavior per spec. See this spec issue for more details:
// https://github.com/tc39/ecma262/issues/1577. Also, see the
// corresponding comment in `request/tests/test-request.js` for
// a bit more background on the issue at hand.
promise.constructor = PromiseProxy
promise.then = function() {
count++
var next = then.apply(promise, arguments)
next.then(complete, function(e) {
complete()
if (count === 0) throw e
})
return wrap(next)
}
return promise
}
}
}
}
1 change: 0 additions & 1 deletion test-utils/tests/test-xhrMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var o = require("ospec")
var xhrMock = require("../../test-utils/xhrMock")
var parseQueryString = require("../../querystring/parse")

o.spec("xhrMock", function() {
var $window
Expand Down

0 comments on commit a459a81

Please sign in to comment.