mirror of
https://git.hmsn.ink/kospo/helptalk/api.git
synced 2026-03-20 10:43:44 +09:00
first
This commit is contained in:
15
sample/dev/talk/bundle/mithril-2.2.2/promise/.eslintrc.js
Normal file
15
sample/dev/talk/bundle/mithril-2.2.2/promise/.eslintrc.js
Normal file
@@ -0,0 +1,15 @@
|
||||
"use strict"
|
||||
|
||||
module.exports = {
|
||||
"extends": "../.eslintrc.js",
|
||||
"env": {
|
||||
"browser": null,
|
||||
"es6": null,
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 5,
|
||||
},
|
||||
"rules": {
|
||||
"no-process-env": "off",
|
||||
},
|
||||
};
|
||||
112
sample/dev/talk/bundle/mithril-2.2.2/promise/polyfill.js
Normal file
112
sample/dev/talk/bundle/mithril-2.2.2/promise/polyfill.js
Normal file
@@ -0,0 +1,112 @@
|
||||
"use strict"
|
||||
/** @constructor */
|
||||
var PromisePolyfill = function(executor) {
|
||||
if (!(this instanceof PromisePolyfill)) throw new Error("Promise must be called with 'new'.")
|
||||
if (typeof executor !== "function") throw new TypeError("executor must be a function.")
|
||||
|
||||
var self = this, resolvers = [], rejectors = [], resolveCurrent = handler(resolvers, true), rejectCurrent = handler(rejectors, false)
|
||||
var instance = self._instance = {resolvers: resolvers, rejectors: rejectors}
|
||||
var callAsync = typeof setImmediate === "function" ? setImmediate : setTimeout
|
||||
function handler(list, shouldAbsorb) {
|
||||
return function execute(value) {
|
||||
var then
|
||||
try {
|
||||
if (shouldAbsorb && value != null && (typeof value === "object" || typeof value === "function") && typeof (then = value.then) === "function") {
|
||||
if (value === self) throw new TypeError("Promise can't be resolved with itself.")
|
||||
executeOnce(then.bind(value))
|
||||
}
|
||||
else {
|
||||
callAsync(function() {
|
||||
if (!shouldAbsorb && list.length === 0) console.error("Possible unhandled promise rejection:", value)
|
||||
for (var i = 0; i < list.length; i++) list[i](value)
|
||||
resolvers.length = 0, rejectors.length = 0
|
||||
instance.state = shouldAbsorb
|
||||
instance.retry = function() {execute(value)}
|
||||
})
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
rejectCurrent(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
function executeOnce(then) {
|
||||
var runs = 0
|
||||
function run(fn) {
|
||||
return function(value) {
|
||||
if (runs++ > 0) return
|
||||
fn(value)
|
||||
}
|
||||
}
|
||||
var onerror = run(rejectCurrent)
|
||||
try {then(run(resolveCurrent), onerror)} catch (e) {onerror(e)}
|
||||
}
|
||||
|
||||
executeOnce(executor)
|
||||
}
|
||||
PromisePolyfill.prototype.then = function(onFulfilled, onRejection) {
|
||||
var self = this, instance = self._instance
|
||||
function handle(callback, list, next, state) {
|
||||
list.push(function(value) {
|
||||
if (typeof callback !== "function") next(value)
|
||||
else try {resolveNext(callback(value))} catch (e) {if (rejectNext) rejectNext(e)}
|
||||
})
|
||||
if (typeof instance.retry === "function" && state === instance.state) instance.retry()
|
||||
}
|
||||
var resolveNext, rejectNext
|
||||
var promise = new PromisePolyfill(function(resolve, reject) {resolveNext = resolve, rejectNext = reject})
|
||||
handle(onFulfilled, instance.resolvers, resolveNext, true), handle(onRejection, instance.rejectors, rejectNext, false)
|
||||
return promise
|
||||
}
|
||||
PromisePolyfill.prototype.catch = function(onRejection) {
|
||||
return this.then(null, onRejection)
|
||||
}
|
||||
PromisePolyfill.prototype.finally = function(callback) {
|
||||
return this.then(
|
||||
function(value) {
|
||||
return PromisePolyfill.resolve(callback()).then(function() {
|
||||
return value
|
||||
})
|
||||
},
|
||||
function(reason) {
|
||||
return PromisePolyfill.resolve(callback()).then(function() {
|
||||
return PromisePolyfill.reject(reason);
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
PromisePolyfill.resolve = function(value) {
|
||||
if (value instanceof PromisePolyfill) return value
|
||||
return new PromisePolyfill(function(resolve) {resolve(value)})
|
||||
}
|
||||
PromisePolyfill.reject = function(value) {
|
||||
return new PromisePolyfill(function(resolve, reject) {reject(value)})
|
||||
}
|
||||
PromisePolyfill.all = function(list) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
var total = list.length, count = 0, values = []
|
||||
if (list.length === 0) resolve([])
|
||||
else for (var i = 0; i < list.length; i++) {
|
||||
(function(i) {
|
||||
function consume(value) {
|
||||
count++
|
||||
values[i] = value
|
||||
if (count === total) resolve(values)
|
||||
}
|
||||
if (list[i] != null && (typeof list[i] === "object" || typeof list[i] === "function") && typeof list[i].then === "function") {
|
||||
list[i].then(consume, reject)
|
||||
}
|
||||
else consume(list[i])
|
||||
})(i)
|
||||
}
|
||||
})
|
||||
}
|
||||
PromisePolyfill.race = function(list) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
list[i].then(resolve, reject)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = PromisePolyfill
|
||||
22
sample/dev/talk/bundle/mithril-2.2.2/promise/promise.js
Normal file
22
sample/dev/talk/bundle/mithril-2.2.2/promise/promise.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/* global window */
|
||||
"use strict"
|
||||
|
||||
var PromisePolyfill = require("./polyfill")
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
if (typeof window.Promise === "undefined") {
|
||||
window.Promise = PromisePolyfill
|
||||
} else if (!window.Promise.prototype.finally) {
|
||||
window.Promise.prototype.finally = PromisePolyfill.prototype.finally
|
||||
}
|
||||
module.exports = window.Promise
|
||||
} else if (typeof global !== "undefined") {
|
||||
if (typeof global.Promise === "undefined") {
|
||||
global.Promise = PromisePolyfill
|
||||
} else if (!global.Promise.prototype.finally) {
|
||||
global.Promise.prototype.finally = PromisePolyfill.prototype.finally
|
||||
}
|
||||
module.exports = global.Promise
|
||||
} else {
|
||||
module.exports = PromisePolyfill
|
||||
}
|
||||
Reference in New Issue
Block a user