@player-ui/asset-transform-plugin 0.0.1-next.1
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/dist/index.cjs.js +79 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.esm.js +73 -0
- package/package.json +19 -0
- package/src/index.ts +73 -0
- package/src/utils.ts +87 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var partialMatchRegistry = require('@player-ui/partial-match-registry');
|
|
6
|
+
|
|
7
|
+
function composeTransforms(...args) {
|
|
8
|
+
const [fn, ...fns] = args.reverse();
|
|
9
|
+
return (asset, options, store) => {
|
|
10
|
+
const value = fn(asset, options, store);
|
|
11
|
+
if (!fns.length) {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
return fns.reduce((prevValue, current) => {
|
|
15
|
+
return current(prevValue, options, store);
|
|
16
|
+
}, value);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function composeBefore(...args) {
|
|
20
|
+
return {
|
|
21
|
+
beforeResolve: composeTransforms(...args)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function compose(...args) {
|
|
25
|
+
const beforeResolveFns = [];
|
|
26
|
+
const resolveFns = [];
|
|
27
|
+
for (const arg of args) {
|
|
28
|
+
if (typeof arg === "function") {
|
|
29
|
+
resolveFns.push(arg);
|
|
30
|
+
} else {
|
|
31
|
+
if (arg == null ? void 0 : arg.resolve) {
|
|
32
|
+
resolveFns.push(arg.resolve);
|
|
33
|
+
}
|
|
34
|
+
if (arg == null ? void 0 : arg.beforeResolve) {
|
|
35
|
+
beforeResolveFns.push(arg.beforeResolve);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
beforeResolve: beforeResolveFns.length ? composeTransforms(...beforeResolveFns) : void 0,
|
|
41
|
+
resolve: resolveFns.length ? composeTransforms(...resolveFns) : void 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function maybeCompose(maybeFn) {
|
|
46
|
+
if (typeof maybeFn === "object") {
|
|
47
|
+
return maybeFn;
|
|
48
|
+
}
|
|
49
|
+
return compose(maybeFn);
|
|
50
|
+
}
|
|
51
|
+
function cleanupTransformRegistry(maybeRegistry) {
|
|
52
|
+
if (Array.isArray(maybeRegistry)) {
|
|
53
|
+
const wrappedTransforms = maybeRegistry.map(([key, value]) => {
|
|
54
|
+
return [key, maybeCompose(value)];
|
|
55
|
+
});
|
|
56
|
+
return new partialMatchRegistry.Registry(wrappedTransforms);
|
|
57
|
+
}
|
|
58
|
+
const registry = new partialMatchRegistry.Registry();
|
|
59
|
+
maybeRegistry.forEach(({ key, value }) => {
|
|
60
|
+
registry.set(key, maybeCompose(value));
|
|
61
|
+
});
|
|
62
|
+
return registry;
|
|
63
|
+
}
|
|
64
|
+
class AssetTransformPlugin {
|
|
65
|
+
constructor(transforms) {
|
|
66
|
+
this.name = "asset-transform";
|
|
67
|
+
this.registry = cleanupTransformRegistry(transforms);
|
|
68
|
+
}
|
|
69
|
+
apply(player) {
|
|
70
|
+
player.hooks.viewController.tap(this.name, (vc) => {
|
|
71
|
+
this.registry.forEach(({ key, value }) => vc.transformRegistry.set(key, maybeCompose(value)));
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.AssetTransformPlugin = AssetTransformPlugin;
|
|
77
|
+
exports.compose = compose;
|
|
78
|
+
exports.composeBefore = composeBefore;
|
|
79
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BeforeTransformFunction, TransformFunctions, TransformFunction, PlayerPlugin, TransformRegistry, Player } from '@player-ui/player';
|
|
2
|
+
import { Registry } from '@player-ui/partial-match-registry';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Helper function to make it easier to create transforms that need to be ran in
|
|
6
|
+
* the `beforeResolve` hook. Just like `compose`, functions are evaluated from
|
|
7
|
+
* right-to-left.
|
|
8
|
+
*/
|
|
9
|
+
declare function composeBefore(...args: BeforeTransformFunction<any>[]): TransformFunctions;
|
|
10
|
+
/**
|
|
11
|
+
* Performs right-to-left function evaluation of each transform function. Unlike
|
|
12
|
+
* other compose functions, this does not require unary arguments for all but the
|
|
13
|
+
* last function. The value returned from each function will be used as the value
|
|
14
|
+
* for the next function.
|
|
15
|
+
*/
|
|
16
|
+
declare function compose(...args: Array<TransformFunction<any> | TransformFunctions>): TransformFunctions;
|
|
17
|
+
|
|
18
|
+
declare type TransformType = TransformFunction<any> | TransformFunctions;
|
|
19
|
+
declare type TransformRegistryEntries = Array<[any, TransformType]>;
|
|
20
|
+
declare type AssetTransformInit = Registry<TransformType> | TransformRegistryEntries;
|
|
21
|
+
/**
|
|
22
|
+
* A plugin to register custom transforms on certain asset types
|
|
23
|
+
* This allows users to embed stateful data into transforms.
|
|
24
|
+
*/
|
|
25
|
+
declare class AssetTransformPlugin implements PlayerPlugin {
|
|
26
|
+
name: string;
|
|
27
|
+
readonly registry: TransformRegistry;
|
|
28
|
+
constructor(transforms: AssetTransformInit);
|
|
29
|
+
apply(player: Player): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { AssetTransformInit, AssetTransformPlugin, TransformRegistryEntries, TransformType, compose, composeBefore };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Registry } from '@player-ui/partial-match-registry';
|
|
2
|
+
|
|
3
|
+
function composeTransforms(...args) {
|
|
4
|
+
const [fn, ...fns] = args.reverse();
|
|
5
|
+
return (asset, options, store) => {
|
|
6
|
+
const value = fn(asset, options, store);
|
|
7
|
+
if (!fns.length) {
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
return fns.reduce((prevValue, current) => {
|
|
11
|
+
return current(prevValue, options, store);
|
|
12
|
+
}, value);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function composeBefore(...args) {
|
|
16
|
+
return {
|
|
17
|
+
beforeResolve: composeTransforms(...args)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function compose(...args) {
|
|
21
|
+
const beforeResolveFns = [];
|
|
22
|
+
const resolveFns = [];
|
|
23
|
+
for (const arg of args) {
|
|
24
|
+
if (typeof arg === "function") {
|
|
25
|
+
resolveFns.push(arg);
|
|
26
|
+
} else {
|
|
27
|
+
if (arg == null ? void 0 : arg.resolve) {
|
|
28
|
+
resolveFns.push(arg.resolve);
|
|
29
|
+
}
|
|
30
|
+
if (arg == null ? void 0 : arg.beforeResolve) {
|
|
31
|
+
beforeResolveFns.push(arg.beforeResolve);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
beforeResolve: beforeResolveFns.length ? composeTransforms(...beforeResolveFns) : void 0,
|
|
37
|
+
resolve: resolveFns.length ? composeTransforms(...resolveFns) : void 0
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function maybeCompose(maybeFn) {
|
|
42
|
+
if (typeof maybeFn === "object") {
|
|
43
|
+
return maybeFn;
|
|
44
|
+
}
|
|
45
|
+
return compose(maybeFn);
|
|
46
|
+
}
|
|
47
|
+
function cleanupTransformRegistry(maybeRegistry) {
|
|
48
|
+
if (Array.isArray(maybeRegistry)) {
|
|
49
|
+
const wrappedTransforms = maybeRegistry.map(([key, value]) => {
|
|
50
|
+
return [key, maybeCompose(value)];
|
|
51
|
+
});
|
|
52
|
+
return new Registry(wrappedTransforms);
|
|
53
|
+
}
|
|
54
|
+
const registry = new Registry();
|
|
55
|
+
maybeRegistry.forEach(({ key, value }) => {
|
|
56
|
+
registry.set(key, maybeCompose(value));
|
|
57
|
+
});
|
|
58
|
+
return registry;
|
|
59
|
+
}
|
|
60
|
+
class AssetTransformPlugin {
|
|
61
|
+
constructor(transforms) {
|
|
62
|
+
this.name = "asset-transform";
|
|
63
|
+
this.registry = cleanupTransformRegistry(transforms);
|
|
64
|
+
}
|
|
65
|
+
apply(player) {
|
|
66
|
+
player.hooks.viewController.tap(this.name, (vc) => {
|
|
67
|
+
this.registry.forEach(({ key, value }) => vc.transformRegistry.set(key, maybeCompose(value)));
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { AssetTransformPlugin, compose, composeBefore };
|
|
73
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-ui/asset-transform-plugin",
|
|
3
|
+
"version": "0.0.1-next.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"@player-ui/binding-grammar": "0.0.1-next.1",
|
|
10
|
+
"@player-ui/types": "0.0.1-next.1"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@player-ui/partial-match-registry": "0.0.1-next.1",
|
|
14
|
+
"@babel/runtime": "7.15.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.cjs.js",
|
|
17
|
+
"module": "dist/index.esm.js",
|
|
18
|
+
"typings": "dist/index.d.ts"
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Player,
|
|
3
|
+
PlayerPlugin,
|
|
4
|
+
TransformRegistry,
|
|
5
|
+
TransformFunction,
|
|
6
|
+
TransformFunctions,
|
|
7
|
+
} from '@player-ui/player';
|
|
8
|
+
import { Registry } from '@player-ui/partial-match-registry';
|
|
9
|
+
import { compose } from './utils';
|
|
10
|
+
|
|
11
|
+
export * from './utils';
|
|
12
|
+
export type TransformType = TransformFunction<any> | TransformFunctions;
|
|
13
|
+
export type TransformRegistryEntries = Array<[any, TransformType]>;
|
|
14
|
+
export type AssetTransformInit =
|
|
15
|
+
| Registry<TransformType>
|
|
16
|
+
| TransformRegistryEntries;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Normalize asset transform values so that they are all objects that contains
|
|
20
|
+
* the resolve & beforeResolve functions.
|
|
21
|
+
*/
|
|
22
|
+
function maybeCompose(maybeFn: any): TransformFunctions {
|
|
23
|
+
if (typeof maybeFn === 'object') {
|
|
24
|
+
return maybeFn;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return compose(maybeFn);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Standardize each variation of Asset Registry input into the same transform
|
|
32
|
+
* registry shape.
|
|
33
|
+
*/
|
|
34
|
+
function cleanupTransformRegistry(
|
|
35
|
+
maybeRegistry: AssetTransformInit
|
|
36
|
+
): TransformRegistry {
|
|
37
|
+
if (Array.isArray(maybeRegistry)) {
|
|
38
|
+
const wrappedTransforms = maybeRegistry.map(([key, value]) => {
|
|
39
|
+
return [key, maybeCompose(value)] as [any, TransformFunctions];
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return new Registry(wrappedTransforms);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const registry = new Registry<TransformFunctions>();
|
|
46
|
+
|
|
47
|
+
maybeRegistry.forEach(({ key, value }) => {
|
|
48
|
+
registry.set(key, maybeCompose(value));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return registry;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A plugin to register custom transforms on certain asset types
|
|
56
|
+
* This allows users to embed stateful data into transforms.
|
|
57
|
+
*/
|
|
58
|
+
export class AssetTransformPlugin implements PlayerPlugin {
|
|
59
|
+
name = 'asset-transform';
|
|
60
|
+
public readonly registry: TransformRegistry;
|
|
61
|
+
|
|
62
|
+
constructor(transforms: AssetTransformInit) {
|
|
63
|
+
this.registry = cleanupTransformRegistry(transforms);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
apply(player: Player) {
|
|
67
|
+
player.hooks.viewController.tap(this.name, (vc) => {
|
|
68
|
+
this.registry.forEach(({ key, value }) =>
|
|
69
|
+
vc.transformRegistry.set(key, maybeCompose(value))
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Resolve } from '@player-ui/view';
|
|
2
|
+
import type {
|
|
3
|
+
Store,
|
|
4
|
+
BeforeTransformFunction,
|
|
5
|
+
TransformFunction,
|
|
6
|
+
TransformFunctions,
|
|
7
|
+
} from '@player-ui/player';
|
|
8
|
+
|
|
9
|
+
function composeTransforms(
|
|
10
|
+
...args: TransformFunction<any>[]
|
|
11
|
+
): TransformFunction<any>;
|
|
12
|
+
|
|
13
|
+
function composeTransforms(
|
|
14
|
+
...args: BeforeTransformFunction<any>[]
|
|
15
|
+
): BeforeTransformFunction<any>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* More closely resembles the `compose` function you may have used. Performs
|
|
19
|
+
* right-to-left function evaluation, but leveraging the common signature for
|
|
20
|
+
* Transform Functions. The `options` and `store` is unchanging for each
|
|
21
|
+
* transform since only `value` is returned, allowing them to safely be passed
|
|
22
|
+
* into each transform.
|
|
23
|
+
*/
|
|
24
|
+
function composeTransforms(
|
|
25
|
+
...args: TransformFunction<any>[] | BeforeTransformFunction<any>[]
|
|
26
|
+
): TransformFunction<any> | BeforeTransformFunction<any> {
|
|
27
|
+
const [fn, ...fns] = args.reverse();
|
|
28
|
+
|
|
29
|
+
return (asset: any, options: Resolve.NodeResolveOptions, store: Store) => {
|
|
30
|
+
const value = fn(asset, options, store);
|
|
31
|
+
|
|
32
|
+
if (!fns.length) {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return fns.reduce((prevValue, current) => {
|
|
37
|
+
return current(prevValue, options, store);
|
|
38
|
+
}, value);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to make it easier to create transforms that need to be ran in
|
|
44
|
+
* the `beforeResolve` hook. Just like `compose`, functions are evaluated from
|
|
45
|
+
* right-to-left.
|
|
46
|
+
*/
|
|
47
|
+
export function composeBefore(
|
|
48
|
+
...args: BeforeTransformFunction<any>[]
|
|
49
|
+
): TransformFunctions {
|
|
50
|
+
return {
|
|
51
|
+
beforeResolve: composeTransforms(...args),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Performs right-to-left function evaluation of each transform function. Unlike
|
|
57
|
+
* other compose functions, this does not require unary arguments for all but the
|
|
58
|
+
* last function. The value returned from each function will be used as the value
|
|
59
|
+
* for the next function.
|
|
60
|
+
*/
|
|
61
|
+
export function compose(
|
|
62
|
+
...args: Array<TransformFunction<any> | TransformFunctions>
|
|
63
|
+
): TransformFunctions {
|
|
64
|
+
const beforeResolveFns: BeforeTransformFunction<any>[] = [];
|
|
65
|
+
const resolveFns: TransformFunction<any>[] = [];
|
|
66
|
+
|
|
67
|
+
for (const arg of args) {
|
|
68
|
+
if (typeof arg === 'function') {
|
|
69
|
+
resolveFns.push(arg);
|
|
70
|
+
} else {
|
|
71
|
+
if (arg?.resolve) {
|
|
72
|
+
resolveFns.push(arg.resolve);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (arg?.beforeResolve) {
|
|
76
|
+
beforeResolveFns.push(arg.beforeResolve);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
beforeResolve: beforeResolveFns.length
|
|
83
|
+
? composeTransforms(...beforeResolveFns)
|
|
84
|
+
: undefined,
|
|
85
|
+
resolve: resolveFns.length ? composeTransforms(...resolveFns) : undefined,
|
|
86
|
+
};
|
|
87
|
+
}
|