larry babby and threejs for glsl

This commit is contained in:
Sam
2024-06-24 21:24:00 +12:00
parent 87d5dc634d
commit 907ebae4c0
6474 changed files with 1279596 additions and 8 deletions

View File

@@ -0,0 +1,118 @@
import { polyfillNeeded } from './utils.js';
/**
* Note: the "fetch.Request" default value is available for fetch imported from
* the "node-fetch" package and not in browsers. This is OK since browsers
* will be importing umd-polyfill.js from that path "self" is passed the
* decorator so the default value will not be used (because browsers that define
* fetch also has Request). One quirky setup where self.fetch exists but
* self.Request does not is when the "unfetch" minimal fetch polyfill is used
* on top of IE11; for this case the browser will try to use the fetch.Request
* default value which in turn will be undefined but then then "if (Request)"
* will ensure that you get a patched fetch but still no Request (as expected).
* @param {fetch, Request = fetch.Request}
* @returns {fetch: abortableFetch, Request: AbortableRequest}
*/
export default function abortableFetchDecorator(patchTargets) {
if ('function' === typeof patchTargets) {
patchTargets = { fetch: patchTargets };
}
const {
fetch,
Request: NativeRequest = fetch.Request,
AbortController: NativeAbortController,
__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL = false,
} = patchTargets;
if (
!polyfillNeeded({
fetch,
Request: NativeRequest,
AbortController: NativeAbortController,
__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL,
})
) {
return { fetch, Request };
}
let Request = NativeRequest;
// Note that the "unfetch" minimal fetch polyfill defines fetch() without
// defining window.Request, and this polyfill need to work on top of unfetch
// hence we only patch it if it's available. Also we don't patch it if signal
// is already available on the Request prototype because in this case support
// is present and the patching below can cause a crash since it assigns to
// request.signal which is technically a read-only property. This latter error
// happens when you run the main5.js node-fetch example in the repo
// "abortcontroller-polyfill-examples". The exact error is:
// request.signal = init.signal;
// ^
// TypeError: Cannot set property signal of #<Request> which has only a getter
if ((Request && !Request.prototype.hasOwnProperty('signal')) || __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL) {
Request = function Request(input, init) {
let signal;
if (init && init.signal) {
signal = init.signal;
// Never pass init.signal to the native Request implementation when the polyfill has
// been installed because if we're running on top of a browser with a
// working native AbortController (i.e. the polyfill was installed due to
// __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL being set), then passing our
// fake AbortSignal to the native fetch will trigger:
// TypeError: Failed to construct 'Request': member signal is not of type AbortSignal.
delete init.signal;
}
const request = new NativeRequest(input, init);
if (signal) {
Object.defineProperty(request, 'signal', {
writable: false,
enumerable: false,
configurable: true,
value: signal,
});
}
return request;
};
Request.prototype = NativeRequest.prototype;
}
const realFetch = fetch;
const abortableFetch = (input, init) => {
const signal = Request && Request.prototype.isPrototypeOf(input) ? input.signal : init ? init.signal : undefined;
if (signal) {
let abortError;
try {
abortError = new DOMException('Aborted', 'AbortError');
} catch (err) {
// IE 11 does not support calling the DOMException constructor, use a
// regular error object on it instead.
abortError = new Error('Aborted');
abortError.name = 'AbortError';
}
// Return early if already aborted, thus avoiding making an HTTP request
if (signal.aborted) {
return Promise.reject(abortError);
}
// Turn an event into a promise, reject it once `abort` is dispatched
const cancellation = new Promise((_, reject) => {
signal.addEventListener('abort', () => reject(abortError), { once: true });
});
if (init && init.signal) {
// Never pass .signal to the native implementation when the polyfill has
// been installed because if we're running on top of a browser with a
// working native AbortController (i.e. the polyfill was installed due to
// __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL being set), then passing our
// fake AbortSignal to the native fetch will trigger:
// TypeError: Failed to execute 'fetch' on 'Window': member signal is not of type AbortSignal.
delete init.signal;
}
// Return the fastest promise (don't need to wait for request to finish)
return Promise.race([cancellation, realFetch(input, init)]);
}
return realFetch(input, init);
};
return { fetch: abortableFetch, Request };
}

View File

@@ -0,0 +1,13 @@
import AbortController, { AbortSignal } from './abortcontroller';
import { polyfillNeeded } from './utils';
(function (self) {
'use strict';
if (!polyfillNeeded(self)) {
return;
}
self.AbortController = AbortController;
self.AbortSignal = AbortSignal;
})(typeof self !== 'undefined' ? self : global);

View File

