@motion-core/motion-gpu
Version:
Framework-agnostic WebGPU runtime for fullscreen WGSL shaders with explicit Svelte, React, and Vue adapter entrypoints.
90 lines (89 loc) • 2.64 kB
JavaScript
import { createCurrentWritable } from "./current-value.js";
//#region src/lib/core/motiongpu-context.ts
function createUserContextSnapshot(source) {
const snapshot = Object.create(null);
if (source) Object.assign(snapshot, source);
return Object.freeze(snapshot);
}
/**
* Creates a user-context store that publishes frozen, null-prototype namespace snapshots.
*/
function createMotionGPUUserContextStore() {
const store = createCurrentWritable(createUserContextSnapshot());
return {
get current() {
return store.current;
},
subscribe: store.subscribe,
set(value) {
if (Object.is(value, store.current)) return;
store.set(createUserContextSnapshot(value));
},
update(updater) {
const value = updater(store.current);
if (Object.is(value, store.current)) return;
store.set(createUserContextSnapshot(value));
}
};
}
function createMotionGPUUserContextReadable(userStore, namespace) {
if (namespace === void 0) return {
get current() {
return userStore.current;
},
subscribe(run) {
return userStore.subscribe((context) => run(context));
}
};
return {
get current() {
return userStore.current[namespace];
},
subscribe(run) {
return userStore.subscribe((context) => run(context[namespace]));
}
};
}
function isPlainRecord(value) {
if (typeof value !== "object" || value === null) return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
/**
* Applies the shared skip, merge and replace semantics to a user context store.
*/
function setMotionGPUUserContextValue(userStore, namespace, value, options) {
const mode = options?.existing ?? "skip";
const functionValueMode = options?.functionValue ?? "factory";
let resolvedValue;
userStore.update((context) => {
const hasExisting = Object.hasOwn(context, namespace);
if (hasExisting && mode === "skip") {
resolvedValue = context[namespace];
return context;
}
const nextValue = typeof value === "function" && functionValueMode === "factory" ? value() : value;
if (hasExisting && mode === "merge") {
const currentValue = context[namespace];
if (isPlainRecord(currentValue) && isPlainRecord(nextValue)) {
resolvedValue = {
...currentValue,
...nextValue
};
return {
...context,
[namespace]: resolvedValue
};
}
}
resolvedValue = nextValue;
return {
...context,
[namespace]: nextValue
};
});
return resolvedValue;
}
//#endregion
export { createMotionGPUUserContextReadable, createMotionGPUUserContextStore, setMotionGPUUserContextValue };
//# sourceMappingURL=motiongpu-context.js.map