@@ -0,0 +1,143 @@
class Emitter {
constructor() {
Object.defineProperty(this, 'listeners', { value: {}, writable: true, configurable: true });
}
addEventListener(type, callback, options) {
if (!(type in this.listeners)) {
this.listeners[type] = [];
}
this.listeners[type].push({ callback, options });
}
removeEventListener(type, callback) {
if (!(type in this.listeners)) {
return;
}
const stack = this.listeners[type];
for (let i = 0, l = stack.length; i < l; i++) {
if (stack[i].callback === callback) {
stack.splice(i, 1);
return;
}
}
}
dispatchEvent(event) {
if (!(event.type in this.listeners)) {
return;
}
const stack = this.listeners[event.type];
const stackToCall = stack.slice();
for (let i = 0, l = stackToCall.length; i < l; i++) {
const listener = stackToCall[i];
try {
listener.callback.call(this, event);
} catch (e) {
Promise.resolve().then(() => {
throw e;
});
}
if (listener.options && listener.options.once) {
this.removeEventListener(event.type, listener.callback);
}
}
return !event.defaultPrevented;
}
}
export class AbortSignal extends Emitter {
constructor() {
super();
// Some versions of babel does not transpile super() correctly for IE <= 10, if the parent
// constructor has failed to run, then "this.listeners" will still be undefined and then we call
// the parent constructor directly instead as a workaround. For general details, see babel bug:
// https://github.com/babel/babel/issues/3041
// This hack was added as a fix for the issue described here:
// https://github.com/Financial-Times/polyfill-library/pull/59#issuecomment-477558042
if (!this.listeners) {
Emitter.call(this);
}
// Compared to assignment, Object.defineProperty makes properties non-enumerable by default and
// we want Object.keys(new AbortController().signal) to be [] for compat with the native impl
Object.defineProperty(this, 'aborted', { value: false, writable: true, configurable: true });
Object.defineProperty(this, 'onabort', { value: null, writable: true, configurable: true });
Object.defineProperty(this, 'reason', { value: undefined, writable: true, configurable: true });
}
toString() {
return '[object AbortSignal]';
}
dispatchEvent(event) {
if (event.type === 'abort') {
this.aborted = true;
if (typeof this.onabort === 'function') {
this.onabort.call(this, event);
}
}
super.dispatchEvent(event);
}
}
export class AbortController {
constructor() {
// Compared to assignment, Object.defineProperty makes properties non-enumerable by default and
// we want Object.keys(new AbortController()) to be [] for compat with the native impl
Object.defineProperty(this, 'signal', { value: new AbortSignal(), writable: true, configurable: true });
}
abort(reason) {
let event;
try {
event = new Event('abort');
} catch (e) {
if (typeof document !== 'undefined') {
if (!document.createEvent) {
// For Internet Explorer 8:
event = document.createEventObject();
event.type = 'abort';
} else {
// For Internet Explorer 11:
event = document.createEvent('Event');
event.initEvent('abort', false, false);
}
} else {
// Fallback where document isn't available:
event = {
type: 'abort',
bubbles: false,
cancelable: false,
};
}
}
let signalReason = reason;
if (signalReason === undefined) {
if (typeof document === 'undefined') {
signalReason = new Error('This operation was aborted');
signalReason.name = 'AbortError';
} else {
try {
signalReason = new DOMException('signal is aborted without reason');
} catch (err) {
// IE 11 does not support calling the DOMException constructor, use a
// regular error object on it instead.
signalReason = new Error('This operation was aborted');
signalReason.name = 'AbortError';
}
}
}
this.signal.reason = signalReason;
this.signal.dispatchEvent(event);
}
toString() {
return '[object AbortController]';
}
}
export default AbortController;
if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
// These are necessary to make sure that we get correct output for:
// Object.prototype.toString.call(new AbortController())
AbortController.prototype[Symbol.toStringTag] = 'AbortController';
AbortSignal.prototype[Symbol.toStringTag] = 'AbortSignal';
}

View File

@@ -0,0 +1,34 @@
import AbortController, { AbortSignal } from './abortcontroller';
import abortableFetch from './abortableFetch';
import { polyfillNeeded } from './utils';
(function (self) {
'use strict';
if (!polyfillNeeded(self)) {
return;
}
if (!self.fetch) {
console.warn('fetch() is not available, cannot install abortcontroller-polyfill');
return;
}
const { fetch, Request } = abortableFetch(self);
self.fetch = fetch;
self.Request = Request;
Object.defineProperty(self, 'AbortController', {
writable: true,
enumerable: false,
configurable: true,
value: AbortController,
});
Object.defineProperty(self, 'AbortSignal', {
writable: true,
enumerable: false,
configurable: true,
value: AbortSignal,
});
})(typeof self !== 'undefined' ? self : global);

View File

@@ -0,0 +1,2 @@
export { default as AbortController, AbortSignal } from './abortcontroller';
export { default as abortableFetch } from './abortableFetch';

View File

@@ -0,0 +1,17 @@
export function polyfillNeeded(self) {
if (self.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL) {
console.log('__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill');
return true;
}
// Note that the "unfetch" minimal fetch polyfill defines fetch() without
// defining window.Request, and this polyfill need to work on top of unfetch
// so the below feature detection needs the !self.AbortController part.
// The Request.prototype check is also needed because Safari versions 11.1.2
// up to and including 12.1.x has a window.AbortController present but still
// does NOT correctly implement abortable fetch:
// https://bugs.webkit.org/show_bug.cgi?id=174980#c2
return (
(typeof self.Request === 'function' && !self.Request.prototype.hasOwnProperty('signal')) || !self.AbortController
);
}